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.text.DateFormat;
18  
19  import java.util.Date;
20  
21  /**
22   * <a href="GetterUtil.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Brian Wing Shun Chan
25   */
26  public class GetterUtil {
27  
28      public static String[] BOOLEANS = {"true", "t", "y", "on", "1"};
29  
30      public static final boolean DEFAULT_BOOLEAN = false;
31  
32      public static final boolean[] DEFAULT_BOOLEAN_VALUES = new boolean[0];
33  
34      public static final Date[] DEFAULT_DATE_VALUES = new Date[0];
35  
36      public static final double DEFAULT_DOUBLE = 0.0;
37  
38      public static final double[] DEFAULT_DOUBLE_VALUES = new double[0];
39  
40      public static final float DEFAULT_FLOAT = 0;
41  
42      public static final float[] DEFAULT_FLOAT_VALUES = new float[0];
43  
44      public static final int DEFAULT_INTEGER = 0;
45  
46      public static final int[] DEFAULT_INTEGER_VALUES = new int[0];
47  
48      public static final long DEFAULT_LONG = 0;
49  
50      public static final long[] DEFAULT_LONG_VALUES = new long[0];
51  
52      public static final short DEFAULT_SHORT = 0;
53  
54      public static final short[] DEFAULT_SHORT_VALUES = new short[0];
55  
56      public static final String DEFAULT_STRING = StringPool.BLANK;
57  
58      public static boolean get(String value, boolean defaultValue) {
59          if (value == null) {
60              return defaultValue;
61          }
62  
63          try {
64              value = value.trim();
65  
66              if (value.equalsIgnoreCase(BOOLEANS[0]) ||
67                  value.equalsIgnoreCase(BOOLEANS[1]) ||
68                  value.equalsIgnoreCase(BOOLEANS[2]) ||
69                  value.equalsIgnoreCase(BOOLEANS[3]) ||
70                  value.equalsIgnoreCase(BOOLEANS[4])) {
71  
72                  return true;
73              }
74              else {
75                  return false;
76              }
77          }
78          catch (Exception e) {
79          }
80  
81          return defaultValue;
82      }
83  
84      public static Date get(
85          String value, DateFormat dateFormat, Date defaultValue) {
86  
87          if (value == null) {
88              return defaultValue;
89          }
90  
91          try {
92              Date date = dateFormat.parse(value.trim());
93  
94              if (date != null) {
95                  return date;
96              }
97          }
98          catch (Exception e) {
99          }
100 
101         return defaultValue;
102     }
103 
104     public static double get(String value, double defaultValue) {
105         if (value != null) {
106             try {
107                 return Double.parseDouble(_trim(value));
108             }
109             catch (Exception e) {
110             }
111         }
112 
113         return defaultValue;
114     }
115 
116     public static float get(String value, float defaultValue) {
117         if (value == null) {
118             return defaultValue;
119         }
120 
121         try {
122             return Float.parseFloat(_trim(value));
123         }
124         catch (Exception e) {
125         }
126 
127         return defaultValue;
128     }
129 
130     public static int get(String value, int defaultValue) {
131         if (value == null) {
132             return defaultValue;
133         }
134 
135         return _parseInt(_trim(value), defaultValue);
136     }
137 
138     public static long get(String value, long defaultValue) {
139         if (value == null) {
140             return defaultValue;
141         }
142 
143         return _parseLong(_trim(value), defaultValue);
144     }
145 
146     public static short get(String value, short defaultValue) {
147         if (value == null) {
148             return defaultValue;
149         }
150 
151         return _parseShort(_trim(value), defaultValue);
152     }
153 
154     public static String get(String value, String defaultValue) {
155         if (value == null) {
156             return defaultValue;
157         }
158 
159         return StringUtil.replace(
160             value.trim(), StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE);
161     }
162 
163     public static boolean getBoolean(String value) {
164         return getBoolean(value, DEFAULT_BOOLEAN);
165     }
166 
167     public static boolean getBoolean(String value, boolean defaultValue) {
168         return get(value, defaultValue);
169     }
170 
171     public static boolean[] getBooleanValues(String[] values) {
172         return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES);
173     }
174 
175     public static boolean[] getBooleanValues(
176         String[] values, boolean[] defaultValue) {
177 
178         if (values == null) {
179             return defaultValue;
180         }
181 
182         boolean[] booleanValues = new boolean[values.length];
183 
184         for (int i = 0; i < values.length; i++) {
185             booleanValues[i] = getBoolean(values[i]);
186         }
187 
188         return booleanValues;
189     }
190 
191     public static Date getDate(String value, DateFormat dateFormat) {
192         return getDate(value, dateFormat, new Date());
193     }
194 
195     public static Date getDate(
196         String value, DateFormat dateFormat, Date defaultValue) {
197 
198         return get(value, dateFormat, defaultValue);
199     }
200 
201     public static Date[] getDateValues(String[] values, DateFormat dateFormat) {
202         return getDateValues(values, dateFormat, DEFAULT_DATE_VALUES);
203     }
204 
205     public static Date[] getDateValues(
206         String[] values, DateFormat dateFormat, Date[] defaultValue) {
207 
208         if (values == null) {
209             return defaultValue;
210         }
211 
212         Date[] dateValues = new Date[values.length];
213 
214         for (int i = 0; i < values.length; i++) {
215             dateValues[i] = getDate(values[i], dateFormat);
216         }
217 
218         return dateValues;
219     }
220 
221     public static double getDouble(String value) {
222         return getDouble(value, DEFAULT_DOUBLE);
223     }
224 
225     public static double getDouble(String value, double defaultValue) {
226         return get(value, defaultValue);
227     }
228 
229     public static double[] getDoubleValues(String[] values) {
230         return getDoubleValues(values, DEFAULT_DOUBLE_VALUES);
231     }
232 
233     public static double[] getDoubleValues(
234         String[] values, double[] defaultValue) {
235 
236         if (values == null) {
237             return defaultValue;
238         }
239 
240         double[] doubleValues = new double[values.length];
241 
242         for (int i = 0; i < values.length; i++) {
243             doubleValues[i] = getDouble(values[i]);
244         }
245 
246         return doubleValues;
247     }
248 
249     public static float getFloat(String value) {
250         return getFloat(value, DEFAULT_FLOAT);
251     }
252 
253     public static float getFloat(String value, float defaultValue) {
254         return get(value, defaultValue);
255     }
256 
257     public static float[] getFloatValues(String[] values) {
258         return getFloatValues(values, DEFAULT_FLOAT_VALUES);
259     }
260 
261     public static float[] getFloatValues(
262         String[] values, float[] defaultValue) {
263 
264         if (values == null) {
265             return defaultValue;
266         }
267 
268         float[] floatValues = new float[values.length];
269 
270         for (int i = 0; i < values.length; i++) {
271             floatValues[i] = getFloat(values[i]);
272         }
273 
274         return floatValues;
275     }
276 
277     public static int getInteger(String value) {
278         return getInteger(value, DEFAULT_INTEGER);
279     }
280 
281     public static int getInteger(String value, int defaultValue) {
282         return get(value, defaultValue);
283     }
284 
285     public static int[] getIntegerValues(String[] values) {
286         return getIntegerValues(values, DEFAULT_INTEGER_VALUES);
287     }
288 
289     public static int[] getIntegerValues(String[] values, int[] defaultValue) {
290         if (values == null) {
291             return defaultValue;
292         }
293 
294         int[] intValues = new int[values.length];
295 
296         for (int i = 0; i < values.length; i++) {
297             intValues[i] = getInteger(values[i]);
298         }
299 
300         return intValues;
301     }
302 
303     public static long getLong(String value) {
304         return getLong(value, DEFAULT_LONG);
305     }
306 
307     public static long getLong(String value, long defaultValue) {
308         return get(value, defaultValue);
309     }
310 
311     public static long[] getLongValues(String[] values) {
312         return getLongValues(values, DEFAULT_LONG_VALUES);
313     }
314 
315     public static long[] getLongValues(String[] values, long[] defaultValue) {
316         if (values == null) {
317             return defaultValue;
318         }
319 
320         long[] longValues = new long[values.length];
321 
322         for (int i = 0; i < values.length; i++) {
323             longValues[i] = getLong(values[i]);
324         }
325 
326         return longValues;
327     }
328 
329     public static short getShort(String value) {
330         return getShort(value, DEFAULT_SHORT);
331     }
332 
333     public static short getShort(String value, short defaultValue) {
334         return get(value, defaultValue);
335     }
336 
337     public static short[] getShortValues(String[] values) {
338         return getShortValues(values, DEFAULT_SHORT_VALUES);
339     }
340 
341     public static short[] getShortValues(
342         String[] values, short[] defaultValue) {
343 
344         if (values == null) {
345             return defaultValue;
346         }
347 
348         short[] shortValues = new short[values.length];
349 
350         for (int i = 0; i < values.length; i++) {
351             shortValues[i] = getShort(values[i]);
352         }
353 
354         return shortValues;
355     }
356 
357     public static String getString(String value) {
358         return getString(value, DEFAULT_STRING);
359     }
360 
361     public static String getString(String value, String defaultValue) {
362         return get(value, defaultValue);
363     }
364 
365     private static int _parseInt(String value, int defaultValue) {
366         int length = value.length();
367 
368         if (length <= 0) {
369             return defaultValue;
370         }
371 
372         int pos = 0;
373         int limit = -Integer.MAX_VALUE;
374         boolean negative = false;
375 
376         char c = value.charAt(0);
377 
378         if (c < CharPool.NUMBER_0) {
379             if (c == CharPool.MINUS) {
380                 limit = Integer.MIN_VALUE;
381                 negative = true;
382             }
383             else if (c != CharPool.PLUS) {
384                 return defaultValue;
385             }
386 
387             if (length == 1) {
388                 return defaultValue;
389             }
390 
391             pos++;
392         }
393 
394         int smallLimit = limit / 10;
395 
396         int result = 0;
397 
398         while (pos < length) {
399             if (result < smallLimit) {
400                 return defaultValue;
401             }
402 
403             c = value.charAt(pos++);
404 
405             if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
406                 return defaultValue;
407             }
408 
409             int number = c - CharPool.NUMBER_0;
410 
411             result *= 10;
412 
413             if (result < (limit + number)) {
414                 return defaultValue;
415             }
416 
417             result -= number;
418         }
419 
420         if (negative) {
421             return result;
422         }
423         else {
424             return -result;
425         }
426     }
427 
428     private static long _parseLong(String value, long defaultValue) {
429         int length = value.length();
430 
431         if (length <= 0) {
432             return defaultValue;
433         }
434 
435         int pos = 0;
436         long limit = -Long.MAX_VALUE;
437         boolean negative = false;
438 
439         char c = value.charAt(0);
440 
441         if (c < CharPool.NUMBER_0) {
442             if (c == CharPool.MINUS) {
443                 limit = Long.MIN_VALUE;
444                 negative = true;
445             }
446             else if (c != CharPool.PLUS) {
447                 return defaultValue;
448             }
449 
450             if (length == 1) {
451                 return defaultValue;
452             }
453 
454             pos++;
455         }
456 
457         long smallLimit = limit / 10;
458 
459         long result = 0;
460 
461         while (pos < length) {
462             if (result < smallLimit) {
463                 return defaultValue;
464             }
465 
466             c = value.charAt(pos++);
467 
468             if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
469                 return defaultValue;
470             }
471 
472             int number = c - CharPool.NUMBER_0;
473 
474             result *= 10;
475 
476             if (result < (limit + number)) {
477                 return defaultValue;
478             }
479 
480             result -= number;
481         }
482 
483         if (negative) {
484             return result;
485         }
486         else {
487             return -result;
488         }
489     }
490 
491     private static short _parseShort(String value, short defaultValue) {
492         int i = _parseInt(value, defaultValue);
493 
494         if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) {
495             return defaultValue;
496         }
497 
498         return (short)i;
499     }
500 
501     private static String _trim(String value) {
502         value = value.trim();
503 
504         int length = value.length();
505 
506         StringBuilder sb = new StringBuilder(length);
507 
508         for (int i = 0; i < length; i++) {
509             char c = value.charAt(i);
510 
511             if ((Character.isDigit(c)) ||
512                 ((c == CharPool.DASH) && (i == 0)) ||
513                 (c == CharPool.PERIOD) || (c == CharPool.UPPER_CASE_E) ||
514                 (c == CharPool.LOWER_CASE_E)) {
515 
516                 sb.append(c);
517             }
518         }
519 
520         return sb.toString();
521     }
522 
523 }