View Javadoc

1   /*
2    * $Id: NumberUtils.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
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 org.apache.commons.lang.math.NumberUtils
18  {
19  
20      public static long toLong(Object obj)
21      {
22          if (obj == null)
23          {
24              throw new IllegalArgumentException("Unable to convert null object to long");
25          }
26          else if (obj instanceof String)
27          {
28              return toLong((String) obj);
29          }
30          else if (obj instanceof Number)
31          {
32              return ((Number) obj).longValue();
33          }
34          else
35          {
36              throw new IllegalArgumentException("Unable to convert object of type: "
37                                                 + obj.getClass().getName() + " to long.");
38          }
39      }
40  
41      public static int toInt(Object obj)
42      {
43          if (obj == null)
44          {
45              throw new IllegalArgumentException("Unable to convert null object to int");
46          }
47          else if (obj instanceof String)
48          {
49              return toInt((String) obj);
50          }
51          else if (obj instanceof Number)
52          {
53              return ((Number) obj).intValue();
54          }
55          else
56          {
57              throw new IllegalArgumentException("Unable to convert object of type: "
58                                                 + obj.getClass().getName() + " to int.");
59          }
60      }
61  
62  }