Coverage Report - org.mule.util.ObjectUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectUtils
0%
0/86
0%
0/68
7.4
 
 1  
 /*
 2  
  * $Id: ObjectUtils.java 20220 2010-11-17 17:49:36Z aperepel $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.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  
     /** logger used by this class */
 22  0
     protected static final Log logger = LogFactory.getLog(ObjectUtils.class);
 23  
 
 24  
     /**
 25  
      * Like {@link #identityToString(Object)} but without the object's full package
 26  
      * name.
 27  
      *
 28  
      * @param obj the object for which the identity description is to be generated
 29  
      * @return the object's identity description in the form of
 30  
      *         "ClassName@IdentityCode" or "null" if the argument was null.
 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  
      * Gets a String from a value in a null-safe manner.
 50  
      * <p/>
 51  
      *
 52  
      * @param answer       the object value
 53  
      * @param defaultValue the default to use if null or of incorrect type
 54  
      * @return the value as a String, or the defaultValue
 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  
      * Gets a boolean from a value in a null-safe manner.
 70  
      * <p/>
 71  
      *
 72  
      * @param answer       the object value
 73  
      * @param defaultValue the default to use if null or of incorrect type
 74  
      * @return the value as a boolean, or the defaultValue
 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  
      * Gets a byte from a value in a null-safe manner.
 110  
      * <p/>
 111  
      *
 112  
      * @param answer       the object value
 113  
      * @param defaultValue the default to use if null or of incorrect type
 114  
      * @return the value as a byte, or the defaultValue
 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  
                 //handled below
 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  
      * Gets a short from a value in a null-safe manner.
 147  
      * <p/>
 148  
      *
 149  
      * @param answer       the object value
 150  
      * @param defaultValue the default to use if null or of incorrect type
 151  
      * @return the value as a short, or the defaultValue
 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  
                 //handled below
 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  
      * Gets a int from a value in a null-safe manner.
 184  
      * <p/>
 185  
      *
 186  
      * @param answer       the object value
 187  
      * @param defaultValue the default to use if null or of incorrect type
 188  
      * @return the value as a int, or the defaultValue
 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  
                 //handled below
 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  
      * Gets a long from a value in a null-safe manner.
 221  
      * <p/>
 222  
      *
 223  
      * @param answer       the object value
 224  
      * @param defaultValue the default to use if null or of incorrect type
 225  
      * @return the value as a long, or the defaultValue
 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  
                 //handled below
 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  
      * Gets a float from a value in a null-safe manner.
 259  
      * <p/>
 260  
      *
 261  
      * @param answer       the object value
 262  
      * @param defaultValue the default to use if null or of incorrect type
 263  
      * @return the value as a float, or the defaultValue
 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  
                 //handled below
 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  
      * Gets a double from a value in a null-safe manner.
 297  
      * <p/>
 298  
      *
 299  
      * @param answer       the object value
 300  
      * @param defaultValue the default to use if null or of incorrect type
 301  
      * @return the value as a double, or the defaultValue
 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  
                 //handled below
 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 += String.format(" '%s'", ((NamedObject) obj).getName());
 343  
         }
 344  0
         return str;
 345  
     }
 346  
 }