001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.math.BigDecimal;
018
019 import java.text.DateFormat;
020
021 import java.util.Date;
022
023
026 public class GetterUtil {
027
028 public static final String[] BOOLEANS = {"true", "t", "y", "on", "1"};
029
030 public static final boolean DEFAULT_BOOLEAN = false;
031
032 public static final boolean[] DEFAULT_BOOLEAN_VALUES = new boolean[0];
033
034 public static final byte DEFAULT_BYTE = 0;
035
036 public static final byte[] DEFAULT_BYTE_VALUES = new byte[0];
037
038 public static final Date[] DEFAULT_DATE_VALUES = new Date[0];
039
040 public static final double DEFAULT_DOUBLE = 0.0;
041
042 public static final double[] DEFAULT_DOUBLE_VALUES = new double[0];
043
044 public static final float DEFAULT_FLOAT = 0;
045
046 public static final float[] DEFAULT_FLOAT_VALUES = new float[0];
047
048 public static final int DEFAULT_INTEGER = 0;
049
050 public static final int[] DEFAULT_INTEGER_VALUES = new int[0];
051
052 public static final long DEFAULT_LONG = 0;
053
054 public static final long[] DEFAULT_LONG_VALUES = new long[0];
055
056 public static final Number DEFAULT_NUMBER = 0;
057
058 public static final Number[] DEFAULT_NUMBER_VALUES = new Number[0];
059
060 public static final Number DEFAULT_OBJECT = null;
061
062 public static final short DEFAULT_SHORT = 0;
063
064 public static final short[] DEFAULT_SHORT_VALUES = new short[0];
065
066 public static final String DEFAULT_STRING = StringPool.BLANK;
067
068 public static final String[] DEFAULT_STRING_VALUES = new String[0];
069
070 public static boolean get(Object value, boolean defaultValue) {
071 if (value == null) {
072 return defaultValue;
073 }
074
075 if (value instanceof String) {
076 return get((String)value, defaultValue);
077 }
078
079 Class<?> clazz = value.getClass();
080
081 if (Boolean.class.isAssignableFrom(clazz)) {
082 return (Boolean)value;
083 }
084
085 return defaultValue;
086 }
087
088 public static Date get(
089 Object value, DateFormat dateFormat, Date defaultValue) {
090
091 if (value == null) {
092 return defaultValue;
093 }
094
095 if (value instanceof String) {
096 return get((String)value, dateFormat, defaultValue);
097 }
098
099 Class<?> clazz = value.getClass();
100
101 if (Date.class.isAssignableFrom(clazz)) {
102 return (Date)value;
103 }
104
105 return defaultValue;
106 }
107
108 public static double get(Object value, double defaultValue) {
109 if (value == null) {
110 return defaultValue;
111 }
112
113 if (value instanceof String) {
114 return get((String)value, defaultValue);
115 }
116
117 Class<?> clazz = value.getClass();
118
119 if (Double.class.isAssignableFrom(clazz)) {
120 return (Double)value;
121 }
122
123 if (value instanceof Number) {
124 Number number = (Number)value;
125
126 return number.doubleValue();
127 }
128
129 return defaultValue;
130 }
131
132 public static float get(Object value, float defaultValue) {
133 if (value == null) {
134 return defaultValue;
135 }
136
137 if (value instanceof String) {
138 return get((String)value, defaultValue);
139 }
140
141 Class<?> clazz = value.getClass();
142
143 if (Float.class.isAssignableFrom(clazz)) {
144 return (Float)value;
145 }
146
147 if (value instanceof Number) {
148 Number number = (Number)value;
149
150 return number.floatValue();
151 }
152
153 return defaultValue;
154 }
155
156 public static int get(Object value, int defaultValue) {
157 if (value == null) {
158 return defaultValue;
159 }
160
161 if (value instanceof String) {
162 return get((String)value, defaultValue);
163 }
164
165 Class<?> clazz = value.getClass();
166
167 if (Integer.class.isAssignableFrom(clazz)) {
168 return (Integer)value;
169 }
170
171 if (value instanceof Number) {
172 Number number = (Number)value;
173
174 return number.intValue();
175 }
176
177 return defaultValue;
178 }
179
180 public static long get(Object value, long defaultValue) {
181 if (value == null) {
182 return defaultValue;
183 }
184
185 if (value instanceof String) {
186 return get((String)value, defaultValue);
187 }
188
189 Class<?> clazz = value.getClass();
190
191 if (Long.class.isAssignableFrom(clazz)) {
192 return (Long)value;
193 }
194
195 if (value instanceof Number) {
196 Number number = (Number)value;
197
198 return number.longValue();
199 }
200
201 return defaultValue;
202 }
203
204 public static Number get(Object value, Number defaultValue) {
205 if (value == null) {
206 return defaultValue;
207 }
208
209 if (value instanceof String) {
210 if (Validator.isNull(value)) {
211 return defaultValue;
212 }
213
214 return new BigDecimal((String)value);
215 }
216
217 Class<?> clazz = value.getClass();
218
219 if (Byte.class.isAssignableFrom(clazz)) {
220 return (Byte)value;
221 }
222 else if (Double.class.isAssignableFrom(clazz)) {
223 return (Double)value;
224 }
225 else if (Float.class.isAssignableFrom(clazz)) {
226 return (Float)value;
227 }
228 else if (Integer.class.isAssignableFrom(clazz)) {
229 return (Integer)value;
230 }
231 else if (Long.class.isAssignableFrom(clazz)) {
232 return (Long)value;
233 }
234 else if (Short.class.isAssignableFrom(clazz)) {
235 return (Short)value;
236 }
237
238 if (value instanceof Number) {
239 return (Number)value;
240 }
241
242 return defaultValue;
243 }
244
245 public static short get(Object value, short defaultValue) {
246 if (value == null) {
247 return defaultValue;
248 }
249
250 if (value instanceof String) {
251 return get((String)value, defaultValue);
252 }
253
254 Class<?> clazz = value.getClass();
255
256 if (Short.class.isAssignableFrom(clazz)) {
257 return (Short)value;
258 }
259
260 if (value instanceof Number) {
261 Number number = (Number)value;
262
263 return number.shortValue();
264 }
265
266 return defaultValue;
267 }
268
269 public static String get(Object value, String defaultValue) {
270 if (value == null) {
271 return defaultValue;
272 }
273
274 if (value instanceof String) {
275 return get((String)value, defaultValue);
276 }
277
278 return defaultValue;
279 }
280
281 public static boolean get(String value, boolean defaultValue) {
282 if (value == null) {
283 return defaultValue;
284 }
285
286 try {
287 value = value.trim().toLowerCase();
288
289 if (value.equals(BOOLEANS[0]) || value.equals(BOOLEANS[1]) ||
290 value.equals(BOOLEANS[2]) || value.equals(BOOLEANS[3]) ||
291 value.equals(BOOLEANS[4])) {
292
293 return true;
294 }
295 else {
296 return false;
297 }
298 }
299 catch (Exception e) {
300 }
301
302 return defaultValue;
303 }
304
305 public static Date get(
306 String value, DateFormat dateFormat, Date defaultValue) {
307
308 if (value == null) {
309 return defaultValue;
310 }
311
312 try {
313 Date date = dateFormat.parse(value.trim());
314
315 if (date != null) {
316 return date;
317 }
318 }
319 catch (Exception e) {
320 }
321
322 return defaultValue;
323 }
324
325 public static double get(String value, double defaultValue) {
326 if (value != null) {
327 try {
328 return Double.parseDouble(_trim(value));
329 }
330 catch (Exception e) {
331 }
332 }
333
334 return defaultValue;
335 }
336
337 public static float get(String value, float defaultValue) {
338 if (value == null) {
339 return defaultValue;
340 }
341
342 try {
343 return Float.parseFloat(_trim(value));
344 }
345 catch (Exception e) {
346 }
347
348 return defaultValue;
349 }
350
351 public static int get(String value, int defaultValue) {
352 if (value == null) {
353 return defaultValue;
354 }
355
356 return _parseInt(_trim(value), defaultValue);
357 }
358
359 public static long get(String value, long defaultValue) {
360 if (value == null) {
361 return defaultValue;
362 }
363
364 return _parseLong(_trim(value), defaultValue);
365 }
366
367 public static short get(String value, short defaultValue) {
368 if (value == null) {
369 return defaultValue;
370 }
371
372 return _parseShort(_trim(value), defaultValue);
373 }
374
375 public static String get(String value, String defaultValue) {
376 if (value == null) {
377 return defaultValue;
378 }
379
380 value = value.trim();
381
382 if (value.indexOf(CharPool.RETURN) != -1) {
383 value = StringUtil.replace(
384 value, StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE);
385 }
386
387 return value;
388 }
389
390 public static boolean getBoolean(Object value) {
391 return getBoolean(value, DEFAULT_BOOLEAN);
392 }
393
394 public static boolean getBoolean(Object value, boolean defaultValue) {
395 return get(value, defaultValue);
396 }
397
398 public static boolean getBoolean(String value) {
399 return getBoolean(value, DEFAULT_BOOLEAN);
400 }
401
402 public static boolean getBoolean(String value, boolean defaultValue) {
403 return get(value, defaultValue);
404 }
405
406 public static boolean[] getBooleanValues(Object value) {
407 return getBooleanValues(value, DEFAULT_BOOLEAN_VALUES);
408 }
409
410 public static boolean[] getBooleanValues(
411 Object value, boolean[] defaultValue) {
412
413 if (value == null) {
414 return defaultValue;
415 }
416
417 Class<?> clazz = value.getClass();
418
419 if (clazz.isArray()) {
420 Class<?> componentType = clazz.getComponentType();
421
422 if (String.class.isAssignableFrom(componentType)) {
423 return getBooleanValues((String[])value, defaultValue);
424 }
425 else if (Boolean.class.isAssignableFrom(componentType)) {
426 return (boolean[])value;
427 }
428 }
429
430 return defaultValue;
431 }
432
433 public static boolean[] getBooleanValues(String[] values) {
434 return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES);
435 }
436
437 public static boolean[] getBooleanValues(
438 String[] values, boolean[] defaultValue) {
439
440 if (values == null) {
441 return defaultValue;
442 }
443
444 boolean[] booleanValues = new boolean[values.length];
445
446 for (int i = 0; i < values.length; i++) {
447 booleanValues[i] = getBoolean(values[i]);
448 }
449
450 return booleanValues;
451 }
452
453 public static Date getDate(Object value, DateFormat dateFormat) {
454 return getDate(value, dateFormat, new Date());
455 }
456
457 public static Date getDate(
458 Object value, DateFormat dateFormat, Date defaultValue) {
459
460 return get(value, dateFormat, defaultValue);
461 }
462
463 public static Date getDate(String value, DateFormat dateFormat) {
464 return getDate(value, dateFormat, new Date());
465 }
466
467 public static Date getDate(
468 String value, DateFormat dateFormat, Date defaultValue) {
469
470 return get(value, dateFormat, defaultValue);
471 }
472
473 public static Date[] getDateValues(Object value, DateFormat dateFormat) {
474 return getDateValues(value, dateFormat, DEFAULT_DATE_VALUES);
475 }
476
477 public static Date[] getDateValues(
478 Object value, DateFormat dateFormat, Date[] defaultValue) {
479
480 if (value == null) {
481 return defaultValue;
482 }
483
484 Class<?> clazz = value.getClass();
485
486 if (clazz.isArray()) {
487 Class<?> componentType = clazz.getComponentType();
488
489 if (String.class.isAssignableFrom(componentType)) {
490 return getDateValues((String[])value, dateFormat, defaultValue);
491 }
492 else if (Date.class.isAssignableFrom(componentType)) {
493 return (Date[])value;
494 }
495 }
496
497 return defaultValue;
498 }
499
500 public static Date[] getDateValues(String[] values, DateFormat dateFormat) {
501 return getDateValues(values, dateFormat, DEFAULT_DATE_VALUES);
502 }
503
504 public static Date[] getDateValues(
505 String[] values, DateFormat dateFormat, Date[] defaultValue) {
506
507 if (values == null) {
508 return defaultValue;
509 }
510
511 Date[] dateValues = new Date[values.length];
512
513 for (int i = 0; i < values.length; i++) {
514 dateValues[i] = getDate(values[i], dateFormat);
515 }
516
517 return dateValues;
518 }
519
520 public static double getDouble(Object value) {
521 return getDouble(value, DEFAULT_DOUBLE);
522 }
523
524 public static double getDouble(Object value, double defaultValue) {
525 return get(value, defaultValue);
526 }
527
528 public static double getDouble(String value) {
529 return getDouble(value, DEFAULT_DOUBLE);
530 }
531
532 public static double getDouble(String value, double defaultValue) {
533 return get(value, defaultValue);
534 }
535
536 public static double[] getDoubleValues(Object value) {
537 return getDoubleValues(value, DEFAULT_DOUBLE_VALUES);
538 }
539
540 public static double[] getDoubleValues(
541 Object value, double[] defaultValue) {
542
543 if (value == null) {
544 return defaultValue;
545 }
546
547 Class<?> clazz = value.getClass();
548
549 if (clazz.isArray()) {
550 Class<?> componentType = clazz.getComponentType();
551
552 if (String.class.isAssignableFrom(componentType)) {
553 return getDoubleValues((String[])value, defaultValue);
554 }
555 else if (Double.class.isAssignableFrom(componentType)) {
556 return (double[])value;
557 }
558 }
559
560 return defaultValue;
561 }
562
563 public static double[] getDoubleValues(String[] values) {
564 return getDoubleValues(values, DEFAULT_DOUBLE_VALUES);
565 }
566
567 public static double[] getDoubleValues(
568 String[] values, double[] defaultValue) {
569
570 if (values == null) {
571 return defaultValue;
572 }
573
574 double[] doubleValues = new double[values.length];
575
576 for (int i = 0; i < values.length; i++) {
577 doubleValues[i] = getDouble(values[i]);
578 }
579
580 return doubleValues;
581 }
582
583 public static float getFloat(Object value) {
584 return getFloat(value, DEFAULT_FLOAT);
585 }
586
587 public static float getFloat(Object value, float defaultValue) {
588 return get(value, defaultValue);
589 }
590
591 public static float getFloat(String value) {
592 return getFloat(value, DEFAULT_FLOAT);
593 }
594
595 public static float getFloat(String value, float defaultValue) {
596 return get(value, defaultValue);
597 }
598
599 public static float[] getFloatValues(Object value) {
600 return getFloatValues(value, DEFAULT_FLOAT_VALUES);
601 }
602
603 public static float[] getFloatValues(Object value, float[] defaultValue) {
604 if (value == null) {
605 return defaultValue;
606 }
607
608 Class<?> clazz = value.getClass();
609
610 if (clazz.isArray()) {
611 Class<?> componentType = clazz.getComponentType();
612
613 if (String.class.isAssignableFrom(componentType)) {
614 return getFloatValues((String[])value, defaultValue);
615 }
616 else if (Float.class.isAssignableFrom(componentType)) {
617 return (float[])value;
618 }
619 }
620
621 return defaultValue;
622 }
623
624 public static float[] getFloatValues(String[] values) {
625 return getFloatValues(values, DEFAULT_FLOAT_VALUES);
626 }
627
628 public static float[] getFloatValues(
629 String[] values, float[] defaultValue) {
630
631 if (values == null) {
632 return defaultValue;
633 }
634
635 float[] floatValues = new float[values.length];
636
637 for (int i = 0; i < values.length; i++) {
638 floatValues[i] = getFloat(values[i]);
639 }
640
641 return floatValues;
642 }
643
644 public static int getInteger(Object value) {
645 return getInteger(value, DEFAULT_INTEGER);
646 }
647
648 public static int getInteger(Object value, int defaultValue) {
649 return get(value, defaultValue);
650 }
651
652 public static int getInteger(String value) {
653 return getInteger(value, DEFAULT_INTEGER);
654 }
655
656 public static int getInteger(String value, int defaultValue) {
657 return get(value, defaultValue);
658 }
659
660 public static int getIntegerStrict(String value) {
661 int length = value.length();
662
663 if (length <= 0) {
664 throw new NumberFormatException("Unable to parse " + value);
665 }
666
667 int index = 0;
668 int limit = -Integer.MAX_VALUE;
669 boolean negative = false;
670
671 char c = value.charAt(0);
672
673 if (c < CharPool.NUMBER_0) {
674 if (c == CharPool.MINUS) {
675 limit = Integer.MIN_VALUE;
676 negative = true;
677 }
678 else if (c != CharPool.PLUS) {
679 throw new NumberFormatException("Unable to parse " + value);
680 }
681
682 if (length == 1) {
683 throw new NumberFormatException("Unable to parse " + value);
684 }
685
686 index++;
687 }
688
689 int smallLimit = limit / 10;
690
691 int result = 0;
692
693 while (index < length) {
694 if (result < smallLimit) {
695 throw new NumberFormatException("Unable to parse " + value);
696 }
697
698 c = value.charAt(index++);
699
700 if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
701 throw new NumberFormatException("Unable to parse " + value);
702 }
703
704 int number = c - CharPool.NUMBER_0;
705
706 result *= 10;
707
708 if (result < (limit + number)) {
709 throw new NumberFormatException("Unable to parse " + value);
710 }
711
712 result -= number;
713 }
714
715 if (negative) {
716 return result;
717 }
718 else {
719 return -result;
720 }
721 }
722
723 public static int[] getIntegerValues(Object value) {
724 return getIntegerValues(value, DEFAULT_INTEGER_VALUES);
725 }
726
727 public static int[] getIntegerValues(Object value, int[] defaultValue) {
728 if (value == null) {
729 return defaultValue;
730 }
731
732 Class<?> clazz = value.getClass();
733
734 if (clazz.isArray()) {
735 Class<?> componentType = clazz.getComponentType();
736
737 if (String.class.isAssignableFrom(componentType)) {
738 return getIntegerValues((String[])value, defaultValue);
739 }
740 else if (Integer.class.isAssignableFrom(componentType)) {
741 return (int[])value;
742 }
743 }
744
745 return defaultValue;
746 }
747
748 public static int[] getIntegerValues(String[] values) {
749 return getIntegerValues(values, DEFAULT_INTEGER_VALUES);
750 }
751
752 public static int[] getIntegerValues(String[] values, int[] defaultValue) {
753 if (values == null) {
754 return defaultValue;
755 }
756
757 int[] intValues = new int[values.length];
758
759 for (int i = 0; i < values.length; i++) {
760 intValues[i] = getInteger(values[i]);
761 }
762
763 return intValues;
764 }
765
766 public static long getLong(Object value) {
767 return getLong(value, DEFAULT_LONG);
768 }
769
770 public static long getLong(Object value, long defaultValue) {
771 return get(value, defaultValue);
772 }
773
774 public static long getLong(String value) {
775 return getLong(value, DEFAULT_LONG);
776 }
777
778 public static long getLong(String value, long defaultValue) {
779 return get(value, defaultValue);
780 }
781
782 public static long getLongStrict(String value) {
783 int length = value.length();
784
785 if (length <= 0) {
786 throw new NumberFormatException("Unable to parse " + value);
787 }
788
789 int index = 0;
790 long limit = -Long.MAX_VALUE;
791 boolean negative = false;
792
793 char c = value.charAt(0);
794
795 if (c < CharPool.NUMBER_0) {
796 if (c == CharPool.MINUS) {
797 limit = Long.MIN_VALUE;
798 negative = true;
799 }
800 else if (c != CharPool.PLUS) {
801 throw new NumberFormatException("Unable to parse " + value);
802 }
803
804 if (length == 1) {
805 throw new NumberFormatException("Unable to parse " + value);
806 }
807
808 index++;
809 }
810
811 long smallLimit = limit / 10;
812
813 long result = 0;
814
815 while (index < length) {
816 if (result < smallLimit) {
817 throw new NumberFormatException("Unable to parse " + value);
818 }
819
820 c = value.charAt(index++);
821
822 if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
823 throw new NumberFormatException("Unable to parse " + value);
824 }
825
826 int number = c - CharPool.NUMBER_0;
827
828 result *= 10;
829
830 if (result < (limit + number)) {
831 throw new NumberFormatException("Unable to parse " + value);
832 }
833
834 result -= number;
835 }
836
837 if (negative) {
838 return result;
839 }
840 else {
841 return -result;
842 }
843 }
844
845 public static long[] getLongValues(Object value) {
846 return getLongValues(value, DEFAULT_LONG_VALUES);
847 }
848
849 public static long[] getLongValues(Object value, long[] defaultValue) {
850 if (value == null) {
851 return defaultValue;
852 }
853
854 Class<?> clazz = value.getClass();
855
856 if (!clazz.isArray()) {
857 return defaultValue;
858 }
859
860 Class<?> componentType = clazz.getComponentType();
861
862 if (String.class.isAssignableFrom(componentType)) {
863 return getLongValues((String[])value, defaultValue);
864 }
865 else if (Long.class.isAssignableFrom(componentType)) {
866 return (long[])value;
867 }
868 else if (Number.class.isAssignableFrom(componentType)) {
869 Number[] numbers = (Number[])value;
870
871 long[] values = new long[numbers.length];
872
873 for (int i = 0; i < values.length; i++) {
874 values[i] = numbers[i].longValue();
875 }
876
877 return values;
878 }
879
880 return defaultValue;
881 }
882
883 public static long[] getLongValues(String[] values) {
884 return getLongValues(values, DEFAULT_LONG_VALUES);
885 }
886
887 public static long[] getLongValues(String[] values, long[] defaultValue) {
888 if (values == null) {
889 return defaultValue;
890 }
891
892 long[] longValues = new long[values.length];
893
894 for (int i = 0; i < values.length; i++) {
895 longValues[i] = getLong(values[i]);
896 }
897
898 return longValues;
899 }
900
901 public static Number getNumber(Object value) {
902 return getNumber(value, DEFAULT_NUMBER);
903 }
904
905 public static Number getNumber(Object value, Number defaultValue) {
906 return get(value, defaultValue);
907 }
908
909 public static Number getNumber(String value) {
910 return getNumber(value, DEFAULT_NUMBER);
911 }
912
913 public static Number getNumber(String value, Number defaultValue) {
914 return get(value, defaultValue);
915 }
916
917 public static Number[] getNumberValues(Object value) {
918 return getNumberValues(value, DEFAULT_NUMBER_VALUES);
919 }
920
921 public static Number[] getNumberValues(
922 Object value, Number[] defaultValue) {
923
924 if (value == null) {
925 return defaultValue;
926 }
927
928 Class<?> clazz = value.getClass();
929
930 if (clazz.isArray()) {
931 Class<?> componentType = clazz.getComponentType();
932
933 if (String.class.isAssignableFrom(componentType)) {
934 return getNumberValues((String[])value, defaultValue);
935 }
936 else if (Number.class.isAssignableFrom(componentType)) {
937 return (Number[])value;
938 }
939 }
940
941 return defaultValue;
942 }
943
944 public static Number[] getNumberValues(String[] values) {
945 return getNumberValues(values, DEFAULT_NUMBER_VALUES);
946 }
947
948 public static Number[] getNumberValues(
949 String[] values, Number[] defaultValue) {
950
951 if (values == null) {
952 return defaultValue;
953 }
954
955 Number[] numberValues = new Number[values.length];
956
957 for (int i = 0; i < values.length; i++) {
958 numberValues[i] = getNumber(values[i]);
959 }
960
961 return numberValues;
962 }
963
964 public static Object getObject(Object value) {
965 return getObject(value, DEFAULT_OBJECT);
966 }
967
968 public static Object getObject(Object value, Object defaultValue) {
969 if (value == null) {
970 return defaultValue;
971 }
972
973 return value;
974 }
975
976 public static short getShort(Object value) {
977 return getShort(value, DEFAULT_SHORT);
978 }
979
980 public static short getShort(Object value, short defaultValue) {
981 return get(value, defaultValue);
982 }
983
984 public static short getShort(String value) {
985 return getShort(value, DEFAULT_SHORT);
986 }
987
988 public static short getShort(String value, short defaultValue) {
989 return get(value, defaultValue);
990 }
991
992 public static short getShortStrict(String value) {
993 int i = getIntegerStrict(value);
994
995 if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) {
996 throw new NumberFormatException("Out of range value " + value);
997 }
998
999 return (short)i;
1000 }
1001
1002 public static short[] getShortValues(Object value) {
1003 return getShortValues(value, DEFAULT_SHORT_VALUES);
1004 }
1005
1006 public static short[] getShortValues(Object value, short[] defaultValue) {
1007 if (value == null) {
1008 return defaultValue;
1009 }
1010
1011 Class<?> clazz = value.getClass();
1012
1013 if (clazz.isArray()) {
1014 Class<?> componentType = clazz.getComponentType();
1015
1016 if (String.class.isAssignableFrom(componentType)) {
1017 return getShortValues((String[])value, defaultValue);
1018 }
1019 else if (Short.class.isAssignableFrom(componentType)) {
1020 return (short[])value;
1021 }
1022 }
1023
1024 return defaultValue;
1025 }
1026
1027 public static short[] getShortValues(String[] values) {
1028 return getShortValues(values, DEFAULT_SHORT_VALUES);
1029 }
1030
1031 public static short[] getShortValues(
1032 String[] values, short[] defaultValue) {
1033
1034 if (values == null) {
1035 return defaultValue;
1036 }
1037
1038 short[] shortValues = new short[values.length];
1039
1040 for (int i = 0; i < values.length; i++) {
1041 shortValues[i] = getShort(values[i]);
1042 }
1043
1044 return shortValues;
1045 }
1046
1047 public static String getString(Object value) {
1048 return getString(value, DEFAULT_STRING);
1049 }
1050
1051 public static String getString(Object value, String defaultValue) {
1052 return get(value, defaultValue);
1053 }
1054
1055 public static String getString(String value) {
1056 return getString(value, DEFAULT_STRING);
1057 }
1058
1059 public static String getString(String value, String defaultValue) {
1060 return get(value, defaultValue);
1061 }
1062
1063 public static String[] getStringValues(Object value) {
1064 return getStringValues(value, DEFAULT_STRING_VALUES);
1065 }
1066
1067 public static String[] getStringValues(
1068 Object value, String[] defaultValue) {
1069
1070 if (value == null) {
1071 return defaultValue;
1072 }
1073
1074 Class<?> clazz = value.getClass();
1075
1076 if (clazz.isArray()) {
1077 Class<?> componentType = clazz.getComponentType();
1078
1079 if (String.class.isAssignableFrom(componentType)) {
1080 return getStringValues((String[])value, defaultValue);
1081 }
1082 }
1083
1084 return defaultValue;
1085 }
1086
1087 public static String[] getStringValues(
1088 Object[] values, String[] defaultValue) {
1089
1090 if (values == null) {
1091 return defaultValue;
1092 }
1093
1094 String[] stringValues = new String[values.length];
1095
1096 for (int i = 0; i < values.length; i++) {
1097 stringValues[i] = String.valueOf(values[i]);
1098 }
1099
1100 return stringValues;
1101 }
1102
1103 public static String[] getStringValues(String[] values) {
1104 return getStringValues(values, DEFAULT_STRING_VALUES);
1105 }
1106
1107 private static int _parseInt(String value, int defaultValue) {
1108 int length = value.length();
1109
1110 if (length <= 0) {
1111 return defaultValue;
1112 }
1113
1114 int pos = 0;
1115 int limit = -Integer.MAX_VALUE;
1116 boolean negative = false;
1117
1118 char c = value.charAt(0);
1119
1120 if (c < CharPool.NUMBER_0) {
1121 if (c == CharPool.MINUS) {
1122 limit = Integer.MIN_VALUE;
1123 negative = true;
1124 }
1125 else if (c != CharPool.PLUS) {
1126 return defaultValue;
1127 }
1128
1129 if (length == 1) {
1130 return defaultValue;
1131 }
1132
1133 pos++;
1134 }
1135
1136 int smallLimit = limit / 10;
1137
1138 int result = 0;
1139
1140 while (pos < length) {
1141 if (result < smallLimit) {
1142 return defaultValue;
1143 }
1144
1145 c = value.charAt(pos++);
1146
1147 if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
1148 return defaultValue;
1149 }
1150
1151 int number = c - CharPool.NUMBER_0;
1152
1153 result *= 10;
1154
1155 if (result < (limit + number)) {
1156 return defaultValue;
1157 }
1158
1159 result -= number;
1160 }
1161
1162 if (negative) {
1163 return result;
1164 }
1165 else {
1166 return -result;
1167 }
1168 }
1169
1170 private static long _parseLong(String value, long defaultValue) {
1171 int length = value.length();
1172
1173 if (length <= 0) {
1174 return defaultValue;
1175 }
1176
1177 int pos = 0;
1178 long limit = -Long.MAX_VALUE;
1179 boolean negative = false;
1180
1181 char c = value.charAt(0);
1182
1183 if (c < CharPool.NUMBER_0) {
1184 if (c == CharPool.MINUS) {
1185 limit = Long.MIN_VALUE;
1186 negative = true;
1187 }
1188 else if (c != CharPool.PLUS) {
1189 return defaultValue;
1190 }
1191
1192 if (length == 1) {
1193 return defaultValue;
1194 }
1195
1196 pos++;
1197 }
1198
1199 long smallLimit = limit / 10;
1200
1201 long result = 0;
1202
1203 while (pos < length) {
1204 if (result < smallLimit) {
1205 return defaultValue;
1206 }
1207
1208 c = value.charAt(pos++);
1209
1210 if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
1211 return defaultValue;
1212 }
1213
1214 int number = c - CharPool.NUMBER_0;
1215
1216 result *= 10;
1217
1218 if (result < (limit + number)) {
1219 return defaultValue;
1220 }
1221
1222 result -= number;
1223 }
1224
1225 if (negative) {
1226 return result;
1227 }
1228 else {
1229 return -result;
1230 }
1231 }
1232
1233 private static short _parseShort(String value, short defaultValue) {
1234 int i = _parseInt(value, defaultValue);
1235
1236 if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) {
1237 return defaultValue;
1238 }
1239
1240 return (short)i;
1241 }
1242
1243 private static String _trim(String value) {
1244 value = value.trim();
1245
1246 int length = value.length();
1247
1248 StringBuilder sb = new StringBuilder(length);
1249
1250 for (int i = 0; i < length; i++) {
1251 char c = value.charAt(i);
1252
1253 if (Character.isDigit(c) ||
1254 ((c == CharPool.DASH) &&
1255 ((i == 0) || (value.charAt(i - 1) == CharPool.UPPER_CASE_E) ||
1256 (value.charAt(i - 1) == CharPool.LOWER_CASE_E))) ||
1257 (c == CharPool.PERIOD) || (c == CharPool.UPPER_CASE_E) ||
1258 (c == CharPool.LOWER_CASE_E)) {
1259
1260 sb.append(c);
1261 }
1262 }
1263
1264 return sb.toString();
1265 }
1266
1267 }