Method: toReal(String)

1: /*******************************************************************************
2: * Copyright (c) 2015 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.query.services;
12:
13: import com.google.common.base.Strings;
14:
15: import java.util.ArrayList;
16: import java.util.List;
17: import java.util.StringTokenizer;
18: import java.util.regex.Matcher;
19: import java.util.regex.Pattern;
20: import java.util.regex.PatternSyntaxException;
21:
22: import org.eclipse.acceleo.annotations.api.documentation.Documentation;
23: import org.eclipse.acceleo.annotations.api.documentation.Example;
24: import org.eclipse.acceleo.annotations.api.documentation.Param;
25: import org.eclipse.acceleo.annotations.api.documentation.ServiceProvider;
26: import org.eclipse.acceleo.annotations.api.documentation.Throw;
27:
28: //@formatter:off
29: @ServiceProvider(
30:         value = "Services available for Strings"
31: )
32: //@formatter:on
33: @SuppressWarnings({"checkstyle:javadocmethod", "checkstyle:javadoctype" })
34: public class StringServices {
35:
36:         /**
37:          * Public constructor.
38:          */
39:         public StringServices() {
40:         }
41:
42:         // @formatter:off
43:         @Documentation(
44:                 value = "Returns a string that is the result of the concatenation of the current string and the string \"b\".",
45:                 params = {
46:                         @Param(name = "self", value = "The current String."),
47:                         @Param(name = "b", value = "The String that will be appended at the end of the current String.")
48:                 },
49:                 result = "The concatenated String.",
50:                 examples = {
51:                         @Example(expression = "'Hello'.concat('World')", result = "HelloWorld")
52:                 },
53:                 comment = "This operation behaves like '+' between two strings."
54:         )
55:         // @formatter:on
56:         public String concat(String self, String b) {
57:                 return Strings.nullToEmpty(self) + Strings.nullToEmpty(b);
58:         }
59:
60:         // @formatter:off
61:         @Documentation(
62:                 value = "Returns a string that is the result of the concatenation of the current string and the string \"b\".",
63:                 params = {
64:                         @Param(name = "self", value = "The current String."),
65:                         @Param(name = "b", value = "The String that will be appended at the end of the current String.")
66:                 },
67:                 result = "The concatenated String.",
68:                 examples = {
69:                         @Example(expression = "'Hello'.add('World')", result = "HelloWorld")
70:                 },
71:                 comment = "This operation behaves like '+' between two strings."
72:         )
73:         // @formatter:on
74:         public String add(String self, String b) {
75:                 return Strings.nullToEmpty(self) + Strings.nullToEmpty(b);
76:         }
77:
78:         // @formatter:off
79:         @Documentation(
80:                 value = "Replaces the first substring of the current String that matches the regular expression \"regex\" " +
81:          "with the String \"replacement\".",
82:                 params = {
83:                         @Param(name = "self", value = "The current String."),
84:                         @Param(name = "regex", value = "The regular expression used to find the substring to replace in the current String."),
85:                         @Param(name = "replacement", value = "The replacement String.")
86:                 },
87:                 result = "Returns the resulting String of a substitution of the first substring matching the given regex by the given replacement",
88:                 exceptions = {
89:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String or \"regex\" or \"replacement\" is null."),
90:                         @Throw(type = PatternSyntaxException.class, value = "If the regular expression's syntax is invalid")
91:                 },
92:                 examples = {
93:                         @Example(expression = "'Hello'.replace('(.*)ll', 'Wh')", result = "'Who'")
94:                 }
95:         )
96:         // @formatter:on
97:         public String replace(String self, String regex, String replacement) {
98:                 return Strings.nullToEmpty(self).replaceFirst(Strings.nullToEmpty(regex),
99:                                 Strings.nullToEmpty(replacement));
100:         }
101:
102:         // @formatter:off
103:         @Documentation(
104:                 value = "Replaces each substring of the current String that matches the given regular expression \"regex\" " +
105:          "with the String \"replacement\".",
106:                 params = {
107:                         @Param(name = "self", value = "The current String."),
108:                         @Param(name = "regex", value = "The regular expression used to find all the substrings to replace in the current String."),
109:                         @Param(name = "replacement", value = "The replacement String.")
110:                 },
111:                 result = "Returns the resulting String of a substitution of all the substrings matching the given regex by the given replacement",
112:                 exceptions = {
113:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String or \"regex\" or \"replacement\" is null."),
114:                         @Throw(type = PatternSyntaxException.class, value = "If the regular expression's syntax is invalid")
115:                 },
116:                 examples = {
117:                         @Example(expression = "'TestTest'.replace('.st', 'erminated')", result = "'TerminatedTerminated'")
118:                 }
119:         )
120:         // @formatter:on
121:         public String replaceAll(String self, String regex, String replacement) {
122:                 return Strings.nullToEmpty(self).replaceAll(Strings.nullToEmpty(regex),
123:                                 Strings.nullToEmpty(replacement));
124:         }
125:
126:         // @formatter:off
127:         @Documentation(
128:                 value = "Returns the current String prefixed with the given \"prefix\".",
129:                 params = {
130:                         @Param(name = "self", value = "The current String that will be prefixed"),
131:                         @Param(name = "prefix", value = "The String that will be prepended before the current String")
132:                 },
133:                 result = "The current String prefixed with the given \"prefix\"",
134:                 examples = {
135:                         @Example(expression = "'World'.prefix('Hello')", result = "'HelloWorld'")
136:                 }
137:         )
138:         // @formatter:on
139:         public String prefix(String self, String prefix) {
140:                 return Strings.nullToEmpty(prefix) + Strings.nullToEmpty(self);
141:         }
142:
143:         // @formatter:off
144:         @Documentation(
145:                 value = "Returns \"true\" if the current String contains the String \"b\"",
146:                 params = {
147:                         @Param(name = "self", value = "The current String"),
148:                         @Param(name = "b", value = "The String that we will look for in the current String")
149:                 },
150:                 result = "\"true\" if the current String contains the String \"b\", \"false\" otherwise",
151:                 exceptions = {
152:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String or \"b\" is null.")
153:                 },
154:                 examples = {
155:                         @Example(expression = "'Hello'.contains('llo')", result = "true")
156:                 }
157:         )
158:         // @formatter:on
159:         public Boolean contains(String self, String b) {
160:                 return Boolean.valueOf(Strings.nullToEmpty(self).contains(Strings.nullToEmpty(b)));
161:         }
162:
163:         // @formatter:off
164:         @Documentation(
165:                 value = "Returns \"true\" if the current String matches the given \"regex\".",
166:                 params = {
167:                         @Param(name = "self", value = "The current String"),
168:                         @Param(name = "regex", value = "The regex used for the match")
169:                 },
170:                 result = "\"true\" if \"self\" matches the given regex, \"false\" otherwise.",
171:                 exceptions = {
172:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String or \"regex\" is null.")
173:                 },
174:                 examples = {
175:                         @Example(expression = "'Hello'.matches('*llo')", result = "true")
176:                 }
177:         )
178:         // @formatter:on
179:         public Boolean matches(String self, String regex) {
180:                 return Boolean.valueOf(Strings.nullToEmpty(self).matches(Strings.nullToEmpty(regex)));
181:         }
182:
183:         // @formatter:off
184:         @Documentation(
185:                 value = "Returns true if the current String ends with the string \"b\".",
186:                 params = {
187:                         @Param(name = "self", value = "The current String"),
188:                         @Param(name = "b", value = "The String that may be at the end of the current String")
189:                 },
190:                 result = "\"true\" if the current String ends with the string \"b\", \"false\" otherwise.",
191:                 exceptions = {
192:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String or \"b\" is null.")
193:                 },
194:                 examples = {
195:                         @Example(expression = "'Hello'.endsWidth('llo')", result = "true")
196:                 }
197:         )
198:         // @formatter:on
199:         public Boolean endsWith(String self, String b) {
200:                 return Boolean.valueOf(Strings.nullToEmpty(self).endsWith(Strings.nullToEmpty(b)));
201:         }
202:
203:         // @formatter:off
204:         @Documentation(
205:                 value = "Returns true if the current String starts with the string \"b\".",
206:                 params = {
207:                         @Param(name = "self", value = "The current String"),
208:                         @Param(name = "b", value = "The String that may be at the beginning of the current String")
209:                 },
210:                 result = "\"true\" if the current String starts with the string \"b\", \"false\" otherwise.",
211:                 exceptions = {
212:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String or \"b\" is null.")
213:                 },
214:                 examples = {
215:                         @Example(expression = "'Hello'.startsWith('Hell')", result = "true")
216:                 }
217:         )
218:         // @formatter:on
219:         public Boolean startsWith(String self, String b) {
220:                 return Boolean.valueOf(Strings.nullToEmpty(self).startsWith(Strings.nullToEmpty(b)));
221:         }
222:
223:         // @formatter:off
224:         @Documentation(
225:                 value = "Returns true if the current String is equals to the String \"b\" without considering case in the comparison.",
226:                 params = {
227:                         @Param(name = "self", value = "The current String"),
228:                         @Param(name = "b", value = "The String to compare with the current String")
229:                 },
230:                 result = "\"true\" if the current String is equal to the string \"b\", without considering case, \"false\" otherwise.",
231:                 exceptions = {
232:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String or \"b\" is null.")
233:                 },
234:                 examples = {
235:                         @Example(expression = "'Hello'.equalsIgnoreCase('hello')", result = "true")
236:                 }
237:         )
238:         // @formatter:on
239:         public Boolean equalsIgnoreCase(String self, String b) {
240:                 return Boolean.valueOf(Strings.nullToEmpty(self).equalsIgnoreCase(Strings.nullToEmpty(b)));
241:         }
242:
243:         // @formatter:off
244:         @Documentation(
245:                 value = "Returns the \"n\" first characters of the current String, or the current String itself if its size is less than \"n\".",
246:                 params = {
247:                         @Param(name = "self", value = "The current String"),
248:                         @Param(name = "n", value = "The number of characters that must be retrieved from the beginning of the current String.")
249:                 },
250:                 result = "The \"n\" first characters of the current String",
251:                 exceptions = {
252:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String or \"n\" is null."),
253:                         @Throw(type = IndexOutOfBoundsException.class, value = "If \"n\" is not a valid index of self (i.e it is inferior to 0).")
254:                 },
255:                 examples = {
256:                         @Example(expression = "'HelloWorld'.first(5)", result = "'Hello'")
257:                 }
258:         )
259:         // @formatter:on
260:         public String first(String self, Integer n) {
261:                 final String result;
262:
263:                 if (self == null || self.length() < n) {
264:                         result = self;
265:                 } else {
266:                         result = self.substring(0, n);
267:                 }
268:
269:                 return result;
270:         }
271:
272:         // @formatter:off
273:         @Documentation(
274:                 value = "Returns the \"n\" last characters of the current String, or the current String if its size is less than \"n\".",
275:                 params = {
276:                         @Param(name = "self", value = "The current String"),
277:                         @Param(name = "n", value = "The number of characters that must be retrieved from the end of the current String")
278:                 },
279:                 result = "The \"n\" last characters of the current String",
280:                 exceptions = {
281:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String or \"n\" is null."),
282:                         @Throw(type = IndexOutOfBoundsException.class, value = "If \"n\" is not a valid index of self (i.e it is greater than the size of the current String).")
283:                 },
284:                 examples = {
285:                         @Example(expression = "'HelloWorld'.last(5)", result = "'World'")
286:                 }
287:         )
288:         // @formatter:on
289:         public String last(String self, Integer n) {
290:                 final String result;
291:
292:                 if (self == null || self.length() < n) {
293:                         result = self;
294:                 } else {
295:                         result = self.substring(self.length() - n, self.length());
296:                 }
297:
298:                 return result;
299:         }
300:
301:         // @formatter:off
302:         @Documentation(
303:                 value = "Returns the index of the last occurrence of \"subString\" in the current String, \"-1\" if " +
304:          "the current String doesn't contain this particular substring. The index referential is 1 as in OCL and not 0.",
305:                 params = {
306:                         @Param(name = "self", value = "The current String"),
307:                         @Param(name = "subString", value = "The substring that we want to find in the current String")
308:                 },
309:                 result = "The index of the last occurrence of subString in the current String or -1 if not found",
310:                 exceptions = {
311:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String or \"subString\" is null.")
312:                 },
313:                 examples = {
314:                         @Example(expression = "'HelloHello'.lastIndex('World')", result = "6")
315:                 }
316:         )
317:         // @formatter:on
318:         public Integer lastIndex(String self, String subString) {
319:                 int index = Strings.nullToEmpty(self).lastIndexOf(Strings.nullToEmpty(subString)) + 1;
320:
321:                 if (index == 0) {
322:                         index = -1;
323:                 }
324:
325:                 return Integer.valueOf(index);
326:         }
327:
328:         // @formatter:off
329:         @Documentation(
330:                 value = "Returns the index of the first occurrence \"subString\" in the current String, or -1 if \"subString\" is not " +
331:          "in the current String. The index referential is 1 as in OCL and not 0.",
332:                 params = {
333:                         @Param(name = "self", value = "The current String"),
334:                         @Param(name = "subString", value = "The substring that we want to find in the current String")
335:                 },
336:                 result = "The index of the first occurrence of subString in the current String or -1 if not found",
337:                 exceptions = {
338:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String or \"subString\" is null.")
339:                 },
340:                 examples = {
341:                         @Example(expression = "'HelloHello'.index('Hello')", result = "1")
342:                 }
343:         )
344:         // @formatter:on
345:         public Integer index(String self, String b) {
346:                 int index = Strings.nullToEmpty(self).indexOf(Strings.nullToEmpty(b)) + 1;
347:
348:                 if (index == 0) {
349:                         index = -1;
350:                 }
351:
352:                 return Integer.valueOf(index);
353:         }
354:
355:         // @formatter:off
356:         @Documentation(
357:                 value = "Returns the index of the first occurrence \"subString\" in the current String from the given index, or -1 if \"subString\" is not " +
358:          "in the current String. The index referential is 1 as in OCL and not 0.",
359:                 params = {
360:                         @Param(name = "self", value = "The current String"),
361:                         @Param(name = "subString", value = "The substring that we want to find in the current String"),
362:                         @Param(name = "indexString", value = "The starting index from which the substring will be searched")
363:                 },
364:                 result = "The index of the first occurrence of subString in the current String or -1 if not found",
365:                 exceptions = {
366:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String, \"subString\" or index is null.")
367:                 },
368:                 examples = {
369:                         @Example(expression = "'HelloHello'.index('Hello', 2)", result = "6")
370:                 }
371:         )
372:         // @formatter:on
373:         public Integer index(String self, String subString, Integer index) {
374:                 int indexResult = Strings.nullToEmpty(self).indexOf(Strings.nullToEmpty(subString), index) + 1;
375:
376:                 if (indexResult == 0) {
377:                         indexResult = -1;
378:                 }
379:
380:                 return Integer.valueOf(indexResult);
381:         }
382:
383:         // @formatter:off
384:         @Documentation(
385:                 value = "Returns the index of the last occurrence \"subString\" in the current String searching backward from the given index, " +
386:          "or -1 if \"subString\" is not in the current String. The index referential is 1 as in OCL and not 0.",
387:                 params = {
388:                         @Param(name = "self", value = "The current String"),
389:                         @Param(name = "subString", value = "The substring that we want to find in the current String"),
390:                         @Param(name = "indexString", value = "The starting index from which the substring will be searched")
391:                 },
392:                 result = "The index of the last occurrence of subString in the current String or -1 if not found",
393:                 exceptions = {
394:                         @Throw(type = NullPointerException.class, value = "Thrown if the current String, \"subString\" or index is null.")
395:                 },
396:                 examples = {
397:                         @Example(expression = "'HelloHello'.lastIndex('Hello', 7)", result = "1")
398:                 }
399:         )
400:         // @formatter:on
401:         public Integer lastIndex(String self, String subString, Integer index) {
402:                 int indexResult = Strings.nullToEmpty(self).lastIndexOf(Strings.nullToEmpty(subString), index) + 1;
403:
404:                 if (indexResult == 0) {
405:                         indexResult = -1;
406:                 }
407:
408:                 return Integer.valueOf(indexResult);
409:
410:         }
411:
412:         // @formatter:off
413:         @Documentation(
414:                 value = "Returns the current String with all characters transformed to lower case.",
415:                 params = {
416:                         @Param(name = "self", value = "The current String from which we want to convert all characters to lower case.")
417:                 },
418:                 result = "The current String with all upper case characters converted to lower case.",
419:                 exceptions = {
420:                         @Throw(type = NullPointerException.class, value = "Thrown if \"self\" is \"null\".")
421:                 },
422:                 examples = {
423:                         @Example(expression = "'HelloWorld'.toLower()", result = "'helloworld'")
424:                 }
425:         )
426:         // @formatter:on
427:         public String toLower(String self) {
428:                 final String result;
429:
430:                 if (self == null) {
431:                         result = null;
432:                 } else {
433:                         result = self.toLowerCase();
434:                 }
435:
436:                 return result;
437:         }
438:
439:         // @formatter:off
440:         @Documentation(
441:                 value = "Returns the self string with the first characters transformed to lower case.",
442:                 params = {
443:                         @Param(name = "self", value = "The current String from which we want to convert the first character to lower case.")
444:                 },
445:                 result = "The current String with the first character transformed to lower case.",
446:                 exceptions = {
447:                         @Throw(type = NullPointerException.class, value = "Thrown if \"self\" is \"null\".")
448:                 },
449:                 examples = {
450:                         @Example(expression = "'HelloWorld'.toLowerFirst()", result = "'helloWorld'")
451:                 }
452:         )
453:         // @formatter:on
454:         public String toLowerFirst(String self) {
455:                 final String resultString;
456:
457:                 if (self == null) {
458:                         resultString = null;
459:                 } else if (self.length() == 0) {
460:                         resultString = self;
461:                 } else if (self.length() == 1) {
462:                         resultString = self.toLowerCase();
463:                 } else {
464:                         resultString = Character.toLowerCase(self.charAt(0)) + self.substring(1);
465:                 }
466:
467:                 return resultString;
468:         }
469:
470:         // @formatter:off
471:         @Documentation(
472:                 value = "Returns the current String with all characters transformed to upper case.",
473:                 params = {
474:                         @Param(name = "self", value = "The current String from which we want to convert all characters to upper case.")
475:                 },
476:                 result = "The current String with all lower case characters converted to upper case.",
477:                 exceptions = {
478:                         @Throw(type = NullPointerException.class, value = "Thrown if \"self\" is \"null\".")
479:                 },
480:                 examples = {
481:                         @Example(expression = "'HelloWorld'.toUpper()", result = "'HELLOWORLD'")
482:                 }
483:         )
484:         // @formatter:on
485:         public String toUpper(String self) {
486:                 final String result;
487:
488:                 if (self == null) {
489:                         result = null;
490:                 } else {
491:                         result = self.toUpperCase();
492:                 }
493:
494:                 return result;
495:         }
496:
497:         // @formatter:off
498:         @Documentation(
499:                 value = "Returns the current String with the first characters transformed to upper case.",
500:                 params = {
501:                         @Param(name = "self", value = "The current String from which we want to convert the first character to upper case.")
502:                 },
503:                 result = "The current String with the first character transformed to upper case.",
504:                 exceptions = {
505:                         @Throw(type = NullPointerException.class, value = "Thrown if \"self\" is \"null\".")
506:                 },
507:                 examples = {
508:                         @Example(expression = "'helloworld'.toUpperFirst()", result = "'Helloworld'")
509:                 }
510:         )
511:         // @formatter:on
512:         public String toUpperFirst(String self) {
513:                 final String resultString;
514:
515:                 if (self == null) {
516:                         resultString = null;
517:                 } else if (self.length() == 0) {
518:                         resultString = self;
519:                 } else if (self.length() == 1) {
520:                         resultString = self.toUpperCase();
521:                 } else {
522:                         resultString = Character.toUpperCase(self.charAt(0)) + self.substring(1);
523:                 }
524:
525:                 return resultString;
526:         }
527:
528:         // @formatter:off
529:         @Documentation(
530:                 value = "Returns \"true\" if self consists only of alphabetical characters, \"false\" otherwise.",
531:                 params = {
532:                         @Param(name = "self", value = "The string we want to ensure it consists only of alphabetical characters.")
533:                 },
534:                 result = "\"true\" if self consists only of alphabetical characters, \"false\" otherwise.",
535:                 examples = {
536:                         @Example(expression = "'abc123'.isAlpha()", result = "false"),
537:                         @Example(expression = "'abcdef'.isAlpha()", result = "true"),
538:                 }
539:         )
540:         // @formatter:on
541:         public Boolean isAlpha(String self) {
542:                 Boolean result = Boolean.TRUE;
543:
544:                 if (self != null) {
545:                         final char[] chars = self.toCharArray();
546:                         for (final char c : chars) {
547:                                 if (!Character.isLetter(c)) {
548:                                         result = Boolean.FALSE;
549:                                         break;
550:                                 }
551:                         }
552:                 } else {
553:                         result = Boolean.FALSE;
554:                 }
555:
556:                 return result;
557:         }
558:
559:         // @formatter:off
560:         @Documentation(
561:                 value = "Returns \"true\" if self consists only of alphanumeric characters, \"false\" otherwise.",
562:                 params = {
563:                         @Param(name = "self", value = "The string we want to ensure it consists only of alphanumeric characters.")
564:                 },
565:                 result = "\"true\" if self consists only of alphanumeric characters, \"false\" otherwise.",
566:                 examples = {
567:                         @Example(expression = "'abc123'.isAlphaNum()", result = "true"),
568:                         @Example(expression = "'abcdef'.isAlphaNum()", result = "true"),
569:                 }
570:         )
571:         // @formatter:on
572:         public Boolean isAlphaNum(String self) {
573:                 Boolean result = Boolean.TRUE;
574:
575:                 if (self != null) {
576:                         final char[] chars = self.toCharArray();
577:                         for (final char c : chars) {
578:                                 if (!Character.isLetterOrDigit(c)) {
579:                                         result = Boolean.FALSE;
580:                                         break;
581:                                 }
582:                         }
583:                 } else {
584:                         result = Boolean.FALSE;
585:                 }
586:
587:                 return result;
588:         }
589:
590:         // @formatter:off
591:         @Documentation(
592:                 value = "Return the length of the current String.",
593:                 params = {
594:                         @Param(name = "self", value = "The current String")
595:                 },
596:                 result = "The length of the specified string",
597:                 examples = {
598:                         @Example(expression = "'HelloWorld'.size()", result = "10")
599:                 }
600:         )
601:         // @formatter:on
602:         public Integer size(String self) {
603:                 final int result;
604:
605:                 if (self == null || self.isEmpty()) {
606:                         result = 0;
607:                 } else {
608:                         result = Strings.nullToEmpty(self).length();
609:                 }
610:
611:                 return Integer.valueOf(result);
612:         }
613:
614:         // @formatter:off
615:         @Documentation(
616:                 value = "Returns a string containing all characters from self starting from index lower up to the end of the string " +
617:          "included. The lower parameter should be contained between 1 and self.size() included. Lower cannot be greater " +
618:                                 "than the size of the String.",
619:                 params = {
620:                         @Param(name = "self", value = "The current String"),
621:                         @Param(name = "lower", value = "The lower bound")
622:                 },
623:                 result = "A string containing all characters from self starting from index lower included.",
624:                 examples = {
625:                         @Example(expression = "'HelloWorld'.substring(5)", result = "'World'"),
626:                         @Example(expression = "'HelloWorld'.substring(1)", result = "'HelloWorld'"),
627:                 }
628:         )
629:         // @formatter:on
630:         public String substring(String self, Integer lower) {
631:                 final String result;
632:
633:                 if (self == null) {
634:                         result = null;
635:                 } else {
636:                         result = self.substring(lower.intValue() - 1);
637:                 }
638:
639:                 return result;
640:         }
641:
642:         // @formatter:off
643:         @Documentation(
644:                 value = "Returns a string containing all characters from self starting from index lower up to index upper " +
645:          "included. Both lower and upper parameters should be contained between 1 and self.size() included. Lower " +
646:                                 "cannot be greater than upper.",
647:                 params = {
648:                         @Param(name = "self", value = "The current String"),
649:                         @Param(name = "lower", value = "The lower bound"),
650:                         @Param(name = "upper", value = "The upper bound")
651:                 },
652:                 result = "a string containing all characters from self starting from index lower up to index upper " +
653:                  "included. Both lower and upper parameters should be contained between 1 and self.size() " +
654:                                  "included. lower cannot be greater than upper",
655:                 examples = {
656:                                 @Example(expression = "'HelloWorld'.substring(1, 5)", result = "'Hello'")
657:                 }
658:         )
659:         // @formatter:on
660:         public String substring(String self, Integer lower, Integer upper) {
661:                 final String result;
662:
663:                 if (self == null) {
664:                         result = null;
665:                 } else {
666:                         result = self.substring(lower.intValue() - 1, upper.intValue());
667:                 }
668:
669:                 return result;
670:         }
671:
672:         // @formatter:off
673:         @Documentation(
674:                 value = "Returns an integer of value equal to self",
675:                 params = {
676:                         @Param(name = "self", value = "The current String")
677:                 },
678:                 result = "An integer of value equal to self",
679:                 exceptions = {
680:                         @Throw(type = NumberFormatException.class, value = "Thrown if self does not represent an integer")
681:                 },
682:                 examples = {
683:                         @Example(expression = "'42'.toInteger()", result = "42")
684:                 }
685:         )
686:         // @formatter:on
687:         public Integer toInteger(String self) {
688:                 return Integer.valueOf(Strings.nullToEmpty(self));
689:         }
690:
691:         // @formatter:off
692:         @Documentation(
693:                 value = "Returns a real of value equal to self",
694:                 params = {
695:                         @Param(name = "self", value = "The current String")
696:                 },
697:                 result = "A real of value equal to self",
698:                 exceptions = {
699:                         @Throw(type = NumberFormatException.class, value = "Thrown if self does not represent a real")
700:                 },
701:                 examples = {
702:                         @Example(expression = "'41.9'.toReal()", result = "41.9")
703:                 }
704:         )
705:         // @formatter:on
706:         public Double toReal(String self) {
707:                 return Double.valueOf(Strings.nullToEmpty(self));
708:         }
709:
710:         // @formatter:off
711:         @Documentation(
712:                 value = "Returns an integer that is either negative, zero or positive depending on whether s1 is alphabetically " +
713:          "less than, equal to or greater than self. Note that upper case letters come before lower case ones, so " +
714:                                 "that 'AA' is closer to 'AC' than it is to 'Ab'.",
715:                 params = {
716:                         @Param(name = "self", value = "The current String"),
717:                         @Param(name = "s1", value = "The other String")
718:                 },
719:                 result = "An integer that is either negative, zero or positive depending on whether s1 is alphabetically " +
720:                  "less than, equal to or greater than self. Note that upper case letters come before lower case " +
721:                                  "ones, so that 'AA' is closer to 'AC' than it is to 'Ab'",
722:                 examples = {
723:                         @Example(expression = "'strcmp operation'.strcmp('strcmp')", result = "10"),
724:                         @Example(expression = "'strcmp operation'.strcmp('strcmp operation')", result = "0"),
725:                         @Example(expression = "'strcmp operation'.strcmp('strtok')", result = "-17")
726:                 }
727:         )
728:         // @formatter:on
729:         public Integer strcmp(String self, String s1) {
730:                 return Integer.valueOf(Strings.nullToEmpty(self).compareTo(Strings.nullToEmpty(s1)));
731:         }
732:
733:         // @formatter:off
734:         @Documentation(
735:                 value = "Searches r in self.",
736:                 params = {
737:                         @Param(name = "self", value = "The current String"),
738:                         @Param(name = "r", value = "The String to search")
739:                 },
740:                 result = "\"true\" if r is found, \"false\" otherwise",
741:                 examples = {
742:                         @Example(expression = "'HelloWorld'.strstr('World')", result = "true")
743:                 }
744:         )
745:         // @formatter:on
746:         public Boolean strstr(String self, String r) {
747:                 return Boolean.valueOf(Strings.nullToEmpty(self).indexOf(Strings.nullToEmpty(r)) > -1);
748:         }
749:
750:         // @formatter:off
751:         @Documentation(
752:                 value = "Substitutes the first occurrence of the substring \"r\" in self by \"t\" and returns the resulting string." +
753:                  " Will return self if it contains no occurrence of the substring r.",
754:                 params = {
755:                         @Param(name = "self", value = "The current String"),
756:                         @Param(name = "r", value = "The String to replace"),
757:                         @Param(name = "t", value = "The replacement String")
758:                 },
759:                 result = "A new String",
760:                 examples = {
761:                         @Example(expression = "'WorldWorld'.substitute('World', 'Hello')", result = "'HelloWorld'")
762:                 }
763:         )
764:         // @formatter:on
765:         public String substitute(String self, String r, String t) {
766:                 return Pattern.compile(Strings.nullToEmpty(r), Pattern.LITERAL).matcher(Strings.nullToEmpty(self))
767:                                 .replaceFirst(Matcher.quoteReplacement(Strings.nullToEmpty(t)));
768:         }
769:
770:         // @formatter:off
771:         @Documentation(
772:                 value = "Substitutes all occurences of the substring \"r\" in self by \"t\" and returns the resulting string." +
773:          " Will return self if it contains no occurrence of the substring r.",
774:                 params = {
775:                         @Param(name = "self", value = "The current String"),
776:                         @Param(name = "r", value = "The String to replace"),
777:                         @Param(name = "t", value = "The replacement String")
778:                 },
779:                 result = "A new String",
780:                 examples = {
781:                         @Example(expression = "'WorldWorld'.substituteAll('World', 'Hello')", result = "'HelloHello'")
782:                 }
783:         )
784:         // @formatter:on
785:         public String substituteAll(String self, String r, String t) {
786:                 return Pattern.compile(Strings.nullToEmpty(r), Pattern.LITERAL).matcher(Strings.nullToEmpty(self))
787:                                 .replaceAll(Matcher.quoteReplacement(Strings.nullToEmpty(t)));
788:         }
789:
790:         // @formatter:off
791:         @Documentation(
792:                 value = "Trims the given String.",
793:                 params = {
794:                         @Param(name = "self", value = "The String to trim")
795:                 },
796:                 result = "The trimmed String",
797:                 examples = {
798:                         @Example(expression = "' Hello World '.trim()", result = "'Hello World'")
799:                 }
800:         )
801:         // @formatter:on
802:         public String trim(String self) {
803:                 final String result;
804:
805:                 if (self == null) {
806:                         result = null;
807:                 } else if (self.isEmpty()) {
808:                         result = self;
809:                 } else {
810:                         result = Strings.nullToEmpty(self).trim();
811:                 }
812:
813:                 return result;
814:         }
815:
816:         // @formatter:off
817:         @Documentation(
818:                 value = "Splits the current String by whitespace delimiter into a collection of String",
819:                 params = {
820:                         @Param(name = "self", value = "The current String")
821:                 },
822:                 result = "The collection of substrings of the current String delimited by whitespaces",
823:                 examples = {
824:                         @Example(expression = "'a, b, c, d'.tokenize()", result = "['a,', 'b,', 'c,', 'd']")
825:                 }
826:         )
827:         // @formatter:on
828:         public List<String> tokenize(String self) {
829:                 final List<String> segments = new ArrayList<String>();
830:                 final StringTokenizer tokenizer = new StringTokenizer(Strings.nullToEmpty(self));
831:
832:                 while (tokenizer.hasMoreTokens()) {
833:                         segments.add(tokenizer.nextToken());
834:                 }
835:
836:                 return segments;
837:         }
838:
839:         // @formatter:off
840:         @Documentation(
841:                 value = "Splits the current String by using the given \"delimiter\" into a collection of String",
842:                 params = {
843:                         @Param(name = "self", value = "The current String"),
844:                         @Param(name = "delimiter", value = "The current String")
845:                 },
846:                 result = "The collection of substrings of the current String delimited by the given \"delimiter\"",
847:                 examples = {
848:                         @Example(expression = "'a, b, c, d'.tokenize(', ')", result = "['a', 'b', 'c', 'd']")
849:                 }
850:         )
851:         // @formatter:on
852:         public List<String> tokenize(String self, String delimiter) {
853:                 final List<String> segments = new ArrayList<String>();
854:
855:                 StringTokenizer tokenizer = new StringTokenizer(Strings.nullToEmpty(self), Strings
856:                                 .nullToEmpty(delimiter));
857:                 while (tokenizer.hasMoreTokens()) {
858:                         segments.add(tokenizer.nextToken());
859:                 }
860:
861:                 return segments;
862:         }
863:
864: }