View Javadoc

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  public class ObjectUtils extends org.apache.commons.lang.ObjectUtils
17  {
18  
19      /** logger used by this class */
20      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          if (obj == null)
33          {
34              return "null";
35          }
36          else
37          {
38              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          if (answer != null)
57          {
58              return answer.toString();
59          }
60          else
61          {
62              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          if (answer != null)
77          {
78              if (answer instanceof Boolean)
79              {
80                  return ((Boolean) answer).booleanValue();
81  
82              }
83              else if (answer instanceof String)
84              {
85                  return Boolean.valueOf((String) answer).booleanValue();
86  
87              }
88              else if (answer instanceof Number)
89              {
90                  Number n = (Number) answer;
91                  return ((n.intValue() > 0) ? Boolean.TRUE : Boolean.FALSE).booleanValue();
92              }
93              else
94              {
95                  if (logger.isWarnEnabled())
96                  {
97                      logger.warn("Value exists but cannot be converted to boolean: "
98                              + answer + ", returning default value: " + defaultValue);
99                  }
100                 return defaultValue;
101             }
102         }
103         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         if (answer == null)
117         {
118             return defaultValue;
119         }
120         else if (answer instanceof Number)
121         {
122             return ((Number) answer).byteValue();
123         }
124         else if (answer instanceof String)
125         {
126             try
127             {
128                 return Byte.valueOf((String) answer).byteValue();
129             }
130             catch (NumberFormatException e)
131             {
132                 //handled below
133             }
134         }
135         if (logger.isWarnEnabled())
136         {
137             logger.warn("Value exists but cannot be converted to byte: "
138                     + answer + ", returning default value: " + defaultValue);
139         }
140         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         if (answer == null)
154         {
155             return defaultValue;
156         }
157         else if (answer instanceof Number)
158         {
159             return ((Number) answer).shortValue();
160         }
161         else if (answer instanceof String)
162         {
163             try
164             {
165                 return Short.valueOf((String) answer).shortValue();
166             }
167             catch (NumberFormatException e)
168             {
169                 //handled below
170             }
171         }
172         if (logger.isWarnEnabled())
173         {
174             logger.warn("Value exists but cannot be converted to short: "
175                     + answer + ", returning default value: " + defaultValue);
176         }
177         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         if (answer == null)
191         {
192             return defaultValue;
193         }
194         else if (answer instanceof Number)
195         {
196             return ((Number) answer).intValue();
197         }
198         else if (answer instanceof String)
199         {
200             try
201             {
202                 return Integer.valueOf((String) answer).intValue();
203             }
204             catch (NumberFormatException e)
205             {
206                 //handled below
207             }
208         }
209         if (logger.isWarnEnabled())
210         {
211             logger.warn("Value exists but cannot be converted to int: "
212                     + answer + ", returning default value: " + defaultValue);
213         }
214         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         if (answer == null)
228         {
229             return defaultValue;
230         }
231         else if (answer instanceof Number)
232         {
233             return ((Number) answer).longValue();
234         }
235         else if (answer instanceof String)
236         {
237             try
238             {
239                 return Long.valueOf((String) answer).longValue();
240             }
241             catch (NumberFormatException e)
242             {
243                 //handled below
244 
245             }
246         }
247         if (logger.isWarnEnabled())
248         {
249             logger.warn("Value exists but cannot be converted to long: "
250                     + answer + ", returning default value: " + defaultValue);
251         }
252         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         if (answer == null)
266         {
267             return defaultValue;
268         }
269         else if (answer instanceof Number)
270         {
271             return ((Number) answer).floatValue();
272         }
273         else if (answer instanceof String)
274         {
275             try
276             {
277                 return Float.valueOf((String) answer).floatValue();
278             }
279             catch (NumberFormatException e)
280             {
281                 //handled below
282 
283             }
284         }
285         if (logger.isWarnEnabled())
286         {
287             logger.warn("Value exists but cannot be converted to float: "
288                     + answer + ", returning default value: " + defaultValue);
289         }
290         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         if (answer == null)
304         {
305             return defaultValue;
306         }
307         else if (answer instanceof Number)
308         {
309             return ((Number) answer).doubleValue();
310         }
311         else if (answer instanceof String)
312         {
313             try
314             {
315                 return Double.valueOf((String) answer).doubleValue();
316             }
317             catch (NumberFormatException e)
318             {
319                 //handled below
320             }
321         }
322         if (logger.isWarnEnabled())
323         {
324             logger.warn("Value exists but cannot be converted to double: "
325                     + answer + ", returning default value: " + defaultValue);
326         }
327         return defaultValue;
328     }
329 
330 }