001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.Serializable;
018    
019    import java.text.DateFormat;
020    
021    import java.util.Date;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class GetterUtil {
027    
028            public static 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_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 boolean get(Serializable value, boolean defaultValue) {
067                    if (value == null) {
068                            return defaultValue;
069                    }
070    
071                    if (value instanceof String) {
072                            return get((String)value, defaultValue);
073                    }
074                    else if (value.getClass().isAssignableFrom(Boolean.class)) {
075                            return (Boolean)value;
076                    }
077    
078                    return defaultValue;
079            }
080    
081            public static Date get(
082                    Serializable value, DateFormat dateFormat, Date defaultValue) {
083    
084                    if (value == null) {
085                            return defaultValue;
086                    }
087    
088                    if (value instanceof String) {
089                            return get((String)value, dateFormat, defaultValue);
090                    }
091                    else if (value.getClass().isAssignableFrom(Date.class)) {
092                            return (Date)value;
093                    }
094    
095                    return defaultValue;
096            }
097    
098            public static double get(Serializable value, double defaultValue) {
099                    if (value == null) {
100                            return defaultValue;
101                    }
102    
103                    if (value instanceof String) {
104                            return get((String)value, defaultValue);
105                    }
106                    else if (value.getClass().isAssignableFrom(Double.class)) {
107                            return (Double)value;
108                    }
109    
110                    return defaultValue;
111            }
112    
113            public static float get(Serializable value, float defaultValue) {
114                    if (value == null) {
115                            return defaultValue;
116                    }
117    
118                    if (value instanceof String) {
119                            return get((String)value, defaultValue);
120                    }
121                    else if (value.getClass().isAssignableFrom(Float.class)) {
122                            return (Float)value;
123                    }
124    
125                    return defaultValue;
126            }
127    
128            public static int get(Serializable value, int defaultValue) {
129                    if (value == null) {
130                            return defaultValue;
131                    }
132    
133                    if (value instanceof String) {
134                            return get((String)value, defaultValue);
135                    }
136                    else if (value.getClass().isAssignableFrom(Integer.class)) {
137                            return (Integer)value;
138                    }
139    
140                    return defaultValue;
141            }
142    
143            public static long get(Serializable value, long defaultValue) {
144                    if (value == null) {
145                            return defaultValue;
146                    }
147    
148                    if (value instanceof String) {
149                            return get((String)value, defaultValue);
150                    }
151                    else if (value.getClass().isAssignableFrom(Long.class)) {
152                            return (Long)value;
153                    }
154    
155                    return defaultValue;
156            }
157    
158            public static Number get(Serializable value, Number defaultValue) {
159                    if (value == null) {
160                            return defaultValue;
161                    }
162    
163                    if (value instanceof String) {
164                            if (Validator.isNull(value)) {
165                                    return defaultValue;
166                            }
167    
168                            if (getFloat(value) == getInteger(value)) {
169                                    return getInteger(value);
170                            }
171                            else {
172                                    return getFloat(value);
173                            }
174                    }
175                    else if (value.getClass().isAssignableFrom(Byte.class)) {
176                            return (Byte)value;
177                    }
178                    else if (value.getClass().isAssignableFrom(Double.class)) {
179                            return (Double)value;
180                    }
181                    else if (value.getClass().isAssignableFrom(Float.class)) {
182                            return (Float)value;
183                    }
184                    else if (value.getClass().isAssignableFrom(Integer.class)) {
185                            return (Integer)value;
186                    }
187                    else if (value.getClass().isAssignableFrom(Long.class)) {
188                            return (Long)value;
189                    }
190                    else if (value.getClass().isAssignableFrom(Number.class)) {
191                            return (Number)value;
192                    }
193                    else if (value.getClass().isAssignableFrom(Short.class)) {
194                            return (Short)value;
195                    }
196    
197                    return defaultValue;
198            }
199    
200            public static short get(Serializable value, short defaultValue) {
201                    if (value == null) {
202                            return defaultValue;
203                    }
204    
205                    if (value instanceof String) {
206                            return get((String)value, defaultValue);
207                    }
208                    else if (value.getClass().isAssignableFrom(Short.class)) {
209                            return (Short)value;
210                    }
211    
212                    return defaultValue;
213            }
214    
215            public static String get(Serializable value, String defaultValue) {
216                    if (value == null) {
217                            return defaultValue;
218                    }
219    
220                    if (value instanceof String) {
221                            return get((String)value, defaultValue);
222                    }
223    
224                    return defaultValue;
225            }
226    
227            public static boolean get(String value, boolean defaultValue) {
228                    if (value == null) {
229                            return defaultValue;
230                    }
231    
232                    try {
233                            value = value.trim();
234    
235                            if (value.equalsIgnoreCase(BOOLEANS[0]) ||
236                                    value.equalsIgnoreCase(BOOLEANS[1]) ||
237                                    value.equalsIgnoreCase(BOOLEANS[2]) ||
238                                    value.equalsIgnoreCase(BOOLEANS[3]) ||
239                                    value.equalsIgnoreCase(BOOLEANS[4])) {
240    
241                                    return true;
242                            }
243                            else {
244                                    return false;
245                            }
246                    }
247                    catch (Exception e) {
248                    }
249    
250                    return defaultValue;
251            }
252    
253            public static Date get(
254                    String value, DateFormat dateFormat, Date defaultValue) {
255    
256                    if (value == null) {
257                            return defaultValue;
258                    }
259    
260                    try {
261                            Date date = dateFormat.parse(value.trim());
262    
263                            if (date != null) {
264                                    return date;
265                            }
266                    }
267                    catch (Exception e) {
268                    }
269    
270                    return defaultValue;
271            }
272    
273            public static double get(String value, double defaultValue) {
274                    if (value != null) {
275                            try {
276                                    return Double.parseDouble(_trim(value));
277                            }
278                            catch (Exception e) {
279                            }
280                    }
281    
282                    return defaultValue;
283            }
284    
285            public static float get(String value, float defaultValue) {
286                    if (value == null) {
287                            return defaultValue;
288                    }
289    
290                    try {
291                            return Float.parseFloat(_trim(value));
292                    }
293                    catch (Exception e) {
294                    }
295    
296                    return defaultValue;
297            }
298    
299            public static int get(String value, int defaultValue) {
300                    if (value == null) {
301                            return defaultValue;
302                    }
303    
304                    return _parseInt(_trim(value), defaultValue);
305            }
306    
307            public static long get(String value, long defaultValue) {
308                    if (value == null) {
309                            return defaultValue;
310                    }
311    
312                    return _parseLong(_trim(value), defaultValue);
313            }
314    
315            public static short get(String value, short defaultValue) {
316                    if (value == null) {
317                            return defaultValue;
318                    }
319    
320                    return _parseShort(_trim(value), defaultValue);
321            }
322    
323            public static String get(String value, String defaultValue) {
324                    if (value == null) {
325                            return defaultValue;
326                    }
327    
328                    value = value.trim();
329    
330                    if (value.indexOf(CharPool.RETURN) != -1) {
331                            value = StringUtil.replace(
332                                    value, StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE);
333                    }
334    
335                    return value;
336            }
337    
338            public static boolean getBoolean(Serializable value) {
339                    return getBoolean(value, DEFAULT_BOOLEAN);
340            }
341    
342            public static boolean getBoolean(Serializable value, boolean defaultValue) {
343                    return get(value, defaultValue);
344            }
345    
346            public static boolean getBoolean(String value) {
347                    return getBoolean(value, DEFAULT_BOOLEAN);
348            }
349    
350            public static boolean getBoolean(String value, boolean defaultValue) {
351                    return get(value, defaultValue);
352            }
353    
354            public static boolean[] getBooleanValues(Serializable value) {
355                    return getBooleanValues(value, DEFAULT_BOOLEAN_VALUES);
356            }
357    
358            public static boolean[] getBooleanValues(
359                    Serializable value, boolean[] defaultValue) {
360    
361                    Class<?> clazz = value.getClass();
362    
363                    if (clazz.isArray()) {
364                            Class<?> componentType = clazz.getComponentType();
365    
366                            if (componentType.isAssignableFrom(String.class)) {
367                                    return getBooleanValues((String[])value, defaultValue);
368                            }
369                            else if (componentType.isAssignableFrom(Boolean.class)) {
370                                    return (boolean[])value;
371                            }
372                    }
373    
374                    return defaultValue;
375            }
376    
377            public static boolean[] getBooleanValues(String[] values) {
378                    return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES);
379            }
380    
381            public static boolean[] getBooleanValues(
382                    String[] values, boolean[] defaultValue) {
383    
384                    if (values == null) {
385                            return defaultValue;
386                    }
387    
388                    boolean[] booleanValues = new boolean[values.length];
389    
390                    for (int i = 0; i < values.length; i++) {
391                            booleanValues[i] = getBoolean(values[i]);
392                    }
393    
394                    return booleanValues;
395            }
396    
397            public static Date getDate(Serializable value, DateFormat dateFormat) {
398                    return getDate(value, dateFormat, new Date());
399            }
400    
401            public static Date getDate(
402                    Serializable value, DateFormat dateFormat, Date defaultValue) {
403    
404                    return get(value, dateFormat, defaultValue);
405            }
406    
407            public static Date getDate(String value, DateFormat dateFormat) {
408                    return getDate(value, dateFormat, new Date());
409            }
410    
411            public static Date getDate(
412                    String value, DateFormat dateFormat, Date defaultValue) {
413    
414                    return get(value, dateFormat, defaultValue);
415            }
416    
417            public static Date[] getDateValues(
418                    Serializable value, DateFormat dateFormat) {
419    
420                    return getDateValues(value, dateFormat, DEFAULT_DATE_VALUES);
421            }
422    
423            public static Date[] getDateValues(
424                    Serializable value, DateFormat dateFormat, Date[] defaultValue) {
425    
426                    Class<?> clazz = value.getClass();
427    
428                    if (clazz.isArray()) {
429                            Class<?> componentType = clazz.getComponentType();
430    
431                            if (componentType.isAssignableFrom(String.class)) {
432                                    return getDateValues((String[])value, dateFormat, defaultValue);
433                            }
434                            else if (componentType.isAssignableFrom(Date.class)) {
435                                    return (Date[])value;
436                            }
437                    }
438    
439                    return defaultValue;
440            }
441    
442            public static Date[] getDateValues(String[] values, DateFormat dateFormat) {
443                    return getDateValues(values, dateFormat, DEFAULT_DATE_VALUES);
444            }
445    
446            public static Date[] getDateValues(
447                    String[] values, DateFormat dateFormat, Date[] defaultValue) {
448    
449                    if (values == null) {
450                            return defaultValue;
451                    }
452    
453                    Date[] dateValues = new Date[values.length];
454    
455                    for (int i = 0; i < values.length; i++) {
456                            dateValues[i] = getDate(values[i], dateFormat);
457                    }
458    
459                    return dateValues;
460            }
461    
462            public static double getDouble(Serializable value) {
463                    return getDouble(value, DEFAULT_DOUBLE);
464            }
465    
466            public static double getDouble(Serializable value, double defaultValue) {
467                    return get(value, defaultValue);
468            }
469    
470            public static double getDouble(String value) {
471                    return getDouble(value, DEFAULT_DOUBLE);
472            }
473    
474            public static double getDouble(String value, double defaultValue) {
475                    return get(value, defaultValue);
476            }
477    
478            public static double[] getDoubleValues(Serializable value) {
479                    return getDoubleValues(value, DEFAULT_DOUBLE_VALUES);
480            }
481    
482            public static double[] getDoubleValues(
483                    Serializable value, double[] defaultValue) {
484    
485                    Class<?> clazz = value.getClass();
486    
487                    if (clazz.isArray()) {
488                            Class<?> componentType = clazz.getComponentType();
489    
490                            if (componentType.isAssignableFrom(String.class)) {
491                                    return getDoubleValues((String[])value, defaultValue);
492                            }
493                            else if (componentType.isAssignableFrom(Double.class)) {
494                                    return (double[])value;
495                            }
496                    }
497    
498                    return defaultValue;
499            }
500    
501            public static double[] getDoubleValues(String[] values) {
502                    return getDoubleValues(values, DEFAULT_DOUBLE_VALUES);
503            }
504    
505            public static double[] getDoubleValues(
506                    String[] values, double[] defaultValue) {
507    
508                    if (values == null) {
509                            return defaultValue;
510                    }
511    
512                    double[] doubleValues = new double[values.length];
513    
514                    for (int i = 0; i < values.length; i++) {
515                            doubleValues[i] = getDouble(values[i]);
516                    }
517    
518                    return doubleValues;
519            }
520    
521            public static float getFloat(Serializable value) {
522                    return getFloat(value, DEFAULT_FLOAT);
523            }
524    
525            public static float getFloat(Serializable value, float defaultValue) {
526                    return get(value, defaultValue);
527            }
528    
529            public static float getFloat(String value) {
530                    return getFloat(value, DEFAULT_FLOAT);
531            }
532    
533            public static float getFloat(String value, float defaultValue) {
534                    return get(value, defaultValue);
535            }
536    
537            public static float[] getFloatValues(Serializable value) {
538                    return getFloatValues(value, DEFAULT_FLOAT_VALUES);
539            }
540    
541            public static float[] getFloatValues(
542                    Serializable value, float[] defaultValue) {
543    
544                    Class<?> clazz = value.getClass();
545    
546                    if (clazz.isArray()) {
547                            Class<?> componentType = clazz.getComponentType();
548    
549                            if (componentType.isAssignableFrom(String.class)) {
550                                    return getFloatValues((String[])value, defaultValue);
551                            }
552                            else if (componentType.isAssignableFrom(Float.class)) {
553                                    return (float[])value;
554                            }
555                    }
556    
557                    return defaultValue;
558            }
559    
560            public static float[] getFloatValues(String[] values) {
561                    return getFloatValues(values, DEFAULT_FLOAT_VALUES);
562            }
563    
564            public static float[] getFloatValues(
565                    String[] values, float[] defaultValue) {
566    
567                    if (values == null) {
568                            return defaultValue;
569                    }
570    
571                    float[] floatValues = new float[values.length];
572    
573                    for (int i = 0; i < values.length; i++) {
574                            floatValues[i] = getFloat(values[i]);
575                    }
576    
577                    return floatValues;
578            }
579    
580            public static int getInteger(Serializable value) {
581                    return getInteger(value, DEFAULT_INTEGER);
582            }
583    
584            public static int getInteger(Serializable value, int defaultValue) {
585                    return get(value, defaultValue);
586            }
587    
588            public static int getInteger(String value) {
589                    return getInteger(value, DEFAULT_INTEGER);
590            }
591    
592            public static int getInteger(String value, int defaultValue) {
593                    return get(value, defaultValue);
594            }
595    
596            public static int[] getIntegerValues(Serializable value) {
597                    return getIntegerValues(value, DEFAULT_INTEGER_VALUES);
598            }
599    
600            public static int[] getIntegerValues(
601                    Serializable value, int[] defaultValue) {
602    
603                    Class<?> clazz = value.getClass();
604    
605                    if (clazz.isArray()) {
606                            Class<?> componentType = clazz.getComponentType();
607    
608                            if (componentType.isAssignableFrom(String.class)) {
609                                    return getIntegerValues((String[])value, defaultValue);
610                            }
611                            else if (componentType.isAssignableFrom(Integer.class)) {
612                                    return (int[])value;
613                            }
614                    }
615    
616                    return defaultValue;
617            }
618    
619            public static int[] getIntegerValues(String[] values) {
620                    return getIntegerValues(values, DEFAULT_INTEGER_VALUES);
621            }
622    
623            public static int[] getIntegerValues(String[] values, int[] defaultValue) {
624                    if (values == null) {
625                            return defaultValue;
626                    }
627    
628                    int[] intValues = new int[values.length];
629    
630                    for (int i = 0; i < values.length; i++) {
631                            intValues[i] = getInteger(values[i]);
632                    }
633    
634                    return intValues;
635            }
636    
637            public static long getLong(Serializable value) {
638                    return getLong(value, DEFAULT_LONG);
639            }
640    
641            public static long getLong(Serializable value, long defaultValue) {
642                    return get(value, defaultValue);
643            }
644    
645            public static long getLong(String value) {
646                    return getLong(value, DEFAULT_LONG);
647            }
648    
649            public static long getLong(String value, long defaultValue) {
650                    return get(value, defaultValue);
651            }
652    
653            public static long[] getLongValues(Serializable value) {
654                    return getLongValues(value, DEFAULT_LONG_VALUES);
655            }
656    
657            public static long[] getLongValues(
658                    Serializable value, long[] defaultValue) {
659    
660                    Class<?> clazz = value.getClass();
661    
662                    if (clazz.isArray()) {
663                            Class<?> componentType = clazz.getComponentType();
664    
665                            if (componentType.isAssignableFrom(String.class)) {
666                                    return getLongValues((String[])value, defaultValue);
667                            }
668                            else if (componentType.isAssignableFrom(Long.class)) {
669                                    return (long[])value;
670                            }
671                    }
672    
673                    return defaultValue;
674            }
675    
676            public static long[] getLongValues(String[] values) {
677                    return getLongValues(values, DEFAULT_LONG_VALUES);
678            }
679    
680            public static long[] getLongValues(String[] values, long[] defaultValue) {
681                    if (values == null) {
682                            return defaultValue;
683                    }
684    
685                    long[] longValues = new long[values.length];
686    
687                    for (int i = 0; i < values.length; i++) {
688                            longValues[i] = getLong(values[i]);
689                    }
690    
691                    return longValues;
692            }
693    
694            public static Number getNumber(Serializable value) {
695                    return getNumber(value, DEFAULT_NUMBER);
696            }
697    
698            public static Number getNumber(Serializable value, Number defaultValue) {
699                    return get(value, defaultValue);
700            }
701    
702            public static Number getNumber(String value) {
703                    return getNumber(value, DEFAULT_NUMBER);
704            }
705    
706            public static Number getNumber(String value, Number defaultValue) {
707                    return get(value, defaultValue);
708            }
709    
710            public static Object getObject(Object value) {
711                    return getObject(value, DEFAULT_OBJECT);
712            }
713    
714            public static Object getObject(Object value, Object defaultValue) {
715                    if (value == null) {
716                            return defaultValue;
717                    }
718    
719                    return value;
720            }
721    
722            public static short getShort(Serializable value) {
723                    return getShort(value, DEFAULT_SHORT);
724            }
725    
726            public static short getShort(Serializable value, short defaultValue) {
727                    return get(value, defaultValue);
728            }
729    
730            public static short getShort(String value) {
731                    return getShort(value, DEFAULT_SHORT);
732            }
733    
734            public static short getShort(String value, short defaultValue) {
735                    return get(value, defaultValue);
736            }
737    
738            public static short[] getShortValues(Serializable value) {
739                    return getShortValues(value, DEFAULT_SHORT_VALUES);
740            }
741    
742            public static short[] getShortValues(
743                    Serializable value, short[] defaultValue) {
744    
745                    Class<?> clazz = value.getClass();
746    
747                    if (clazz.isArray()) {
748                            Class<?> componentType = clazz.getComponentType();
749    
750                            if (componentType.isAssignableFrom(String.class)) {
751                                    return getShortValues((String[])value, defaultValue);
752                            }
753                            else if (componentType.isAssignableFrom(Short.class)) {
754                                    return (short[])value;
755                            }
756                    }
757    
758                    return defaultValue;
759            }
760    
761            public static short[] getShortValues(String[] values) {
762                    return getShortValues(values, DEFAULT_SHORT_VALUES);
763            }
764    
765            public static short[] getShortValues(
766                    String[] values, short[] defaultValue) {
767    
768                    if (values == null) {
769                            return defaultValue;
770                    }
771    
772                    short[] shortValues = new short[values.length];
773    
774                    for (int i = 0; i < values.length; i++) {
775                            shortValues[i] = getShort(values[i]);
776                    }
777    
778                    return shortValues;
779            }
780    
781            public static String getString(Serializable value) {
782                    return getString(value, DEFAULT_STRING);
783            }
784    
785            public static String getString(Serializable value, String defaultValue) {
786                    return get(value, defaultValue);
787            }
788    
789            public static String getString(String value) {
790                    return getString(value, DEFAULT_STRING);
791            }
792    
793            public static String getString(String value, String defaultValue) {
794                    return get(value, defaultValue);
795            }
796    
797            private static int _parseInt(String value, int defaultValue) {
798                    int length = value.length();
799    
800                    if (length <= 0) {
801                            return defaultValue;
802                    }
803    
804                    int pos = 0;
805                    int limit = -Integer.MAX_VALUE;
806                    boolean negative = false;
807    
808                    char c = value.charAt(0);
809    
810                    if (c < CharPool.NUMBER_0) {
811                            if (c == CharPool.MINUS) {
812                                    limit = Integer.MIN_VALUE;
813                                    negative = true;
814                            }
815                            else if (c != CharPool.PLUS) {
816                                    return defaultValue;
817                            }
818    
819                            if (length == 1) {
820                                    return defaultValue;
821                            }
822    
823                            pos++;
824                    }
825    
826                    int smallLimit = limit / 10;
827    
828                    int result = 0;
829    
830                    while (pos < length) {
831                            if (result < smallLimit) {
832                                    return defaultValue;
833                            }
834    
835                            c = value.charAt(pos++);
836    
837                            if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
838                                    return defaultValue;
839                            }
840    
841                            int number = c - CharPool.NUMBER_0;
842    
843                            result *= 10;
844    
845                            if (result < (limit + number)) {
846                                    return defaultValue;
847                            }
848    
849                            result -= number;
850                    }
851    
852                    if (negative) {
853                            return result;
854                    }
855                    else {
856                            return -result;
857                    }
858            }
859    
860            private static long _parseLong(String value, long defaultValue) {
861                    if (_useJDKParseLong == null) {
862                            if (OSDetector.isAIX() && ServerDetector.isWebSphere() &&
863                                    JavaProps.isIBM() && JavaProps.is64bit()) {
864    
865                                    _useJDKParseLong = Boolean.TRUE;
866                            }
867                            else {
868                                    _useJDKParseLong = Boolean.FALSE;
869                            }
870                    }
871    
872                    if (_useJDKParseLong) {
873                            try {
874                                    return Long.parseLong(value);
875                            }
876                            catch (NumberFormatException nfe) {
877                                    return defaultValue;
878                            }
879                    }
880    
881                    int length = value.length();
882    
883                    if (length <= 0) {
884                            return defaultValue;
885                    }
886    
887                    int pos = 0;
888                    long limit = -Long.MAX_VALUE;
889                    boolean negative = false;
890    
891                    char c = value.charAt(0);
892    
893                    if (c < CharPool.NUMBER_0) {
894                            if (c == CharPool.MINUS) {
895                                    limit = Long.MIN_VALUE;
896                                    negative = true;
897                            }
898                            else if (c != CharPool.PLUS) {
899                                    return defaultValue;
900                            }
901    
902                            if (length == 1) {
903                                    return defaultValue;
904                            }
905    
906                            pos++;
907                    }
908    
909                    long smallLimit = limit / 10;
910    
911                    long result = 0;
912    
913                    while (pos < length) {
914                            if (result < smallLimit) {
915                                    return defaultValue;
916                            }
917    
918                            c = value.charAt(pos++);
919    
920                            if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
921                                    return defaultValue;
922                            }
923    
924                            int number = c - CharPool.NUMBER_0;
925    
926                            result *= 10;
927    
928                            if (result < (limit + number)) {
929                                    return defaultValue;
930                            }
931    
932                            result -= number;
933                    }
934    
935                    if (negative) {
936                            return result;
937                    }
938                    else {
939                            return -result;
940                    }
941            }
942    
943            private static short _parseShort(String value, short defaultValue) {
944                    int i = _parseInt(value, defaultValue);
945    
946                    if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) {
947                            return defaultValue;
948                    }
949    
950                    return (short)i;
951            }
952    
953            private static String _trim(String value) {
954                    value = value.trim();
955    
956                    int length = value.length();
957    
958                    StringBuilder sb = new StringBuilder(length);
959    
960                    for (int i = 0; i < length; i++) {
961                            char c = value.charAt(i);
962    
963                            if ((Character.isDigit(c)) ||
964                                    ((c == CharPool.DASH) && (i == 0)) ||
965                                    (c == CharPool.PERIOD) || (c == CharPool.UPPER_CASE_E) ||
966                                    (c == CharPool.LOWER_CASE_E)) {
967    
968                                    sb.append(c);
969                            }
970                    }
971    
972                    return sb.toString();
973            }
974    
975            private static Boolean _useJDKParseLong;
976    
977    }