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