Coverage Report - org.mule.util.NumberUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
NumberUtils
21%
7/33
21%
5/24
4.5
 
 1  
 /*
 2  
  * $Id: NumberUtils.java 11723 2008-05-12 17:24:54Z tcarlson $
 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  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  
     //@Override
 109  
     public static int toInt(String str) {
 110  0
         return toInt(str, INTEGER_ERROR);
 111  
     }
 112  
 
 113  
     //@Override
 114  
     public static long toLong(String str) {
 115  2
         return toLong(str, LONG_ERROR);
 116  
     }
 117  
 
 118  
     //@Override
 119  
     public static float toFloat(String str) {
 120  0
         return toFloat(str, FLOAT_ERROR);
 121  
     }
 122  
     
 123  
     //@Override
 124  
     public static double toDouble(String str) {
 125  0
         return toDouble(str, DOUBLE_ERROR);
 126  
     }
 127  
 }