View Javadoc

1   /*
2    * @(#) Primitive.java Jan 15, 2004 Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5, 2628
3    * BX Delft, the Netherlands. All rights reserved. This software is proprietary information of Delft
4    * University of Technology The code is published under the Lesser General Public License
5    */
6   package nl.tudelft.simulation.language.primitives;
7   
8   /***
9    * The Primitive class is a utility class to deal with primitives. Besides widening and unwidening this class
10   * casts and parses UTF8 strings into appropriate primitive classes.
11   * <p>
12   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the
13   * Netherlands.
14   * <p>
15   * See for project information <a
16   * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language </a> <br>
17   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL)
18   * </a>, no warranty
19   * 
20   * @version $Revision: 1.7 $ $Date: 2005/07/04 12:21:30 $
21   * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>
22   */
23  public final class Primitive
24  {
25      /***
26       * constructs a new Primitive
27       */
28      private Primitive()
29      {
30          super();
31          // unreachable code
32      }
33  
34      /***
35       * casts a set of values to classes.
36       * 
37       * @param classes
38       *            the classes to cast to
39       * @param values
40       *            the values
41       * @return the newly creates values
42       */
43      public static Object[] cast(final Class[] classes, final Object[] values)
44      {
45          for (int i = 0; i < classes.length; i++)
46          {
47              values[i] = Primitive.cast(classes[i], values[i]);
48          }
49          return values;
50      }
51  
52      /***
53       * casts an object to a instance of clazz.
54       * 
55       * @param clazz
56       *            the class to cast to
57       * @param object
58       *            the object to cast
59       * @return the casted object
60       */
61      public static Object cast(final Class clazz, final Object object)
62      {
63          if (clazz.isInstance(object) || !clazz.isPrimitive()
64                  || (clazz.equals(Primitive.getPrimitive(object.getClass()))))
65          {
66              return object;
67          }
68  
69          // Boolean
70          if (clazz.equals(boolean.class))
71          {
72              return Primitive.toBoolean(object);
73          }
74  
75          // Character
76          if (clazz.equals(char.class))
77          {
78              return Primitive.toCharacter(object);
79          }
80  
81          // Byte
82          if (clazz.equals(byte.class))
83          {
84              return Primitive.toByte(object);
85          }
86  
87          // Double
88          if (clazz.equals(double.class))
89          {
90              return Primitive.toDouble(object);
91          }
92  
93          // Float
94          if (clazz.equals(float.class))
95          {
96              return Primitive.toFloat(object);
97          }
98  
99          // Long
100         if (clazz.equals(long.class))
101         {
102             return Primitive.toLong(object);
103         }
104 
105         // Integer
106         if (clazz.equals(int.class))
107         {
108             return Primitive.toInteger(object);
109         }
110 
111         // Short
112         if (clazz.equals(short.class))
113         {
114             return Primitive.toShort(object);
115         }
116         return object;
117     }
118 
119     /***
120      * returns the primitiveClass of the name given as defined by the Java VM class constants. (i.e. both
121      * "int" and "I" return int.class). Both void and "V" return void.class. null is returned whenever an
122      * unknown className is given.
123      * 
124      * @param className
125      *            the className
126      * @return Class the primitiveClass
127      */
128     public static Class forName(final String className)
129     {
130         if (className.equals("int") || className.equals("I"))
131         {
132             return int.class;
133         }
134         if (className.equals("double") || className.equals("D"))
135         {
136             return double.class;
137         }
138         if (className.equals("byte") || className.equals("B"))
139         {
140             return byte.class;
141         }
142         if (className.equals("float") || className.equals("F"))
143         {
144             return float.class;
145         }
146         if (className.equals("long") || className.equals("J"))
147         {
148             return long.class;
149         }
150         if (className.equals("boolean") || className.equals("Z"))
151         {
152             return boolean.class;
153         }
154         if (className.equals("char") || className.equals("C"))
155         {
156             return char.class;
157         }
158         if (className.equals("short") || className.equals("S"))
159         {
160             return short.class;
161         }
162         if (className.equals("void") || className.equals("V"))
163         {
164             return void.class;
165         }
166         return null;
167     }
168 
169     /***
170      * gets the primitive of the given wrapperClass.
171      * 
172      * @param wrapperClass
173      *            the wrapper class
174      * @return the primitive Class. null is returned whenever wrapperClass is not a wrapperclass.
175      */
176     public static Class getPrimitive(final Class wrapperClass)
177     {
178         if (wrapperClass.equals(Integer.class))
179         {
180             return int.class;
181         }
182         if (wrapperClass.equals(Double.class))
183         {
184             return double.class;
185         }
186         if (wrapperClass.equals(Byte.class))
187         {
188             return byte.class;
189         }
190         if (wrapperClass.equals(Float.class))
191         {
192             return float.class;
193         }
194         if (wrapperClass.equals(Long.class))
195         {
196             return long.class;
197         }
198         if (wrapperClass.equals(Boolean.class))
199         {
200             return boolean.class;
201         }
202         if (wrapperClass.equals(Character.class))
203         {
204             return char.class;
205         }
206         if (wrapperClass.equals(Short.class))
207         {
208             return short.class;
209         }
210         return null;
211     }
212 
213     /***
214      * gets the wrapper of this primitive class.
215      * 
216      * @param primitiveClass
217      *            the primitive class
218      * @return the Class. null is returned whenever wrapperClass is not a wrapperclass.
219      */
220     public static Class getWrapper(final Class primitiveClass)
221     {
222         if (primitiveClass.equals(int.class))
223         {
224             return Integer.class;
225         }
226         if (primitiveClass.equals(double.class))
227         {
228             return Double.class;
229         }
230         if (primitiveClass.equals(byte.class))
231         {
232             return Byte.class;
233         }
234         if (primitiveClass.equals(float.class))
235         {
236             return Float.class;
237         }
238         if (primitiveClass.equals(long.class))
239         {
240             return Long.class;
241         }
242         if (primitiveClass.equals(boolean.class))
243         {
244             return Boolean.class;
245         }
246         if (primitiveClass.equals(char.class))
247         {
248             return Character.class;
249         }
250         if (primitiveClass.equals(short.class))
251         {
252             return Short.class;
253         }
254         throw new IllegalArgumentException(primitiveClass + " != primitive");
255     }
256 
257     /***
258      * casts an object to Boolean.
259      * 
260      * @param object
261      *            the object
262      * @return Boolean
263      */
264     public static Boolean toBoolean(final Object object)
265     {
266         if (object instanceof Number)
267         {
268             int value = ((Number) object).intValue();
269             if (value == 1)
270             {
271                 return Boolean.TRUE;
272             }
273             if (value == 0)
274             {
275                 return Boolean.FALSE;
276             }
277             throw new IllegalArgumentException("object.intValue !=0 && !=1");
278         }
279         return (Boolean) object;
280     }
281 
282     /***
283      * casts an object to Byte.
284      * 
285      * @param object
286      *            the object
287      * @return Byte
288      */
289     public static Byte toByte(final Object object)
290     {
291         if (object instanceof Number)
292         {
293             return new Byte(((Number) object).byteValue());
294         }
295         return (Byte) object;
296     }
297 
298     /***
299      * casts an object to Character.
300      * 
301      * @param object
302      *            the object to parse
303      * @return Integer the result
304      */
305     public static Character toCharacter(final Object object)
306     {
307         if (object instanceof Number)
308         {
309             return new Character((char) ((Number) object).byteValue());
310         }
311         return (Character) object;
312     }
313 
314     /***
315      * casts an object to Double.
316      * 
317      * @param object
318      *            the object to parse
319      * @return Integer the result
320      */
321     public static Double toDouble(final Object object)
322     {
323         return new Double(((Number) object).doubleValue());
324     }
325 
326     /***
327      * casts an object to Float.
328      * 
329      * @param object
330      *            the object to parse
331      * @return Float the result
332      */
333     public static Float toFloat(final Object object)
334     {
335         return new Float(((Number) object).floatValue());
336     }
337 
338     /***
339      * casts an object to Long.
340      * 
341      * @param object
342      *            the object to parse
343      * @return Long the result
344      */
345     public static Long toLong(final Object object)
346     {
347         return new Long(((Number) object).longValue());
348     }
349 
350     /***
351      * casts an object to Short.
352      * 
353      * @param object
354      *            the object to parse
355      * @return Long the result
356      */
357     public static Short toShort(final Object object)
358     {
359         return new Short(((Number) object).shortValue());
360     }
361 
362     /***
363      * casts an object to Integer.
364      * 
365      * @param object
366      *            the object to parse
367      * @return Integer the result
368      */
369     public static Integer toInteger(final Object object)
370     {
371         if (object instanceof Character)
372         {
373             return new Integer(((Character) object).charValue());
374         }
375         if (object instanceof Boolean)
376         {
377             if (((Boolean) object).booleanValue())
378             {
379                 return new Integer(1);
380             }
381             return new Integer(0);
382         }
383         return new Integer(((Number) object).intValue());
384     }
385 }