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