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