1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import java.math.BigDecimal; |
14 | |
import java.math.BigInteger; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | 0 | public class NumberUtils extends org.apache.commons.lang.math.NumberUtils |
21 | |
{ |
22 | |
public static final int INTEGER_ERROR = -999999999; |
23 | |
public static final long LONG_ERROR = -999999999; |
24 | |
public static final float FLOAT_ERROR = -999999999; |
25 | |
public static final double DOUBLE_ERROR = -999999999; |
26 | |
|
27 | |
public static long toLong(Object obj) |
28 | |
{ |
29 | 0 | if (obj == null) |
30 | |
{ |
31 | 0 | throw new IllegalArgumentException("Unable to convert null object to long"); |
32 | |
} |
33 | 0 | else if (obj instanceof String) |
34 | |
{ |
35 | 0 | return toLong((String) obj); |
36 | |
} |
37 | 0 | else if (obj instanceof Number) |
38 | |
{ |
39 | 0 | return ((Number) obj).longValue(); |
40 | |
} |
41 | |
else |
42 | |
{ |
43 | 0 | throw new IllegalArgumentException("Unable to convert object of type: " |
44 | |
+ obj.getClass().getName() + " to long."); |
45 | |
} |
46 | |
} |
47 | |
|
48 | |
public static int toInt(Object obj) |
49 | |
{ |
50 | 0 | if (obj == null) |
51 | |
{ |
52 | 0 | throw new IllegalArgumentException("Unable to convert null object to int"); |
53 | |
} |
54 | 0 | else if (obj instanceof String) |
55 | |
{ |
56 | 0 | return toInt((String) obj); |
57 | |
} |
58 | 0 | else if (obj instanceof Number) |
59 | |
{ |
60 | 0 | return ((Number) obj).intValue(); |
61 | |
} |
62 | |
else |
63 | |
{ |
64 | 0 | throw new IllegalArgumentException("Unable to convert object of type: " |
65 | |
+ obj.getClass().getName() + " to int."); |
66 | |
} |
67 | |
} |
68 | |
|
69 | |
public static float toFloat(Object obj) |
70 | |
{ |
71 | 0 | if (obj == null) |
72 | |
{ |
73 | 0 | throw new IllegalArgumentException("Unable to convert null object to float"); |
74 | |
} |
75 | 0 | else if (obj instanceof String) |
76 | |
{ |
77 | 0 | return toFloat((String) obj); |
78 | |
} |
79 | 0 | else if (obj instanceof Number) |
80 | |
{ |
81 | 0 | return ((Number) obj).floatValue(); |
82 | |
} |
83 | |
else |
84 | |
{ |
85 | 0 | throw new IllegalArgumentException("Unable to convert object of type: " |
86 | |
+ obj.getClass().getName() + " to float."); |
87 | |
} |
88 | |
} |
89 | |
|
90 | |
public static double toDouble(Object obj) |
91 | |
{ |
92 | 0 | if (obj == null) |
93 | |
{ |
94 | 0 | throw new IllegalArgumentException("Unable to convert null object to double"); |
95 | |
} |
96 | 0 | else if (obj instanceof String) |
97 | |
{ |
98 | 0 | return toDouble((String) obj); |
99 | |
} |
100 | 0 | else if (obj instanceof Number) |
101 | |
{ |
102 | 0 | return ((Number) obj).doubleValue(); |
103 | |
} |
104 | |
else |
105 | |
{ |
106 | 0 | throw new IllegalArgumentException("Unable to convert object of type: " |
107 | |
+ obj.getClass().getName() + " to double."); |
108 | |
} |
109 | |
} |
110 | |
|
111 | |
public static int toInt(String str) |
112 | |
{ |
113 | 0 | return toInt(str, INTEGER_ERROR); |
114 | |
} |
115 | |
|
116 | |
public static long toLong(String str) |
117 | |
{ |
118 | 0 | return toLong(str, LONG_ERROR); |
119 | |
} |
120 | |
|
121 | |
public static float toFloat(String str) |
122 | |
{ |
123 | 0 | return toFloat(str, FLOAT_ERROR); |
124 | |
} |
125 | |
|
126 | |
public static double toDouble(String str) |
127 | |
{ |
128 | 0 | return toDouble(str, DOUBLE_ERROR); |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
@SuppressWarnings("unchecked") |
136 | |
public static <T extends Number> T parseNumber(String text, Class<T> targetClass) |
137 | |
{ |
138 | 0 | text = text.trim(); |
139 | |
|
140 | 0 | if (targetClass.equals(Byte.class)) |
141 | |
{ |
142 | 0 | return (T) Byte.valueOf(text); |
143 | |
} |
144 | 0 | else if (targetClass.equals(Short.class)) |
145 | |
{ |
146 | 0 | return (T) Short.valueOf(text); |
147 | |
} |
148 | 0 | else if (targetClass.equals(Integer.class)) |
149 | |
{ |
150 | 0 | return (T) Integer.valueOf(text); |
151 | |
} |
152 | 0 | else if (targetClass.equals(Long.class)) |
153 | |
{ |
154 | 0 | return (T) Long.valueOf(text); |
155 | |
} |
156 | 0 | else if (targetClass.equals(BigInteger.class)) |
157 | |
{ |
158 | 0 | return (T) new BigInteger(text); |
159 | |
} |
160 | 0 | else if (targetClass.equals(Float.class)) |
161 | |
{ |
162 | 0 | return (T) Float.valueOf(text); |
163 | |
} |
164 | 0 | else if (targetClass.equals(Double.class)) |
165 | |
{ |
166 | 0 | return (T) Double.valueOf(text); |
167 | |
} |
168 | 0 | else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) |
169 | |
{ |
170 | 0 | return (T) new BigDecimal(text); |
171 | |
} |
172 | |
else |
173 | |
{ |
174 | 0 | throw new IllegalArgumentException("Cannot convert String [" + text + "] to target class [" |
175 | |
+ targetClass.getName() + "]"); |
176 | |
} |
177 | |
} |
178 | |
|
179 | |
@SuppressWarnings("unchecked") |
180 | |
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) |
181 | |
throws IllegalArgumentException |
182 | |
{ |
183 | |
|
184 | 0 | if (targetClass.isInstance(number)) |
185 | |
{ |
186 | 0 | return (T) number; |
187 | |
} |
188 | 0 | else if (targetClass.equals(Byte.class)) |
189 | |
{ |
190 | 0 | long value = number.longValue(); |
191 | 0 | if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) |
192 | |
{ |
193 | 0 | raiseOverflowException(number, targetClass); |
194 | |
} |
195 | 0 | return (T) new Byte(number.byteValue()); |
196 | |
} |
197 | 0 | else if (targetClass.equals(Short.class)) |
198 | |
{ |
199 | 0 | long value = number.longValue(); |
200 | 0 | if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) |
201 | |
{ |
202 | 0 | raiseOverflowException(number, targetClass); |
203 | |
} |
204 | 0 | return (T) new Short(number.shortValue()); |
205 | |
} |
206 | 0 | else if (targetClass.equals(Integer.class)) |
207 | |
{ |
208 | 0 | long value = number.longValue(); |
209 | 0 | if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) |
210 | |
{ |
211 | 0 | raiseOverflowException(number, targetClass); |
212 | |
} |
213 | 0 | return (T) new Integer(number.intValue()); |
214 | |
} |
215 | 0 | else if (targetClass.equals(Long.class)) |
216 | |
{ |
217 | 0 | return (T) new Long(number.longValue()); |
218 | |
} |
219 | 0 | else if (targetClass.equals(BigInteger.class)) |
220 | |
{ |
221 | 0 | if (number instanceof BigDecimal) |
222 | |
{ |
223 | |
|
224 | 0 | return (T) ((BigDecimal) number).toBigInteger(); |
225 | |
} |
226 | |
else |
227 | |
{ |
228 | |
|
229 | 0 | return (T) BigInteger.valueOf(number.longValue()); |
230 | |
} |
231 | |
} |
232 | 0 | else if (targetClass.equals(Float.class)) |
233 | |
{ |
234 | 0 | return (T) new Float(number.floatValue()); |
235 | |
} |
236 | 0 | else if (targetClass.equals(Double.class)) |
237 | |
{ |
238 | 0 | return (T) new Double(number.doubleValue()); |
239 | |
} |
240 | 0 | else if (targetClass.equals(BigDecimal.class)) |
241 | |
{ |
242 | |
|
243 | |
|
244 | |
|
245 | 0 | return (T) new BigDecimal(number.toString()); |
246 | |
} |
247 | |
else |
248 | |
{ |
249 | 0 | throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" |
250 | |
+ number.getClass().getName() + "] to unknown target class [" |
251 | |
+ targetClass.getName() + "]"); |
252 | |
} |
253 | |
} |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
private static void raiseOverflowException(Number number, Class targetClass) |
262 | |
{ |
263 | 0 | throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" |
264 | |
+ number.getClass().getName() + "] to target class [" |
265 | |
+ targetClass.getName() + "]: overflow"); |
266 | |
} |
267 | |
|
268 | |
} |