001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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().toLowerCase();
234    
235                            if (value.equals(BOOLEANS[0]) || value.equals(BOOLEANS[1]) ||
236                                    value.equals(BOOLEANS[2]) || value.equals(BOOLEANS[3]) ||
237                                    value.equals(BOOLEANS[4])) {
238    
239                                    return true;
240                            }
241                            else {
242                                    return false;
243                            }
244                    }
245                    catch (Exception e) {
246                    }
247    
248                    return defaultValue;
249            }
250    
251            public static Date get(
252                    String value, DateFormat dateFormat, Date defaultValue) {
253    
254                    if (value == null) {
255                            return defaultValue;
256                    }
257    
258                    try {
259                            Date date = dateFormat.parse(value.trim());
260    
261                            if (date != null) {
262                                    return date;
263                            }
264                    }
265                    catch (Exception e) {
266                    }
267    
268                    return defaultValue;
269            }
270    
271            public static double get(String value, double defaultValue) {
272                    if (value != null) {
273                            try {
274                                    return Double.parseDouble(_trim(value));
275                            }
276                            catch (Exception e) {
277                            }
278                    }
279    
280                    return defaultValue;
281            }
282    
283            public static float get(String value, float defaultValue) {
284                    if (value == null) {
285                            return defaultValue;
286                    }
287    
288                    try {
289                            return Float.parseFloat(_trim(value));
290                    }
291                    catch (Exception e) {
292                    }
293    
294                    return defaultValue;
295            }
296    
297            public static int get(String value, int defaultValue) {
298                    if (value == null) {
299                            return defaultValue;
300                    }
301    
302                    return _parseInt(_trim(value), defaultValue);
303            }
304    
305            public static long get(String value, long defaultValue) {
306                    if (value == null) {
307                            return defaultValue;
308                    }
309    
310                    return _parseLong(_trim(value), defaultValue);
311            }
312    
313            public static short get(String value, short defaultValue) {
314                    if (value == null) {
315                            return defaultValue;
316                    }
317    
318                    return _parseShort(_trim(value), defaultValue);
319            }
320    
321            public static String get(String value, String defaultValue) {
322                    if (value == null) {
323                            return defaultValue;
324                    }
325    
326                    value = value.trim();
327    
328                    if (value.indexOf(CharPool.RETURN) != -1) {
329                            value = StringUtil.replace(
330                                    value, StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE);
331                    }
332    
333                    return value;
334            }
335    
336            public static boolean getBoolean(Serializable value) {
337                    return getBoolean(value, DEFAULT_BOOLEAN);
338            }
339    
340            public static boolean getBoolean(Serializable value, boolean defaultValue) {
341                    return get(value, defaultValue);
342            }
343    
344            public static boolean getBoolean(String value) {
345                    return getBoolean(value, DEFAULT_BOOLEAN);
346            }
347    
348            public static boolean getBoolean(String value, boolean defaultValue) {
349                    return get(value, defaultValue);
350            }
351    
352            public static boolean[] getBooleanValues(Serializable value) {
353                    return getBooleanValues(value, DEFAULT_BOOLEAN_VALUES);
354            }
355    
356            public static boolean[] getBooleanValues(
357                    Serializable value, boolean[] defaultValue) {
358    
359                    Class<?> clazz = value.getClass();
360    
361                    if (clazz.isArray()) {
362                            Class<?> componentType = clazz.getComponentType();
363    
364                            if (componentType.isAssignableFrom(String.class)) {
365                                    return getBooleanValues((String[])value, defaultValue);
366                            }
367                            else if (componentType.isAssignableFrom(Boolean.class)) {
368                                    return (boolean[])value;
369                            }
370                    }
371    
372                    return defaultValue;
373            }
374    
375            public static boolean[] getBooleanValues(String[] values) {
376                    return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES);
377            }
378    
379            public static boolean[] getBooleanValues(
380                    String[] values, boolean[] defaultValue) {
381    
382                    if (values == null) {
383                            return defaultValue;
384                    }
385    
386                    boolean[] booleanValues = new boolean[values.length];
387    
388                    for (int i = 0; i < values.length; i++) {
389                            booleanValues[i] = getBoolean(values[i]);
390                    }
391    
392                    return booleanValues;
393            }
394    
395            public static Date getDate(Serializable value, DateFormat dateFormat) {
396                    return getDate(value, dateFormat, new Date());
397            }
398    
399            public static Date getDate(
400                    Serializable value, DateFormat dateFormat, Date defaultValue) {
401    
402                    return get(value, dateFormat, defaultValue);
403            }
404    
405            public static Date getDate(String value, DateFormat dateFormat) {
406                    return getDate(value, dateFormat, new Date());
407            }
408    
409            public static Date getDate(
410                    String value, DateFormat dateFormat, Date defaultValue) {
411    
412                    return get(value, dateFormat, defaultValue);
413            }
414    
415            public static Date[] getDateValues(
416                    Serializable value, DateFormat dateFormat) {
417    
418                    return getDateValues(value, dateFormat, DEFAULT_DATE_VALUES);
419            }
420    
421            public static Date[] getDateValues(
422                    Serializable value, DateFormat dateFormat, Date[] defaultValue) {
423    
424                    Class<?> clazz = value.getClass();
425    
426                    if (clazz.isArray()) {
427                            Class<?> componentType = clazz.getComponentType();
428    
429                            if (componentType.isAssignableFrom(String.class)) {
430                                    return getDateValues((String[])value, dateFormat, defaultValue);
431                            }
432                            else if (componentType.isAssignableFrom(Date.class)) {
433                                    return (Date[])value;
434                            }
435                    }
436    
437                    return defaultValue;
438            }
439    
440            public static Date[] getDateValues(String[] values, DateFormat dateFormat) {
441                    return getDateValues(values, dateFormat, DEFAULT_DATE_VALUES);
442            }
443    
444            public static Date[] getDateValues(
445                    String[] values, DateFormat dateFormat, Date[] defaultValue) {
446    
447                    if (values == null) {
448                            return defaultValue;
449                    }
450    
451                    Date[] dateValues = new Date[values.length];
452    
453                    for (int i = 0; i < values.length; i++) {
454                            dateValues[i] = getDate(values[i], dateFormat);
455                    }
456    
457                    return dateValues;
458            }
459    
460            public static double getDouble(Serializable value) {
461                    return getDouble(value, DEFAULT_DOUBLE);
462            }
463    
464            public static double getDouble(Serializable value, double defaultValue) {
465                    return get(value, defaultValue);
466            }
467    
468            public static double getDouble(String value) {
469                    return getDouble(value, DEFAULT_DOUBLE);
470            }
471    
472            public static double getDouble(String value, double defaultValue) {
473                    return get(value, defaultValue);
474            }
475    
476            public static double[] getDoubleValues(Serializable value) {
477                    return getDoubleValues(value, DEFAULT_DOUBLE_VALUES);
478            }
479    
480            public static double[] getDoubleValues(
481                    Serializable value, double[] defaultValue) {
482    
483                    Class<?> clazz = value.getClass();
484    
485                    if (clazz.isArray()) {
486                            Class<?> componentType = clazz.getComponentType();
487    
488                            if (componentType.isAssignableFrom(String.class)) {
489                                    return getDoubleValues((String[])value, defaultValue);
490                            }
491                            else if (componentType.isAssignableFrom(Double.class)) {
492                                    return (double[])value;
493                            }
494                    }
495    
496                    return defaultValue;
497            }
498    
499            public static double[] getDoubleValues(String[] values) {
500                    return getDoubleValues(values, DEFAULT_DOUBLE_VALUES);
501            }
502    
503            public static double[] getDoubleValues(
504                    String[] values, double[] defaultValue) {
505    
506                    if (values == null) {
507                            return defaultValue;
508                    }
509    
510                    double[] doubleValues = new double[values.length];
511    
512                    for (int i = 0; i < values.length; i++) {
513                            doubleValues[i] = getDouble(values[i]);
514                    }
515    
516                    return doubleValues;
517            }
518    
519            public static float getFloat(Serializable value) {
520                    return getFloat(value, DEFAULT_FLOAT);
521            }
522    
523            public static float getFloat(Serializable value, float defaultValue) {
524                    return get(value, defaultValue);
525            }
526    
527            public static float getFloat(String value) {
528                    return getFloat(value, DEFAULT_FLOAT);
529            }
530    
531            public static float getFloat(String value, float defaultValue) {
532                    return get(value, defaultValue);
533            }
534    
535            public static float[] getFloatValues(Serializable value) {
536                    return getFloatValues(value, DEFAULT_FLOAT_VALUES);
537            }
538    
539            public static float[] getFloatValues(
540                    Serializable value, float[] defaultValue) {
541    
542                    Class<?> clazz = value.getClass();
543    
544                    if (clazz.isArray()) {
545                            Class<?> componentType = clazz.getComponentType();
546    
547                            if (componentType.isAssignableFrom(String.class)) {
548                                    return getFloatValues((String[])value, defaultValue);
549                            }
550                            else if (componentType.isAssignableFrom(Float.class)) {
551                                    return (float[])value;
552                            }
553                    }
554    
555                    return defaultValue;
556            }
557    
558            public static float[] getFloatValues(String[] values) {
559                    return getFloatValues(values, DEFAULT_FLOAT_VALUES);
560            }
561    
562            public static float[] getFloatValues(
563                    String[] values, float[] defaultValue) {
564    
565                    if (values == null) {
566                            return defaultValue;
567                    }
568    
569                    float[] floatValues = new float[values.length];
570    
571                    for (int i = 0; i < values.length; i++) {
572                            floatValues[i] = getFloat(values[i]);
573                    }
574    
575                    return floatValues;
576            }
577    
578            public static int getInteger(Serializable value) {
579                    return getInteger(value, DEFAULT_INTEGER);
580            }
581    
582            public static int getInteger(Serializable value, int defaultValue) {
583                    return get(value, defaultValue);
584            }
585    
586            public static int getInteger(String value) {
587                    return getInteger(value, DEFAULT_INTEGER);
588            }
589    
590            public static int getInteger(String value, int defaultValue) {
591                    return get(value, defaultValue);
592            }
593    
594            public static int[] getIntegerValues(Serializable value) {
595                    return getIntegerValues(value, DEFAULT_INTEGER_VALUES);
596            }
597    
598            public static int[] getIntegerValues(
599                    Serializable value, int[] defaultValue) {
600    
601                    Class<?> clazz = value.getClass();
602    
603                    if (clazz.isArray()) {
604                            Class<?> componentType = clazz.getComponentType();
605    
606                            if (componentType.isAssignableFrom(String.class)) {
607                                    return getIntegerValues((String[])value, defaultValue);
608                            }
609                            else if (componentType.isAssignableFrom(Integer.class)) {
610                                    return (int[])value;
611                            }
612                    }
613    
614                    return defaultValue;
615            }
616    
617            public static int[] getIntegerValues(String[] values) {
618                    return getIntegerValues(values, DEFAULT_INTEGER_VALUES);
619            }
620    
621            public static int[] getIntegerValues(String[] values, int[] defaultValue) {
622                    if (values == null) {
623                            return defaultValue;
624                    }
625    
626                    int[] intValues = new int[values.length];
627    
628                    for (int i = 0; i < values.length; i++) {
629                            intValues[i] = getInteger(values[i]);
630                    }
631    
632                    return intValues;
633            }
634    
635            public static long getLong(Serializable value) {
636                    return getLong(value, DEFAULT_LONG);
637            }
638    
639            public static long getLong(Serializable value, long defaultValue) {
640                    return get(value, defaultValue);
641            }
642    
643            public static long getLong(String value) {
644                    return getLong(value, DEFAULT_LONG);
645            }
646    
647            public static long getLong(String value, long defaultValue) {
648                    return get(value, defaultValue);
649            }
650    
651            public static long[] getLongValues(Serializable value) {
652                    return getLongValues(value, DEFAULT_LONG_VALUES);
653            }
654    
655            public static long[] getLongValues(
656                    Serializable value, long[] defaultValue) {
657    
658                    Class<?> clazz = value.getClass();
659    
660                    if (clazz.isArray()) {
661                            Class<?> componentType = clazz.getComponentType();
662    
663                            if (componentType.isAssignableFrom(String.class)) {
664                                    return getLongValues((String[])value, defaultValue);
665                            }
666                            else if (componentType.isAssignableFrom(Long.class)) {
667                                    return (long[])value;
668                            }
669                    }
670    
671                    return defaultValue;
672            }
673    
674            public static long[] getLongValues(String[] values) {
675                    return getLongValues(values, DEFAULT_LONG_VALUES);
676            }
677    
678            public static long[] getLongValues(String[] values, long[] defaultValue) {
679                    if (values == null) {
680                            return defaultValue;
681                    }
682    
683                    long[] longValues = new long[values.length];
684    
685                    for (int i = 0; i < values.length; i++) {
686                            longValues[i] = getLong(values[i]);
687                    }
688    
689                    return longValues;
690            }
691    
692            public static Number getNumber(Serializable value) {
693                    return getNumber(value, DEFAULT_NUMBER);
694            }
695    
696            public static Number getNumber(Serializable value, Number defaultValue) {
697                    return get(value, defaultValue);
698            }
699    
700            public static Number getNumber(String value) {
701                    return getNumber(value, DEFAULT_NUMBER);
702            }
703    
704            public static Number getNumber(String value, Number defaultValue) {
705                    return get(value, defaultValue);
706            }
707    
708            public static Object getObject(Object value) {
709                    return getObject(value, DEFAULT_OBJECT);
710            }
711    
712            public static Object getObject(Object value, Object defaultValue) {
713                    if (value == null) {
714                            return defaultValue;
715                    }
716    
717                    return value;
718            }
719    
720            public static short getShort(Serializable value) {
721                    return getShort(value, DEFAULT_SHORT);
722            }
723    
724            public static short getShort(Serializable value, short defaultValue) {
725                    return get(value, defaultValue);
726            }
727    
728            public static short getShort(String value) {
729                    return getShort(value, DEFAULT_SHORT);
730            }
731    
732            public static short getShort(String value, short defaultValue) {
733                    return get(value, defaultValue);
734            }
735    
736            public static short[] getShortValues(Serializable value) {
737                    return getShortValues(value, DEFAULT_SHORT_VALUES);
738            }
739    
740            public static short[] getShortValues(
741                    Serializable value, short[] defaultValue) {
742    
743                    Class<?> clazz = value.getClass();
744    
745                    if (clazz.isArray()) {
746                            Class<?> componentType = clazz.getComponentType();
747    
748                            if (componentType.isAssignableFrom(String.class)) {
749                                    return getShortValues((String[])value, defaultValue);
750                            }
751                            else if (componentType.isAssignableFrom(Short.class)) {
752                                    return (short[])value;
753                            }
754                    }
755    
756                    return defaultValue;
757            }
758    
759            public static short[] getShortValues(String[] values) {
760                    return getShortValues(values, DEFAULT_SHORT_VALUES);
761            }
762    
763            public static short[] getShortValues(
764                    String[] values, short[] defaultValue) {
765    
766                    if (values == null) {
767                            return defaultValue;
768                    }
769    
770                    short[] shortValues = new short[values.length];
771    
772                    for (int i = 0; i < values.length; i++) {
773                            shortValues[i] = getShort(values[i]);
774                    }
775    
776                    return shortValues;
777            }
778    
779            public static String getString(Serializable value) {
780                    return getString(value, DEFAULT_STRING);
781            }
782    
783            public static String getString(Serializable value, String defaultValue) {
784                    return get(value, defaultValue);
785            }
786    
787            public static String getString(String value) {
788                    return getString(value, DEFAULT_STRING);
789            }
790    
791            public static String getString(String value, String defaultValue) {
792                    return get(value, defaultValue);
793            }
794    
795            private static int _parseInt(String value, int defaultValue) {
796                    int length = value.length();
797    
798                    if (length <= 0) {
799                            return defaultValue;
800                    }
801    
802                    int pos = 0;
803                    int limit = -Integer.MAX_VALUE;
804                    boolean negative = false;
805    
806                    char c = value.charAt(0);
807    
808                    if (c < CharPool.NUMBER_0) {
809                            if (c == CharPool.MINUS) {
810                                    limit = Integer.MIN_VALUE;
811                                    negative = true;
812                            }
813                            else if (c != CharPool.PLUS) {
814                                    return defaultValue;
815                            }
816    
817                            if (length == 1) {
818                                    return defaultValue;
819                            }
820    
821                            pos++;
822                    }
823    
824                    int smallLimit = limit / 10;
825    
826                    int result = 0;
827    
828                    while (pos < length) {
829                            if (result < smallLimit) {
830                                    return defaultValue;
831                            }
832    
833                            c = value.charAt(pos++);
834    
835                            if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
836                                    return defaultValue;
837                            }
838    
839                            int number = c - CharPool.NUMBER_0;
840    
841                            result *= 10;
842    
843                            if (result < (limit + number)) {
844                                    return defaultValue;
845                            }
846    
847                            result -= number;
848                    }
849    
850                    if (negative) {
851                            return result;
852                    }
853                    else {
854                            return -result;
855                    }
856            }
857    
858            private static long _parseLong(String value, long defaultValue) {
859                    if (_useJDKParseLong == null) {
860                            if (OSDetector.isAIX() && ServerDetector.isWebSphere() &&
861                                    JavaProps.isIBM() && JavaProps.is64bit()) {
862    
863                                    _useJDKParseLong = Boolean.TRUE;
864                            }
865                            else {
866                                    _useJDKParseLong = Boolean.FALSE;
867                            }
868                    }
869    
870                    if (_useJDKParseLong) {
871                            try {
872                                    return Long.parseLong(value);
873                            }
874                            catch (NumberFormatException nfe) {
875                                    return defaultValue;
876                            }
877                    }
878    
879                    int length = value.length();
880    
881                    if (length <= 0) {
882                            return defaultValue;
883                    }
884    
885                    int pos = 0;
886                    long limit = -Long.MAX_VALUE;
887                    boolean negative = false;
888    
889                    char c = value.charAt(0);
890    
891                    if (c < CharPool.NUMBER_0) {
892                            if (c == CharPool.MINUS) {
893                                    limit = Long.MIN_VALUE;
894                                    negative = true;
895                            }
896                            else if (c != CharPool.PLUS) {
897                                    return defaultValue;
898                            }
899    
900                            if (length == 1) {
901                                    return defaultValue;
902                            }
903    
904                            pos++;
905                    }
906    
907                    long smallLimit = limit / 10;
908    
909                    long result = 0;
910    
911                    while (pos < length) {
912                            if (result < smallLimit) {
913                                    return defaultValue;
914                            }
915    
916                            c = value.charAt(pos++);
917    
918                            if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
919                                    return defaultValue;
920                            }
921    
922                            int number = c - CharPool.NUMBER_0;
923    
924                            result *= 10;
925    
926                            if (result < (limit + number)) {
927                                    return defaultValue;
928                            }
929    
930                            result -= number;
931                    }
932    
933                    if (negative) {
934                            return result;
935                    }
936                    else {
937                            return -result;
938                    }
939            }
940    
941            private static short _parseShort(String value, short defaultValue) {
942                    int i = _parseInt(value, defaultValue);
943    
944                    if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) {
945                            return defaultValue;
946                    }
947    
948                    return (short)i;
949            }
950    
951            private static String _trim(String value) {
952                    value = value.trim();
953    
954                    int length = value.length();
955    
956                    StringBuilder sb = new StringBuilder(length);
957    
958                    for (int i = 0; i < length; i++) {
959                            char c = value.charAt(i);
960    
961                            if ((Character.isDigit(c)) ||
962                                    ((c == CharPool.DASH) && (i == 0)) ||
963                                    (c == CharPool.PERIOD) || (c == CharPool.UPPER_CASE_E) ||
964                                    (c == CharPool.LOWER_CASE_E)) {
965    
966                                    sb.append(c);
967                            }
968                    }
969    
970                    return sb.toString();
971            }
972    
973            private static Boolean _useJDKParseLong;
974    
975    }