View Javadoc

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