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