1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import org.mule.api.NamedObject; |
14 | |
|
15 | |
import org.apache.commons.logging.Log; |
16 | |
import org.apache.commons.logging.LogFactory; |
17 | |
|
18 | 0 | public class ObjectUtils extends org.apache.commons.lang.ObjectUtils |
19 | |
{ |
20 | |
|
21 | |
|
22 | 0 | protected static final Log logger = LogFactory.getLog(ObjectUtils.class); |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public static String identityToShortString(Object obj) |
33 | |
{ |
34 | 0 | if (obj == null) |
35 | |
{ |
36 | 0 | return "null"; |
37 | |
} |
38 | |
else |
39 | |
{ |
40 | 0 | return new StringBuffer(40).append( |
41 | |
ClassUtils.getSimpleName(obj.getClass())) |
42 | |
.append('@') |
43 | |
.append(Integer.toHexString(System.identityHashCode(obj)) |
44 | |
).toString(); |
45 | |
} |
46 | |
} |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public static String getString(final Object answer, String defaultValue) |
57 | |
{ |
58 | 0 | if (answer != null) |
59 | |
{ |
60 | 0 | return answer.toString(); |
61 | |
} |
62 | |
else |
63 | |
{ |
64 | 0 | return defaultValue; |
65 | |
} |
66 | |
} |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public static boolean getBoolean(final Object answer, boolean defaultValue) |
77 | |
{ |
78 | 0 | if (answer != null) |
79 | |
{ |
80 | 0 | if (answer instanceof Boolean) |
81 | |
{ |
82 | 0 | return (Boolean) answer; |
83 | |
|
84 | |
} |
85 | 0 | else if (answer instanceof String) |
86 | |
{ |
87 | 0 | return Boolean.valueOf((String) answer); |
88 | |
|
89 | |
} |
90 | 0 | else if (answer instanceof Number) |
91 | |
{ |
92 | 0 | Number n = (Number) answer; |
93 | 0 | return (n.intValue() > 0) ? Boolean.TRUE : Boolean.FALSE; |
94 | |
} |
95 | |
else |
96 | |
{ |
97 | 0 | if (logger.isWarnEnabled()) |
98 | |
{ |
99 | 0 | logger.warn("Value exists but cannot be converted to boolean: " |
100 | |
+ answer + ", returning default value: " + defaultValue); |
101 | |
} |
102 | 0 | return defaultValue; |
103 | |
} |
104 | |
} |
105 | 0 | return defaultValue; |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public static byte getByte(final Object answer, byte defaultValue) |
117 | |
{ |
118 | 0 | if (answer == null) |
119 | |
{ |
120 | 0 | return defaultValue; |
121 | |
} |
122 | 0 | else if (answer instanceof Number) |
123 | |
{ |
124 | 0 | return ((Number) answer).byteValue(); |
125 | |
} |
126 | 0 | else if (answer instanceof String) |
127 | |
{ |
128 | |
try |
129 | |
{ |
130 | 0 | return Byte.valueOf((String) answer); |
131 | |
} |
132 | 0 | catch (NumberFormatException e) |
133 | |
{ |
134 | |
|
135 | |
} |
136 | |
} |
137 | 0 | if (logger.isWarnEnabled()) |
138 | |
{ |
139 | 0 | logger.warn("Value exists but cannot be converted to byte: " |
140 | |
+ answer + ", returning default value: " + defaultValue); |
141 | |
} |
142 | 0 | return defaultValue; |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public static short getShort(final Object answer, short defaultValue) |
154 | |
{ |
155 | 0 | if (answer == null) |
156 | |
{ |
157 | 0 | return defaultValue; |
158 | |
} |
159 | 0 | else if (answer instanceof Number) |
160 | |
{ |
161 | 0 | return ((Number) answer).shortValue(); |
162 | |
} |
163 | 0 | else if (answer instanceof String) |
164 | |
{ |
165 | |
try |
166 | |
{ |
167 | 0 | return Short.valueOf((String) answer); |
168 | |
} |
169 | 0 | catch (NumberFormatException e) |
170 | |
{ |
171 | |
|
172 | |
} |
173 | |
} |
174 | 0 | if (logger.isWarnEnabled()) |
175 | |
{ |
176 | 0 | logger.warn("Value exists but cannot be converted to short: " |
177 | |
+ answer + ", returning default value: " + defaultValue); |
178 | |
} |
179 | 0 | return defaultValue; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public static int getInt(final Object answer, int defaultValue) |
191 | |
{ |
192 | 0 | if (answer == null) |
193 | |
{ |
194 | 0 | return defaultValue; |
195 | |
} |
196 | 0 | else if (answer instanceof Number) |
197 | |
{ |
198 | 0 | return ((Number) answer).intValue(); |
199 | |
} |
200 | 0 | else if (answer instanceof String) |
201 | |
{ |
202 | |
try |
203 | |
{ |
204 | 0 | return Integer.valueOf((String) answer); |
205 | |
} |
206 | 0 | catch (NumberFormatException e) |
207 | |
{ |
208 | |
|
209 | |
} |
210 | |
} |
211 | 0 | if (logger.isWarnEnabled()) |
212 | |
{ |
213 | 0 | logger.warn("Value exists but cannot be converted to int: " |
214 | |
+ answer + ", returning default value: " + defaultValue); |
215 | |
} |
216 | 0 | return defaultValue; |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public static long getLong(final Object answer, long defaultValue) |
228 | |
{ |
229 | 0 | if (answer == null) |
230 | |
{ |
231 | 0 | return defaultValue; |
232 | |
} |
233 | 0 | else if (answer instanceof Number) |
234 | |
{ |
235 | 0 | return ((Number) answer).longValue(); |
236 | |
} |
237 | 0 | else if (answer instanceof String) |
238 | |
{ |
239 | |
try |
240 | |
{ |
241 | 0 | return Long.valueOf((String) answer); |
242 | |
} |
243 | 0 | catch (NumberFormatException e) |
244 | |
{ |
245 | |
|
246 | |
|
247 | |
} |
248 | |
} |
249 | 0 | if (logger.isWarnEnabled()) |
250 | |
{ |
251 | 0 | logger.warn("Value exists but cannot be converted to long: " |
252 | |
+ answer + ", returning default value: " + defaultValue); |
253 | |
} |
254 | 0 | return defaultValue; |
255 | |
} |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
public static float getFloat(final Object answer, float defaultValue) |
266 | |
{ |
267 | 0 | if (answer == null) |
268 | |
{ |
269 | 0 | return defaultValue; |
270 | |
} |
271 | 0 | else if (answer instanceof Number) |
272 | |
{ |
273 | 0 | return ((Number) answer).floatValue(); |
274 | |
} |
275 | 0 | else if (answer instanceof String) |
276 | |
{ |
277 | |
try |
278 | |
{ |
279 | 0 | return Float.valueOf((String) answer); |
280 | |
} |
281 | 0 | catch (NumberFormatException e) |
282 | |
{ |
283 | |
|
284 | |
|
285 | |
} |
286 | |
} |
287 | 0 | if (logger.isWarnEnabled()) |
288 | |
{ |
289 | 0 | logger.warn("Value exists but cannot be converted to float: " |
290 | |
+ answer + ", returning default value: " + defaultValue); |
291 | |
} |
292 | 0 | return defaultValue; |
293 | |
} |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
public static double getDouble(final Object answer, double defaultValue) |
304 | |
{ |
305 | 0 | if (answer == null) |
306 | |
{ |
307 | 0 | return defaultValue; |
308 | |
} |
309 | 0 | else if (answer instanceof Number) |
310 | |
{ |
311 | 0 | return ((Number) answer).doubleValue(); |
312 | |
} |
313 | 0 | else if (answer instanceof String) |
314 | |
{ |
315 | |
try |
316 | |
{ |
317 | 0 | return Double.valueOf((String) answer); |
318 | |
} |
319 | 0 | catch (NumberFormatException e) |
320 | |
{ |
321 | |
|
322 | |
} |
323 | |
} |
324 | 0 | if (logger.isWarnEnabled()) |
325 | |
{ |
326 | 0 | logger.warn("Value exists but cannot be converted to double: " |
327 | |
+ answer + ", returning default value: " + defaultValue); |
328 | |
} |
329 | 0 | return defaultValue; |
330 | |
} |
331 | |
|
332 | |
public static String toString(Object obj) |
333 | |
{ |
334 | 0 | if (obj == null) |
335 | |
{ |
336 | 0 | return ""; |
337 | |
} |
338 | |
|
339 | 0 | String str = obj.getClass().getName(); |
340 | 0 | if (obj instanceof NamedObject) |
341 | |
{ |
342 | 0 | str += " '" + ((NamedObject) obj).getName() + "'"; |
343 | |
} |
344 | 0 | return str; |
345 | |
} |
346 | |
} |