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