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