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