001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.Serializable;
018    
019    import java.text.DateFormat;
020    
021    import java.util.Date;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class GetterUtil {
027    
028            public static String[] BOOLEANS = {"true", "t", "y", "on", "1"};
029    
030            public static final boolean DEFAULT_BOOLEAN = false;
031    
032            public static final boolean[] DEFAULT_BOOLEAN_VALUES = new boolean[0];
033    
034            public static final byte DEFAULT_BYTE = 0;
035    
036            public static final byte[] DEFAULT_BYTE_VALUES = new byte[0];
037    
038            public static final Date[] DEFAULT_DATE_VALUES = new Date[0];
039    
040            public static final double DEFAULT_DOUBLE = 0.0;
041    
042            public static final double[] DEFAULT_DOUBLE_VALUES = new double[0];
043    
044            public static final float DEFAULT_FLOAT = 0;
045    
046            public static final float[] DEFAULT_FLOAT_VALUES = new float[0];
047    
048            public static final int DEFAULT_INTEGER = 0;
049    
050            public static final int[] DEFAULT_INTEGER_VALUES = new int[0];
051    
052            public static final long DEFAULT_LONG = 0;
053    
054            public static final long[] DEFAULT_LONG_VALUES = new long[0];
055    
056            public static final short DEFAULT_SHORT = 0;
057    
058            public static final short[] DEFAULT_SHORT_VALUES = new short[0];
059    
060            public static final String DEFAULT_STRING = StringPool.BLANK;
061    
062            public static boolean get(Serializable value, boolean defaultValue) {
063                    if (value == null) {
064                            return defaultValue;
065                    }
066    
067                    if (value instanceof String) {
068                            return get((String)value, defaultValue);
069                    }
070                    else if (value.getClass().isAssignableFrom(Boolean.class)) {
071                            return (Boolean)value;
072                    }
073    
074                    return defaultValue;
075            }
076    
077            public static Date get(
078                    Serializable value, DateFormat dateFormat, Date defaultValue) {
079    
080                    if (value == null) {
081                            return defaultValue;
082                    }
083    
084                    if (value instanceof String) {
085                            return get((String)value, dateFormat, defaultValue);
086                    }
087                    else if (value.getClass().isAssignableFrom(Date.class)) {
088                            return (Date)value;
089                    }
090    
091                    return defaultValue;
092            }
093    
094            public static double get(Serializable value, double defaultValue) {
095                    if (value == null) {
096                            return defaultValue;
097                    }
098    
099                    if (value instanceof String) {
100                            return get((String)value, defaultValue);
101                    }
102                    else if (value.getClass().isAssignableFrom(Double.class)) {
103                            return (Double)value;
104                    }
105    
106                    return defaultValue;
107            }
108    
109            public static float get(Serializable value, float defaultValue) {
110                    if (value == null) {
111                            return defaultValue;
112                    }
113    
114                    if (value instanceof String) {
115                            return get((String)value, defaultValue);
116                    }
117                    else if (value.getClass().isAssignableFrom(Float.class)) {
118                            return (Float)value;
119                    }
120    
121                    return defaultValue;
122            }
123    
124            public static int get(Serializable value, int defaultValue) {
125                    if (value == null) {
126                            return defaultValue;
127                    }
128    
129                    if (value instanceof String) {
130                            return get((String)value, defaultValue);
131                    }
132                    else if (value.getClass().isAssignableFrom(Integer.class)) {
133                            return (Integer)value;
134                    }
135    
136                    return defaultValue;
137            }
138    
139            public static long get(Serializable value, long defaultValue) {
140                    if (value == null) {
141                            return defaultValue;
142                    }
143    
144                    if (value instanceof String) {
145                            return get((String)value, defaultValue);
146                    }
147                    else if (value.getClass().isAssignableFrom(Long.class)) {
148                            return (Long)value;
149                    }
150    
151                    return defaultValue;
152            }
153    
154            public static short get(Serializable value, short defaultValue) {
155                    if (value == null) {
156                            return defaultValue;
157                    }
158    
159                    if (value instanceof String) {
160                            return get((String)value, defaultValue);
161                    }
162                    else if (value.getClass().isAssignableFrom(Short.class)) {
163                            return (Short)value;
164                    }
165    
166                    return defaultValue;
167            }
168    
169            public static String get(Serializable value, String defaultValue) {
170                    if (value == null) {
171                            return defaultValue;
172                    }
173    
174                    if (value instanceof String) {
175                            return get((String)value, defaultValue);
176                    }
177    
178                    return defaultValue;
179            }
180    
181            public static boolean get(String value, boolean defaultValue) {
182                    if (value == null) {
183                            return defaultValue;
184                    }
185    
186                    try {
187                            value = value.trim();
188    
189                            if (value.equalsIgnoreCase(BOOLEANS[0]) ||
190                                    value.equalsIgnoreCase(BOOLEANS[1]) ||
191                                    value.equalsIgnoreCase(BOOLEANS[2]) ||
192                                    value.equalsIgnoreCase(BOOLEANS[3]) ||
193                                    value.equalsIgnoreCase(BOOLEANS[4])) {
194    
195                                    return true;
196                            }
197                            else {
198                                    return false;
199                            }
200                    }
201                    catch (Exception e) {
202                    }
203    
204                    return defaultValue;
205            }
206    
207            public static Date get(
208                    String value, DateFormat dateFormat, Date defaultValue) {
209    
210                    if (value == null) {
211                            return defaultValue;
212                    }
213    
214                    try {
215                            Date date = dateFormat.parse(value.trim());
216    
217                            if (date != null) {
218                                    return date;
219                            }
220                    }
221                    catch (Exception e) {
222                    }
223    
224                    return defaultValue;
225            }
226    
227            public static double get(String value, double defaultValue) {
228                    if (value != null) {
229                            try {
230                                    return Double.parseDouble(_trim(value));
231                            }
232                            catch (Exception e) {
233                            }
234                    }
235    
236                    return defaultValue;
237            }
238    
239            public static float get(String value, float defaultValue) {
240                    if (value == null) {
241                            return defaultValue;
242                    }
243    
244                    try {
245                            return Float.parseFloat(_trim(value));
246                    }
247                    catch (Exception e) {
248                    }
249    
250                    return defaultValue;
251            }
252    
253            public static int get(String value, int defaultValue) {
254                    if (value == null) {
255                            return defaultValue;
256                    }
257    
258                    return _parseInt(_trim(value), defaultValue);
259            }
260    
261            public static long get(String value, long defaultValue) {
262                    if (value == null) {
263                            return defaultValue;
264                    }
265    
266                    return _parseLong(_trim(value), defaultValue);
267            }
268    
269            public static short get(String value, short defaultValue) {
270                    if (value == null) {
271                            return defaultValue;
272                    }
273    
274                    return _parseShort(_trim(value), defaultValue);
275            }
276    
277            public static String get(String value, String defaultValue) {
278                    if (value == null) {
279                            return defaultValue;
280                    }
281    
282                    return StringUtil.replace(
283                            value.trim(), StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE);
284            }
285    
286            public static boolean getBoolean(Serializable value) {
287                    return getBoolean(value, DEFAULT_BOOLEAN);
288            }
289    
290            public static boolean getBoolean(Serializable value, boolean defaultValue) {
291                    return get(value, defaultValue);
292            }
293    
294            public static boolean getBoolean(String value) {
295                    return getBoolean(value, DEFAULT_BOOLEAN);
296            }
297    
298            public static boolean getBoolean(String value, boolean defaultValue) {
299                    return get(value, defaultValue);
300            }
301    
302            public static boolean[] getBooleanValues(Serializable value) {
303                    return getBooleanValues(value, DEFAULT_BOOLEAN_VALUES);
304            }
305    
306            public static boolean[] getBooleanValues(
307                    Serializable value, boolean[] defaultValue) {
308    
309                    Class<?> classObject = value.getClass();
310    
311                    if (classObject.isArray()) {
312                            Class<?> componentType = classObject.getComponentType();
313    
314                            if (componentType.isAssignableFrom(String.class)) {
315                                    return getBooleanValues((String[])value, defaultValue);
316                            }
317                            else if (componentType.isAssignableFrom(Boolean.class)) {
318                                    return (boolean[])value;
319                            }
320                    }
321    
322                    return defaultValue;
323            }
324    
325            public static boolean[] getBooleanValues(String[] values) {
326                    return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES);
327            }
328    
329            public static boolean[] getBooleanValues(
330                    String[] values, boolean[] defaultValue) {
331    
332                    if (values == null) {
333                            return defaultValue;
334                    }
335    
336                    boolean[] booleanValues = new boolean[values.length];
337    
338                    for (int i = 0; i < values.length; i++) {
339                            booleanValues[i] = getBoolean(values[i]);
340                    }
341    
342                    return booleanValues;
343            }
344    
345            public static Date getDate(Serializable value, DateFormat dateFormat) {
346                    return getDate(value, dateFormat, new Date());
347            }
348    
349            public static Date getDate(
350                    Serializable value, DateFormat dateFormat, Date defaultValue) {
351    
352                    return get(value, dateFormat, defaultValue);
353            }
354    
355            public static Date getDate(String value, DateFormat dateFormat) {
356                    return getDate(value, dateFormat, new Date());
357            }
358    
359            public static Date getDate(
360                    String value, DateFormat dateFormat, Date defaultValue) {
361    
362                    return get(value, dateFormat, defaultValue);
363            }
364    
365            public static Date[] getDateValues(
366                    Serializable value, DateFormat dateFormat) {
367    
368                    return getDateValues(value, dateFormat, DEFAULT_DATE_VALUES);
369            }
370    
371            public static Date[] getDateValues(
372                    Serializable value, DateFormat dateFormat, Date[] defaultValue) {
373    
374                    Class<?> classObject = value.getClass();
375    
376                    if (classObject.isArray()) {
377                            Class<?> componentType = classObject.getComponentType();
378    
379                            if (componentType.isAssignableFrom(String.class)) {
380                                    return getDateValues((String[])value, dateFormat, defaultValue);
381                            }
382                            else if (componentType.isAssignableFrom(Date.class)) {
383                                    return (Date[])value;
384                            }
385                    }
386    
387                    return defaultValue;
388            }
389    
390            public static Date[] getDateValues(String[] values, DateFormat dateFormat) {
391                    return getDateValues(values, dateFormat, DEFAULT_DATE_VALUES);
392            }
393    
394            public static Date[] getDateValues(
395                    String[] values, DateFormat dateFormat, Date[] defaultValue) {
396    
397                    if (values == null) {
398                            return defaultValue;
399                    }
400    
401                    Date[] dateValues = new Date[values.length];
402    
403                    for (int i = 0; i < values.length; i++) {
404                            dateValues[i] = getDate(values[i], dateFormat);
405                    }
406    
407                    return dateValues;
408            }
409    
410            public static double getDouble(Serializable value) {
411                    return getDouble(value, DEFAULT_DOUBLE);
412            }
413    
414            public static double getDouble(Serializable value, double defaultValue) {
415                    return get(value, defaultValue);
416            }
417    
418            public static double getDouble(String value) {
419                    return getDouble(value, DEFAULT_DOUBLE);
420            }
421    
422            public static double getDouble(String value, double defaultValue) {
423                    return get(value, defaultValue);
424            }
425    
426            public static double[] getDoubleValues(Serializable value) {
427                    return getDoubleValues(value, DEFAULT_DOUBLE_VALUES);
428            }
429    
430            public static double[] getDoubleValues(
431                    Serializable value, double[] defaultValue) {
432    
433                    Class<?> classObject = value.getClass();
434    
435                    if (classObject.isArray()) {
436                            Class<?> componentType = classObject.getComponentType();
437    
438                            if (componentType.isAssignableFrom(String.class)) {
439                                    return getDoubleValues((String[])value, defaultValue);
440                            }
441                            else if (componentType.isAssignableFrom(Double.class)) {
442                                    return (double[])value;
443                            }
444                    }
445    
446                    return defaultValue;
447            }
448    
449            public static double[] getDoubleValues(String[] values) {
450                    return getDoubleValues(values, DEFAULT_DOUBLE_VALUES);
451            }
452    
453            public static double[] getDoubleValues(
454                    String[] values, double[] defaultValue) {
455    
456                    if (values == null) {
457                            return defaultValue;
458                    }
459    
460                    double[] doubleValues = new double[values.length];
461    
462                    for (int i = 0; i < values.length; i++) {
463                            doubleValues[i] = getDouble(values[i]);
464                    }
465    
466                    return doubleValues;
467            }
468    
469            public static float getFloat(Serializable value) {
470                    return getFloat(value, DEFAULT_FLOAT);
471            }
472    
473            public static float getFloat(Serializable value, float defaultValue) {
474                    return get(value, defaultValue);
475            }
476    
477            public static float getFloat(String value) {
478                    return getFloat(value, DEFAULT_FLOAT);
479            }
480    
481            public static float getFloat(String value, float defaultValue) {
482                    return get(value, defaultValue);
483            }
484    
485            public static float[] getFloatValues(Serializable value) {
486                    return getFloatValues(value, DEFAULT_FLOAT_VALUES);
487            }
488    
489            public static float[] getFloatValues(
490                    Serializable value, float[] defaultValue) {
491    
492                    Class<?> classObject = value.getClass();
493    
494                    if (classObject.isArray()) {
495                            Class<?> componentType = classObject.getComponentType();
496    
497                            if (componentType.isAssignableFrom(String.class)) {
498                                    return getFloatValues((String[])value, defaultValue);
499                            }
500                            else if (componentType.isAssignableFrom(Float.class)) {
501                                    return (float[])value;
502                            }
503                    }
504    
505                    return defaultValue;
506            }
507    
508            public static float[] getFloatValues(String[] values) {
509                    return getFloatValues(values, DEFAULT_FLOAT_VALUES);
510            }
511    
512            public static float[] getFloatValues(
513                    String[] values, float[] defaultValue) {
514    
515                    if (values == null) {
516                            return defaultValue;
517                    }
518    
519                    float[] floatValues = new float[values.length];
520    
521                    for (int i = 0; i < values.length; i++) {
522                            floatValues[i] = getFloat(values[i]);
523                    }
524    
525                    return floatValues;
526            }
527    
528            public static int getInteger(Serializable value) {
529                    return getInteger(value, DEFAULT_INTEGER);
530            }
531    
532            public static int getInteger(Serializable value, int defaultValue) {
533                    return get(value, defaultValue);
534            }
535    
536            public static int getInteger(String value) {
537                    return getInteger(value, DEFAULT_INTEGER);
538            }
539    
540            public static int getInteger(String value, int defaultValue) {
541                    return get(value, defaultValue);
542            }
543    
544            public static int[] getIntegerValues(Serializable value) {
545                    return getIntegerValues(value, DEFAULT_INTEGER_VALUES);
546            }
547    
548            public static int[] getIntegerValues(
549                    Serializable value, int[] defaultValue) {
550    
551                    Class<?> classObject = value.getClass();
552    
553                    if (classObject.isArray()) {
554                            Class<?> componentType = classObject.getComponentType();
555    
556                            if (componentType.isAssignableFrom(String.class)) {
557                                    return getIntegerValues((String[])value, defaultValue);
558                            }
559                            else if (componentType.isAssignableFrom(Integer.class)) {
560                                    return (int[])value;
561                            }
562                    }
563    
564                    return defaultValue;
565            }
566    
567            public static int[] getIntegerValues(String[] values) {
568                    return getIntegerValues(values, DEFAULT_INTEGER_VALUES);
569            }
570    
571            public static int[] getIntegerValues(String[] values, int[] defaultValue) {
572                    if (values == null) {
573                            return defaultValue;
574                    }
575    
576                    int[] intValues = new int[values.length];
577    
578                    for (int i = 0; i < values.length; i++) {
579                            intValues[i] = getInteger(values[i]);
580                    }
581    
582                    return intValues;
583            }
584    
585            public static long getLong(Serializable value) {
586                    return getLong(value, DEFAULT_LONG);
587            }
588    
589            public static long getLong(Serializable value, long defaultValue) {
590                    return get(value, defaultValue);
591            }
592    
593            public static long getLong(String value) {
594                    return getLong(value, DEFAULT_LONG);
595            }
596    
597            public static long getLong(String value, long defaultValue) {
598                    return get(value, defaultValue);
599            }
600    
601            public static long[] getLongValues(Serializable value) {
602                    return getLongValues(value, DEFAULT_LONG_VALUES);
603            }
604    
605            public static long[] getLongValues(
606                    Serializable value, long[] defaultValue) {
607    
608                    Class<?> classObject = value.getClass();
609    
610                    if (classObject.isArray()) {
611                            Class<?> componentType = classObject.getComponentType();
612    
613                            if (componentType.isAssignableFrom(String.class)) {
614                                    return getLongValues((String[])value, defaultValue);
615                            }
616                            else if (componentType.isAssignableFrom(Long.class)) {
617                                    return (long[])value;
618                            }
619                    }
620    
621                    return defaultValue;
622            }
623    
624            public static long[] getLongValues(String[] values) {
625                    return getLongValues(values, DEFAULT_LONG_VALUES);
626            }
627    
628            public static long[] getLongValues(String[] values, long[] defaultValue) {
629                    if (values == null) {
630                            return defaultValue;
631                    }
632    
633                    long[] longValues = new long[values.length];
634    
635                    for (int i = 0; i < values.length; i++) {
636                            longValues[i] = getLong(values[i]);
637                    }
638    
639                    return longValues;
640            }
641    
642            public static short getShort(Serializable value) {
643                    return getShort(value, DEFAULT_SHORT);
644            }
645    
646            public static short getShort(Serializable value, short defaultValue) {
647                    return get(value, defaultValue);
648            }
649    
650            public static short getShort(String value) {
651                    return getShort(value, DEFAULT_SHORT);
652            }
653    
654            public static short getShort(String value, short defaultValue) {
655                    return get(value, defaultValue);
656            }
657    
658            public static short[] getShortValues(Serializable value) {
659                    return getShortValues(value, DEFAULT_SHORT_VALUES);
660            }
661    
662            public static short[] getShortValues(
663                    Serializable value, short[] defaultValue) {
664    
665                    Class<?> classObject = value.getClass();
666    
667                    if (classObject.isArray()) {
668                            Class<?> componentType = classObject.getComponentType();
669    
670                            if (componentType.isAssignableFrom(String.class)) {
671                                    return getShortValues((String[])value, defaultValue);
672                            }
673                            else if (componentType.isAssignableFrom(Short.class)) {
674                                    return (short[])value;
675                            }
676                    }
677    
678                    return defaultValue;
679            }
680    
681            public static short[] getShortValues(String[] values) {
682                    return getShortValues(values, DEFAULT_SHORT_VALUES);
683            }
684    
685            public static short[] getShortValues(
686                    String[] values, short[] defaultValue) {
687    
688                    if (values == null) {
689                            return defaultValue;
690                    }
691    
692                    short[] shortValues = new short[values.length];
693    
694                    for (int i = 0; i < values.length; i++) {
695                            shortValues[i] = getShort(values[i]);
696                    }
697    
698                    return shortValues;
699            }
700    
701            public static String getString(Serializable value) {
702                    return getString(value, DEFAULT_STRING);
703            }
704    
705            public static String getString(Serializable value, String defaultValue) {
706                    return get(value, defaultValue);
707            }
708    
709            public static String getString(String value) {
710                    return getString(value, DEFAULT_STRING);
711            }
712    
713            public static String getString(String value, String defaultValue) {
714                    return get(value, defaultValue);
715            }
716    
717            private static int _parseInt(String value, int defaultValue) {
718                    int length = value.length();
719    
720                    if (length <= 0) {
721                            return defaultValue;
722                    }
723    
724                    int pos = 0;
725                    int limit = -Integer.MAX_VALUE;
726                    boolean negative = false;
727    
728                    char c = value.charAt(0);
729    
730                    if (c < CharPool.NUMBER_0) {
731                            if (c == CharPool.MINUS) {
732                                    limit = Integer.MIN_VALUE;
733                                    negative = true;
734                            }
735                            else if (c != CharPool.PLUS) {
736                                    return defaultValue;
737                            }
738    
739                            if (length == 1) {
740                                    return defaultValue;
741                            }
742    
743                            pos++;
744                    }
745    
746                    int smallLimit = limit / 10;
747    
748                    int result = 0;
749    
750                    while (pos < length) {
751                            if (result < smallLimit) {
752                                    return defaultValue;
753                            }
754    
755                            c = value.charAt(pos++);
756    
757                            if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
758                                    return defaultValue;
759                            }
760    
761                            int number = c - CharPool.NUMBER_0;
762    
763                            result *= 10;
764    
765                            if (result < (limit + number)) {
766                                    return defaultValue;
767                            }
768    
769                            result -= number;
770                    }
771    
772                    if (negative) {
773                            return result;
774                    }
775                    else {
776                            return -result;
777                    }
778            }
779    
780            private static long _parseLong(String value, long defaultValue) {
781                    int length = value.length();
782    
783                    if (length <= 0) {
784                            return defaultValue;
785                    }
786    
787                    int pos = 0;
788                    long limit = -Long.MAX_VALUE;
789                    boolean negative = false;
790    
791                    char c = value.charAt(0);
792    
793                    if (c < CharPool.NUMBER_0) {
794                            if (c == CharPool.MINUS) {
795                                    limit = Long.MIN_VALUE;
796                                    negative = true;
797                            }
798                            else if (c != CharPool.PLUS) {
799                                    return defaultValue;
800                            }
801    
802                            if (length == 1) {
803                                    return defaultValue;
804                            }
805    
806                            pos++;
807                    }
808    
809                    long smallLimit = limit / 10;
810    
811                    long result = 0;
812    
813                    while (pos < length) {
814                            if (result < smallLimit) {
815                                    return defaultValue;
816                            }
817    
818                            c = value.charAt(pos++);
819    
820                            if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
821                                    return defaultValue;
822                            }
823    
824                            int number = c - CharPool.NUMBER_0;
825    
826                            result *= 10;
827    
828                            if (result < (limit + number)) {
829                                    return defaultValue;
830                            }
831    
832                            result -= number;
833                    }
834    
835                    if (negative) {
836                            return result;
837                    }
838                    else {
839                            return -result;
840                    }
841            }
842    
843            private static short _parseShort(String value, short defaultValue) {
844                    int i = _parseInt(value, defaultValue);
845    
846                    if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) {
847                            return defaultValue;
848                    }
849    
850                    return (short)i;
851            }
852    
853            private static String _trim(String value) {
854                    value = value.trim();
855    
856                    int length = value.length();
857    
858                    StringBuilder sb = new StringBuilder(length);
859    
860                    for (int i = 0; i < length; i++) {
861                            char c = value.charAt(i);
862    
863                            if ((Character.isDigit(c)) ||
864                                    ((c == CharPool.DASH) && (i == 0)) ||
865                                    (c == CharPool.PERIOD) || (c == CharPool.UPPER_CASE_E) ||
866                                    (c == CharPool.LOWER_CASE_E)) {
867    
868                                    sb.append(c);
869                            }
870                    }
871    
872                    return sb.toString();
873            }
874    
875    }