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