View Javadoc

1   /*
2    * Copyright 2002,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.commons.jexl.util;
17  
18  /***
19   *  Coercion utilities for the JSTL EL-like coercion.
20   *
21   *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
22   */
23  public class Coercion
24  {
25  
26      public static Boolean coerceBoolean(Object val) {
27          if (val == null)
28          {
29              return Boolean.FALSE;
30          }
31          else if (val instanceof Boolean)
32          {
33              return (Boolean) val;
34          }
35          else if (val instanceof String)
36          {
37              return Boolean.valueOf((String) val);
38          }
39  
40          return null;
41      }
42  
43      public static Integer coerceInteger(Object val)
44          throws Exception
45      {
46          if (val == null)
47          {
48              return new Integer(0);
49          }
50          else if (val instanceof String)
51          {
52              if("".equals((String) val))
53                  return new Integer(0);
54  
55              return Integer.valueOf((String)val);
56          }
57          else if(val instanceof Character)
58          {
59              return new Integer((int)((Character)val).charValue());
60          }
61          else if(val instanceof Boolean)
62          {
63              throw new Exception("Boolean->Integer coercion exception");
64          }
65          else if(val instanceof Number)
66          {
67              return new Integer(((Number)val).intValue());
68          }
69  
70          throw new Exception("Integer coercion exception");
71      }
72  
73      public static Long coerceLong(Object val)
74          throws Exception
75      {
76          if (val == null)
77          {
78              return new Long(0);
79          }
80          else if (val instanceof String)
81          {
82              if("".equals((String) val))
83                  return new Long(0);
84  
85              return Long.valueOf((String)val);
86          }
87          else if(val instanceof Character)
88          {
89              return new Long((long)((Character)val).charValue());
90          }
91          else if(val instanceof Boolean)
92          {
93              throw new Exception("Boolean->Integer coercion exception");
94          }
95          else if(val instanceof Number)
96          {
97              return new Long(((Number)val).longValue());
98          }
99  
100         throw new Exception("Long coercion exception");
101     }
102 
103     public static Double coerceDouble(Object val)
104         throws Exception
105     {
106         if (val == null)
107         {
108             return new Double(0);
109         }
110         else if (val instanceof String)
111         {
112             if("".equals((String) val))
113                 return new Double(0);
114 
115             /*
116              * the spec seems to be iffy about this.  Going to give it a wack
117              *  anyway
118              */
119 
120             return new Double((String) val);
121         }
122         else if(val instanceof Character)
123         {
124             int i = ((Character)val).charValue();
125 
126             return new Double(Double.parseDouble(String.valueOf(i)));
127         }
128         else if(val instanceof Boolean)
129         {
130             throw new Exception("Boolean->Integer coercion exception");
131         }
132         else if(val instanceof Double)
133         {
134             return (Double) val;
135         }
136         else if (val instanceof Number)
137         {
138             //The below construct is used rather than ((Number)val).doubleValue() to ensure
139             //equality between comparint new Double( 6.4 / 3 ) and the jexl expression of 6.4 / 3
140             return new Double(Double.parseDouble(String.valueOf(val)));
141         }
142 
143         throw new Exception("Double coercion exception");
144     }
145 
146     public static boolean isFloatingPoint( final Object o )
147     {
148         return o instanceof Float || o instanceof Double;
149     }
150 
151     public static boolean isNumberable( final Object o )
152     {
153         return o instanceof Integer
154             || o instanceof Long
155             || o instanceof Byte
156             || o instanceof Short
157             || o instanceof Character;
158     }
159 
160 }