KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > convert > ConverterUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 /**
20  * TODO: Move to util package and rename to better name
21  *
22  * @author Manfred Geiler (latest modification by $Author: matze $)
23  * @version $Revision: 1.52 $ $Date: 2004/10/13 11:51:00 $
24  */

25 public final class ConverterUtils
26 {
27     //private static final Log log = LogFactory.getLog(ConverterUtils.class);
28

29     private ConverterUtils() {}
30
31
32     public static int convertToInt(Object JavaDoc value)
33     {
34         if (value instanceof Number JavaDoc)
35         {
36             return ((Number JavaDoc)value).intValue();
37         }
38         else if (value instanceof String JavaDoc)
39         {
40             try
41             {
42                 return Integer.parseInt((String JavaDoc)value);
43             }
44             catch (NumberFormatException JavaDoc e)
45             {
46                 throw new IllegalArgumentException JavaDoc("Cannot convert " + value.toString() + " to int");
47             }
48         }
49         else
50         {
51             throw new IllegalArgumentException JavaDoc("Cannot convert " + value.toString() + " to int");
52         }
53     }
54
55     public static boolean convertToBoolean(Object JavaDoc value)
56     {
57         if (value instanceof Boolean JavaDoc)
58         {
59             return ((Boolean JavaDoc)value).booleanValue();
60         }
61         else if (value instanceof String JavaDoc)
62         {
63             try
64             {
65                 return new Boolean JavaDoc((String JavaDoc)value).booleanValue();
66             }
67             catch (Exception JavaDoc e)
68             {
69                 throw new IllegalArgumentException JavaDoc("Cannot convert " + value.toString() + " to boolean");
70             }
71         }
72         else
73         {
74             throw new IllegalArgumentException JavaDoc("Cannot convert " + value.toString() + " to int");
75         }
76     }
77
78     public static long convertToLong(Object JavaDoc value)
79     {
80         if (value instanceof Number JavaDoc)
81         {
82             return ((Number JavaDoc)value).longValue();
83         }
84         else if (value instanceof String JavaDoc)
85         {
86             try
87             {
88                 return Long.parseLong((String JavaDoc)value);
89             }
90             catch (NumberFormatException JavaDoc e)
91             {
92                 throw new IllegalArgumentException JavaDoc("Cannot convert " + value.toString() + " to long");
93             }
94         }
95         else
96         {
97             throw new IllegalArgumentException JavaDoc("Cannot convert " + value.toString() + " to long");
98         }
99     }
100
101     public static double convertToDouble(Object JavaDoc value)
102     {
103         if (value instanceof Number JavaDoc)
104         {
105             return ((Number JavaDoc)value).doubleValue();
106         }
107         else if (value instanceof String JavaDoc)
108         {
109             try
110             {
111                 return Double.parseDouble((String JavaDoc)value);
112             }
113             catch (NumberFormatException JavaDoc e)
114             {
115                 throw new IllegalArgumentException JavaDoc("Cannot convert " + value.toString() + " to double");
116             }
117         }
118         else
119         {
120             throw new IllegalArgumentException JavaDoc("Cannot convert " + value.toString() + " to double");
121         }
122     }
123
124
125 }
126
Popular Tags