1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13
14
15
16
17 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 if (obj == null)
27 {
28 throw new IllegalArgumentException("Unable to convert null object to long");
29 }
30 else if (obj instanceof String)
31 {
32 return toLong((String) obj);
33 }
34 else if (obj instanceof Number)
35 {
36 return ((Number) obj).longValue();
37 }
38 else
39 {
40 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 if (obj == null)
48 {
49 throw new IllegalArgumentException("Unable to convert null object to int");
50 }
51 else if (obj instanceof String)
52 {
53 return toInt((String) obj);
54 }
55 else if (obj instanceof Number)
56 {
57 return ((Number) obj).intValue();
58 }
59 else
60 {
61 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 if (obj == null)
69 {
70 throw new IllegalArgumentException("Unable to convert null object to float");
71 }
72 else if (obj instanceof String)
73 {
74 return toFloat((String) obj);
75 }
76 else if (obj instanceof Number)
77 {
78 return ((Number) obj).floatValue();
79 }
80 else
81 {
82 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 if (obj == null)
90 {
91 throw new IllegalArgumentException("Unable to convert null object to double");
92 }
93 else if (obj instanceof String)
94 {
95 return toDouble((String) obj);
96 }
97 else if (obj instanceof Number)
98 {
99 return ((Number) obj).doubleValue();
100 }
101 else
102 {
103 throw new IllegalArgumentException("Unable to convert object of type: "
104 + obj.getClass().getName() + " to double.");
105 }
106 }
107
108 public static int toInt(String str)
109 {
110 return toInt(str, INTEGER_ERROR);
111 }
112
113 public static long toLong(String str)
114 {
115 return toLong(str, LONG_ERROR);
116 }
117
118 public static float toFloat(String str)
119 {
120 return toFloat(str, FLOAT_ERROR);
121 }
122
123 public static double toDouble(String str)
124 {
125 return toDouble(str, DOUBLE_ERROR);
126 }
127 }