1
14
15 package com.liferay.portal.kernel.util;
16
17 import java.io.Serializable;
18
19 import java.text.DateFormat;
20
21 import java.util.Date;
22
23
28 public class GetterUtil {
29
30 public static String[] BOOLEANS = {"true", "t", "y", "on", "1"};
31
32 public static final boolean DEFAULT_BOOLEAN = false;
33
34 public static final boolean[] DEFAULT_BOOLEAN_VALUES = new boolean[0];
35
36 public static final byte DEFAULT_BYTE = 0;
37
38 public static final byte[] DEFAULT_BYTE_VALUES = new byte[0];
39
40 public static final Date[] DEFAULT_DATE_VALUES = new Date[0];
41
42 public static final double DEFAULT_DOUBLE = 0.0;
43
44 public static final double[] DEFAULT_DOUBLE_VALUES = new double[0];
45
46 public static final float DEFAULT_FLOAT = 0;
47
48 public static final float[] DEFAULT_FLOAT_VALUES = new float[0];
49
50 public static final int DEFAULT_INTEGER = 0;
51
52 public static final int[] DEFAULT_INTEGER_VALUES = new int[0];
53
54 public static final long DEFAULT_LONG = 0;
55
56 public static final long[] DEFAULT_LONG_VALUES = new long[0];
57
58 public static final Number DEFAULT_NUMBER = 0;
59
60 public static final Number DEFAULT_OBJECT = null;
61
62 public static final short DEFAULT_SHORT = 0;
63
64 public static final short[] DEFAULT_SHORT_VALUES = new short[0];
65
66 public static final String DEFAULT_STRING = StringPool.BLANK;
67
68 public static boolean get(Serializable value, boolean defaultValue) {
69 if (value == null) {
70 return defaultValue;
71 }
72
73 if (value instanceof String) {
74 return get((String)value, defaultValue);
75 }
76 else if (value.getClass().isAssignableFrom(Boolean.class)) {
77 return (Boolean)value;
78 }
79
80 return defaultValue;
81 }
82
83 public static Date get(
84 Serializable value, DateFormat dateFormat, Date defaultValue) {
85
86 if (value == null) {
87 return defaultValue;
88 }
89
90 if (value instanceof String) {
91 return get((String)value, dateFormat, defaultValue);
92 }
93 else if (value.getClass().isAssignableFrom(Date.class)) {
94 return (Date)value;
95 }
96
97 return defaultValue;
98 }
99
100 public static double get(Serializable value, double defaultValue) {
101 if (value == null) {
102 return defaultValue;
103 }
104
105 if (value instanceof String) {
106 return get((String)value, defaultValue);
107 }
108 else if (value.getClass().isAssignableFrom(Double.class)) {
109 return (Double)value;
110 }
111
112 return defaultValue;
113 }
114
115 public static float get(Serializable value, float defaultValue) {
116 if (value == null) {
117 return defaultValue;
118 }
119
120 if (value instanceof String) {
121 return get((String)value, defaultValue);
122 }
123 else if (value.getClass().isAssignableFrom(Float.class)) {
124 return (Float)value;
125 }
126
127 return defaultValue;
128 }
129
130 public static int get(Serializable value, int defaultValue) {
131 if (value == null) {
132 return defaultValue;
133 }
134
135 if (value instanceof String) {
136 return get((String)value, defaultValue);
137 }
138 else if (value.getClass().isAssignableFrom(Integer.class)) {
139 return (Integer)value;
140 }
141
142 return defaultValue;
143 }
144
145 public static long get(Serializable value, long defaultValue) {
146 if (value == null) {
147 return defaultValue;
148 }
149
150 if (value instanceof String) {
151 return get((String)value, defaultValue);
152 }
153 else if (value.getClass().isAssignableFrom(Long.class)) {
154 return (Long)value;
155 }
156
157 return defaultValue;
158 }
159
160 public static Number get(Serializable value, Number defaultValue) {
161 if (value == null) {
162 return defaultValue;
163 }
164
165 if (value instanceof String) {
166 if (Validator.isNull(value)) {
167 return defaultValue;
168 }
169
170 if (getFloat(value) == getInteger(value)) {
171 return getInteger(value);
172 }
173 else {
174 return getFloat(value);
175 }
176 }
177 else if (value.getClass().isAssignableFrom(Byte.class)) {
178 return (Byte)value;
179 }
180 else if (value.getClass().isAssignableFrom(Double.class)) {
181 return (Double)value;
182 }
183 else if (value.getClass().isAssignableFrom(Float.class)) {
184 return (Float)value;
185 }
186 else if (value.getClass().isAssignableFrom(Integer.class)) {
187 return (Integer)value;
188 }
189 else if (value.getClass().isAssignableFrom(Long.class)) {
190 return (Long)value;
191 }
192 else if (value.getClass().isAssignableFrom(Number.class)) {
193 return (Number)value;
194 }
195 else if (value.getClass().isAssignableFrom(Short.class)) {
196 return (Short)value;
197 }
198
199 return defaultValue;
200 }
201
202 public static short get(Serializable value, short defaultValue) {
203 if (value == null) {
204 return defaultValue;
205 }
206
207 if (value instanceof String) {
208 return get((String)value, defaultValue);
209 }
210 else if (value.getClass().isAssignableFrom(Short.class)) {
211 return (Short)value;
212 }
213
214 return defaultValue;
215 }
216
217 public static String get(Serializable value, String defaultValue) {
218 if (value == null) {
219 return defaultValue;
220 }
221
222 if (value instanceof String) {
223 return get((String)value, defaultValue);
224 }
225
226 return defaultValue;
227 }
228
229 public static boolean get(String value, boolean defaultValue) {
230 if (value == null) {
231 return defaultValue;
232 }
233
234 try {
235 value = value.trim();
236
237 if (value.equalsIgnoreCase(BOOLEANS[0]) ||
238 value.equalsIgnoreCase(BOOLEANS[1]) ||
239 value.equalsIgnoreCase(BOOLEANS[2]) ||
240 value.equalsIgnoreCase(BOOLEANS[3]) ||
241 value.equalsIgnoreCase(BOOLEANS[4])) {
242
243 return true;
244 }
245 else {
246 return false;
247 }
248 }
249 catch (Exception e) {
250 }
251
252 return defaultValue;
253 }
254
255 public static Date get(
256 String value, DateFormat dateFormat, Date defaultValue) {
257
258 if (value == null) {
259 return defaultValue;
260 }
261
262 try {
263 Date date = dateFormat.parse(value.trim());
264
265 if (date != null) {
266 return date;
267 }
268 }
269 catch (Exception e) {
270 }
271
272 return defaultValue;
273 }
274
275 public static double get(String value, double defaultValue) {
276 if (value != null) {
277 try {
278 return Double.parseDouble(_trim(value));
279 }
280 catch (Exception e) {
281 }
282 }
283
284 return defaultValue;
285 }
286
287 public static float get(String value, float defaultValue) {
288 if (value == null) {
289 return defaultValue;
290 }
291
292 try {
293 return Float.parseFloat(_trim(value));
294 }
295 catch (Exception e) {
296 }
297
298 return defaultValue;
299 }
300
301 public static int get(String value, int defaultValue) {
302 if (value == null) {
303 return defaultValue;
304 }
305
306 return _parseInt(_trim(value), defaultValue);
307 }
308
309 public static long get(String value, long defaultValue) {
310 if (value == null) {
311 return defaultValue;
312 }
313
314 return _parseLong(_trim(value), defaultValue);
315 }
316
317 public static short get(String value, short defaultValue) {
318 if (value == null) {
319 return defaultValue;
320 }
321
322 return _parseShort(_trim(value), defaultValue);
323 }
324
325 public static String get(String value, String defaultValue) {
326 if (value == null) {
327 return defaultValue;
328 }
329
330 return StringUtil.replace(
331 value.trim(), StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE);
332 }
333
334 public static boolean getBoolean(Serializable value) {
335 return getBoolean(value, DEFAULT_BOOLEAN);
336 }
337
338 public static boolean getBoolean(Serializable value, boolean defaultValue) {
339 return get(value, defaultValue);
340 }
341
342 public static boolean getBoolean(String value) {
343 return getBoolean(value, DEFAULT_BOOLEAN);
344 }
345
346 public static boolean getBoolean(String value, boolean defaultValue) {
347 return get(value, defaultValue);
348 }
349
350 public static boolean[] getBooleanValues(Serializable value) {
351 return getBooleanValues(value, DEFAULT_BOOLEAN_VALUES);
352 }
353
354 public static boolean[] getBooleanValues(
355 Serializable value, boolean[] defaultValue) {
356
357 Class<?> classObject = value.getClass();
358
359 if (classObject.isArray()) {
360 Class<?> componentType = classObject.getComponentType();
361
362 if (componentType.isAssignableFrom(String.class)) {
363 return getBooleanValues((String[])value, defaultValue);
364 }
365 else if (componentType.isAssignableFrom(Boolean.class)) {
366 return (boolean[])value;
367 }
368 }
369
370 return defaultValue;
371 }
372
373 public static boolean[] getBooleanValues(String[] values) {
374 return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES);
375 }
376
377 public static boolean[] getBooleanValues(
378 String[] values, boolean[] defaultValue) {
379
380 if (values == null) {
381 return defaultValue;
382 }
383
384 boolean[] booleanValues = new boolean[values.length];
385
386 for (int i = 0; i < values.length; i++) {
387 booleanValues[i] = getBoolean(values[i]);
388 }
389
390 return booleanValues;
391 }
392
393 public static Date getDate(Serializable value, DateFormat dateFormat) {
394 return getDate(value, dateFormat, new Date());
395 }
396
397 public static Date getDate(
398 Serializable value, DateFormat dateFormat, Date defaultValue) {
399
400 return get(value, dateFormat, defaultValue);
401 }
402
403 public static Date getDate(String value, DateFormat dateFormat) {
404 return getDate(value, dateFormat, new Date());
405 }
406
407 public static Date getDate(
408 String value, DateFormat dateFormat, Date defaultValue) {
409
410 return get(value, dateFormat, defaultValue);
411 }
412
413 public static Date[] getDateValues(
414 Serializable value, DateFormat dateFormat) {
415
416 return getDateValues(value, dateFormat, DEFAULT_DATE_VALUES);
417 }
418
419 public static Date[] getDateValues(
420 Serializable value, DateFormat dateFormat, Date[] defaultValue) {
421
422 Class<?> classObject = value.getClass();
423
424 if (classObject.isArray()) {
425 Class<?> componentType = classObject.getComponentType();
426
427 if (componentType.isAssignableFrom(String.class)) {
428 return getDateValues((String[])value, dateFormat, defaultValue);
429 }
430 else if (componentType.isAssignableFrom(Date.class)) {
431 return (Date[])value;
432 }
433 }
434
435 return defaultValue;
436 }
437
438 public static Date[] getDateValues(String[] values, DateFormat dateFormat) {
439 return getDateValues(values, dateFormat, DEFAULT_DATE_VALUES);
440 }
441
442 public static Date[] getDateValues(
443 String[] values, DateFormat dateFormat, Date[] defaultValue) {
444
445 if (values == null) {
446 return defaultValue;
447 }
448
449 Date[] dateValues = new Date[values.length];
450
451 for (int i = 0; i < values.length; i++) {
452 dateValues[i] = getDate(values[i], dateFormat);
453 }
454
455 return dateValues;
456 }
457
458 public static double getDouble(Serializable value) {
459 return getDouble(value, DEFAULT_DOUBLE);
460 }
461
462 public static double getDouble(Serializable value, double defaultValue) {
463 return get(value, defaultValue);
464 }
465
466 public static double getDouble(String value) {
467 return getDouble(value, DEFAULT_DOUBLE);
468 }
469
470 public static double getDouble(String value, double defaultValue) {
471 return get(value, defaultValue);
472 }
473
474 public static double[] getDoubleValues(Serializable value) {
475 return getDoubleValues(value, DEFAULT_DOUBLE_VALUES);
476 }
477
478 public static double[] getDoubleValues(
479 Serializable value, double[] defaultValue) {
480
481 Class<?> classObject = value.getClass();
482
483 if (classObject.isArray()) {
484 Class<?> componentType = classObject.getComponentType();
485
486 if (componentType.isAssignableFrom(String.class)) {
487 return getDoubleValues((String[])value, defaultValue);
488 }
489 else if (componentType.isAssignableFrom(Double.class)) {
490 return (double[])value;
491 }
492 }
493
494 return defaultValue;
495 }
496
497 public static double[] getDoubleValues(String[] values) {
498 return getDoubleValues(values, DEFAULT_DOUBLE_VALUES);
499 }
500
501 public static double[] getDoubleValues(
502 String[] values, double[] defaultValue) {
503
504 if (values == null) {
505 return defaultValue;
506 }
507
508 double[] doubleValues = new double[values.length];
509
510 for (int i = 0; i < values.length; i++) {
511 doubleValues[i] = getDouble(values[i]);
512 }
513
514 return doubleValues;
515 }
516
517 public static float getFloat(Serializable value) {
518 return getFloat(value, DEFAULT_FLOAT);
519 }
520
521 public static float getFloat(Serializable value, float defaultValue) {
522 return get(value, defaultValue);
523 }
524
525 public static float getFloat(String value) {
526 return getFloat(value, DEFAULT_FLOAT);
527 }
528
529 public static float getFloat(String value, float defaultValue) {
530 return get(value, defaultValue);
531 }
532
533 public static float[] getFloatValues(Serializable value) {
534 return getFloatValues(value, DEFAULT_FLOAT_VALUES);
535 }
536
537 public static float[] getFloatValues(
538 Serializable value, float[] defaultValue) {
539
540 Class<?> classObject = value.getClass();
541
542 if (classObject.isArray()) {
543 Class<?> componentType = classObject.getComponentType();
544
545 if (componentType.isAssignableFrom(String.class)) {
546 return getFloatValues((String[])value, defaultValue);
547 }
548 else if (componentType.isAssignableFrom(Float.class)) {
549 return (float[])value;
550 }
551 }
552
553 return defaultValue;
554 }
555
556 public static float[] getFloatValues(String[] values) {
557 return getFloatValues(values, DEFAULT_FLOAT_VALUES);
558 }
559
560 public static float[] getFloatValues(
561 String[] values, float[] defaultValue) {
562
563 if (values == null) {
564 return defaultValue;
565 }
566
567 float[] floatValues = new float[values.length];
568
569 for (int i = 0; i < values.length; i++) {
570 floatValues[i] = getFloat(values[i]);
571 }
572
573 return floatValues;
574 }
575
576 public static int getInteger(Serializable value) {
577 return getInteger(value, DEFAULT_INTEGER);
578 }
579
580 public static int getInteger(Serializable value, int defaultValue) {
581 return get(value, defaultValue);
582 }
583
584 public static int getInteger(String value) {
585 return getInteger(value, DEFAULT_INTEGER);
586 }
587
588 public static int getInteger(String value, int defaultValue) {
589 return get(value, defaultValue);
590 }
591
592 public static int[] getIntegerValues(Serializable value) {
593 return getIntegerValues(value, DEFAULT_INTEGER_VALUES);
594 }
595
596 public static int[] getIntegerValues(
597 Serializable value, int[] defaultValue) {
598
599 Class<?> classObject = value.getClass();
600
601 if (classObject.isArray()) {
602 Class<?> componentType = classObject.getComponentType();
603
604 if (componentType.isAssignableFrom(String.class)) {
605 return getIntegerValues((String[])value, defaultValue);
606 }
607 else if (componentType.isAssignableFrom(Integer.class)) {
608 return (int[])value;
609 }
610 }
611
612 return defaultValue;
613 }
614
615 public static int[] getIntegerValues(String[] values) {
616 return getIntegerValues(values, DEFAULT_INTEGER_VALUES);
617 }
618
619 public static int[] getIntegerValues(String[] values, int[] defaultValue) {
620 if (values == null) {
621 return defaultValue;
622 }
623
624 int[] intValues = new int[values.length];
625
626 for (int i = 0; i < values.length; i++) {
627 intValues[i] = getInteger(values[i]);
628 }
629
630 return intValues;
631 }
632
633 public static long getLong(Serializable value) {
634 return getLong(value, DEFAULT_LONG);
635 }
636
637 public static long getLong(Serializable value, long defaultValue) {
638 return get(value, defaultValue);
639 }
640
641 public static long getLong(String value) {
642 return getLong(value, DEFAULT_LONG);
643 }
644
645 public static long getLong(String value, long defaultValue) {
646 return get(value, defaultValue);
647 }
648
649 public static long[] getLongValues(Serializable value) {
650 return getLongValues(value, DEFAULT_LONG_VALUES);
651 }
652
653 public static long[] getLongValues(
654 Serializable value, long[] defaultValue) {
655
656 Class<?> classObject = value.getClass();
657
658 if (classObject.isArray()) {
659 Class<?> componentType = classObject.getComponentType();
660
661 if (componentType.isAssignableFrom(String.class)) {
662 return getLongValues((String[])value, defaultValue);
663 }
664 else if (componentType.isAssignableFrom(Long.class)) {
665 return (long[])value;
666 }
667 }
668
669 return defaultValue;
670 }
671
672 public static long[] getLongValues(String[] values) {
673 return getLongValues(values, DEFAULT_LONG_VALUES);
674 }
675
676 public static long[] getLongValues(String[] values, long[] defaultValue) {
677 if (values == null) {
678 return defaultValue;
679 }
680
681 long[] longValues = new long[values.length];
682
683 for (int i = 0; i < values.length; i++) {
684 longValues[i] = getLong(values[i]);
685 }
686
687 return longValues;
688 }
689
690 public static Number getNumber(Serializable value) {
691 return getNumber(value, DEFAULT_NUMBER);
692 }
693
694 public static Number getNumber(Serializable value, Number defaultValue) {
695 return get(value, defaultValue);
696 }
697
698 public static Number getNumber(String value) {
699 return getNumber(value, DEFAULT_NUMBER);
700 }
701
702 public static Number getNumber(String value, Number defaultValue) {
703 return get(value, defaultValue);
704 }
705
706 public static Object getObject(Object value) {
707 return getObject(value, DEFAULT_OBJECT);
708 }
709
710 public static Object getObject(Object value, Object defaultValue) {
711 if (value == null) {
712 return defaultValue;
713 }
714
715 return value;
716 }
717
718 public static short getShort(Serializable value) {
719 return getShort(value, DEFAULT_SHORT);
720 }
721
722 public static short getShort(Serializable value, short defaultValue) {
723 return get(value, defaultValue);
724 }
725
726 public static short getShort(String value) {
727 return getShort(value, DEFAULT_SHORT);
728 }
729
730 public static short getShort(String value, short defaultValue) {
731 return get(value, defaultValue);
732 }
733
734 public static short[] getShortValues(Serializable value) {
735 return getShortValues(value, DEFAULT_SHORT_VALUES);
736 }
737
738 public static short[] getShortValues(
739 Serializable value, short[] defaultValue) {
740
741 Class<?> classObject = value.getClass();
742
743 if (classObject.isArray()) {
744 Class<?> componentType = classObject.getComponentType();
745
746 if (componentType.isAssignableFrom(String.class)) {
747 return getShortValues((String[])value, defaultValue);
748 }
749 else if (componentType.isAssignableFrom(Short.class)) {
750 return (short[])value;
751 }
752 }
753
754 return defaultValue;
755 }
756
757 public static short[] getShortValues(String[] values) {
758 return getShortValues(values, DEFAULT_SHORT_VALUES);
759 }
760
761 public static short[] getShortValues(
762 String[] values, short[] defaultValue) {
763
764 if (values == null) {
765 return defaultValue;
766 }
767
768 short[] shortValues = new short[values.length];
769
770 for (int i = 0; i < values.length; i++) {
771 shortValues[i] = getShort(values[i]);
772 }
773
774 return shortValues;
775 }
776
777 public static String getString(Serializable value) {
778 return getString(value, DEFAULT_STRING);
779 }
780
781 public static String getString(Serializable value, String defaultValue) {
782 return get(value, defaultValue);
783 }
784
785 public static String getString(String value) {
786 return getString(value, DEFAULT_STRING);
787 }
788
789 public static String getString(String value, String defaultValue) {
790 return get(value, defaultValue);
791 }
792
793 private static int _parseInt(String value, int defaultValue) {
794 int length = value.length();
795
796 if (length <= 0) {
797 return defaultValue;
798 }
799
800 int pos = 0;
801 int limit = -Integer.MAX_VALUE;
802 boolean negative = false;
803
804 char c = value.charAt(0);
805
806 if (c < CharPool.NUMBER_0) {
807 if (c == CharPool.MINUS) {
808 limit = Integer.MIN_VALUE;
809 negative = true;
810 }
811 else if (c != CharPool.PLUS) {
812 return defaultValue;
813 }
814
815 if (length == 1) {
816 return defaultValue;
817 }
818
819 pos++;
820 }
821
822 int smallLimit = limit / 10;
823
824 int result = 0;
825
826 while (pos < length) {
827 if (result < smallLimit) {
828 return defaultValue;
829 }
830
831 c = value.charAt(pos++);
832
833 if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
834 return defaultValue;
835 }
836
837 int number = c - CharPool.NUMBER_0;
838
839 result *= 10;
840
841 if (result < (limit + number)) {
842 return defaultValue;
843 }
844
845 result -= number;
846 }
847
848 if (negative) {
849 return result;
850 }
851 else {
852 return -result;
853 }
854 }
855
856 private static long _parseLong(String value, long defaultValue) {
857 if (_useJDKParseLong == null) {
858 if (OSDetector.isAIX() && ServerDetector.isWebSphere() &&
859 JavaProps.isIBM() && JavaProps.is64bit()) {
860
861 _useJDKParseLong = Boolean.TRUE;
862 }
863 else {
864 _useJDKParseLong = Boolean.FALSE;
865 }
866 }
867
868 if (_useJDKParseLong) {
869 try {
870 return Long.parseLong(value);
871 }
872 catch (NumberFormatException nfe) {
873 return defaultValue;
874 }
875 }
876
877 int length = value.length();
878
879 if (length <= 0) {
880 return defaultValue;
881 }
882
883 int pos = 0;
884 long limit = -Long.MAX_VALUE;
885 boolean negative = false;
886
887 char c = value.charAt(0);
888
889 if (c < CharPool.NUMBER_0) {
890 if (c == CharPool.MINUS) {
891 limit = Long.MIN_VALUE;
892 negative = true;
893 }
894 else if (c != CharPool.PLUS) {
895 return defaultValue;
896 }
897
898 if (length == 1) {
899 return defaultValue;
900 }
901
902 pos++;
903 }
904
905 long smallLimit = limit / 10;
906
907 long result = 0;
908
909 while (pos < length) {
910 if (result < smallLimit) {
911 return defaultValue;
912 }
913
914 c = value.charAt(pos++);
915
916 if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
917 return defaultValue;
918 }
919
920 int number = c - CharPool.NUMBER_0;
921
922 result *= 10;
923
924 if (result < (limit + number)) {
925 return defaultValue;
926 }
927
928 result -= number;
929 }
930
931 if (negative) {
932 return result;
933 }
934 else {
935 return -result;
936 }
937 }
938
939 private static short _parseShort(String value, short defaultValue) {
940 int i = _parseInt(value, defaultValue);
941
942 if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) {
943 return defaultValue;
944 }
945
946 return (short)i;
947 }
948
949 private static String _trim(String value) {
950 value = value.trim();
951
952 int length = value.length();
953
954 StringBuilder sb = new StringBuilder(length);
955
956 for (int i = 0; i < length; i++) {
957 char c = value.charAt(i);
958
959 if ((Character.isDigit(c)) ||
960 ((c == CharPool.DASH) && (i == 0)) ||
961 (c == CharPool.PERIOD) || (c == CharPool.UPPER_CASE_E) ||
962 (c == CharPool.LOWER_CASE_E)) {
963
964 sb.append(c);
965 }
966 }
967
968 return sb.toString();
969 }
970
971 private static Boolean _useJDKParseLong;
972
973 }