01: /*
02: * $Id: NumberUtils.java 8077 2007-08-27 20:15:25Z aperepel $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.util;
12:
13: /**
14: * <code>NumberUtils</code> contains useful methods for manipulating numbers.
15: */
16: // @ThreadSafe
17: public class NumberUtils extends
18: org.apache.commons.lang.math.NumberUtils {
19:
20: public static long toLong(Object obj) {
21: if (obj == null) {
22: throw new IllegalArgumentException(
23: "Unable to convert null object to long");
24: } else if (obj instanceof String) {
25: return toLong((String) obj);
26: } else if (obj instanceof Number) {
27: return ((Number) obj).longValue();
28: } else {
29: throw new IllegalArgumentException(
30: "Unable to convert object of type: "
31: + obj.getClass().getName() + " to long.");
32: }
33: }
34:
35: public static int toInt(Object obj) {
36: if (obj == null) {
37: throw new IllegalArgumentException(
38: "Unable to convert null object to int");
39: } else if (obj instanceof String) {
40: return toInt((String) obj);
41: } else if (obj instanceof Number) {
42: return ((Number) obj).intValue();
43: } else {
44: throw new IllegalArgumentException(
45: "Unable to convert object of type: "
46: + obj.getClass().getName() + " to int.");
47: }
48: }
49:
50: }
|