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