01: /*
02: * Copyright 2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.myfaces.convert;
17:
18: /**
19: * TODO: Move to util package and rename to better name
20: *
21: * @author Manfred Geiler (latest modification by $Author: baranda $)
22: * @version $Revision: 291285 $ $Date: 2005-09-24 13:58:36 +0200 (Sa, 24 Sep 2005) $
23: */
24: public final class ConverterUtils {
25: //private static final Log log = LogFactory.getLog(ConverterUtils.class);
26:
27: private ConverterUtils() {
28: }
29:
30: public static int convertToInt(Object value) {
31: if (value instanceof Number) {
32: return ((Number) value).intValue();
33: } else if (value instanceof String) {
34: try {
35: return Integer.parseInt((String) value);
36: } catch (NumberFormatException e) {
37: throw new IllegalArgumentException("Cannot convert "
38: + value.toString() + " to int");
39: }
40: } else {
41: throw new IllegalArgumentException("Cannot convert "
42: + value.toString() + " to int");
43: }
44: }
45:
46: public static boolean convertToBoolean(Object value) {
47: if (value instanceof Boolean) {
48: return ((Boolean) value).booleanValue();
49: } else if (value instanceof String) {
50: try {
51: return new Boolean((String) value).booleanValue();
52: } catch (Exception e) {
53: throw new IllegalArgumentException("Cannot convert "
54: + value.toString() + " to boolean");
55: }
56: } else {
57: throw new IllegalArgumentException("Cannot convert "
58: + value.toString() + " to boolean");
59: }
60: }
61:
62: public static long convertToLong(Object value) {
63: if (value instanceof Number) {
64: return ((Number) value).longValue();
65: } else if (value instanceof String) {
66: try {
67: return Long.parseLong((String) value);
68: } catch (NumberFormatException e) {
69: throw new IllegalArgumentException("Cannot convert "
70: + value.toString() + " to long");
71: }
72: } else {
73: throw new IllegalArgumentException("Cannot convert "
74: + value.toString() + " to long");
75: }
76: }
77:
78: public static double convertToDouble(Object value) {
79: if (value instanceof Number) {
80: return ((Number) value).doubleValue();
81: } else if (value instanceof String) {
82: try {
83: return Double.parseDouble((String) value);
84: } catch (NumberFormatException e) {
85: throw new IllegalArgumentException("Cannot convert "
86: + value.toString() + " to double");
87: }
88: } else {
89: throw new IllegalArgumentException("Cannot convert "
90: + value.toString() + " to double");
91: }
92: }
93:
94: }
|