View Javadoc

1   /*
2    * @(#) Primitive.java Jan 15, 2004
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.language.primitives;
11  
12  /***
13   * The Primitive class is a utility class to deal with primitives. Besides
14   * widening and unwidening this class casts and parses UTF8 strings into
15   * appropriate primitive classes. <br>
16   * (c) copyright 2004 <a href="http://www.simulation.tudelft.nl">Delft
17   * University of Technology </a>, the Netherlands. <br>
18   * See for project information <a
19   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
20   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
21   * License (GPL) </a>, no warranty <br>
22   * 
23   * @version 1.0 Jan 15, 2004 <br>
24   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
25   *         Jacobs </a>
26   */
27  public final class Primitive
28  {
29  	/***
30  	 * constructs a new Primitive
31  	 */
32  	private Primitive()
33  	{
34  		super();
35  		//unreachable code
36  	}
37  
38  	/***
39  	 * casts a set of values to classes
40  	 * 
41  	 * @param classes the classes to cast to
42  	 * @param values the values
43  	 * @return the newly creates values
44  	 */
45  	public static Object[] cast(final Class[] classes, final Object[] values)
46  	{
47  		for (int i = 0; i < classes.length; i++)
48  		{
49  			values[i] = Primitive.cast(classes[i], values[i]);
50  		}
51  		return values;
52  	}
53  
54  	/***
55  	 * casts an object to a instance of clazz
56  	 * 
57  	 * @param clazz the class to cast to
58  	 * @param object 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
121 	 * class constants. (i.e. both "int" and "I" return int.class). Both void
122 	 * and "V" return void.class. null is returned whenever an unknown className
123 	 * is given.
124 	 * 
125 	 * @param className 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 the wrapper class
173 	 * @return the primitive Class. null is returned whenever wrapperClass is
174 	 *         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 the primitive class
217 	 * @return the Class. null is returned whenever wrapperClass is not a
218 	 *         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 the object
261 	 * @return Boolean
262 	 */
263 	public static Boolean toBoolean(final Object object)
264 	{
265 		if (object instanceof Number)
266 		{
267 			int value = ((Number) object).intValue();
268 			if (value == 1)
269 			{
270 				return Boolean.TRUE;
271 			}
272 			if (value == 0)
273 			{
274 				return Boolean.FALSE;
275 			}
276 			throw new IllegalArgumentException("object.intValue !=0 && !=1");
277 		}
278 		return (Boolean) object;
279 	}
280 
281 	/***
282 	 * casts an object to Byte
283 	 * 
284 	 * @param object the object
285 	 * @return Byte
286 	 */
287 	public static Byte toByte(final Object object)
288 	{
289 		if (object instanceof Number)
290 		{
291 			return new Byte(((Number) object).byteValue());
292 		}
293 		return (Byte) object;
294 	}
295 
296 	/***
297 	 * casts an object to Character
298 	 * 
299 	 * @param object the object to parse
300 	 * @return Integer the result
301 	 */
302 	public static Character toCharacter(final Object object)
303 	{
304 		if (object instanceof Number)
305 		{
306 			return new Character((char) ((Number) object).byteValue());
307 		}
308 		return (Character) object;
309 	}
310 
311 	/***
312 	 * casts an object to Double
313 	 * 
314 	 * @param object the object to parse
315 	 * @return Integer the result
316 	 */
317 	public static Double toDouble(final Object object)
318 	{
319 		return new Double(((Number) object).doubleValue());
320 	}
321 
322 	/***
323 	 * casts an object to Float
324 	 * 
325 	 * @param object the object to parse
326 	 * @return Float the result
327 	 */
328 	public static Float toFloat(final Object object)
329 	{
330 		return new Float(((Number) object).floatValue());
331 	}
332 
333 	/***
334 	 * casts an object to Long
335 	 * 
336 	 * @param object the object to parse
337 	 * @return Long the result
338 	 */
339 	public static Long toLong(final Object object)
340 	{
341 		return new Long(((Number) object).longValue());
342 	}
343 
344 	/***
345 	 * casts an object to Short
346 	 * 
347 	 * @param object the object to parse
348 	 * @return Long the result
349 	 */
350 	public static Short toShort(final Object object)
351 	{
352 		return new Short(((Number) object).shortValue());
353 	}
354 
355 	/***
356 	 * casts an object to Integer
357 	 * 
358 	 * @param object the object to parse
359 	 * @return Integer the result
360 	 */
361 	public static Integer toInteger(final Object object)
362 	{
363 		if (object instanceof Character)
364 		{
365 			return new Integer(((Character) object).charValue());
366 		}
367 		if (object instanceof Boolean)
368 		{
369 			if (((Boolean) object).booleanValue())
370 			{
371 				return new Integer(1);
372 			}
373 			return new Integer(0);
374 		}
375 		return new Integer(((Number) object).intValue());
376 	}
377 }