1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: Conversions.java,v 1.2 2004/02/03 07:31:04 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.message.util;
46
47 import java.util.HashMap;
48
49
50 /***
51 * A helper class returning the conversion types supported by MapMessage and
52 * StreamMessage
53 *
54 * @version $Revision: 1.2 $ $Date: 2004/02/03 07:31:04 $
55 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
56 */
57 public final class Conversions {
58
59 /***
60 * All types supported by MapMessage and StreamMessage
61 */
62 public static final Class[] TYPES = {
63 Boolean.class, Byte.class, Short.class, Character.class,
64 Integer.class, Long.class, Float.class, Double.class, String.class,
65 byte[].class};
66
67 /***
68 * Valid boolean conversions
69 */
70 private static final Class[] BOOLEAN = {Boolean.class, String.class};
71
72 /***
73 * Valid byte conversions
74 */
75 private static final Class[] BYTE = {
76 Byte.class, Short.class, Integer.class, Long.class, String.class};
77
78 /***
79 * Valid short conversions
80 */
81 private static final Class[] SHORT = {Short.class, Integer.class,
82 Long.class, String.class};
83
84 /***
85 * Valid char conversions
86 */
87 private static final Class[] CHAR = {Character.class, String.class};
88
89 /***
90 * Valid int conversions
91 */
92 private static final Class[] INT = {Integer.class, Long.class,
93 String.class};
94
95 /***
96 * Valid long conversions
97 */
98 private static final Class[] LONG = {Long.class, String.class};
99
100 /***
101 * Valid float conversions
102 */
103 private static final Class[] FLOAT = {Float.class, Double.class,
104 String.class};
105
106 /***
107 * Valid double conversions
108 */
109 private static final Class[] DOUBLE = {Double.class, String.class};
110
111 /***
112 * Valid String conversions
113 */
114 private static final Class[] STRING = {
115 Boolean.class, Byte.class, Short.class, Integer.class, Long.class,
116 Float.class, Double.class, String.class};
117
118 /***
119 * Valid byte[] conversions
120 */
121 private static final Class[] BYTES = {byte[].class};
122
123 /***
124 * The map of valid conversions
125 */
126 private static final HashMap VALID;
127
128 /***
129 * The map of invalid conversions
130 */
131 private static final HashMap INVALID;
132
133
134 /***
135 * Prevent construction of utility class
136 */
137 private Conversions() {
138 }
139
140 /***
141 * Returns the list of valid conversions for a particular type
142 *
143 * @param type the type to convert from. For primitives, this must be the
144 * corresponding objectified type
145 * @return the list of valid conversions for type
146 */
147 public static Class[] getValidConversions(Class type) {
148 Class[] result = (Class[]) VALID.get(type);
149 if (result == null) {
150 throw new IllegalArgumentException(
151 "Class=" + type.getName() + " is not a type supported by "
152 + "MapMessage or StreamMessage");
153 }
154 return result;
155 }
156
157 /***
158 * Returns the list of invalid conversions for a particular type
159 *
160 * @param type the type to convert from. For primitives, this must be the
161 * corresponding objectified type
162 * @return the list of invalid conversions for type
163 */
164 public static Class[] getInvalidConversions(Class type) {
165 Class[] result = (Class[]) INVALID.get(type);
166 if (result == null) {
167 throw new IllegalArgumentException(
168 "Class=" + type.getName() + " is not a type supported by "
169 + "MapMessage or StreamMessage");
170 }
171 return result;
172 }
173
174 /***
175 * Returns true if it is valid to convert a value to a particular type
176 *
177 * @param value the value to convert from.
178 * @param type the type to convert to. For primitives, this must be the
179 * corresponding objectified type
180 * @return true if it is valid to perform a conversion
181 */
182 public static boolean isValidConversion(Object value, Class type) {
183 boolean result = false;
184 Class[] valid = getValidConversions(value.getClass());
185 for (int i = 0; i < valid.length; ++i) {
186 if (valid[i].equals(type)) {
187 result = true;
188 break;
189 }
190 }
191 return result;
192 }
193
194 /***
195 * Converts an object from one type to another
196 *
197 * @param value the value to convert from.
198 * @param type the type to convert to. For primitives, this must be the
199 * corresponding objectified type
200 * @return the converted object
201 */
202 public static Object convert(Object value, Class type) {
203 Object result = null;
204 if (value instanceof Boolean) {
205 result = convertBoolean((Boolean) value, type);
206 } else if (value instanceof Byte) {
207 result = convertByte((Byte) value, type);
208 } else if (value instanceof Short) {
209 result = convertShort((Short) value, type);
210 } else if (value instanceof Character) {
211 result = convertChar((Character) value, type);
212 } else if (value instanceof Integer) {
213 result = convertInt((Integer) value, type);
214 } else if (value instanceof Long) {
215 result = convertLong((Long) value, type);
216 } else if (value instanceof Float) {
217 result = convertFloat((Float) value, type);
218 } else if (value instanceof Double) {
219 result = convertDouble((Double) value, type);
220 } else if (value instanceof String) {
221 result = convertString((String) value, type);
222 } else if (value instanceof byte[]) {
223 if (type.equals(byte[].class)) {
224 result = value;
225 } else {
226 illegalConversion(value, type);
227 }
228 }
229 return result;
230 }
231
232 /***
233 * Convert a boolean to the specified type
234 *
235 * @param value the value to convert
236 * @param type the type to convert to
237 * @return the converted value
238 */
239 public static Object convertBoolean(Boolean value, Class type) {
240 Object result = null;
241 if (type.equals(Boolean.class)) {
242 result = value;
243 } else if (type.equals(String.class)) {
244 result = value.toString();
245 } else {
246 illegalConversion(value, type);
247 }
248 return result;
249 }
250
251 /***
252 * Convert a byte to the specified type
253 *
254 * @param value the value to convert
255 * @param type the type to convert to
256 * @return the converted value
257 */
258 public static Object convertByte(Byte value, Class type) {
259 return convertNumber(value, type);
260 }
261
262 /***
263 * Convert a short to the specified type
264 *
265 * @param value the value to convert
266 * @param type the type to convert to
267 * @return the converted value
268 */
269 public static Object convertShort(Short value, Class type) {
270 return convertNumber(value, type);
271 }
272
273 /***
274 * Convert a char to the specified type
275 *
276 * @param value the value to convert
277 * @param type the type to convert to
278 * @return the converted value
279 */
280 public static Object convertChar(Character value, Class type) {
281 Object result = null;
282 if (type.equals(Character.class)) {
283 result = value;
284 } else if (type.equals(String.class)) {
285 result = value.toString();
286 } else {
287 illegalConversion(value, type);
288 }
289 return result;
290 }
291
292 /***
293 * Convert an int to the specified type
294 *
295 * @param value the value to convert
296 * @param type the type to convert to
297 * @return the converted value
298 */
299 public static Object convertInt(Integer value, Class type) {
300 return convertNumber(value, type);
301 }
302
303 /***
304 * Convert a long to the specified type
305 *
306 * @param value the value to convert
307 * @param type the type to convert to
308 * @return the converted value
309 */
310 public static Object convertLong(Long value, Class type) {
311 return convertNumber(value, type);
312 }
313
314 /***
315 * Convert a float to the specified type
316 *
317 * @param value the value to convert
318 * @param type the type to convert to
319 * @return the converted value
320 */
321 public static Object convertFloat(Float value, Class type) {
322 return convertNumber(value, type);
323 }
324
325 /***
326 * Convert a double to the specified type
327 *
328 * @param value the value to convert
329 * @param type the type to convert to
330 * @return the converted value
331 */
332 public static Object convertDouble(Double value, Class type) {
333 return convertNumber(value, type);
334 }
335
336 /***
337 * Convert a String to the specified type
338 *
339 * @param value the value to convert
340 * @param type the type to convert to
341 * @return the converted value
342 */
343 public static Object convertString(String value, Class type) {
344 Object result = null;
345 if (type.equals(Boolean.class)) {
346 result = Boolean.valueOf(value);
347 } else if (type.equals(Byte.class)) {
348 result = Byte.valueOf(value);
349 } else if (type.equals(Short.class)) {
350 result = Short.valueOf(value);
351 } else if (type.equals(Integer.class)) {
352 result = Integer.valueOf(value);
353 } else if (type.equals(Float.class)) {
354 result = Float.valueOf(value);
355 } else if (type.equals(Double.class)) {
356 result = Double.valueOf(value);
357 } else if (type.equals(String.class)) {
358 result = value;
359 } else {
360 illegalConversion(value, type);
361 }
362 return result;
363 }
364
365 /***
366 * Convert a Number to the specified type
367 *
368 * @param value the value to convert
369 * @param type the type to convert to
370 * @return the converted value
371 */
372 private static Object convertNumber(Number value, Class type) {
373 Object result = null;
374 if (!isValidConversion(value, type)) {
375 illegalConversion(value, type);
376 }
377 if (type.equals(Byte.class)) {
378 result = new Byte(value.byteValue());
379 } else if (type.equals(Short.class)) {
380 result = new Short(value.shortValue());
381 } else if (type.equals(Integer.class)) {
382 result = new Integer(value.intValue());
383 } else if (type.equals(Long.class)) {
384 result = new Long(value.longValue());
385 } else if (type.equals(Float.class)) {
386 result = new Float(value.floatValue());
387 } else if (type.equals(Double.class)) {
388 result = new Double(value.doubleValue());
389 } else if (type.equals(String.class)) {
390 result = value.toString();
391 }
392 return result;
393 }
394
395 /***
396 * Raises an IllegalArgumentExcption for an invalid conversion
397 *
398 * @param value the value to convert
399 * @param type the type to convert to
400 * @throws IllegalArgumentException containing a formatted reason
401 */
402 private static void illegalConversion(Object value, Class type) {
403 throw new IllegalArgumentException(
404 "Type=" + type.getName() + " is not a valid conversion for "
405 + value.getClass().getName());
406 }
407
408 /***
409 * Returns the list of invalid conversions for a particular type
410 *
411 * @param type the type
412 * @return the invalid conversions for <code>type</code>
413 */
414 private static Class[] getInvalid(Class type) {
415 Class[] valid = getValidConversions(type);
416 Class[] invalid = new Class[TYPES.length - valid.length];
417 int index = 0;
418 for (int i = 0; i < TYPES.length; ++i) {
419 boolean found = false;
420 for (int j = 0; j < valid.length; ++j) {
421 if (TYPES[i].equals(valid[j])) {
422 found = true;
423 break;
424 }
425 }
426 if (!found) {
427 invalid[index++] = TYPES[i];
428 }
429 }
430 return invalid;
431 }
432
433 static {
434 VALID = new HashMap();
435 VALID.put(Boolean.class, BOOLEAN);
436 VALID.put(Byte.class, BYTE);
437 VALID.put(Short.class, SHORT);
438 VALID.put(Character.class, CHAR);
439 VALID.put(Integer.class, INT);
440 VALID.put(Long.class, LONG);
441 VALID.put(Float.class, FLOAT);
442 VALID.put(Double.class, DOUBLE);
443 VALID.put(String.class, STRING);
444 VALID.put(byte[].class, BYTES);
445
446 INVALID = new HashMap();
447 INVALID.put(Boolean.class, getInvalid(Boolean.class));
448 INVALID.put(Byte.class, getInvalid(Byte.class));
449 INVALID.put(Short.class, getInvalid(Short.class));
450 INVALID.put(Character.class, getInvalid(Character.class));
451 INVALID.put(Integer.class, getInvalid(Integer.class));
452 INVALID.put(Long.class, getInvalid(Long.class));
453 INVALID.put(Float.class, getInvalid(Float.class));
454 INVALID.put(Double.class, getInvalid(Double.class));
455 INVALID.put(String.class, getInvalid(String.class));
456 INVALID.put(byte[].class, getInvalid(byte[].class));
457 }
458
459 }
This page was automatically generated by Maven