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