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