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