1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | 0 | public class NumberUtils extends org.apache.commons.lang.math.NumberUtils |
18 | |
{ |
19 | |
public static final int INTEGER_ERROR = -999999999; |
20 | |
public static final long LONG_ERROR = -999999999; |
21 | |
public static final float FLOAT_ERROR = -999999999; |
22 | |
public static final double DOUBLE_ERROR = -999999999; |
23 | |
|
24 | |
public static long toLong(Object obj) |
25 | |
{ |
26 | 8 | if (obj == null) |
27 | |
{ |
28 | 2 | throw new IllegalArgumentException("Unable to convert null object to long"); |
29 | |
} |
30 | 6 | else if (obj instanceof String) |
31 | |
{ |
32 | 0 | return toLong((String) obj); |
33 | |
} |
34 | 6 | else if (obj instanceof Number) |
35 | |
{ |
36 | 4 | return ((Number) obj).longValue(); |
37 | |
} |
38 | |
else |
39 | |
{ |
40 | 2 | throw new IllegalArgumentException("Unable to convert object of type: " |
41 | |
+ obj.getClass().getName() + " to long."); |
42 | |
} |
43 | |
} |
44 | |
|
45 | |
public static int toInt(Object obj) |
46 | |
{ |
47 | 0 | if (obj == null) |
48 | |
{ |
49 | 0 | throw new IllegalArgumentException("Unable to convert null object to int"); |
50 | |
} |
51 | 0 | else if (obj instanceof String) |
52 | |
{ |
53 | 0 | return toInt((String) obj); |
54 | |
} |
55 | 0 | else if (obj instanceof Number) |
56 | |
{ |
57 | 0 | return ((Number) obj).intValue(); |
58 | |
} |
59 | |
else |
60 | |
{ |
61 | 0 | throw new IllegalArgumentException("Unable to convert object of type: " |
62 | |
+ obj.getClass().getName() + " to int."); |
63 | |
} |
64 | |
} |
65 | |
|
66 | |
public static float toFloat(Object obj) |
67 | |
{ |
68 | 0 | if (obj == null) |
69 | |
{ |
70 | 0 | throw new IllegalArgumentException("Unable to convert null object to float"); |
71 | |
} |
72 | 0 | else if (obj instanceof String) |
73 | |
{ |
74 | 0 | return toFloat((String) obj); |
75 | |
} |
76 | 0 | else if (obj instanceof Number) |
77 | |
{ |
78 | 0 | return ((Number) obj).floatValue(); |
79 | |
} |
80 | |
else |
81 | |
{ |
82 | 0 | throw new IllegalArgumentException("Unable to convert object of type: " |
83 | |
+ obj.getClass().getName() + " to float."); |
84 | |
} |
85 | |
} |
86 | |
|
87 | |
public static double toDouble(Object obj) |
88 | |
{ |
89 | 0 | if (obj == null) |
90 | |
{ |
91 | 0 | throw new IllegalArgumentException("Unable to convert null object to double"); |
92 | |
} |
93 | 0 | else if (obj instanceof String) |
94 | |
{ |
95 | 0 | return toDouble((String) obj); |
96 | |
} |
97 | 0 | else if (obj instanceof Number) |
98 | |
{ |
99 | 0 | return ((Number) obj).doubleValue(); |
100 | |
} |
101 | |
else |
102 | |
{ |
103 | 0 | throw new IllegalArgumentException("Unable to convert object of type: " |
104 | |
+ obj.getClass().getName() + " to double."); |
105 | |
} |
106 | |
} |
107 | |
|
108 | |
|
109 | |
public static int toInt(String str) { |
110 | 0 | return toInt(str, INTEGER_ERROR); |
111 | |
} |
112 | |
|
113 | |
|
114 | |
public static long toLong(String str) { |
115 | 2 | return toLong(str, LONG_ERROR); |
116 | |
} |
117 | |
|
118 | |
|
119 | |
public static float toFloat(String str) { |
120 | 0 | return toFloat(str, FLOAT_ERROR); |
121 | |
} |
122 | |
|
123 | |
|
124 | |
public static double toDouble(String str) { |
125 | 0 | return toDouble(str, DOUBLE_ERROR); |
126 | |
} |
127 | |
} |