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