View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.convert;
20  
21  
22  /**
23   * TODO: Move to util package and rename to better name
24   *
25   * @author Manfred Geiler (latest modification by $Author$)
26   * @version $Revision$ $Date$
27   */
28  public final class ConverterUtils
29  {
30      private ConverterUtils() {}
31  
32  
33      public static int convertToInt(Object value)
34      {
35          if (value instanceof Number)
36          {
37              return ((Number) value).intValue();
38          }
39          else if (value instanceof String)
40          {
41              try
42              {
43                  return Integer.parseInt((String) value);
44              }
45              catch (NumberFormatException e)
46              {
47                  throw new IllegalArgumentException("Cannot convert " + value.toString() + " to int");
48              }
49          }
50          else
51          {
52              throw new IllegalArgumentException("Cannot convert " + value.toString() + " to int");
53          }
54      }
55  
56      public static boolean convertToBoolean(Object value)
57      {
58          if (value instanceof Boolean)
59          {
60              return ((Boolean) value);
61          }
62          else if (value instanceof String)
63          {
64              try
65              {
66                  return Boolean.parseBoolean((String) value);
67              }
68              catch (Exception e)
69              {
70                  throw new IllegalArgumentException("Cannot convert " + value.toString() + " to boolean");
71              }
72          }
73          else
74          {
75              throw new IllegalArgumentException("Cannot convert " + value.toString() + " to boolean");
76          }
77      }    
78  
79      public static long convertToLong(Object value)
80      {
81          if (value instanceof Number)
82          {
83              return ((Number) value).longValue();
84          }
85          else if (value instanceof String)
86          {
87              try
88              {
89                  return Long.parseLong((String) value);
90              }
91              catch (NumberFormatException e)
92              {
93                  throw new IllegalArgumentException("Cannot convert " + value.toString() + " to long");
94              }
95          }
96          else
97          {
98              throw new IllegalArgumentException("Cannot convert " + value.toString() + " to long");
99          }
100     }
101 
102     public static double convertToDouble(Object value)
103     {
104         if (value instanceof Number)
105         {
106             return ((Number) value).doubleValue();
107         }
108         else if (value instanceof String)
109         {
110             try
111             {
112                 return Double.parseDouble((String) value);
113             }
114             catch (NumberFormatException e)
115             {
116                 throw new IllegalArgumentException("Cannot convert " + value.toString() + " to double");
117             }
118         }
119         else
120         {
121             throw new IllegalArgumentException("Cannot convert " + value.toString() + " to double");
122         }
123     }
124 
125 
126 }