1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.jexl.util;
18
19 /**
20 * Coercion utilities for the JSTL EL-like coercion.
21 *
22 * @since 1.0
23 * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
24 */
25 public class Coercion {
26
27 /**
28 * Coerce to a Boolean.
29 *
30 * @param val Object to be coerced.
31 * @return The Boolean coerced value, or null if none possible.
32 */
33 public static Boolean coerceBoolean(Object val) {
34 if (val == null) {
35 return Boolean.FALSE;
36 } else if (val instanceof Boolean) {
37 return (Boolean) val;
38 } else if (val instanceof String) {
39 return Boolean.valueOf((String) val);
40 }
41 return null;
42 }
43
44 /**
45 * Coerce to a Integer.
46 *
47 * @param val Object to be coerced.
48 * @return The Integer coerced value.
49 * @throws Exception If Integer coercion fails.
50 */
51 public static Integer coerceInteger(Object val)
52 throws Exception {
53 if (val == null) {
54 return new Integer(0);
55 } else if (val instanceof String) {
56 if ("".equals(val)) {
57 return new Integer(0);
58 }
59 return Integer.valueOf((String) val);
60 } else if (val instanceof Character) {
61 return new Integer(((Character) val).charValue());
62 } else if (val instanceof Boolean) {
63 throw new Exception("Boolean->Integer coercion exception");
64 } else if (val instanceof Number) {
65 return new Integer(((Number) val).intValue());
66 }
67
68 throw new Exception("Integer coercion exception");
69 }
70
71 /**
72 * Coerce to a Long.
73 *
74 * @param val Object to be coerced.
75 * @return The Long coerced value.
76 * @throws Exception If Long coercion fails.
77 */
78 public static Long coerceLong(Object val)
79 throws Exception {
80 if (val == null) {
81 return new Long(0);
82 } else if (val instanceof String) {
83 if ("".equals(val)) {
84 return new Long(0);
85 }
86 return Long.valueOf((String) val);
87 } else if (val instanceof Character) {
88 return new Long(((Character) val).charValue());
89 } else if (val instanceof Boolean) {
90 throw new Exception("Boolean->Long coercion exception");
91 } else if (val instanceof Number) {
92 return new Long(((Number) val).longValue());
93 }
94
95 throw new Exception("Long coercion exception");
96 }
97
98 /**
99 * Coerce to a Double.
100 *
101 * @param val Object to be coerced.
102 * @return The Double coerced value.
103 * @throws Exception If Double coercion fails.
104 */
105 public static Double coerceDouble(Object val)
106 throws Exception {
107 if (val == null) {
108 return new Double(0);
109 } else if (val instanceof String) {
110 if ("".equals(val)) {
111 return new Double(0);
112 }
113
114 /*
115 * the spec seems to be iffy about this. Going to give it a wack
116 * anyway
117 */
118
119 return new Double((String) val);
120 } else if (val instanceof Character) {
121 int i = ((Character) val).charValue();
122
123 return new Double(Double.parseDouble(String.valueOf(i)));
124 } else if (val instanceof Boolean) {
125 throw new Exception("Boolean->Double coercion exception");
126 } else if (val instanceof Double) {
127 return (Double) val;
128 } else if (val instanceof Number) {
129 //The below construct is used rather than ((Number)val).doubleValue() to ensure
130 //equality between comparint new Double( 6.4 / 3 ) and the jexl expression of 6.4 / 3
131 return new Double(Double.parseDouble(String.valueOf(val)));
132 }
133
134 throw new Exception("Double coercion exception");
135 }
136
137 /**
138 * Is Object a floating point number.
139 *
140 * @param o Object to be analyzed.
141 * @return true if it is a Float or a Double.
142 */
143 public static boolean isFloatingPoint(final Object o) {
144 return o instanceof Float || o instanceof Double;
145 }
146
147 /**
148 * Is Object a whole number.
149 *
150 * @param o Object to be analyzed.
151 * @return true if Integer, Long, Byte, Short or Character.
152 */
153 public static boolean isNumberable(final Object o) {
154 return o instanceof Integer
155 || o instanceof Long
156 || o instanceof Byte
157 || o instanceof Short
158 || o instanceof Character;
159 }
160
161 }