1
2
3
4 package edu.internet2.middleware.grouperActivemq.utils;
5
6 import java.io.IOException;
7 import java.io.PrintWriter;
8 import java.io.StringWriter;
9 import java.io.UnsupportedEncodingException;
10 import java.io.Writer;
11 import java.lang.reflect.Array;
12 import java.lang.reflect.Field;
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.math.BigDecimal;
16 import java.net.URLDecoder;
17 import java.net.URLEncoder;
18 import java.security.MessageDigest;
19 import java.security.NoSuchAlgorithmException;
20 import java.sql.SQLException;
21 import java.sql.Timestamp;
22 import java.text.DecimalFormat;
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Calendar;
27 import java.util.Collection;
28 import java.util.Date;
29 import java.util.GregorianCalendar;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.LinkedHashMap;
34 import java.util.LinkedHashSet;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40
41 import edu.internet2.middleware.grouperClient.util.GrouperClientUtils;
42 import edu.internet2.middleware.grouperClientExt.org.apache.commons.codec.binary.Base64;
43 import edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log;
44
45
46
47
48
49
50
51 public class GcElUtilsSafe {
52
53
54
55
56
57
58 public static String append(Object... objects) {
59
60 if (objects == null) {
61 return null;
62 }
63
64 if (objects.length == 0) {
65 return stringValue(objects[0]);
66 }
67
68 StringBuilder result = new StringBuilder();
69
70 for (Object object : objects) {
71 result.append(stringValue(object));
72 }
73
74 return result.toString();
75 }
76
77
78
79
80
81
82 public static String normalizeEmailAddresses(String emailAddresses) {
83 if (emailAddresses == null) {
84 return null;
85 }
86 emailAddresses = replace(emailAddresses, ",", " ");
87 emailAddresses = replace(emailAddresses, ";", " ");
88 emailAddresses = replace(emailAddresses, "\n", " ");
89 emailAddresses = replace(emailAddresses, "\t", " ");
90 emailAddresses = replace(emailAddresses, "\r", " ");
91 emailAddresses = join(splitTrim(emailAddresses, " "), ";");
92 return emailAddresses;
93 }
94
95
96
97
98
99 private static Pattern emailPattern = Pattern.compile("^[^@]+@[^.]+\\..+$");
100
101
102
103
104
105
106 public static boolean validEmail(String email) {
107 Matcher matcher = emailPattern.matcher(email);
108 return matcher.matches();
109 }
110
111
112
113
114
115
116
117
118 public static <T> Set<T> setShorten(Set<T> theSet, int maxSize) {
119
120 if (length(theSet) < maxSize) {
121 return theSet;
122 }
123
124
125 Set<T> newList = new LinkedHashSet<T>();
126 int i = 0;
127
128
129 for (T t : theSet) {
130
131 if (i >= maxSize) {
132 break;
133 }
134
135 newList.add(t);
136 i++;
137 }
138 return newList;
139 }
140
141
142
143
144
145
146 public static String formatNumberWithCommas(Long number) {
147 if (number == null) {
148 return "null";
149 }
150 DecimalFormat df = new DecimalFormat();
151 return df.format(number);
152 }
153
154
155
156
157
158
159
160 @SuppressWarnings("unchecked")
161 public static int compare(Comparable first, Comparable second) {
162 if (first == second) {
163 return 0;
164 }
165 if (first == null) {
166 return -1;
167 }
168 if (second == null) {
169 return 1;
170 }
171 return first.compareTo(second);
172 }
173
174
175
176
177
178
179 public static Map<String, String> toMap(String... strings) {
180 Map<String, String> map = new LinkedHashMap<String, String>();
181 if (strings != null) {
182 if (strings.length % 2 != 0) {
183 throw new RuntimeException("Must pass in an even number of strings: "
184 + strings.length);
185 }
186 for (int i = 0; i < strings.length; i += 2) {
187 map.put(strings[i], strings[i + 1]);
188 }
189 }
190 return map;
191 }
192
193
194
195
196
197
198
199 public static Map<String, Object> toStringObjectMap(Object... stringObjects) {
200 Map<String, Object> map = new LinkedHashMap<String, Object>();
201 if (stringObjects != null) {
202 if (stringObjects.length % 2 != 0) {
203 throw new RuntimeException("Must pass in an even number of strings: "
204 + stringObjects.length);
205 }
206 for (int i = 0; i < stringObjects.length; i += 2) {
207 String key = (String) stringObjects[i];
208 map.put(key, stringObjects[i + 1]);
209 }
210 }
211 return map;
212 }
213
214
215
216
217
218
219 public static String convertMillisToFriendlyString(Integer duration) {
220 if (duration == null) {
221 return convertMillisToFriendlyString((Long) null);
222 }
223 return convertMillisToFriendlyString(new Long(duration.intValue()));
224 }
225
226
227
228
229
230
231 public static String convertMillisToFriendlyString(Long duration) {
232
233 if (duration == null) {
234 return "";
235 }
236
237 if (duration < 1000) {
238 return duration + "ms";
239 }
240
241 long ms = duration % 1000;
242 duration = duration / 1000;
243 long s = duration % 60;
244 duration = duration / 60;
245
246 if (duration == 0) {
247 return s + "s, " + ms + "ms";
248 }
249
250 long m = duration % 60;
251 duration = duration / 60;
252
253 if (duration == 0) {
254 return m + "m, " + s + "s, " + ms + "ms";
255 }
256
257 long h = duration % 24;
258 duration = duration / 24;
259
260 if (duration == 0) {
261 return h + "h, " + m + "m, " + s + "s, " + ms + "ms";
262 }
263
264 long d = duration;
265
266 return d + "d, " + h + "h, " + m + "m, " + s + "s, " + ms + "ms";
267 }
268
269
270
271
272
273
274
275
276 public static String argAfter(String[] args, String argBefore) {
277 if (length(args) <= 1) {
278 return null;
279 }
280 int argBeforeIndex = -1;
281 for (int i = 0; i < args.length; i++) {
282 if (equals(args[i], argBefore)) {
283 argBeforeIndex = i;
284 break;
285 }
286 }
287 if (argBeforeIndex == -1) {
288 throw new RuntimeException("Cant find arg before");
289 }
290 if (argBeforeIndex < args.length - 1) {
291 return args[argBeforeIndex + 1];
292 }
293 return null;
294 }
295
296
297
298
299
300
301
302 public static void append(StringBuilder result,
303 String separatorIfResultNotEmpty, String stringToAppend) {
304 if (result.length() != 0) {
305 result.append(separatorIfResultNotEmpty);
306 }
307 result.append(stringToAppend);
308 }
309
310
311
312
313 public static final String LOG_ERROR = "Error trying to make parent dirs for logger or logging first statement, check to make "
314 +
315 "sure you have proper file permissions, and that your servlet container is giving "
316 +
317 "your app rights to access the log directory (e.g. for tomcat set TOMCAT5_SECURITY=no), g"
318 +
319 "oogle it for more info";
320
321
322
323
324 public static final long ONE_KB = 1024;
325
326
327
328
329 public static final long ONE_MB = ONE_KB * ONE_KB;
330
331
332
333
334 public static final long ONE_GB = ONE_KB * ONE_MB;
335
336
337
338
339
340
341
342
343
344 public static String byteCountToDisplaySize(long size) {
345 String displaySize;
346
347 if (size / ONE_GB > 0) {
348 displaySize = String.valueOf(size / ONE_GB) + " GB";
349 } else if (size / ONE_MB > 0) {
350 displaySize = String.valueOf(size / ONE_MB) + " MB";
351 } else if (size / ONE_KB > 0) {
352 displaySize = String.valueOf(size / ONE_KB) + " KB";
353 } else {
354 displaySize = String.valueOf(size) + " bytes";
355 }
356
357 return displaySize;
358 }
359
360
361
362
363
364
365
366 public static String suffixAfterChar(String input, char theChar) {
367 if (input == null) {
368 return null;
369 }
370
371 int lastIndex = input.lastIndexOf(theChar);
372 if (lastIndex > -1) {
373 input = input.substring(lastIndex + 1, input.length());
374 }
375 return input;
376 }
377
378
379
380
381
382 public static void sleep(long millis) {
383 try {
384 Thread.sleep(millis);
385 } catch (InterruptedException ie) {
386 throw new RuntimeException(ie);
387 }
388 }
389
390
391
392
393
394 public static void sleepWithStdoutCountdown(int seconds) {
395 for (int i = seconds; i > 0; i--) {
396 System.out.println("Sleeping: " + i);
397 sleep(1000);
398 }
399 }
400
401
402
403
404
405
406 public synchronized static String encryptSha(String plaintext) {
407 MessageDigest md = null;
408 try {
409 md = MessageDigest.getInstance("SHA");
410 } catch (NoSuchAlgorithmException e) {
411 throw new RuntimeException(e);
412 }
413 try {
414 md.update(plaintext.getBytes("UTF-8"));
415 } catch (UnsupportedEncodingException e) {
416 throw new RuntimeException(e);
417 }
418 byte raw[] = md.digest();
419 byte[] encoded = Base64.encodeBase64(raw);
420 String hash = new String(encoded);
421
422 return hash;
423 }
424
425
426
427
428
429
430
431
432 public static String uniqueId() {
433
434 synchronized (GcElUtilsSafe.class) {
435 lastId = incrementStringInt(lastId);
436 }
437
438 return String.valueOf(lastId);
439 }
440
441
442
443
444
445
446
447
448 @SuppressWarnings( { "unchecked", "cast" })
449 public static <T> T[] nonNull(T[] array, Class<?> theClass) {
450 if (int.class.equals(theClass)) {
451 return (T[]) (Object) new int[0];
452 }
453 if (float.class.equals(theClass)) {
454 return (T[]) (Object) new float[0];
455 }
456 if (double.class.equals(theClass)) {
457 return (T[]) (Object) new double[0];
458 }
459 if (short.class.equals(theClass)) {
460 return (T[]) (Object) new short[0];
461 }
462 if (long.class.equals(theClass)) {
463 return (T[]) (Object) new long[0];
464 }
465 if (byte.class.equals(theClass)) {
466 return (T[]) (Object) new byte[0];
467 }
468 if (boolean.class.equals(theClass)) {
469 return (T[]) (Object) new boolean[0];
470 }
471 if (char.class.equals(theClass)) {
472 return (T[]) (Object) new char[0];
473 }
474 return array == null ? ((T[]) Array.newInstance(theClass, 0)) : array;
475 }
476
477
478
479
480
481
482
483 public static String stripSuffix(String string, String suffix) {
484 if (string == null || suffix == null) {
485 return string;
486 }
487 if (string.endsWith(suffix)) {
488 return string.substring(0, string.length() - suffix.length());
489 }
490 return string;
491 }
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506 public static String prefixOrSuffix(String startString, String separator,
507 boolean isPrefix) {
508 String prefixOrSuffix = null;
509
510
511 if (startString == null) {
512 return startString;
513 }
514
515
516 int separatorIndex = startString.indexOf(separator);
517
518
519 if (separatorIndex == -1) {
520 return startString;
521 }
522
523
524 int separatorLength = separator.length();
525
526 if (isPrefix) {
527 prefixOrSuffix = startString.substring(0, separatorIndex);
528 } else {
529 prefixOrSuffix = startString.substring(separatorIndex + separatorLength,
530 startString.length());
531 }
532
533 return prefixOrSuffix;
534 }
535
536
537
538
539
540
541
542 public static String extensionFromName(String name) {
543 if (isBlank(name)) {
544 return name;
545 }
546 int lastColonIndex = name.lastIndexOf(':');
547 if (lastColonIndex == -1) {
548 return name;
549 }
550 String extension = name.substring(lastColonIndex + 1);
551 return extension;
552 }
553
554
555
556
557
558
559
560
561 public static String parentStemNameFromName(String name) {
562 return parentStemNameFromName(name, true);
563 }
564
565
566
567
568
569
570
571
572
573 public static String parentStemNameFromName(String name, boolean nullForRoot) {
574
575
576 if (isBlank(name)) {
577 return name;
578 }
579
580 int lastColonIndex = name.lastIndexOf(':');
581 if (lastColonIndex == -1) {
582
583 if (nullForRoot) {
584 return null;
585 }
586 return ":";
587 }
588 String parentStemName = name.substring(0, lastColonIndex);
589 return parentStemName;
590
591 }
592
593
594
595
596
597
598
599 public static String defaultIfBlank(String string, String defaultStringIfBlank) {
600 return isBlank(string) ? defaultStringIfBlank : string;
601 }
602
603
604
605
606
607
608
609
610 public static <T> T defaultIfNull(T theValue, T defaultIfTheValueIsNull) {
611 return theValue != null ? theValue : defaultIfTheValueIsNull;
612 }
613
614
615
616
617
618
619
620 public static <T> void addIfNotThere(Collection<T> list, Collection<T> listToAdd) {
621
622 if (listToAdd == null) {
623 return;
624 }
625 for (T t : listToAdd) {
626 if (!list.contains(t)) {
627 list.add(t);
628 }
629 }
630 }
631
632
633
634
635
636
637
638
639
640 @SuppressWarnings("unchecked")
641 private static void toStringForLogHelper(Object object, int maxChars,
642 StringBuilder result) {
643
644 try {
645 if (object == null) {
646 result.append("null");
647 } else if (object.getClass().isArray()) {
648
649 int length = Array.getLength(object);
650 if (length == 0) {
651 result.append("Empty array");
652 } else {
653 result.append("Array size: ").append(length).append(": ");
654 for (int i = 0; i < length; i++) {
655 result.append("[").append(i).append("]: ").append(
656 Array.get(object, i)).append("\n");
657 if (maxChars != -1 && result.length() > maxChars) {
658 return;
659 }
660 }
661 }
662 } else if (object instanceof Collection) {
663
664 Collection<Object> collection = (Collection<Object>) object;
665 int collectionSize = collection.size();
666 if (collectionSize == 0) {
667 result.append("Empty ").append(object.getClass().getSimpleName());
668 } else {
669 result.append(object.getClass().getSimpleName()).append(" size: ").append(
670 collectionSize).append(": ");
671 int i = 0;
672 for (Object collectionObject : collection) {
673 result.append("[").append(i).append("]: ").append(
674 collectionObject).append("\n");
675 if (maxChars != -1 && result.length() > maxChars) {
676 return;
677 }
678 i++;
679 }
680 }
681 } else {
682 result.append(object.toString());
683 }
684 } catch (Exception e) {
685 result.append("<<exception>> ").append(object.getClass()).append(":\n")
686 .append(getFullStackTrace(e)).append("\n");
687 }
688 }
689
690
691
692
693
694
695 public static String collectionToString(Collection collection) {
696 if (collection == null) {
697 return "null";
698 }
699 if (collection.size() == 0) {
700 return "empty";
701 }
702 StringBuilder result = new StringBuilder();
703 boolean first = true;
704 for (Object object : collection) {
705 if (!first) {
706 result.append(", ");
707 }
708 first = false;
709 result.append(object);
710 }
711 return result.toString();
712
713 }
714
715
716
717
718
719
720 public static String setToString(Set set) {
721 return collectionToString(set);
722 }
723
724
725
726
727
728
729
730 @Deprecated
731 public static String MapToString(Map map) {
732 return mapToString(map);
733 }
734
735
736
737
738
739
740 public static String mapToString(Map map) {
741 if (map == null) {
742 return "null";
743 }
744 if (map.size() == 0) {
745 return "empty";
746 }
747 StringBuilder result = new StringBuilder();
748 boolean first = true;
749 for (Object object : map.keySet()) {
750 if (!first) {
751 result.append(", ");
752 }
753 first = false;
754 result.append(object).append(": ").append(map.get(object));
755 }
756 return result.toString();
757 }
758
759
760
761
762
763
764
765 public static String toStringForLog(Object object) {
766 StringBuilder result = new StringBuilder();
767 toStringForLogHelper(object, -1, result);
768 return result.toString();
769 }
770
771
772
773
774
775
776
777
778 public static String toStringForLog(Object object, int maxChars) {
779 StringBuilder result = new StringBuilder();
780 toStringForLogHelper(object, -1, result);
781 String resultString = result.toString();
782 if (maxChars != -1) {
783 return abbreviate(resultString, maxChars);
784 }
785 return resultString;
786 }
787
788
789
790
791
792
793
794 public static int batchNumberOfBatches(int count, int batchSize) {
795
796 if (batchSize == 0) {
797 return 0;
798 }
799 int batches = 1 + ((count - 1) / batchSize);
800 return batches;
801
802 }
803
804
805
806
807
808
809
810 public static int batchNumberOfBatches(Collection<?> collection, int batchSize) {
811 int arrraySize = length(collection);
812 return batchNumberOfBatches(arrraySize, batchSize);
813
814 }
815
816
817
818
819
820
821
822
823
824
825
826
827 @SuppressWarnings("unchecked")
828 public static <T> List<T> batchList(Collection<T> collection, int batchSize,
829 int batchIndex) {
830
831 int numberOfBatches = batchNumberOfBatches(collection, batchSize);
832 int arraySize = length(collection);
833
834
835 if (arraySize == 0) {
836 return new ArrayList<T>();
837 }
838
839 List<T> theBatchObjects = new ArrayList<T>();
840
841
842
843
844
845
846
847 if (batchIndex == numberOfBatches - 1) {
848
849
850
851
852 int collectionIndex = 0;
853 for (T t : collection) {
854 if (collectionIndex++ < batchIndex * batchSize) {
855 continue;
856 }
857
858
859
860
861
862 theBatchObjects.add(t);
863 }
864
865 } else {
866
867
868 int collectionIndex = 0;
869 for (T t : collection) {
870 if (collectionIndex < batchIndex * batchSize) {
871 collectionIndex++;
872 continue;
873 }
874
875 if (collectionIndex >= (batchIndex + 1) * batchSize) {
876 break;
877 }
878 theBatchObjects.add(t);
879 collectionIndex++;
880 }
881 }
882 return theBatchObjects;
883 }
884
885
886
887
888
889
890
891
892
893
894
895
896 public static String[] splitTrim(String input, String separator) {
897 return splitTrim(input, separator, true);
898 }
899
900
901
902
903
904
905
906
907
908
909
910
911 public static List<String> splitTrimToList(String input, String separator) {
912 if (isBlank(input)) {
913 return null;
914 }
915 String[] array = splitTrim(input, separator);
916 return toList(array);
917 }
918
919
920
921
922
923
924
925
926
927
928
929
930 public static Set<String> splitTrimToSet(String input, String separator) {
931 if (isBlank(input)) {
932 return null;
933 }
934 String[] array = splitTrim(input, separator);
935 return toSet(array);
936 }
937
938
939
940
941
942
943
944
945
946
947
948
949 public static String[] splitTrim(String input, String separator,
950 boolean treatAdjacentSeparatorsAsOne) {
951 if (isBlank(input)) {
952 return null;
953 }
954
955
956 String[] items = treatAdjacentSeparatorsAsOne ? splitByWholeSeparator(input,
957 separator) :
958 split(input, separator);
959
960
961 for (int i = 0; (items != null) && (i < items.length); i++) {
962 items[i] = trim(items[i]);
963 }
964
965
966 return items;
967 }
968
969
970
971
972
973
974 public static String escapeUrlEncode(String string) {
975 String result = null;
976 try {
977 result = URLEncoder.encode(string, "UTF-8");
978 } catch (UnsupportedEncodingException ex) {
979 throw new RuntimeException("UTF-8 not supported", ex);
980 }
981 return result;
982 }
983
984
985
986
987
988
989 public static String escapeUrlDecode(String string) {
990 String result = null;
991 try {
992 result = URLDecoder.decode(string, "UTF-8");
993 } catch (UnsupportedEncodingException ex) {
994 throw new RuntimeException("UTF-8 not supported", ex);
995 }
996 return result;
997 }
998
999
1000
1001
1002
1003
1004
1005 public static <T> List<T> nonNull(List<T> list) {
1006 return list == null ? new ArrayList<T>() : list;
1007 }
1008
1009
1010
1011
1012
1013
1014
1015 public static <T> Collection<T> nonNull(Collection<T> list) {
1016 return list == null ? new ArrayList<T>() : list;
1017 }
1018
1019
1020
1021
1022
1023
1024
1025 public static <T> Set<T> nonNull(Set<T> set) {
1026 return set == null ? new HashSet<T>() : set;
1027 }
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 public static <K, V> Map<K, V> nonNull(Map<K, V> map) {
1038 return map == null ? new HashMap<K, V>() : map;
1039 }
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050 @SuppressWarnings("unchecked")
1051 public static <T> List<T> toList(T... objects) {
1052 if (objects == null) {
1053 return null;
1054 }
1055 if (objects.length == 1 && objects[0] instanceof List) {
1056 return (List<T>) objects[0];
1057 }
1058
1059 List<T> result = new ArrayList<T>();
1060 for (T object : objects) {
1061 result.add(object);
1062 }
1063 return result;
1064 }
1065
1066
1067
1068
1069
1070
1071
1072
1073 public static List<Object> toListObject(Object... objects) {
1074 if (objects == null) {
1075 return null;
1076 }
1077 List<Object> result = new ArrayList<Object>();
1078 for (Object object : objects) {
1079 result.add(object);
1080 }
1081 return result;
1082 }
1083
1084
1085
1086
1087
1088
1089
1090
1091 public static <T> Set<T> toSet(T... objects) {
1092 if (objects == null) {
1093 return null;
1094 }
1095 Set<T> result = new LinkedHashSet<T>();
1096 for (T object : objects) {
1097 result.add(object);
1098 }
1099 return result;
1100 }
1101
1102
1103
1104
1105
1106
1107
1108
1109 public static <T> Set<T> toSetObject(T object) {
1110 if (object == null) {
1111 return null;
1112 }
1113 Set<T> result = new LinkedHashSet<T>();
1114 result.add(object);
1115 return result;
1116 }
1117
1118
1119
1120
1121 public static final String DATE_FORMAT = "yyyyMMdd";
1122
1123
1124
1125
1126 public static final String TIMESTAMP_FILE_FORMAT = "yyyy_MM_dd__HH_mm_ss_SSS";
1127
1128
1129
1130
1131 final static SimpleDateFormat timestampFileFormat = new SimpleDateFormat(
1132 TIMESTAMP_FILE_FORMAT);
1133
1134
1135
1136
1137 public static final String DATE_FORMAT2 = "yyyy/MM/dd";
1138
1139
1140
1141
1142 public static final String DATE_MINUTES_SECONDS_FORMAT = "yyyy/MM/dd HH:mm:ss";
1143
1144
1145
1146
1147 public static final String DATE_MINUTES_SECONDS_NO_SLASH_FORMAT = "yyyyMMdd HH:mm:ss";
1148
1149
1150
1151
1152 public static final String TIMESTAMP_FORMAT = "yyyy/MM/dd HH:mm:ss.SSS";
1153
1154
1155
1156
1157 public static final String TIMESTAMP_NO_SLASH_FORMAT = "yyyyMMdd HH:mm:ss.SSS";
1158
1159
1160
1161
1162 final static SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
1163
1164
1165
1166
1167 final static SimpleDateFormat dateFormat2 = new SimpleDateFormat(DATE_FORMAT2);
1168
1169
1170
1171
1172 final static SimpleDateFormat dateMinutesSecondsFormat = new SimpleDateFormat(
1173 DATE_MINUTES_SECONDS_FORMAT);
1174
1175
1176
1177
1178 final static SimpleDateFormat dateMinutesSecondsNoSlashFormat = new SimpleDateFormat(
1179 DATE_MINUTES_SECONDS_NO_SLASH_FORMAT);
1180
1181
1182
1183
1184 final static SimpleDateFormat timestampFormat = new SimpleDateFormat(TIMESTAMP_FORMAT);
1185
1186
1187
1188
1189 final static SimpleDateFormat timestampNoSlashFormat = new SimpleDateFormat(
1190 TIMESTAMP_NO_SLASH_FORMAT);
1191
1192
1193
1194
1195
1196
1197
1198 public static void assertion(boolean isTrue, String reason) {
1199 if (!isTrue) {
1200 throw new RuntimeException(reason);
1201 }
1202
1203 }
1204
1205
1206
1207
1208 private static char[] lastId = convertLongToStringSmall(new Date().getTime())
1209 .toCharArray();
1210
1211
1212
1213
1214
1215
1216
1217 public static Iterator iterator(Object collection) {
1218 if (collection == null) {
1219 return null;
1220 }
1221
1222 if (collection instanceof Collection
1223 && !(collection instanceof ArrayList)) {
1224 return ((Collection) collection).iterator();
1225 }
1226 return null;
1227 }
1228
1229
1230
1231
1232
1233
1234
1235 public static int length(Object arrayOrCollection) {
1236 if (arrayOrCollection == null) {
1237 return 0;
1238 }
1239 if (arrayOrCollection.getClass().isArray()) {
1240 return Array.getLength(arrayOrCollection);
1241 }
1242 if (arrayOrCollection instanceof Collection) {
1243 return ((Collection) arrayOrCollection).size();
1244 }
1245 if (arrayOrCollection instanceof Map) {
1246 return ((Map) arrayOrCollection).size();
1247 }
1248
1249 return 1;
1250 }
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261 public static Object next(Object arrayOrCollection, Iterator iterator,
1262 int index) {
1263 if (arrayOrCollection.getClass().isArray()) {
1264 return Array.get(arrayOrCollection, index);
1265 }
1266 if (arrayOrCollection instanceof ArrayList) {
1267 return ((ArrayList) arrayOrCollection).get(index);
1268 }
1269 if (arrayOrCollection instanceof Collection) {
1270 return iterator.next();
1271 }
1272
1273 if (0 == index) {
1274 return arrayOrCollection;
1275 }
1276 throw new RuntimeException("Invalid class type: "
1277 + arrayOrCollection.getClass().getName());
1278 }
1279
1280
1281
1282
1283
1284
1285
1286
1287 public static Object remove(Object arrayOrCollection,
1288 int index) {
1289 return remove(arrayOrCollection, null, index);
1290 }
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300 public static Object remove(Object arrayOrCollection, Iterator iterator,
1301 int index) {
1302
1303
1304 if (iterator != null) {
1305 iterator.remove();
1306 return arrayOrCollection;
1307 }
1308 if (arrayOrCollection.getClass().isArray()) {
1309 int newLength = Array.getLength(arrayOrCollection) - 1;
1310 Object newArray = Array.newInstance(
1311 arrayOrCollection.getClass().getComponentType(), newLength);
1312 if (newLength == 0) {
1313 return newArray;
1314 }
1315 if (index > 0) {
1316 System.arraycopy(arrayOrCollection, 0, newArray, 0, index);
1317 }
1318 if (index < newLength) {
1319 System
1320 .arraycopy(arrayOrCollection, index + 1, newArray, index, newLength - index);
1321 }
1322 return newArray;
1323 }
1324 if (arrayOrCollection instanceof List) {
1325 ((List) arrayOrCollection).remove(index);
1326 return arrayOrCollection;
1327 } else if (arrayOrCollection instanceof Collection) {
1328
1329 ((Collection) arrayOrCollection).remove(get(arrayOrCollection, index));
1330 return arrayOrCollection;
1331 }
1332 throw new RuntimeException("Invalid class type: "
1333 + arrayOrCollection.getClass().getName());
1334 }
1335
1336
1337
1338
1339
1340
1341 public static String classesString(Object object) {
1342 StringBuilder result = new StringBuilder();
1343 if (object.getClass().isArray()) {
1344 int length = Array.getLength(object);
1345 for (int i = 0; i < length; i++) {
1346 result.append(((Class) Array.get(object, i)).getSimpleName());
1347 if (i < length - 1) {
1348 result.append(", ");
1349 }
1350 }
1351 return result.toString();
1352 }
1353
1354 throw new RuntimeException("Not implemented: " + className(object));
1355 }
1356
1357
1358
1359
1360
1361
1362
1363 public static String classNameCollection(Object object) {
1364 if (object == null) {
1365 return null;
1366 }
1367 StringBuffer result = new StringBuffer();
1368
1369 Iterator iterator = iterator(object);
1370 int length = length(object);
1371 for (int i = 0; i < length && i < 20; i++) {
1372 result.append(className(next(object, iterator, i)));
1373 if (i != length - 1) {
1374 result.append(", ");
1375 }
1376 }
1377 return result.toString();
1378 }
1379
1380
1381
1382
1383
1384
1385
1386 public static String className(Object object) {
1387 return object == null ? null : object.getClass().getName();
1388 }
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398 public static Object toArray(Object objectOrArrayOrCollection) {
1399
1400
1401 if (objectOrArrayOrCollection != null
1402 && objectOrArrayOrCollection.getClass().isArray()) {
1403 return objectOrArrayOrCollection;
1404 }
1405 int length = length(objectOrArrayOrCollection);
1406 if (length == 0) {
1407 return null;
1408 }
1409
1410 if (objectOrArrayOrCollection instanceof Collection) {
1411 Collection collection = (Collection) objectOrArrayOrCollection;
1412 Object first = collection.iterator().next();
1413 return toArray(collection, first == null ? Object.class : first
1414 .getClass());
1415 }
1416
1417 Object array = Array.newInstance(objectOrArrayOrCollection.getClass(),
1418 1);
1419 Array.set(array, 0, objectOrArrayOrCollection);
1420 return array;
1421 }
1422
1423
1424
1425
1426
1427
1428
1429
1430 @SuppressWarnings("unchecked")
1431 public static <T> T[] toArray(Collection collection, Class<T> theClass) {
1432 if (collection == null || collection.size() == 0) {
1433 return null;
1434 }
1435
1436 return (T[]) collection.toArray((Object[]) Array.newInstance(theClass,
1437 collection.size()));
1438
1439 }
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453 public static String replace(String text, Object searchFor,
1454 Object replaceWith) {
1455 return replace(null, null, text, searchFor, replaceWith, false, 0,
1456 false);
1457 }
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473 public static String replace(String text, Object searchFor,
1474 Object replaceWith, boolean recurse) {
1475 return replace(null, null, text, searchFor, replaceWith, recurse,
1476 recurse ? length(searchFor) : 0, false);
1477 }
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495 public static String replace(String text, Object searchFor,
1496 Object replaceWith, boolean recurse, boolean removeIfFound) {
1497 return replace(null, null, text, searchFor, replaceWith, recurse,
1498 recurse ? length(searchFor) : 0, removeIfFound);
1499 }
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531 public static String replace(String text, String repl, String with) {
1532 return replace(text, repl, with, -1);
1533 }
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572 public static String replace(String text, String repl, String with, int max) {
1573 if (text == null || isEmpty(repl) || with == null || max == 0) {
1574 return text;
1575 }
1576
1577 StringBuffer buf = new StringBuffer(text.length());
1578 int start = 0, end = 0;
1579 while ((end = text.indexOf(repl, start)) != -1) {
1580 buf.append(text.substring(start, end)).append(with);
1581 start = end + repl.length();
1582
1583 if (--max == 0) {
1584 break;
1585 }
1586 }
1587 buf.append(text.substring(start));
1588 return buf.toString();
1589 }
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613 public static boolean isEmpty(String str) {
1614 return str == null || str.length() == 0;
1615 }
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630 public static void replace(StringBuffer outBuffer, String text,
1631 Object searchFor, Object replaceWith) {
1632 replace(outBuffer, null, text, searchFor, replaceWith, false, 0, false);
1633 }
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650 public static void replace(StringBuffer outBuffer, String text,
1651 Object searchFor, Object replaceWith, boolean recurse) {
1652 replace(outBuffer, null, text, searchFor, replaceWith, recurse,
1653 recurse ? length(searchFor) : 0, false);
1654 }
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685 private static String replace(StringBuffer outBuffer, Writer outWriter,
1686 String text, Object searchFor, Object replaceWith, boolean recurse,
1687 int timeToLive, boolean removeIfFound) {
1688
1689
1690
1691 if (!recurse) {
1692 return replaceHelper(outBuffer, outWriter, text, searchFor,
1693 replaceWith, recurse, timeToLive, removeIfFound);
1694 }
1695
1696 String result = replaceHelper(null, null, text, searchFor, replaceWith,
1697 recurse, timeToLive, removeIfFound);
1698 if (outBuffer != null) {
1699 outBuffer.append(result);
1700 return null;
1701 }
1702
1703 if (outWriter != null) {
1704 try {
1705 outWriter.write(result);
1706 } catch (IOException ioe) {
1707 throw new RuntimeException(ioe);
1708 }
1709 return null;
1710 }
1711
1712 return result;
1713
1714 }
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729 public static void replace(Writer outWriter, String text, Object searchFor,
1730 Object replaceWith) {
1731 replace(null, outWriter, text, searchFor, replaceWith, false, 0, false);
1732 }
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749 public static void replace(Writer outWriter, String text, Object searchFor,
1750 Object replaceWith, boolean recurse) {
1751 replace(null, outWriter, text, searchFor, replaceWith, recurse,
1752 recurse ? length(searchFor) : 0, false);
1753 }
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784 private static String replaceHelper(StringBuffer outBuffer,
1785 Writer outWriter, String text, Object searchFor,
1786 Object replaceWith, boolean recurse, int timeToLive,
1787 boolean removeIfFound) {
1788
1789 try {
1790
1791 if (timeToLive < 0) {
1792 throw new IllegalArgumentException("TimeToLive under 0: "
1793 + timeToLive + ", " + text);
1794 }
1795
1796 int searchForLength = length(searchFor);
1797 boolean done = false;
1798
1799 if (isEmpty(text)) {
1800 return text;
1801 }
1802
1803 if (searchForLength == 0) {
1804 done = true;
1805 }
1806
1807 boolean[] noMoreMatchesForReplIndex = null;
1808 int inputIndex = -1;
1809 int replaceIndex = -1;
1810 long resultPacked = -1;
1811
1812 if (!done) {
1813
1814 if (searchForLength != length(replaceWith)) {
1815 throw new IndexOutOfBoundsException("Lengths dont match: "
1816 + searchForLength + ", " + length(replaceWith));
1817 }
1818
1819
1820 noMoreMatchesForReplIndex = new boolean[searchForLength];
1821
1822
1823
1824
1825 resultPacked = findNextIndexHelper(searchForLength, searchFor,
1826 replaceWith,
1827 noMoreMatchesForReplIndex, text, 0);
1828
1829 inputIndex = unpackInt(resultPacked, true);
1830 replaceIndex = unpackInt(resultPacked, false);
1831 }
1832
1833
1834
1835
1836 boolean writeToWriter = outWriter != null;
1837
1838
1839 if (done || inputIndex == -1) {
1840 if (writeToWriter) {
1841 outWriter.write(text, 0, text.length());
1842 return null;
1843 }
1844 if (outBuffer != null) {
1845 appendSubstring(outBuffer, text, 0, text.length());
1846 return null;
1847 }
1848 return text;
1849 }
1850
1851
1852 StringBuffer bufferToWriteTo = outBuffer != null ? outBuffer
1853 : (writeToWriter ? null : new StringBuffer(text.length()
1854 + replaceStringsBufferIncrease(text, searchFor,
1855 replaceWith)));
1856
1857 String searchString = null;
1858 String replaceString = null;
1859
1860 int start = 0;
1861
1862 while (inputIndex != -1) {
1863
1864 searchString = (String) get(searchFor, replaceIndex);
1865 replaceString = (String) get(replaceWith, replaceIndex);
1866 if (writeToWriter) {
1867 outWriter.write(text, start, inputIndex - start);
1868 outWriter.write(replaceString);
1869 } else {
1870 appendSubstring(bufferToWriteTo, text, start, inputIndex)
1871 .append(replaceString);
1872 }
1873
1874 if (removeIfFound) {
1875
1876 searchFor = remove(searchFor, replaceIndex);
1877 replaceWith = remove(replaceWith, replaceIndex);
1878 noMoreMatchesForReplIndex = (boolean[]) remove(noMoreMatchesForReplIndex,
1879 replaceIndex);
1880
1881 searchForLength--;
1882 }
1883
1884 start = inputIndex + searchString.length();
1885
1886 resultPacked = findNextIndexHelper(searchForLength, searchFor,
1887 replaceWith,
1888 noMoreMatchesForReplIndex, text, start);
1889 inputIndex = unpackInt(resultPacked, true);
1890 replaceIndex = unpackInt(resultPacked, false);
1891 }
1892 if (writeToWriter) {
1893 outWriter.write(text, start, text.length() - start);
1894
1895 } else {
1896 appendSubstring(bufferToWriteTo, text, start, text.length());
1897 }
1898
1899
1900 if (writeToWriter || outBuffer != null) {
1901 if (recurse) {
1902 throw new IllegalArgumentException(
1903 "Cannot recurse and write to existing buffer or writer!");
1904 }
1905 return null;
1906 }
1907 String resultString = bufferToWriteTo.toString();
1908
1909 if (recurse) {
1910 return replaceHelper(outBuffer, outWriter, resultString,
1911 searchFor, replaceWith, recurse, timeToLive - 1, false);
1912 }
1913
1914 return resultString;
1915 } catch (IOException ioe) {
1916 throw new RuntimeException(ioe);
1917 }
1918 }
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930 static int replaceStringsBufferIncrease(String text, Object repl,
1931 Object with) {
1932
1933 int increase = 0;
1934 Iterator iteratorReplace = iterator(repl);
1935 Iterator iteratorWith = iterator(with);
1936 int replLength = length(repl);
1937 String currentRepl = null;
1938 String currentWith = null;
1939 for (int i = 0; i < replLength; i++) {
1940 currentRepl = (String) next(repl, iteratorReplace, i);
1941 currentWith = (String) next(with, iteratorWith, i);
1942 if (currentRepl == null || currentWith == null) {
1943 throw new NullPointerException("Replace string is null: "
1944 + text + ", " + currentRepl + ", " + currentWith);
1945 }
1946 int greater = currentWith.length() - currentRepl.length();
1947 increase += greater > 0 ? 3 * greater : 0;
1948 }
1949
1950 increase = Math.min(increase, text.length() / 5);
1951 return increase;
1952 }
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966 private static long findNextIndexHelper(int searchForLength,
1967 Object searchFor, Object replaceWith, boolean[] noMoreMatchesForReplIndex,
1968 String input, int start) {
1969
1970 int inputIndex = -1;
1971 int replaceIndex = -1;
1972
1973 Iterator iteratorSearchFor = iterator(searchFor);
1974 Iterator iteratorReplaceWith = iterator(replaceWith);
1975
1976 String currentSearchFor = null;
1977 String currentReplaceWith = null;
1978 int tempIndex = -1;
1979 for (int i = 0; i < searchForLength; i++) {
1980 currentSearchFor = (String) next(searchFor, iteratorSearchFor, i);
1981 currentReplaceWith = (String) next(replaceWith,
1982 iteratorReplaceWith, i);
1983 if (noMoreMatchesForReplIndex[i] || isEmpty(currentSearchFor)
1984 || currentReplaceWith == null) {
1985 continue;
1986 }
1987 tempIndex = input.indexOf(currentSearchFor, start);
1988
1989
1990 noMoreMatchesForReplIndex[i] = tempIndex == -1;
1991
1992 if (tempIndex != -1 && (inputIndex == -1 || tempIndex < inputIndex)) {
1993 inputIndex = tempIndex;
1994 replaceIndex = i;
1995 }
1996
1997 }
1998
1999 long resultPacked = packInts(inputIndex, replaceIndex);
2000 return resultPacked;
2001 }
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013 public static long packInts(int first, int second) {
2014 long result = first;
2015 result <<= 32;
2016 result |= second;
2017 return result;
2018 }
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029 public static int unpackInt(long theLong, boolean isFirst) {
2030
2031 int result = 0;
2032
2033 if (isFirst) {
2034 theLong >>= 32;
2035 }
2036
2037 result = (int) (theLong & 0xffffffff);
2038 return result;
2039 }
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055 private static StringBuffer appendSubstring(StringBuffer buf,
2056 String string, int start, int end) {
2057 for (int i = start; i < end; i++) {
2058 buf.append(string.charAt(i));
2059 }
2060 return buf;
2061 }
2062
2063
2064
2065
2066
2067
2068
2069 @SuppressWarnings("unchecked")
2070 public static String toStringSafe(Object object) {
2071 if (object == null) {
2072 return null;
2073 }
2074
2075 try {
2076
2077 if (object instanceof Collection) {
2078 Collection<Object> collection = (Collection<Object>) object;
2079 int collectionSize = collection.size();
2080 if (collectionSize == 0) {
2081 return "Empty " + object.getClass().getSimpleName();
2082 }
2083 Object first = collection.iterator().next();
2084 return object.getClass().getSimpleName() + " of size "
2085 + collectionSize + " with first type: " +
2086 (first == null ? null : first.getClass());
2087 }
2088
2089 return object.toString();
2090 } catch (Exception e) {
2091 return "<<exception>> " + object.getClass() + ":\n" + getFullStackTrace(e) + "\n";
2092 }
2093 }
2094
2095
2096
2097
2098
2099
2100
2101 public static boolean booleanValue(Object object) {
2102
2103 if (nullOrBlank(object)) {
2104 throw new RuntimeException(
2105 "Expecting something which can be converted to boolean, but is null or blank: '"
2106 + object + "'");
2107 }
2108
2109 if (object instanceof Boolean) {
2110 return (Boolean) object;
2111 }
2112 if (object instanceof String) {
2113 String string = (String) object;
2114 if (equalsIgnoreCase(string, "true")
2115 || equalsIgnoreCase(string, "t")
2116 || equalsIgnoreCase(string, "yes")
2117 || equalsIgnoreCase(string, "y")) {
2118 return true;
2119 }
2120 if (equalsIgnoreCase(string, "false")
2121 || equalsIgnoreCase(string, "f")
2122 || equalsIgnoreCase(string, "no")
2123 || equalsIgnoreCase(string, "n")) {
2124 return false;
2125 }
2126 throw new RuntimeException(
2127 "Invalid string to boolean conversion: '" + string
2128 + "' expecting true|false or t|f or yes|no or y|n case insensitive");
2129
2130 }
2131 throw new RuntimeException("Cant convert object to boolean: "
2132 + object.getClass());
2133
2134 }
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144 public static boolean booleanValue(Object object, boolean defaultBoolean) {
2145 if (nullOrBlank(object)) {
2146 return defaultBoolean;
2147 }
2148 return booleanValue(object);
2149 }
2150
2151
2152
2153
2154
2155
2156
2157 public static Boolean booleanObjectValue(Object object) {
2158 if (nullOrBlank(object)) {
2159 return null;
2160 }
2161 return booleanValue(object);
2162 }
2163
2164
2165
2166
2167
2168
2169
2170 public static boolean nullOrBlank(Object object) {
2171
2172 if (object == null) {
2173 return true;
2174 }
2175 if (object instanceof String && isBlank(((String) object))) {
2176 return true;
2177 }
2178 return false;
2179
2180 }
2181
2182
2183
2184
2185
2186
2187 public static Long dateLongValue(String date) {
2188 if (isBlank(date)) {
2189 return null;
2190 }
2191 Date dateObject = dateValue(date);
2192 return dateObject.getTime();
2193 }
2194
2195
2196
2197
2198 private static final String TIMESTAMP_XML_FORMAT = "yyyy/MM/dd HH:mm:ss.SSS";
2199
2200
2201
2202
2203
2204
2205 public static String dateStringValue(Date date) {
2206 if (date == null) {
2207 return null;
2208 }
2209 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIMESTAMP_XML_FORMAT);
2210 return simpleDateFormat.format(date);
2211 }
2212
2213
2214
2215
2216
2217
2218 public static String dateStringValue(Long theDate) {
2219 if (theDate == null) {
2220 return null;
2221 }
2222 return dateStringValue(new Date(theDate));
2223 }
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236 public static Date dateValue(Object inputObject) {
2237 if (inputObject == null) {
2238 return null;
2239 }
2240
2241 if (inputObject instanceof java.util.Date) {
2242 return (Date) inputObject;
2243 }
2244
2245 if (inputObject instanceof String) {
2246 String input = (String) inputObject;
2247
2248 if (isBlank(input)) {
2249 return null;
2250 }
2251
2252 try {
2253 if (input.length() == 8) {
2254
2255 return dateFormat().parse(input);
2256 }
2257 if (!contains(input, '.')) {
2258 if (contains(input, '/')) {
2259 return dateMinutesSecondsFormat.parse(input);
2260 }
2261
2262 return dateMinutesSecondsNoSlashFormat.parse(input);
2263 }
2264 if (contains(input, '/')) {
2265
2266 int lastDotIndex = input.lastIndexOf('.');
2267 if (lastDotIndex == input.length() - 7) {
2268 String nonNanoInput = input.substring(0, input.length() - 3);
2269 Date date = timestampFormat.parse(nonNanoInput);
2270
2271 String lastThree = input.substring(input.length() - 3, input.length());
2272 int lastThreeInt = Integer.parseInt(lastThree);
2273 Timestamp timestamp = new Timestamp(date.getTime());
2274 timestamp.setNanos(timestamp.getNanos() + (lastThreeInt * 1000));
2275 return timestamp;
2276 }
2277 return timestampFormat.parse(input);
2278 }
2279
2280 return timestampNoSlashFormat.parse(input);
2281 } catch (ParseException pe) {
2282 throw new RuntimeException(errorStart + toStringForLog(input));
2283 }
2284 }
2285
2286 throw new RuntimeException("Cannot convert Object to date : "
2287 + toStringForLog(inputObject));
2288 }
2289
2290
2291
2292
2293 private static Pattern datePattern_yyyy_mm_dd = Pattern
2294 .compile("^(\\d{4})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})$");
2295
2296
2297
2298
2299 private static Pattern datePattern_dd_mon_yyyy = Pattern
2300 .compile("^(\\d{1,2})[^\\d]+([a-zA-Z]{3,15})[^\\d]+(\\d{4})$");
2301
2302
2303
2304
2305 private static Pattern datePattern_yyyy_mm_dd_hhmmss = Pattern
2306 .compile("^(\\d{4})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})$");
2307
2308
2309
2310
2311 private static Pattern datePattern_dd_mon_yyyy_hhmmss = Pattern
2312 .compile("^(\\d{1,2})[^\\d]+([a-zA-Z]{3,15})[^\\d]+(\\d{4})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})$");
2313
2314
2315
2316
2317 private static Pattern datePattern_yyyy_mm_dd_hhmmss_SSS = Pattern
2318 .compile("^(\\d{4})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,3})$");
2319
2320
2321
2322
2323 private static Pattern datePattern_dd_mon_yyyy_hhmmss_SSS = Pattern
2324 .compile("^(\\d{1,2})[^\\d]+([a-zA-Z]{3,15})[^\\d]+(\\d{4})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,2})[^\\d]+(\\d{1,3})$");
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338 public static Date stringToDate2(String input) {
2339
2340 if (isBlank(input)) {
2341 return null;
2342 }
2343 input = input.trim();
2344 Matcher matcher = null;
2345
2346 int month = 0;
2347 int day = 0;
2348 int year = 0;
2349 int hour = 0;
2350 int minute = 0;
2351 int second = 0;
2352 int milli = 0;
2353
2354 boolean foundMatch = false;
2355
2356
2357 if (!foundMatch) {
2358 matcher = datePattern_yyyy_mm_dd.matcher(input);
2359 if (matcher.matches()) {
2360 year = intValue(matcher.group(1));
2361 month = intValue(matcher.group(2));
2362 day = intValue(matcher.group(3));
2363 foundMatch = true;
2364 }
2365 }
2366
2367
2368 if (!foundMatch) {
2369 matcher = datePattern_dd_mon_yyyy.matcher(input);
2370 if (matcher.matches()) {
2371 day = intValue(matcher.group(1));
2372 month = monthInt(matcher.group(2));
2373 year = intValue(matcher.group(3));
2374 foundMatch = true;
2375 }
2376 }
2377
2378
2379 if (!foundMatch) {
2380 matcher = datePattern_yyyy_mm_dd_hhmmss.matcher(input);
2381 if (matcher.matches()) {
2382 year = intValue(matcher.group(1));
2383 month = intValue(matcher.group(2));
2384 day = intValue(matcher.group(3));
2385 hour = intValue(matcher.group(4));
2386 minute = intValue(matcher.group(5));
2387 second = intValue(matcher.group(6));
2388 foundMatch = true;
2389 }
2390 }
2391
2392
2393 if (!foundMatch) {
2394 matcher = datePattern_dd_mon_yyyy_hhmmss.matcher(input);
2395 if (matcher.matches()) {
2396 day = intValue(matcher.group(1));
2397 month = monthInt(matcher.group(2));
2398 year = intValue(matcher.group(3));
2399 hour = intValue(matcher.group(4));
2400 minute = intValue(matcher.group(5));
2401 second = intValue(matcher.group(6));
2402 foundMatch = true;
2403 }
2404 }
2405
2406
2407 if (!foundMatch) {
2408 matcher = datePattern_yyyy_mm_dd_hhmmss_SSS.matcher(input);
2409 if (matcher.matches()) {
2410 year = intValue(matcher.group(1));
2411 month = intValue(matcher.group(2));
2412 day = intValue(matcher.group(3));
2413 hour = intValue(matcher.group(4));
2414 minute = intValue(matcher.group(5));
2415 second = intValue(matcher.group(6));
2416 milli = intValue(matcher.group(7));
2417 foundMatch = true;
2418 }
2419 }
2420
2421
2422 if (!foundMatch) {
2423 matcher = datePattern_dd_mon_yyyy_hhmmss_SSS.matcher(input);
2424 if (matcher.matches()) {
2425 day = intValue(matcher.group(1));
2426 month = monthInt(matcher.group(2));
2427 year = intValue(matcher.group(3));
2428 hour = intValue(matcher.group(4));
2429 minute = intValue(matcher.group(5));
2430 second = intValue(matcher.group(6));
2431 milli = intValue(matcher.group(7));
2432 foundMatch = true;
2433 }
2434 }
2435
2436 Calendar calendar = Calendar.getInstance();
2437 calendar.set(Calendar.YEAR, year);
2438 calendar.set(Calendar.MONTH, month - 1);
2439 calendar.set(Calendar.DAY_OF_MONTH, day);
2440 calendar.set(Calendar.HOUR_OF_DAY, hour);
2441 calendar.set(Calendar.MINUTE, minute);
2442 calendar.set(Calendar.SECOND, second);
2443 calendar.set(Calendar.MILLISECOND, milli);
2444 return calendar.getTime();
2445 }
2446
2447
2448
2449
2450
2451
2452
2453 public static int monthInt(String mon) {
2454
2455 if (!isBlank(mon)) {
2456 mon = mon.toLowerCase();
2457
2458 if (equals(mon, "jan") || equals(mon, "january")) {
2459 return 1;
2460 }
2461
2462 if (equals(mon, "feb") || equals(mon, "february")) {
2463 return 2;
2464 }
2465
2466 if (equals(mon, "mar") || equals(mon, "march")) {
2467 return 3;
2468 }
2469
2470 if (equals(mon, "apr") || equals(mon, "april")) {
2471 return 4;
2472 }
2473
2474 if (equals(mon, "may")) {
2475 return 5;
2476 }
2477
2478 if (equals(mon, "jun") || equals(mon, "june")) {
2479 return 6;
2480 }
2481
2482 if (equals(mon, "jul") || equals(mon, "july")) {
2483 return 7;
2484 }
2485
2486 if (equals(mon, "aug") || equals(mon, "august")) {
2487 return 8;
2488 }
2489
2490 if (equals(mon, "sep") || equals(mon, "september")) {
2491 return 9;
2492 }
2493
2494 if (equals(mon, "oct") || equals(mon, "october")) {
2495 return 10;
2496 }
2497
2498 if (equals(mon, "nov") || equals(mon, "november")) {
2499 return 11;
2500 }
2501
2502 if (equals(mon, "dec") || equals(mon, "december")) {
2503 return 12;
2504 }
2505
2506 }
2507
2508 throw new RuntimeException("Invalid month: " + mon);
2509 }
2510
2511
2512
2513
2514
2515
2516 public static boolean isBlank(Object input) {
2517 if (null == input) {
2518 return true;
2519 }
2520 return (input instanceof String && isBlank((String) input));
2521 }
2522
2523
2524
2525
2526
2527
2528 public static boolean isScalar(Class<?> type) {
2529
2530 if (type.isArray()) {
2531 return false;
2532 }
2533
2534
2535 if (type.isPrimitive()) {
2536 return true;
2537 }
2538
2539 if (Number.class.isAssignableFrom(type)) {
2540 return true;
2541 }
2542
2543 if (Date.class.isAssignableFrom(type)) {
2544 return true;
2545 }
2546 if (Character.class.equals(type)) {
2547 return true;
2548 }
2549
2550 if (CharSequence.class.equals(type) || CharSequence.class.isAssignableFrom(type)) {
2551 return true;
2552 }
2553 if (Class.class == type || Boolean.class == type || type.isEnum()) {
2554 return true;
2555 }
2556
2557 return false;
2558 }
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579 public static Timestamp toTimestamp(Object input) {
2580
2581 if (null == input) {
2582 return null;
2583 } else if (input instanceof java.sql.Timestamp) {
2584 return (Timestamp) input;
2585 } else if (input instanceof String) {
2586 return stringToTimestamp((String) input);
2587 } else if (input instanceof Date) {
2588 return new Timestamp(((Date) input).getTime());
2589 } else if (input instanceof java.sql.Date) {
2590 return new Timestamp(((java.sql.Date) input).getTime());
2591 } else {
2592 throw new RuntimeException("Cannot convert Object to timestamp : " + input);
2593 }
2594
2595 }
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605 public static String stringValue(Object input) {
2606
2607 if (input == null) {
2608 return (String) input;
2609 }
2610
2611 if (input instanceof Timestamp) {
2612
2613 return timestampToString((Timestamp) input);
2614 }
2615
2616 if (input instanceof Date) {
2617
2618 return stringValue((Date) input);
2619 }
2620
2621 if (input instanceof Number) {
2622 DecimalFormat decimalFormat = new DecimalFormat(
2623 "###################.###############");
2624 return decimalFormat.format(((Number) input).doubleValue());
2625
2626 }
2627
2628 return input.toString();
2629 }
2630
2631
2632
2633
2634
2635
2636 public synchronized static String timestampToString(Date timestamp) {
2637 if (timestamp == null) {
2638 return null;
2639 }
2640 return timestampFormat.format(timestamp);
2641 }
2642
2643
2644
2645
2646
2647
2648 public synchronized static String timestampToFileString(Date timestamp) {
2649 if (timestamp == null) {
2650 return null;
2651 }
2652 return timestampFileFormat.format(timestamp);
2653 }
2654
2655
2656
2657
2658
2659
2660 synchronized static SimpleDateFormat dateFormat() {
2661 return dateFormat;
2662 }
2663
2664
2665
2666
2667
2668
2669 synchronized static SimpleDateFormat dateFormat2() {
2670 return dateFormat2;
2671 }
2672
2673
2674
2675
2676
2677
2678 public static String stringValue(java.util.Date date) {
2679 synchronized (GcElUtilsSafe.class) {
2680 if (date == null) {
2681 return null;
2682 }
2683
2684
2685 Calendar calendar = new GregorianCalendar();
2686 calendar.setTime(date);
2687 if (calendar.get(Calendar.HOUR_OF_DAY) == 0
2688 && calendar.get(Calendar.MINUTE) == 0
2689 && calendar.get(Calendar.SECOND) == 0
2690 && calendar.get(Calendar.MILLISECOND) == 0) {
2691
2692 String theString = dateFormat2().format(date);
2693
2694 return theString;
2695
2696 }
2697
2698
2699 return timestampToString(date);
2700
2701 }
2702 }
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715 public static Timestamp stringToTimestamp(String input) {
2716 Date date = stringToTimestampHelper(input);
2717 if (date == null) {
2718 return null;
2719 }
2720
2721 if (date instanceof Timestamp) {
2722 return (Timestamp) date;
2723 }
2724 return new Timestamp(date.getTime());
2725 }
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739 synchronized static Date stringToTimestampHelper(String input) {
2740
2741 if (isBlank(input)) {
2742 return null;
2743 }
2744 input = input.trim();
2745 try {
2746
2747 if (equals("99999999", input)
2748 || equals("999999", input)) {
2749 input = "20991231";
2750 }
2751 if (input.length() == 8) {
2752
2753 return dateFormat().parse(input);
2754 }
2755 if (input.length() == 10) {
2756
2757 return dateFormat2().parse(input);
2758 }
2759 if (!contains(input, '.')) {
2760 if (contains(input, '/')) {
2761 return dateMinutesSecondsFormat.parse(input);
2762 }
2763
2764 return dateMinutesSecondsNoSlashFormat.parse(input);
2765 }
2766 if (contains(input, '/')) {
2767
2768 int lastDotIndex = input.lastIndexOf('.');
2769 if (lastDotIndex == input.length() - 7) {
2770 String nonNanoInput = input.substring(0, input.length() - 3);
2771 Date date = timestampFormat.parse(nonNanoInput);
2772
2773 String lastThree = input.substring(input.length() - 3, input.length());
2774 int lastThreeInt = Integer.parseInt(lastThree);
2775 Timestamp timestamp = new Timestamp(date.getTime());
2776 timestamp.setNanos(timestamp.getNanos() + (lastThreeInt * 1000));
2777 return timestamp;
2778 }
2779 return timestampFormat.parse(input);
2780 }
2781
2782 return timestampNoSlashFormat.parse(input);
2783 } catch (ParseException pe) {
2784 throw new RuntimeException(errorStart + input);
2785 }
2786 }
2787
2788
2789
2790
2791 private static final String errorStart = "Invalid timestamp, please use any of the formats: "
2792 + DATE_FORMAT + ", " + TIMESTAMP_FORMAT
2793 + ", " + DATE_MINUTES_SECONDS_FORMAT + ": ";
2794
2795
2796
2797
2798
2799
2800 public static BigDecimal bigDecimalObjectValue(Object input) {
2801 if (input instanceof BigDecimal) {
2802 return (BigDecimal) input;
2803 }
2804 if (isBlank(input)) {
2805 return null;
2806 }
2807 return BigDecimal.valueOf(doubleValue(input));
2808 }
2809
2810
2811
2812
2813
2814
2815 public static Byte byteObjectValue(Object input) {
2816 if (input instanceof Byte) {
2817 return (Byte) input;
2818 }
2819 if (isBlank(input)) {
2820 return null;
2821 }
2822 return Byte.valueOf(byteValue(input));
2823 }
2824
2825
2826
2827
2828
2829
2830 public static byte byteValue(Object input) {
2831 if (input instanceof String) {
2832 String string = (String) input;
2833 return Byte.parseByte(string);
2834 }
2835 if (input instanceof Number) {
2836 return ((Number) input).byteValue();
2837 }
2838 throw new RuntimeException("Cannot convert to byte: " + className(input));
2839 }
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850 public static Double doubleObjectValue(Object input, boolean allowNullBlank) {
2851
2852 if (input instanceof Double) {
2853 return (Double) input;
2854 }
2855
2856 if (allowNullBlank && isBlank(input)) {
2857 return null;
2858 }
2859
2860 return Double.valueOf(doubleValue(input));
2861 }
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871 public static double doubleValue(Object input) {
2872 if (input instanceof String) {
2873 String string = (String) input;
2874 return Double.parseDouble(string);
2875 }
2876 if (input instanceof Number) {
2877 return ((Number) input).doubleValue();
2878 }
2879 throw new RuntimeException("Cannot convert to double: " + className(input));
2880 }
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892 public static double doubleValueNoError(Object input) {
2893 if (input == null || (input instanceof String
2894 && isBlank((String) input))) {
2895 return NOT_FOUND;
2896 }
2897
2898 try {
2899 return doubleValue(input);
2900 } catch (Exception e) {
2901
2902 }
2903
2904 return NOT_FOUND;
2905 }
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916 public static Float floatObjectValue(Object input, boolean allowNullBlank) {
2917
2918 if (input instanceof Float) {
2919 return (Float) input;
2920 }
2921
2922 if (allowNullBlank && isBlank(input)) {
2923 return null;
2924 }
2925 return Float.valueOf(floatValue(input));
2926 }
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936 public static float floatValue(Object input) {
2937 if (input instanceof String) {
2938 String string = (String) input;
2939 return Float.parseFloat(string);
2940 }
2941 if (input instanceof Number) {
2942 return ((Number) input).floatValue();
2943 }
2944 throw new RuntimeException("Cannot convert to float: " + className(input));
2945 }
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956 public static float floatValueNoError(Object input) {
2957 if (input == null || (input instanceof String
2958 && isBlank((String) input))) {
2959 return NOT_FOUND;
2960 }
2961 try {
2962 return floatValue(input);
2963 } catch (Exception e) {
2964 LOG.error(e);
2965 }
2966
2967 return NOT_FOUND;
2968 }
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979 public static Integer intObjectValue(Object input, boolean allowNullBlank) {
2980
2981 if (input instanceof Integer) {
2982 return (Integer) input;
2983 }
2984
2985 if (allowNullBlank && isBlank(input)) {
2986 return null;
2987 }
2988
2989 return Integer.valueOf(intValue(input));
2990 }
2991
2992
2993
2994
2995
2996
2997 public static int intValue(Object input) {
2998 if (input instanceof String) {
2999 String string = (String) input;
3000 return Integer.parseInt(string);
3001 }
3002 if (input instanceof Number) {
3003 return ((Number) input).intValue();
3004 }
3005 throw new RuntimeException("Cannot convert to int: " + className(input));
3006 }
3007
3008
3009
3010
3011
3012
3013
3014 public static int intValue(Object input, int valueIfNull) {
3015 if (input == null || "".equals(input)) {
3016 return valueIfNull;
3017 }
3018 return intObjectValue(input, false);
3019 }
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030 public static int intValueNoError(Object input) {
3031 if (input == null || (input instanceof String
3032 && isBlank((String) input))) {
3033 return NOT_FOUND;
3034 }
3035 try {
3036 return intValue(input);
3037 } catch (Exception e) {
3038
3039 }
3040
3041 return NOT_FOUND;
3042 }
3043
3044
3045 public static final int NOT_FOUND = -999999999;
3046
3047
3048
3049
3050 private static Log LOG = GrouperClientUtils.retrieveLog(GcElUtilsSafe.class);
3051
3052
3053
3054
3055 public static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066 public static Long longObjectValue(Object input, boolean allowNullBlank) {
3067
3068 if (input instanceof Long) {
3069 return (Long) input;
3070 }
3071
3072 if (allowNullBlank && isBlank(input)) {
3073 return null;
3074 }
3075
3076 return Long.valueOf(longValue(input));
3077 }
3078
3079
3080
3081
3082
3083
3084 public static long longValue(Object input) {
3085 if (input instanceof String) {
3086 String string = (String) input;
3087 return Long.parseLong(string);
3088 }
3089 if (input instanceof Number) {
3090 return ((Number) input).longValue();
3091 }
3092 throw new RuntimeException("Cannot convert to long: " + className(input));
3093 }
3094
3095
3096
3097
3098
3099
3100
3101 public static long longValue(Object input, long valueIfNull) {
3102 if (input == null || "".equals(input)) {
3103 return valueIfNull;
3104 }
3105 return longObjectValue(input, false);
3106 }
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117 public static long longValueNoError(Object input) {
3118 if (input == null || (input instanceof String
3119 && isBlank((String) input))) {
3120 return NOT_FOUND;
3121 }
3122 try {
3123 return longValue(input);
3124 } catch (Exception e) {
3125
3126 }
3127
3128 return NOT_FOUND;
3129 }
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139 public static Short shortObjectValue(Object input) {
3140
3141 if (input instanceof Short) {
3142 return (Short) input;
3143 }
3144
3145 if (isBlank(input)) {
3146 return null;
3147 }
3148
3149 return Short.valueOf(shortValue(input));
3150 }
3151
3152
3153
3154
3155
3156
3157 public static short shortValue(Object input) {
3158 if (input instanceof String) {
3159 String string = (String) input;
3160 return Short.parseShort(string);
3161 }
3162 if (input instanceof Number) {
3163 return ((Number) input).shortValue();
3164 }
3165 throw new RuntimeException("Cannot convert to short: " + className(input));
3166 }
3167
3168
3169
3170
3171
3172
3173 public static Character charObjectValue(Object input) {
3174 if (input instanceof Character) {
3175 return (Character) input;
3176 }
3177 if (isBlank(input)) {
3178 return null;
3179 }
3180 return new Character(charValue(input));
3181 }
3182
3183
3184
3185
3186
3187
3188 public static char charValue(Object input) {
3189 if (input instanceof Character) {
3190 return ((Character) input).charValue();
3191 }
3192
3193 if (input instanceof String) {
3194 String inputString = (String) input;
3195 if (inputString.length() == 1) {
3196 return inputString.charAt(0);
3197 }
3198 }
3199 throw new RuntimeException("Cannot convert to char: "
3200 + (input == null ? null : (input.getClass() + ", " + input)));
3201 }
3202
3203
3204
3205
3206
3207
3208 public static String replaceWhitespaceWithSpace(String input) {
3209 if (input == null) {
3210 return input;
3211 }
3212 return input.replaceAll("\\s+", " ");
3213 }
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224 public static String convertLongToChar(long theLong) {
3225 if ((theLong < 0) || (theLong >= 62)) {
3226 throw new RuntimeException("convertLongToChar() "
3227 + " invalid input (not >=0 && <62: " + theLong);
3228 } else if (theLong < 26) {
3229 return "" + (char) ('a' + theLong);
3230 } else if (theLong < 52) {
3231 return "" + (char) ('A' + (theLong - 26));
3232 } else {
3233 return "" + (char) ('0' + (theLong - 52));
3234 }
3235 }
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246 public static String convertLongToCharSmall(long theLong) {
3247 if ((theLong < 0) || (theLong >= 36)) {
3248 throw new RuntimeException("convertLongToCharSmall() "
3249 + " invalid input (not >=0 && <36: " + theLong);
3250 } else if (theLong < 26) {
3251 return "" + (char) ('A' + theLong);
3252 } else {
3253 return "" + (char) ('0' + (theLong - 26));
3254 }
3255 }
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266 public static String convertLongToString(long theLong) {
3267 long quotient = theLong / 62;
3268 long remainder = theLong % 62;
3269
3270 if (quotient == 0) {
3271 return convertLongToChar(remainder);
3272 }
3273 StringBuffer result = new StringBuffer();
3274 result.append(convertLongToString(quotient));
3275 result.append(convertLongToChar(remainder));
3276
3277 return result.toString();
3278 }
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289 public static String convertLongToStringSmall(long theLong) {
3290 long quotient = theLong / 36;
3291 long remainder = theLong % 36;
3292
3293 if (quotient == 0) {
3294 return convertLongToCharSmall(remainder);
3295 }
3296 StringBuffer result = new StringBuffer();
3297 result.append(convertLongToStringSmall(quotient));
3298 result.append(convertLongToCharSmall(remainder));
3299
3300 return result.toString();
3301 }
3302
3303
3304
3305
3306
3307
3308
3309
3310 public static char incrementChar(char theChar) {
3311 if (theChar == 'Z') {
3312 return '0';
3313 }
3314
3315 if (theChar == '9') {
3316 return 'A';
3317 }
3318
3319 return ++theChar;
3320 }
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330 public static char[] incrementStringInt(char[] string) {
3331 if (string == null) {
3332 return string;
3333 }
3334
3335
3336 int i = 0;
3337
3338 for (i = string.length - 1; i >= 0; i--) {
3339 char inc = string[i];
3340 inc = incrementChar(inc);
3341 string[i] = inc;
3342
3343 if (inc != 'A') {
3344 break;
3345 }
3346 }
3347
3348
3349 if (i < 0) {
3350 return ("A" + new String(string)).toCharArray();
3351 }
3352
3353 return string;
3354 }
3355
3356
3357
3358
3359
3360
3361 public static boolean isAscii(char input) {
3362 return input < 128;
3363 }
3364
3365
3366
3367
3368
3369
3370 public static int lengthAscii(String input) {
3371 if (input == null) {
3372 return 0;
3373 }
3374
3375 int utfLength = input.length();
3376
3377 int extras = 0;
3378 for (int i = 0; i < utfLength; i++) {
3379
3380 if (!isAscii(input.charAt(i))) {
3381 extras++;
3382 }
3383 }
3384 return utfLength + extras;
3385 }
3386
3387
3388
3389
3390
3391
3392 public static int stringLength(String string) {
3393 return string == null ? 0 : string.length();
3394 }
3395
3396
3397
3398
3399
3400
3401
3402 public static String truncateAscii(String input, int requiredLength) {
3403 if (input == null) {
3404 return input;
3405 }
3406
3407 int utfLength = input.length();
3408
3409
3410 if (utfLength * 2 < requiredLength) {
3411 return input;
3412 }
3413
3414
3415 int asciiLength = 0;
3416 for (int i = 0; i < utfLength; i++) {
3417
3418 asciiLength++;
3419
3420
3421 if (!isAscii(input.charAt(i))) {
3422 asciiLength++;
3423 }
3424
3425
3426 if (asciiLength > requiredLength) {
3427
3428 return input.substring(0, i);
3429 }
3430 }
3431
3432 return input;
3433 }
3434
3435
3436
3437
3438
3439
3440
3441 public static boolean equals(String first, String second) {
3442 if (first == second) {
3443 return true;
3444 }
3445 if (first == null || second == null) {
3446 return false;
3447 }
3448 return first.equals(second);
3449 }
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466 public static boolean isBlank(String str) {
3467 int strLen;
3468 if (str == null || (strLen = str.length()) == 0) {
3469 return true;
3470 }
3471 for (int i = 0; i < strLen; i++) {
3472 if ((Character.isWhitespace(str.charAt(i)) == false)) {
3473 return false;
3474 }
3475 }
3476 return true;
3477 }
3478
3479
3480
3481
3482
3483
3484 public static boolean isNotBlank(String str) {
3485 return !isBlank(str);
3486 }
3487
3488
3489
3490
3491
3492
3493 public static String trim(String str) {
3494 return str == null ? null : str.trim();
3495 }
3496
3497
3498
3499
3500
3501
3502
3503 public static boolean equalsIgnoreCase(String str1, String str2) {
3504 return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
3505 }
3506
3507
3508
3509
3510
3511
3512 public static String trimToEmpty(String str) {
3513 return str == null ? "" : str.trim();
3514 }
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548 public static String abbreviate(String str, int maxWidth) {
3549 return abbreviate(str, 0, maxWidth);
3550 }
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587 public static String abbreviate(String str, int offset, int maxWidth) {
3588 if (str == null) {
3589 return null;
3590 }
3591 if (maxWidth < 4) {
3592 throw new IllegalArgumentException("Minimum abbreviation width is 4");
3593 }
3594 if (str.length() <= maxWidth) {
3595 return str;
3596 }
3597 if (offset > str.length()) {
3598 offset = str.length();
3599 }
3600 if ((str.length() - offset) < (maxWidth - 3)) {
3601 offset = str.length() - (maxWidth - 3);
3602 }
3603 if (offset <= 4) {
3604 return str.substring(0, maxWidth - 3) + "...";
3605 }
3606 if (maxWidth < 7) {
3607 throw new IllegalArgumentException("Minimum abbreviation width with offset is 7");
3608 }
3609 if ((offset + (maxWidth - 3)) < str.length()) {
3610 return "..." + abbreviate(str.substring(offset), maxWidth - 3);
3611 }
3612 return "..." + str.substring(str.length() - (maxWidth - 3));
3613 }
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639 public static String[] split(String str) {
3640 return split(str, null, -1);
3641 }
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669 public static String[] split(String str, char separatorChar) {
3670 return splitWorker(str, separatorChar, false);
3671 }
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698 public static String[] split(String str, String separatorChars) {
3699 return splitWorker(str, separatorChars, -1, false);
3700 }
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732 public static String[] split(String str, String separatorChars, int max) {
3733 return splitWorker(str, separatorChars, max, false);
3734 }
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760 public static String[] splitByWholeSeparator(String str, String separator) {
3761 return splitByWholeSeparator(str, separator, -1);
3762 }
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791 @SuppressWarnings("unchecked")
3792 public static String[] splitByWholeSeparator(String str, String separator, int max) {
3793 if (str == null) {
3794 return null;
3795 }
3796
3797 int len = str.length();
3798
3799 if (len == 0) {
3800 return EMPTY_STRING_ARRAY;
3801 }
3802
3803 if ((separator == null) || ("".equals(separator))) {
3804
3805 return split(str, null, max);
3806 }
3807
3808 int separatorLength = separator.length();
3809
3810 ArrayList substrings = new ArrayList();
3811 int numberOfSubstrings = 0;
3812 int beg = 0;
3813 int end = 0;
3814 while (end < len) {
3815 end = str.indexOf(separator, beg);
3816
3817 if (end > -1) {
3818 if (end > beg) {
3819 numberOfSubstrings += 1;
3820
3821 if (numberOfSubstrings == max) {
3822 end = len;
3823 substrings.add(str.substring(beg));
3824 } else {
3825
3826
3827 substrings.add(str.substring(beg, end));
3828
3829
3830
3831
3832 beg = end + separatorLength;
3833 }
3834 } else {
3835
3836 beg = end + separatorLength;
3837 }
3838 } else {
3839
3840 substrings.add(str.substring(beg));
3841 end = len;
3842 }
3843 }
3844
3845 return (String[]) substrings.toArray(new String[substrings.size()]);
3846 }
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873 public static String[] splitPreserveAllTokens(String str) {
3874 return splitWorker(str, null, -1, true);
3875 }
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909 public static String[] splitPreserveAllTokens(String str, char separatorChar) {
3910 return splitWorker(str, separatorChar, true);
3911 }
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925 @SuppressWarnings("unchecked")
3926 private static String[] splitWorker(String str, char separatorChar,
3927 boolean preserveAllTokens) {
3928
3929
3930 if (str == null) {
3931 return null;
3932 }
3933 int len = str.length();
3934 if (len == 0) {
3935 return EMPTY_STRING_ARRAY;
3936 }
3937 List list = new ArrayList();
3938 int i = 0, start = 0;
3939 boolean match = false;
3940 boolean lastMatch = false;
3941 while (i < len) {
3942 if (str.charAt(i) == separatorChar) {
3943 if (match || preserveAllTokens) {
3944 list.add(str.substring(start, i));
3945 match = false;
3946 lastMatch = true;
3947 }
3948 start = ++i;
3949 continue;
3950 }
3951 lastMatch = false;
3952 match = true;
3953 i++;
3954 }
3955 if (match || (preserveAllTokens && lastMatch)) {
3956 list.add(str.substring(start, i));
3957 }
3958 return (String[]) list.toArray(new String[list.size()]);
3959 }
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994 public static String[] splitPreserveAllTokens(String str, String separatorChars) {
3995 return splitWorker(str, separatorChars, -1, true);
3996 }
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034 public static String[] splitPreserveAllTokens(String str, String separatorChars, int max) {
4035 return splitWorker(str, separatorChars, max, true);
4036 }
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052 @SuppressWarnings("unchecked")
4053 private static String[] splitWorker(String str, String separatorChars, int max,
4054 boolean preserveAllTokens) {
4055
4056
4057
4058
4059 if (str == null) {
4060 return null;
4061 }
4062 int len = str.length();
4063 if (len == 0) {
4064 return EMPTY_STRING_ARRAY;
4065 }
4066 List list = new ArrayList();
4067 int sizePlus1 = 1;
4068 int i = 0, start = 0;
4069 boolean match = false;
4070 boolean lastMatch = false;
4071 if (separatorChars == null) {
4072
4073 while (i < len) {
4074 if (Character.isWhitespace(str.charAt(i))) {
4075 if (match || preserveAllTokens) {
4076 lastMatch = true;
4077 if (sizePlus1++ == max) {
4078 i = len;
4079 lastMatch = false;
4080 }
4081 list.add(str.substring(start, i));
4082 match = false;
4083 }
4084 start = ++i;
4085 continue;
4086 }
4087 lastMatch = false;
4088 match = true;
4089 i++;
4090 }
4091 } else if (separatorChars.length() == 1) {
4092
4093 char sep = separatorChars.charAt(0);
4094 while (i < len) {
4095 if (str.charAt(i) == sep) {
4096 if (match || preserveAllTokens) {
4097 lastMatch = true;
4098 if (sizePlus1++ == max) {
4099 i = len;
4100 lastMatch = false;
4101 }
4102 list.add(str.substring(start, i));
4103 match = false;
4104 }
4105 start = ++i;
4106 continue;
4107 }
4108 lastMatch = false;
4109 match = true;
4110 i++;
4111 }
4112 } else {
4113
4114 while (i < len) {
4115 if (separatorChars.indexOf(str.charAt(i)) >= 0) {
4116 if (match || preserveAllTokens) {
4117 lastMatch = true;
4118 if (sizePlus1++ == max) {
4119 i = len;
4120 lastMatch = false;
4121 }
4122 list.add(str.substring(start, i));
4123 match = false;
4124 }
4125 start = ++i;
4126 continue;
4127 }
4128 lastMatch = false;
4129 match = true;
4130 i++;
4131 }
4132 }
4133 if (match || (preserveAllTokens && lastMatch)) {
4134 list.add(str.substring(start, i));
4135 }
4136 return (String[]) list.toArray(new String[list.size()]);
4137 }
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159 public static String join(Object[] array) {
4160 return join(array, null);
4161 }
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185 public static String join(Object[] array, char separator) {
4186 if (array == null) {
4187 return null;
4188 }
4189 int arraySize = array.length;
4190 int bufSize = (arraySize == 0 ? 0 : ((array[0] == null ? 16 : array[0].toString()
4191 .length()) + 1)
4192 * arraySize);
4193 StringBuffer buf = new StringBuffer(bufSize);
4194
4195 for (int i = 0; i < arraySize; i++) {
4196 if (i > 0) {
4197 buf.append(separator);
4198 }
4199 if (array[i] != null) {
4200 buf.append(array[i]);
4201 }
4202 }
4203 return buf.toString();
4204 }
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229 public static String join(Object[] array, String separator) {
4230 if (array == null) {
4231 return null;
4232 }
4233 if (separator == null) {
4234 separator = "";
4235 }
4236 int arraySize = array.length;
4237
4238
4239
4240
4241 int bufSize = ((arraySize == 0) ? 0 : arraySize
4242 * ((array[0] == null ? 16 : array[0].toString().length()) + separator.length()));
4243
4244 StringBuffer buf = new StringBuffer(bufSize);
4245
4246 for (int i = 0; i < arraySize; i++) {
4247 if (i > 0) {
4248 buf.append(separator);
4249 }
4250 if (array[i] != null) {
4251 buf.append(array[i]);
4252 }
4253 }
4254 return buf.toString();
4255 }
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271 public static String join(Iterator iterator, char separator) {
4272 if (iterator == null) {
4273 return null;
4274 }
4275 StringBuffer buf = new StringBuffer(256);
4276 while (iterator.hasNext()) {
4277 Object obj = iterator.next();
4278 if (obj != null) {
4279 buf.append(obj);
4280 }
4281 if (iterator.hasNext()) {
4282 buf.append(separator);
4283 }
4284 }
4285 return buf.toString();
4286 }
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301 public static String join(Iterator iterator, String separator) {
4302 if (iterator == null) {
4303 return null;
4304 }
4305 StringBuffer buf = new StringBuffer(256);
4306 while (iterator.hasNext()) {
4307 Object obj = iterator.next();
4308 if (obj != null) {
4309 buf.append(obj);
4310 }
4311 if ((separator != null) && iterator.hasNext()) {
4312 buf.append(separator);
4313 }
4314 }
4315 return buf.toString();
4316 }
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333 public static String defaultString(String str) {
4334 return str == null ? "" : str;
4335 }
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353 public static String defaultString(String str, String defaultStr) {
4354 return str == null ? defaultStr : str;
4355 }
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372 public static String defaultIfEmpty(String str, String defaultStr) {
4373 return isEmpty(str) ? defaultStr : str;
4374 }
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393 public static String capitalize(String str) {
4394 int strLen;
4395 if (str == null || (strLen = str.length()) == 0) {
4396 return str;
4397 }
4398 return new StringBuffer(strLen).append(Character.toTitleCase(str.charAt(0))).append(
4399 str.substring(1)).toString();
4400 }
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421 public static boolean contains(String str, char searchChar) {
4422 if (isEmpty(str)) {
4423 return false;
4424 }
4425 return str.indexOf(searchChar) >= 0;
4426 }
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449 public static boolean contains(String str, String searchStr) {
4450 if (str == null || searchStr == null) {
4451 return false;
4452 }
4453 return str.indexOf(searchStr) >= 0;
4454 }
4455
4456
4457
4458
4459 public static final String[] EMPTY_STRING_ARRAY = new String[0];
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480 public static boolean equals(Object object1, Object object2) {
4481 if (object1 == object2) {
4482 return true;
4483 }
4484 if ((object1 == null) || (object2 == null)) {
4485 return false;
4486 }
4487 return object1.equals(object2);
4488 }
4489
4490
4491
4492
4493 public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
4494
4495
4496
4497
4498
4499
4500
4501
4502 public static String stripLastSlashIfExists(String input) {
4503 if ((input == null) || (input.length() == 0)) {
4504 return null;
4505 }
4506
4507 char lastChar = input.charAt(input.length() - 1);
4508
4509 if ((lastChar == '\\') || (lastChar == '/')) {
4510 return input.substring(0, input.length() - 1);
4511 }
4512
4513 return input;
4514 }
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540 public static String stripStart(String str, String stripChars) {
4541 int strLen;
4542 if (str == null || (strLen = str.length()) == 0) {
4543 return str;
4544 }
4545 int start = 0;
4546 if (stripChars == null) {
4547 while ((start != strLen) && Character.isWhitespace(str.charAt(start))) {
4548 start++;
4549 }
4550 } else if (stripChars.length() == 0) {
4551 return str;
4552 } else {
4553 while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != -1)) {
4554 start++;
4555 }
4556 }
4557 return str.substring(start);
4558 }
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584 public static String stripEnd(String str, String stripChars) {
4585 int end;
4586 if (str == null || (end = str.length()) == 0) {
4587 return str;
4588 }
4589
4590 if (stripChars == null) {
4591 while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) {
4592 end--;
4593 }
4594 } else if (stripChars.length() == 0) {
4595 return str;
4596 } else {
4597 while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) {
4598 end--;
4599 }
4600 }
4601 return str.substring(0, end);
4602 }
4603
4604
4605
4606
4607
4608 public static final String EMPTY = "";
4609
4610
4611
4612
4613
4614 public static final int INDEX_NOT_FOUND = -1;
4615
4616
4617
4618
4619 private static final int PAD_LIMIT = 8192;
4620
4621
4622
4623
4624
4625
4626 private static final String[] PADDING = new String[Character.MAX_VALUE];
4627
4628 static {
4629
4630 PADDING[32] = " ";
4631 }
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651 public static String repeat(String str, int repeat) {
4652
4653
4654 if (str == null) {
4655 return null;
4656 }
4657 if (repeat <= 0) {
4658 return EMPTY;
4659 }
4660 int inputLength = str.length();
4661 if (repeat == 1 || inputLength == 0) {
4662 return str;
4663 }
4664 if (inputLength == 1 && repeat <= PAD_LIMIT) {
4665 return padding(repeat, str.charAt(0));
4666 }
4667
4668 int outputLength = inputLength * repeat;
4669 switch (inputLength) {
4670 case 1:
4671 char ch = str.charAt(0);
4672 char[] output1 = new char[outputLength];
4673 for (int i = repeat - 1; i >= 0; i--) {
4674 output1[i] = ch;
4675 }
4676 return new String(output1);
4677 case 2:
4678 char ch0 = str.charAt(0);
4679 char ch1 = str.charAt(1);
4680 char[] output2 = new char[outputLength];
4681 for (int i = repeat * 2 - 2; i >= 0; i--, i--) {
4682 output2[i] = ch0;
4683 output2[i + 1] = ch1;
4684 }
4685 return new String(output2);
4686 default:
4687 StringBuffer buf = new StringBuffer(outputLength);
4688 for (int i = 0; i < repeat; i++) {
4689 buf.append(str);
4690 }
4691 return buf.toString();
4692 }
4693 }
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710 private static String padding(int repeat, char padChar) {
4711
4712
4713 String pad = PADDING[padChar];
4714 if (pad == null) {
4715 pad = String.valueOf(padChar);
4716 }
4717 while (pad.length() < repeat) {
4718 pad = pad.concat(pad);
4719 }
4720 PADDING[padChar] = pad;
4721 return pad.substring(0, repeat);
4722 }
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743 public static String rightPad(String str, int size) {
4744 return rightPad(str, size, ' ');
4745 }
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768 public static String rightPad(String str, int size, char padChar) {
4769 if (str == null) {
4770 return null;
4771 }
4772 int pads = size - str.length();
4773 if (pads <= 0) {
4774 return str;
4775 }
4776 if (pads > PAD_LIMIT) {
4777 return rightPad(str, size, String.valueOf(padChar));
4778 }
4779 return str.concat(padding(pads, padChar));
4780 }
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805 public static String rightPad(String str, int size, String padStr) {
4806 if (str == null) {
4807 return null;
4808 }
4809 if (isEmpty(padStr)) {
4810 padStr = " ";
4811 }
4812 int padLen = padStr.length();
4813 int strLen = str.length();
4814 int pads = size - strLen;
4815 if (pads <= 0) {
4816 return str;
4817 }
4818 if (padLen == 1 && pads <= PAD_LIMIT) {
4819 return rightPad(str, size, padStr.charAt(0));
4820 }
4821
4822 if (pads == padLen) {
4823 return str.concat(padStr);
4824 } else if (pads < padLen) {
4825 return str.concat(padStr.substring(0, pads));
4826 } else {
4827 char[] padding = new char[pads];
4828 char[] padChars = padStr.toCharArray();
4829 for (int i = 0; i < pads; i++) {
4830 padding[i] = padChars[i % padLen];
4831 }
4832 return str.concat(new String(padding));
4833 }
4834 }
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855 public static String leftPad(String str, int size) {
4856 return leftPad(str, size, ' ');
4857 }
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880 public static String leftPad(String str, int size, char padChar) {
4881 if (str == null) {
4882 return null;
4883 }
4884 int pads = size - str.length();
4885 if (pads <= 0) {
4886 return str;
4887 }
4888 if (pads > PAD_LIMIT) {
4889 return leftPad(str, size, String.valueOf(padChar));
4890 }
4891 return padding(pads, padChar).concat(str);
4892 }
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917 public static String leftPad(String str, int size, String padStr) {
4918 if (str == null) {
4919 return null;
4920 }
4921 if (isEmpty(padStr)) {
4922 padStr = " ";
4923 }
4924 int padLen = padStr.length();
4925 int strLen = str.length();
4926 int pads = size - strLen;
4927 if (pads <= 0) {
4928 return str;
4929 }
4930 if (padLen == 1 && pads <= PAD_LIMIT) {
4931 return leftPad(str, size, padStr.charAt(0));
4932 }
4933
4934 if (pads == padLen) {
4935 return padStr.concat(str);
4936 } else if (pads < padLen) {
4937 return padStr.substring(0, pads).concat(str);
4938 } else {
4939 char[] padding = new char[pads];
4940 char[] padChars = padStr.toCharArray();
4941 for (int i = 0; i < pads; i++) {
4942 padding[i] = padChars[i % padLen];
4943 }
4944 return new String(padding).concat(str);
4945 }
4946 }
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973 public static String substringBefore(String str, String separator) {
4974 if (isEmpty(str) || separator == null) {
4975 return str;
4976 }
4977 if (separator.length() == 0) {
4978 return EMPTY;
4979 }
4980 int pos = str.indexOf(separator);
4981 if (pos == -1) {
4982 return str;
4983 }
4984 return str.substring(0, pos);
4985 }
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013 public static String substringAfter(String str, String separator) {
5014 if (isEmpty(str)) {
5015 return str;
5016 }
5017 if (separator == null) {
5018 return EMPTY;
5019 }
5020 int pos = str.indexOf(separator);
5021 if (pos == -1) {
5022 return EMPTY;
5023 }
5024 return str.substring(pos + separator.length());
5025 }
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052 public static String substringBeforeLast(String str, String separator) {
5053 if (isEmpty(str) || isEmpty(separator)) {
5054 return str;
5055 }
5056 int pos = str.lastIndexOf(separator);
5057 if (pos == -1) {
5058 return str;
5059 }
5060 return str.substring(0, pos);
5061 }
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090 public static String substringAfterLast(String str, String separator) {
5091 if (isEmpty(str)) {
5092 return str;
5093 }
5094 if (isEmpty(separator)) {
5095 return EMPTY;
5096 }
5097 int pos = str.lastIndexOf(separator);
5098 if (pos == -1 || pos == (str.length() - separator.length())) {
5099 return EMPTY;
5100 }
5101 return str.substring(pos + separator.length());
5102 }
5103
5104
5105
5106
5107
5108
5109
5110
5111 public static <T> T listPopOne(List<T> list) {
5112 int size = length(list);
5113 if (size == 1) {
5114 return list.get(0);
5115 } else if (size == 0) {
5116 return null;
5117 }
5118 throw new RuntimeException("More than one object of type " + className(list.get(0))
5119 + " was returned when only one was expected. (size:" + size + ")");
5120 }
5121
5122
5123
5124
5125
5126
5127
5128
5129 public static <T> T setPopOne(Set<T> set) {
5130 int size = length(set);
5131 if (size == 1) {
5132 return set.iterator().next();
5133 } else if (size == 0) {
5134 return null;
5135 }
5136 throw new RuntimeException("More than one object of type "
5137 + className(set.iterator().next())
5138 + " was returned when only one was expected. (size:" + size + ")");
5139 }
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149 public static <T> T collectionPopOne(Collection<T> collection,
5150 boolean exceptionIfMoreThanOne) {
5151 int size = length(collection);
5152 if (size > 1 && exceptionIfMoreThanOne) {
5153 throw new RuntimeException("More than one object of type "
5154 + className(get(collection, 0))
5155 + " was returned when only one was expected. (size:" + size + ")");
5156 }
5157 if (size == 0) {
5158 return null;
5159 }
5160 return collection.iterator().next();
5161 }
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172 public static String xmlEscape(String input, boolean isEscape) {
5173 if (isEscape) {
5174 return replace(input, XML_SEARCH_NO_SINGLE, XML_REPLACE_NO_SINGLE);
5175 }
5176 return replace(input, XML_REPLACE_NO_SINGLE, XML_SEARCH_NO_SINGLE);
5177 }
5178
5179
5180
5181
5182
5183
5184
5185
5186 public static boolean appendIfNotBlank(StringBuilder result,
5187 Object theStringOrArrayOrList) {
5188 return appendIfNotBlank(result, null, theStringOrArrayOrList, null);
5189 }
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200 public static String appendIfNotBlankString(String string, String separator,
5201 String suffix) {
5202
5203 string = trimToEmpty(string);
5204 suffix = trimToEmpty(suffix);
5205
5206 boolean stringIsBlank = isBlank(string);
5207 boolean suffixIsBlank = isBlank(suffix);
5208
5209 if (stringIsBlank && suffixIsBlank) {
5210 return "";
5211 }
5212
5213 if (stringIsBlank) {
5214 return suffix;
5215 }
5216
5217 if (suffixIsBlank) {
5218 return string;
5219 }
5220
5221 return string + separator + suffix;
5222
5223 }
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233 public static boolean appendIfNotBlank(StringBuilder result,
5234 String prefix, Object theStringOrArrayOrList) {
5235 return appendIfNotBlank(result, prefix, theStringOrArrayOrList, null);
5236 }
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247 public static boolean appendIfNotBlank(StringBuilder result,
5248 String prefix, Object theStringOrArrayOrList, String suffix) {
5249 return appendIfNotBlank(result, prefix, null, theStringOrArrayOrList, suffix);
5250 }
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262 public static boolean appendIfNotBlank(StringBuilder result,
5263 String prefix, String prefixIfNotBlank, Object theStringOrArrayOrList, String suffix) {
5264 int length = length(theStringOrArrayOrList);
5265 Iterator iterator = iterator(theStringOrArrayOrList);
5266 boolean appendedAnything = false;
5267
5268
5269 boolean hasPrefix = !isEmpty(prefix);
5270 boolean hasPrefixIfNotBlank = !isEmpty(prefixIfNotBlank);
5271 boolean hasSuffix = !isEmpty(suffix);
5272 for (int i = 0; i < length; i++) {
5273 String current = (String) next(theStringOrArrayOrList, iterator, i);
5274
5275
5276 if (!isBlank(current)) {
5277
5278
5279 appendedAnything = true;
5280 if (hasPrefix) {
5281 result.append(prefix);
5282 }
5283 if (hasPrefixIfNotBlank && result.length() > 0) {
5284 result.append(prefixIfNotBlank);
5285 }
5286 result.append(current);
5287 if (hasSuffix) {
5288 result.append(suffix);
5289 }
5290 }
5291 }
5292 return appendedAnything;
5293 }
5294
5295
5296
5297
5298
5299
5300
5301
5302 public static boolean appendIfNotEmpty(StringBuilder result,
5303 Object theStringOrArrayOrList) {
5304 return appendIfNotEmpty(result, null, theStringOrArrayOrList, null);
5305 }
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315 public static boolean appendIfNotEmpty(StringBuilder result,
5316 String prefix, Object theStringOrArrayOrList) {
5317 return appendIfNotEmpty(result, prefix, theStringOrArrayOrList, null);
5318 }
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329 public static boolean appendIfNotEmpty(StringBuilder result,
5330 String prefix, Object theStringOrArrayOrList, String suffix) {
5331 return appendIfNotEmpty(result, prefix, null, theStringOrArrayOrList, suffix);
5332 }
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344 public static boolean appendIfNotEmpty(StringBuilder result,
5345 String prefix, String prefixIfNotEmpty, Object theStringOrArrayOrList, String suffix) {
5346 int length = length(theStringOrArrayOrList);
5347 Iterator iterator = iterator(theStringOrArrayOrList);
5348 boolean appendedAnything = false;
5349 boolean hasPrefix = !isEmpty(prefix);
5350 boolean hasPrefixIfNotEmpty = !isEmpty(prefixIfNotEmpty);
5351 boolean hasSuffix = !isEmpty(suffix);
5352 for (int i = 0; i < length; i++) {
5353 String current = (String) next(theStringOrArrayOrList, iterator, i);
5354
5355
5356 if (!isEmpty(current)) {
5357
5358
5359 appendedAnything = true;
5360 if (hasPrefix) {
5361 result.append(prefix);
5362 }
5363 if (hasPrefixIfNotEmpty && result.length() > 0) {
5364 result.append(prefixIfNotEmpty);
5365 }
5366 result.append(current);
5367 if (hasSuffix) {
5368 result.append(suffix);
5369 }
5370 }
5371 }
5372 return appendedAnything;
5373 }
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385 public static int indexOf(Object[] array, Object objectToFind) {
5386 return indexOf(array, objectToFind, 0);
5387 }
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398 public static boolean contains(Object[] array, Object objectToFind) {
5399 return indexOf(array, objectToFind) != -1;
5400 }
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416 public static int indexOf(Object[] array, Object objectToFind, int startIndex) {
5417 if (array == null) {
5418 return -1;
5419 }
5420 if (startIndex < 0) {
5421 startIndex = 0;
5422 }
5423 if (objectToFind == null) {
5424 for (int i = startIndex; i < array.length; i++) {
5425 if (array[i] == null) {
5426 return i;
5427 }
5428 }
5429 } else {
5430 for (int i = startIndex; i < array.length; i++) {
5431 if (objectToFind.equals(array[i])) {
5432 return i;
5433 }
5434 }
5435 }
5436 return -1;
5437 }
5438
5439
5440
5441
5442
5443
5444
5445
5446 public static <T> T arrayPopOne(T[] array) {
5447 int size = length(array);
5448 if (size == 1) {
5449 return array[0];
5450 } else if (size == 0) {
5451 return null;
5452 }
5453 throw new RuntimeException("More than one object of type " + className(array[0])
5454 + " was returned when only one was expected. (size:" + size + ")");
5455 }
5456
5457
5458
5459
5460
5461 private static final String WS_DATE_FORMAT = "yyyy/MM/dd HH:mm:ss.SSS";
5462
5463
5464
5465
5466
5467 private static final String WS_DATE_FORMAT2 = "yyyy/MM/dd_HH:mm:ss.SSS";
5468
5469
5470
5471
5472
5473
5474
5475
5476 public static String dateToString(Date date) {
5477 if (date == null) {
5478 return null;
5479 }
5480 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(WS_DATE_FORMAT);
5481 return simpleDateFormat.format(date);
5482 }
5483
5484
5485
5486
5487
5488
5489
5490
5491 public static Date stringToDate(String dateString) {
5492 if (isBlank(dateString)) {
5493 return null;
5494 }
5495 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(WS_DATE_FORMAT);
5496 try {
5497 return simpleDateFormat.parse(dateString);
5498 } catch (ParseException e) {
5499 SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(WS_DATE_FORMAT2);
5500 try {
5501 return simpleDateFormat2.parse(dateString);
5502 } catch (ParseException e2) {
5503 throw new RuntimeException("Cannot convert '" + dateString
5504 + "' to a date based on format: " + WS_DATE_FORMAT, e);
5505 }
5506 }
5507 }
5508
5509
5510
5511
5512
5513 public static Long getMaxLongValue(Long... values) {
5514 if (values == null || values.length == 0) {
5515 return null;
5516 }
5517
5518 Long maxValue = null;
5519 for (int i = 0; i < values.length; i++) {
5520 if (values[i] != null) {
5521 if (maxValue == null || maxValue.compareTo(values[i]) < 0) {
5522 maxValue = new Long(values[i]);
5523 }
5524 }
5525 }
5526
5527 return maxValue;
5528 }
5529
5530
5531
5532
5533
5534 public static Long getMinLongValue(Long... values) {
5535 if (values == null || values.length == 0) {
5536 return null;
5537 }
5538
5539 Long minValue = null;
5540 for (int i = 0; i < values.length; i++) {
5541 if (values[i] != null) {
5542 if (minValue == null || minValue.compareTo(values[i]) > 0) {
5543 minValue = new Long(values[i]);
5544 }
5545 }
5546 }
5547
5548 return minValue;
5549 }
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562 public static boolean ipOnNetwork(String ipString, String networkIpString, int mask) {
5563
5564
5565 if (mask == 0) {
5566 return true;
5567 }
5568 int ip = ipInt(ipString);
5569 int networkIp = ipInt(networkIpString);
5570
5571 ip = ipReadyForAnd(ip, mask);
5572 networkIp = ipReadyForAnd(networkIp, mask);
5573
5574 return ip == networkIp;
5575 }
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586 public static boolean ipOnNetworks(String ipString, String networkIpStrings) {
5587
5588 String[] networkIpStringsArray = splitTrim(networkIpStrings, ",");
5589
5590
5591 for (String networkIpString : networkIpStringsArray) {
5592
5593 if (!contains(networkIpString, "/")) {
5594 throw new RuntimeException(
5595 "String must contain slash and CIDR network bits, e.g. 1.2.3.4/14");
5596 }
5597
5598 String network = prefixOrSuffix(networkIpString, "/", true);
5599 network = trim(network);
5600
5601 String mask = prefixOrSuffix(networkIpString, "/", false);
5602 mask = trim(mask);
5603 int maskInt = -1;
5604
5605 maskInt = Integer.parseInt(mask);
5606
5607
5608 if (ipOnNetwork(ipString, network, maskInt)) {
5609 return true;
5610 }
5611
5612 }
5613 return false;
5614 }
5615
5616
5617
5618
5619
5620
5621
5622 public static int ipReadyForAnd(int ip, int maskLength) {
5623 int mask = -1 + (int) Math.pow(2, 32 - maskLength);
5624
5625 return ip | mask;
5626 }
5627
5628
5629
5630
5631
5632
5633 public static int ipInt(String ip) {
5634 int block1;
5635 int block2;
5636 int block3;
5637 int block4;
5638
5639 try {
5640 int periodIndex = ip.indexOf('.');
5641 String blockString = ip.substring(0, periodIndex);
5642 block1 = Integer.parseInt(blockString);
5643
5644
5645 int mathPow = (int) Math.pow(2, 24);
5646 block1 *= mathPow;
5647
5648 int oldPeriodIndex = periodIndex;
5649
5650 periodIndex = ip.indexOf('.', periodIndex + 1);
5651 blockString = ip.substring(oldPeriodIndex + 1, periodIndex);
5652 block2 = Integer.parseInt(blockString);
5653 block2 *= Math.pow(2, 16);
5654 oldPeriodIndex = periodIndex;
5655
5656 periodIndex = ip.indexOf('.', periodIndex + 1);
5657 blockString = ip.substring(oldPeriodIndex + 1, periodIndex);
5658 block3 = Integer.parseInt(blockString);
5659 block3 *= Math.pow(2, 8);
5660
5661 blockString = ip.substring(periodIndex + 1, ip.length());
5662 block4 = Integer.parseInt(blockString);
5663 } catch (NumberFormatException nfe) {
5664 throw new RuntimeException("Could not parse the ipaddress: " + ip);
5665 }
5666
5667 return block1 + block2 + block3 + block4;
5668 }
5669
5670
5671 private static final String[] XML_REPLACE_NO_SINGLE = new String[] { "&", "<",
5672 ">", """ };
5673
5674
5675 private static final String[] XML_SEARCH_NO_SINGLE = new String[] { "&", "<", ">", "\"" };
5676
5677
5678
5679
5680
5681
5682
5683
5684 public static String getFullStackTrace(Throwable throwable) {
5685 StringWriter sw = new StringWriter();
5686 PrintWriter pw = new PrintWriter(sw, true);
5687 Throwable[] ts = getThrowables(throwable);
5688 for (int i = 0; i < ts.length; i++) {
5689 ts[i].printStackTrace(pw);
5690 if (isNestedThrowable(ts[i])) {
5691 break;
5692 }
5693 }
5694 return sw.getBuffer().toString();
5695 }
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705 public static Object get(Object arrayOrCollection, int index) {
5706
5707 if (arrayOrCollection == null) {
5708 if (index == 0) {
5709 return null;
5710 }
5711 throw new RuntimeException("Trying to access index " + index
5712 + " of null");
5713 }
5714
5715
5716 if (arrayOrCollection instanceof List) {
5717 return ((List) arrayOrCollection).get(index);
5718 }
5719 if (arrayOrCollection instanceof Collection) {
5720 Iterator iterator = iterator(arrayOrCollection);
5721 for (int i = 0; i < index; i++) {
5722 next(arrayOrCollection, iterator, i);
5723 }
5724 return next(arrayOrCollection, iterator, index);
5725 }
5726
5727 if (arrayOrCollection.getClass().isArray()) {
5728 return Array.get(arrayOrCollection, index);
5729 }
5730
5731 if (index == 0) {
5732 return arrayOrCollection;
5733 }
5734
5735 throw new RuntimeException("Trying to access index " + index
5736 + " of and object: " + arrayOrCollection);
5737 }
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752 @SuppressWarnings("unchecked")
5753 public static Throwable[] getThrowables(Throwable throwable) {
5754 List list = new ArrayList();
5755 while (throwable != null) {
5756 list.add(throwable);
5757 throwable = getCause(throwable);
5758 }
5759 return (Throwable[]) list.toArray(new Throwable[list.size()]);
5760 }
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770 public static boolean isThrowableNested() {
5771 return THROWABLE_CAUSE_METHOD != null;
5772 }
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783 @SuppressWarnings("unchecked")
5784 public static boolean isNestedThrowable(Throwable throwable) {
5785 if (throwable == null) {
5786 return false;
5787 }
5788
5789
5790
5791 if (throwable instanceof SQLException) {
5792 return true;
5793 } else if (throwable instanceof InvocationTargetException) {
5794 return true;
5795 } else if (isThrowableNested()) {
5796 return true;
5797 }
5798
5799 Class cls = throwable.getClass();
5800 for (int i = 0, isize = CAUSE_METHOD_NAMES.length; i < isize; i++) {
5801 try {
5802 Method method = cls.getMethod(CAUSE_METHOD_NAMES[i], (Class[]) null);
5803 if (method != null && Throwable.class.isAssignableFrom(method.getReturnType())) {
5804 return true;
5805 }
5806 } catch (NoSuchMethodException ignored) {
5807 } catch (SecurityException ignored) {
5808 }
5809 }
5810
5811 try {
5812 Field field = cls.getField("detail");
5813 if (field != null) {
5814 return true;
5815 }
5816 } catch (NoSuchFieldException ignored) {
5817 } catch (SecurityException ignored) {
5818 }
5819
5820 return false;
5821 }
5822
5823
5824
5825
5826 private static final Method THROWABLE_CAUSE_METHOD;
5827
5828 static {
5829 Method getCauseMethod;
5830 try {
5831 getCauseMethod = Throwable.class.getMethod("getCause", (Class[]) null);
5832 } catch (Exception e) {
5833 getCauseMethod = null;
5834 }
5835 THROWABLE_CAUSE_METHOD = getCauseMethod;
5836 }
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867 public static Throwable getCause(Throwable throwable) {
5868 return getCause(throwable, CAUSE_METHOD_NAMES);
5869 }
5870
5871
5872
5873
5874 private static String[] CAUSE_METHOD_NAMES = {
5875 "getCause",
5876 "getNextException",
5877 "getTargetException",
5878 "getException",
5879 "getSourceException",
5880 "getRootCause",
5881 "getCausedByException",
5882 "getNested",
5883 "getLinkedException",
5884 "getNestedException",
5885 "getLinkedCause",
5886 "getThrowable",
5887 };
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907 public static Throwable getCause(Throwable throwable, String[] methodNames) {
5908 if (throwable == null) {
5909 return null;
5910 }
5911 Throwable cause = getCauseUsingWellKnownTypes(throwable);
5912 if (cause == null) {
5913 if (methodNames == null) {
5914 methodNames = CAUSE_METHOD_NAMES;
5915 }
5916 for (int i = 0; i < methodNames.length; i++) {
5917 String methodName = methodNames[i];
5918 if (methodName != null) {
5919 cause = getCauseUsingMethodName(throwable, methodName);
5920 if (cause != null) {
5921 break;
5922 }
5923 }
5924 }
5925
5926 if (cause == null) {
5927 cause = getCauseUsingFieldName(throwable, "detail");
5928 }
5929 }
5930 return cause;
5931 }
5932
5933
5934
5935
5936
5937
5938
5939
5940 private static Throwable getCauseUsingFieldName(Throwable throwable, String fieldName) {
5941 Field field = null;
5942 try {
5943 field = throwable.getClass().getField(fieldName);
5944 } catch (NoSuchFieldException ignored) {
5945 } catch (SecurityException ignored) {
5946 }
5947
5948 if (field != null && Throwable.class.isAssignableFrom(field.getType())) {
5949 try {
5950 return (Throwable) field.get(throwable);
5951 } catch (IllegalAccessException ignored) {
5952 } catch (IllegalArgumentException ignored) {
5953 }
5954 }
5955 return null;
5956 }
5957
5958
5959
5960
5961
5962
5963
5964
5965 private static Throwable getCauseUsingMethodName(Throwable throwable, String methodName) {
5966 Method method = null;
5967 try {
5968 method = throwable.getClass().getMethod(methodName, (Class[]) null);
5969 } catch (NoSuchMethodException ignored) {
5970 } catch (SecurityException ignored) {
5971 }
5972
5973 if (method != null && Throwable.class.isAssignableFrom(method.getReturnType())) {
5974 try {
5975 return (Throwable) method.invoke(throwable, EMPTY_OBJECT_ARRAY);
5976 } catch (IllegalAccessException ignored) {
5977 } catch (IllegalArgumentException ignored) {
5978 } catch (InvocationTargetException ignored) {
5979 }
5980 }
5981 return null;
5982 }
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994 private static Throwable getCauseUsingWellKnownTypes(Throwable throwable) {
5995
5996
5997 if (throwable instanceof SQLException) {
5998 return ((SQLException) throwable).getNextException();
5999 } else if (throwable instanceof InvocationTargetException) {
6000 return ((InvocationTargetException) throwable).getTargetException();
6001 } else {
6002 return null;
6003 }
6004 }
6005
6006 }