KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jexl > util > Coercion


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  * @since 1.0
22  * @author <a HREF="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
23  */

24 public class Coercion {
25
26     /**
27      * Coerce to a Boolean.
28      *
29      * @param val Object to be coerced.
30      * @return The Boolean coerced value, or null if none possible.
31      */

32     public static Boolean JavaDoc coerceBoolean(Object JavaDoc val) {
33         if (val == null) {
34             return Boolean.FALSE;
35         } else if (val instanceof Boolean JavaDoc) {
36             return (Boolean JavaDoc) val;
37         } else if (val instanceof String JavaDoc) {
38             return Boolean.valueOf((String JavaDoc) val);
39         }
40         return null;
41     }
42
43     /**
44      * Coerce to a Integer.
45      *
46      * @param val Object to be coerced.
47      * @return The Integer coerced value.
48      * @throws Exception If Integer coercion fails.
49      */

50     public static Integer JavaDoc coerceInteger(Object JavaDoc val)
51     throws Exception JavaDoc {
52         if (val == null) {
53             return new Integer JavaDoc(0);
54         } else if (val instanceof String JavaDoc) {
55             if ("".equals(val)) {
56                 return new Integer JavaDoc(0);
57             }
58             return Integer.valueOf((String JavaDoc) val);
59         } else if (val instanceof Character JavaDoc) {
60             return new Integer JavaDoc(((Character JavaDoc) val).charValue());
61         } else if (val instanceof Boolean JavaDoc) {
62             throw new Exception JavaDoc("Boolean->Integer coercion exception");
63         } else if (val instanceof Number JavaDoc) {
64             return new Integer JavaDoc(((Number JavaDoc) val).intValue());
65         }
66
67         throw new Exception JavaDoc("Integer coercion exception");
68     }
69
70     /**
71      * Coerce to a Long.
72      *
73      * @param val Object to be coerced.
74      * @return The Long coerced value.
75      * @throws Exception If Long coercion fails.
76      */

77     public static Long JavaDoc coerceLong(Object JavaDoc val)
78     throws Exception JavaDoc {
79         if (val == null) {
80             return new Long JavaDoc(0);
81         } else if (val instanceof String JavaDoc) {
82             if ("".equals(val)) {
83                 return new Long JavaDoc(0);
84             }
85             return Long.valueOf((String JavaDoc) val);
86         } else if (val instanceof Character JavaDoc) {
87             return new Long JavaDoc(((Character JavaDoc) val).charValue());
88         } else if (val instanceof Boolean JavaDoc) {
89             throw new Exception JavaDoc("Boolean->Long coercion exception");
90         } else if (val instanceof Number JavaDoc) {
91             return new Long JavaDoc(((Number JavaDoc) val).longValue());
92         }
93
94         throw new Exception JavaDoc("Long coercion exception");
95     }
96
97     /**
98      * Coerce to a Double.
99      *
100      * @param val Object to be coerced.
101      * @return The Double coerced value.
102      * @throws Exception If Double coercion fails.
103      */

104     public static Double JavaDoc coerceDouble(Object JavaDoc val)
105     throws Exception JavaDoc {
106         if (val == null) {
107             return new Double JavaDoc(0);
108         } else if (val instanceof String JavaDoc) {
109             if ("".equals(val)) {
110                 return new Double JavaDoc(0);
111             }
112
113             /*
114              * the spec seems to be iffy about this. Going to give it a wack
115              * anyway
116              */

117
118             return new Double JavaDoc((String JavaDoc) val);
119         } else if (val instanceof Character JavaDoc) {
120             int i = ((Character JavaDoc) val).charValue();
121
122             return new Double JavaDoc(Double.parseDouble(String.valueOf(i)));
123         } else if (val instanceof Boolean JavaDoc) {
124             throw new Exception JavaDoc("Boolean->Double coercion exception");
125         } else if (val instanceof Double JavaDoc) {
126             return (Double JavaDoc) val;
127         } else if (val instanceof Number JavaDoc) {
128             //The below construct is used rather than ((Number)val).doubleValue() to ensure
129
//equality between comparint new Double( 6.4 / 3 ) and the jexl expression of 6.4 / 3
130
return new Double JavaDoc(Double.parseDouble(String.valueOf(val)));
131         }
132
133         throw new Exception JavaDoc("Double coercion exception");
134     }
135
136     /**
137      * Is Object a floating point number.
138      *
139      * @param o Object to be analyzed.
140      * @return true if it is a Float or a Double.
141      */

142     public static boolean isFloatingPoint(final Object JavaDoc o) {
143         return o instanceof Float JavaDoc || o instanceof Double JavaDoc;
144     }
145
146     /**
147      * Is Object a whole number.
148      *
149      * @param o Object to be analyzed.
150      * @return true if Integer, Long, Byte, Short or Character.
151      */

152     public static boolean isNumberable(final Object JavaDoc o) {
153         return o instanceof Integer JavaDoc
154             || o instanceof Long JavaDoc
155             || o instanceof Byte JavaDoc
156             || o instanceof Short JavaDoc
157             || o instanceof Character JavaDoc;
158     }
159
160 }
161
Popular Tags