View Javadoc

1   /*   Copyright 2004 OSOCO.net (Carsten Ziegeler)
2   *
3   * Licensed under the Apache License, Version 2.0 (the "License");
4   * you may not use this file except in compliance with the License.
5   * You may obtain a copy of the License at
6   *
7   *     http://www.apache.org/licenses/LICENSE-2.0
8   *
9   * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15  package org.osoco.dyninc;
16  
17  import org.apache.commons.beanutils.ConvertUtils;
18  import org.apache.commons.beanutils.Converter;
19  
20  public class ConversionUtil {
21  
22      static Converter booleanConverter;
23      static Converter integerConverter;
24      static Converter longConverter;
25      static Converter doubleConverter;
26  
27      public static Boolean convertToBoolean(Object value) {
28          if ( booleanConverter == null ) {
29              booleanConverter = ConvertUtils.lookup(Boolean.class);
30          }
31          return (Boolean)booleanConverter.convert(Boolean.class, value);
32      }
33      
34      public static Integer convertToInteger(Object value) {
35          if ( integerConverter == null ) {
36              integerConverter = ConvertUtils.lookup(Integer.class);
37          }
38          if ( Boolean.TRUE.equals(convertToBoolean(value))) {
39              return new Integer(1);
40          }
41          return (Integer)integerConverter.convert(Integer.class, value);
42      }
43  
44      public static Long convertToLong(Object value) {
45          if ( longConverter == null ) {
46              longConverter = ConvertUtils.lookup(Long.class);
47          }
48          if ( Boolean.TRUE.equals(convertToBoolean(value))) {
49              return new Long(1);
50          }
51          return (Long)longConverter.convert(Long.class, value);
52      }
53  
54      public static Double convertToDouble(Object value) {
55          if ( doubleConverter == null ) {
56              doubleConverter = ConvertUtils.lookup(Double.class);
57          }
58          if ( Boolean.TRUE.equals(convertToBoolean(value))) {
59              return new Double(1);
60          }
61          return (Double)doubleConverter.convert(Double.class, value);
62      }
63  
64  }