View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  package org.onemind.commons.java.util;
21  
22  /***
23   * Property utilities
24   * @author TiongHiang Lee (thlee@onemindsoft.org)
25   * @version $Id: ObjectUtils.java,v 1.2 2004/08/26 12:33:17 thlee Exp $ $Name:  $
26   */
27  public final class ObjectUtils
28  {
29  
30      /***
31       * Constructor
32       */
33      private ObjectUtils()
34      {
35          super();
36      }
37  
38      /***
39       * Convert the object the boolean, return the default if it cannot be converted
40       * @param value the value
41       * @param def the default
42       * @return the converted value, or def if value is null
43       */
44      public static final boolean toBool(Object value, boolean def)
45      {
46          if (value instanceof Boolean)
47          {
48              return ((Boolean) value).booleanValue();
49          } else if (value instanceof String)
50          {
51              if ("TRUE".equalsIgnoreCase((String) value))
52              {
53                  return true;
54              } else if ("FALSE".equalsIgnoreCase((String) value))
55              {
56                  return false;
57              } else
58              {
59                  return def;
60              }
61          } else
62          {
63              return def;
64          }
65      }
66  
67      /***
68       * Convert the object to int, or return the default if it cannot be converted
69       * @param value the value
70       * @param def the default
71       * @return the converted int, or default if it cannot be converted
72       */
73      public static int toInt(Object value, int def)
74      {
75          if (value instanceof Integer)
76          {
77              return ((Integer) value).intValue();
78          } else if (value instanceof String)
79          {
80              try
81              {
82                  return Integer.parseInt((String) value);
83              } catch (NumberFormatException e)
84              {
85                  return def;
86              }
87          } else
88          {
89              return def;
90          }
91      }
92  
93      /***
94       * Convert the object to long, or return the default if it cannot be converted
95       * @param value the value
96       * @param def the default
97       * @return the converted int, or default if it cannot be converted
98       */
99      public static long toLong(Object value, long def)
100     {
101         if (value instanceof Long)
102         {
103             return ((Long) value).longValue();
104         } else if (value instanceof String)
105         {
106             try
107             {
108                 return Long.parseLong((String) value);
109             } catch (NumberFormatException e)
110             {
111                 return def;
112             }
113         } else
114         {
115             return def;
116         }
117     }
118 
119     /***
120      * Convert the object to float, or return the default if it cannot be converted
121      * @param value the value
122      * @param def the default
123      * @return the converted int, or default if it cannot be converted
124      */
125     public static float toFloat(Object value, float def)
126     {
127         if (value instanceof Float)
128         {
129             return ((Float) value).floatValue();
130         } else if (value instanceof String)
131         {
132             try
133             {
134                 return Float.parseFloat((String) value);
135             } catch (NumberFormatException e)
136             {
137                 return def;
138             }
139         } else
140         {
141             return def;
142         }
143     }
144 
145     /***
146      * Convert the object to double, or return the default if it cannot be converted
147      * @param value the value
148      * @param def the default
149      * @return the converted int, or default if it cannot be converted
150      */
151     public static double toDouble(Object value, double def)
152     {
153         if (value instanceof Double)
154         {
155             return ((Double) value).doubleValue();
156         } else if (value instanceof String)
157         {
158             try
159             {
160                 return Double.parseDouble((String) value);
161             } catch (NumberFormatException e)
162             {
163                 return def;
164             }
165         } else
166         {
167             return def;
168         }
169     }
170 }