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