01: /*
02: * Copyright 2006 Pentaho Corporation. All rights reserved.
03: * This software was developed by Pentaho Corporation and is provided under the terms
04: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
05: * this file except in compliance with the license. If you need a copy of the license,
06: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
07: * BI Platform. The Initial Developer is Pentaho Corporation.
08: *
09: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11: * the license for the specific language governing your rights and limitations.
12: *
13: * @created Jun 20, 2005
14: * @author James Dixon
15: *
16: */
17:
18: package org.pentaho.util;
19:
20: import java.math.BigDecimal;
21: import java.text.DateFormat;
22: import java.util.Date;
23:
24: import org.pentaho.messages.Messages;
25: import org.pentaho.util.logging.Logger;
26:
27: /**
28: * @author James Dixon
29: *
30: * TODO To change the template for this generated type comment go to Window -
31: * Preferences - Java - Code Style - Code Templates
32: */
33: public class ParameterHelper {
34:
35: public static String parameterToString(String value,
36: String defaultValue) {
37: if (value == null) {
38: return defaultValue;
39: }
40: return value;
41: }
42:
43: public static long parameterToLong(String value, long defaultValue) {
44: if (value == null) {
45: return defaultValue;
46: }
47: try {
48: long longValue = Long.valueOf(value).longValue();
49: return longValue;
50: } catch (Exception e) {
51: Logger
52: .error(
53: ParameterHelper.class.getName(),
54: Messages
55: .getErrorString("ParameterHelper.ERROR_0001_INVALID_NUMERIC"), e); //$NON-NLS-1$
56: }
57: return defaultValue;
58: }
59:
60: public static Date parameterToDate(String value, Date defaultValue) {
61: try {
62: Date date = DateFormat.getInstance().parse(value);
63: if (date == null) {
64: return defaultValue;
65: }
66: return date;
67: } catch (Exception e) {
68: return defaultValue;
69: }
70: }
71:
72: public static BigDecimal parameterToDecimal(String value,
73: BigDecimal defaultValue) {
74: if (value == null) {
75: return defaultValue;
76: }
77: try {
78: BigDecimal decimal = new BigDecimal(value);
79: return decimal;
80: } catch (Exception e) {
81: Logger
82: .error(
83: ParameterHelper.class.getName(),
84: Messages
85: .getErrorString("ParameterHelper.ERROR_0001_INVALID_NUMERIC"), e); //$NON-NLS-1$
86: }
87: return defaultValue;
88: }
89:
90: }
|