KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > util > TypeConverter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.jsfext.util;
24
25 import java.math.BigDecimal JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29
30 /**
31  * Provides an efficient and robust mechanism for converting an object
32  * to a different type. For example, one can convert a <code>String</code> to
33  * an <code>Integer</code> using the <code>TypeConverter</code> like this:
34  *
35  * <pre>
36  * Integer i = (Integer) TypeConverter.asType(Integer.class, "123");
37  * </pre>
38  *
39  * or using the shortcut method:
40  *
41  * <pre>
42  * int i = TypeConverter.asInt("123");
43  * </pre>
44  *
45  * The <code>TypeConverter</code> comes ready to convert all the primitive
46  * types, plus a few more like <code>java.sql.Date</code> and <code>
47  * java.math.BigDecimal</code>.<p>
48  *
49  * The conversion process has been optimized so that it is now a constant
50  * time operation (aside from the conversion itself, which may vary).
51  * Because of this optimization, it is possible to register classes that
52  * implement the new <code>TypeConversion</code> interface for conversion
53  * to a custom type. For example, this means that you can define a class to
54  * convert arbitrary objects to type <code>Foo</code>, and register it for
55  * use throughout the VM:
56  *
57  * <pre>
58  * TypeConversion fooConversion = new FooTypeConversion();
59  * TypeConverter.registerTypeConversion(Foo.class, fooConversion);
60  * ...
61  * Bar bar = new Bar();
62  * Foo foo = (Foo) TypeConverter.asType(Foo.class, bar);
63  * ...
64  * String s = "bar";
65  * Foo foo = (Foo) TypeConverter.asType(Foo.class, s);
66  * </pre>
67  *
68  * The TypeConverter allows specification of an arbitrary <i>type key</i> in
69  * the <code>registerTypeConversion()</code> and <code>asType()</code> methods,
70  * so one can simultaneously register a conversion object under a <code>
71  * Class</code> object, a class name, and a logical type name. For example,
72  * the following are valid ways of converting a string to an <code>int</code>
73  * using <code>TypeConverter</code>:
74  *
75  * <pre>
76  * Integer i = (Integer) TypeConverter.asType(Integer.class, "123");
77  * Integer i = (Integer) TypeConverter.asType("java.lang.Integer", "123");
78  * Integer i = (Integer) TypeConverter.asType(TypeConverter.TYPE_INT, "123");
79  * Integer i = (Integer) TypeConverter.asType(TypeConverter.TYPE_INTEGER, "123");
80  * Integer i = (Integer) TypeConverter.asType("int", "123");
81  * Integer i = (Integer) TypeConverter.asType("integer", "123");
82  * int i = TypeConverter.asInt("123");
83  * </pre>
84  *
85  * Default type conversions have been registered under the following keys:
86  *
87  * <pre>
88  * Classes:
89  * java.lang.Object
90  * java.lang.String
91  * java.lang.Integer
92  * java.lang.Integer.TYPE (int)
93  * java.lang.Double
94  * java.lang.Double.TYPE (double)
95  * java.lang.Boolean
96  * java.lang.Boolean.TYPE (boolean)
97  * java.lang.Long
98  * java.lang.Long.TYPE (long)
99  * java.lang.Float
100  * java.lang.Float.TYPE (float)
101  * java.lang.Short
102  * java.lang.Short.TYPE (short)
103  * java.lang.Byte
104  * java.lang.Byte.TYPE (byte)
105  * java.lang.Character
106  * java.lang.Character.TYPE (char)
107  * java.math.BigDecimal
108  * java.sql.Date
109  * java.sql.Time
110  * java.sql.Timestamp
111  *
112  * Class name strings:
113  * "java.lang.Object"
114  * "java.lang.String"
115  * "java.lang.Integer"
116  * "java.lang.Double"
117  * "java.lang.Boolean"
118  * "java.lang.Long"
119  * "java.lang.Float"
120  * "java.lang.Short"
121  * "java.lang.Byte"
122  * "java.lang.Character"
123  * "java.math.BigDecimal"
124  * "java.sql.Date"
125  * "java.sql.Time"
126  * "java.sql.Timestamp"
127  *
128  * Logical type name string constants:
129  * TypeConverter.TYPE_UNKNOWN ("null")
130  * TypeConverter.TYPE_OBJECT ("object")
131  * TypeConverter.TYPE_STRING ("string")
132  * TypeConverter.TYPE_INT ("int")
133  * TypeConverter.TYPE_INTEGER ("integer")
134  * TypeConverter.TYPE_DOUBLE ("double")
135  * TypeConverter.TYPE_BOOLEAN ("boolean")
136  * TypeConverter.TYPE_LONG ("long")
137  * TypeConverter.TYPE_FLOAT ("float")
138  * TypeConverter.TYPE_SHORT ("short")
139  * TypeConverter.TYPE_BYTE ("byte")
140  * TypeConverter.TYPE_CHAR ("char")
141  * TypeConverter.TYPE_CHARACTER ("character")
142  * TypeConverter.TYPE_BIG_DECIMAL ("bigdecimal")
143  * TypeConverter.TYPE_SQL_DATE ("sqldate")
144  * TypeConverter.TYPE_SQL_TIME ("sqltime")
145  * TypeConverter.TYPE_SQL_TIMESTAMP ("sqltimestamp")
146  * </pre>
147  *
148  * The <code>TypeConverter</code> treats type keys of type <code>Class</code>
149  * slightly differently than other keys. If the provided value is already of
150  * the type specified by the type key class, it is returned without a
151  * conversion taking place. For example, a value of type <code>MySub</code>
152  * that extends the class <code>MySuper</code> would not be converted in the
153  * following situation because it is already of the necessary type:
154  *
155  * <pre>
156  * MySub o = (MySub) TypeConverter.asType(MySuper.class, mySub);
157  * </pre>
158  *
159  * Be warned that although the type conversion infrastructure in this class
160  * is desgned to add only minimal overhead to the conversion process, conversion
161  * of an object to another type is a potentially expensive operation and should
162  * be used with discretion.
163  *
164  * @see TypeConversion
165  * @author Todd Fast, todd.fast@sun.com
166  * @author Mike Frisino, michael.frisino@sun.com
167  * @author Ken Paulsen, ken.paulsen@sun.com (stripped down)
168  */

169 public class TypeConverter extends Object JavaDoc {
170
171     /**
172      * Cannot instantiate
173      */

174     private TypeConverter() {
175     super();
176     }
177
178     /**
179      * Return the map of type conversion objects. The keys for the values
180      * in this map may be arbitrary objects, but the values are of type
181      * <code>TypeConversion</code>.
182      */

183     public static Map JavaDoc getTypeConversions() {
184     return typeConversions;
185     }
186
187     /**
188      * Register a type conversion object under the specified key. This
189      * method can be used by developers to register custom type conversion
190      * objects.
191      */

192     public static void registerTypeConversion(Object JavaDoc key, TypeConversion conversion) {
193     typeConversions.put(key, conversion);
194     }
195
196     /**
197      * <p> Convert an object to the type specified by the provided type key.
198      * A type conversion object must have been previously registered
199      * under the provided key in order for the conversion to succeed (with
200      * one exception, see below).</p>
201      *
202      * <p> Note, this method treats type keys of type <code>Class</code>
203      * differently than other type keys. That is, this method will check
204      * if the provided value is the same as or a subclass of the specified
205      * class. If it is, this method returns the value object immediately
206      * without attempting to convert its type. One exception to this
207      * rule is if the provided type key is <code>Object.class</code>, in
208      * which case the conversion is attempted anyway. The reason for this
209      * deviation is that this key may have special meaning based on the
210      * type of the provided value. For example, if the provided value is
211      * a byte array, the <code>ObjectTypeConversion</code> class assumes
212      * it is a serialized object and attempts to deserialize it. Because
213      * all objects, including arrays, are of type <code>Object</code>,
214      * this conversion would never be attempted without this special
215      * handling. (Note that the default conversion for type key <code>
216      * Object.class</code> is to simply return the original object.)</p>
217      *
218      * @param typeKey The key under which the desired type conversion object
219      * has been previously registered. Most commonly, this key
220      * should be a <code>Class</code> object, a class name
221      * string, or a logical type string represented by the
222      * various <code>TYPE_*</code> constants defined in this
223      * class.
224      *
225      * @param value The value to convert to the specified target type
226      *
227      * @return The converted value object, or <code>null</code> if the
228      * original value is <code>null</code>
229      */

230     public static Object JavaDoc asType(Object JavaDoc typeKey, Object JavaDoc value) {
231     if (value == null) {
232         return null;
233     }
234
235     /*
236         System.out.println("COERCE_TYPE: Coercing ("+value+
237         ")\n\tfrom "+value.getClass().getName()+
238         "\n\tto "+typeKey+"\n");
239     */

240
241     if (typeKey == null) {
242         return value;
243     }
244
245     // Check if the provided value is already of the target type
246
if (typeKey instanceof Class JavaDoc && ((Class JavaDoc) typeKey) != Object JavaDoc.class) {
247         if (((Class JavaDoc) typeKey).isInstance(value)) {
248         return value;
249         }
250     }
251
252     // Find the type conversion object
253
TypeConversion conversion = (TypeConversion) typeConversions.get(typeKey);
254
255     // Convert the value
256
if (conversion != null) {
257         return conversion.convertValue(value);
258     } else {
259         throw new IllegalArgumentException JavaDoc("Could not find type conversion for "
260             + "type \"" + typeKey + "\" (value = \"" + value + "\"");
261     }
262     }
263
264     /**
265      *
266      */

267     public static byte asByte(Object JavaDoc value) {
268     return asByte(value, (byte) 0);
269     }
270
271     /**
272      *
273      */

274     public static byte asByte(Object JavaDoc value, byte defaultValue) {
275     value = asType(Byte JavaDoc.class, value);
276     if (value != null) {
277         return ((Byte JavaDoc) value).byteValue();
278     }
279     return defaultValue;
280     }
281
282     /**
283      *
284      */

285     public static short asShort(Object JavaDoc value) {
286     return asShort(value, (short) 0);
287     }
288
289     /**
290      *
291      */

292     public static short asShort(Object JavaDoc value, short defaultValue) {
293     value = asType(Short JavaDoc.class, value);
294     if (value != null) {
295         return ((Short JavaDoc) value).shortValue();
296     }
297     return defaultValue;
298     }
299
300     /**
301      *
302      */

303     public static int asInt(Object JavaDoc value) {
304     return asInt(value, 0);
305     }
306
307     /**
308      *
309      */

310     public static int asInt(Object JavaDoc value, int defaultValue) {
311     value = asType(Integer JavaDoc.class, value);
312     if (value != null) {
313         return ((Integer JavaDoc) value).intValue();
314     }
315     return defaultValue;
316     }
317
318     /**
319      *
320      */

321     public static long asLong(Object JavaDoc value) {
322     return asLong(value, 0L);
323     }
324
325     /**
326      *
327      */

328     public static long asLong(Object JavaDoc value, long defaultValue) {
329     value = asType(Long JavaDoc.class, value);
330     if (value != null) {
331         return ((Long JavaDoc) value).longValue();
332     }
333     return defaultValue;
334     }
335
336     /**
337      *
338      */

339     public static float asFloat(Object JavaDoc value) {
340     return asFloat(value, 0F);
341     }
342
343     /**
344      *
345      */

346     public static float asFloat(Object JavaDoc value, float defaultValue) {
347     value = asType(Float JavaDoc.class, value);
348     if (value != null) {
349         return ((Float JavaDoc) value).floatValue();
350     }
351     return defaultValue;
352     }
353
354     /**
355      *
356      */

357     public static double asDouble(Object JavaDoc value) {
358     return asDouble(value, 0D);
359     }
360
361     /**
362      *
363      */

364     public static double asDouble(Object JavaDoc value, double defaultValue) {
365     value = asType(Double JavaDoc.class, value);
366     if (value != null) {
367         return ((Double JavaDoc) value).doubleValue();
368     }
369     return defaultValue;
370     }
371
372     /**
373      *
374      */

375     public static char asChar(Object JavaDoc value) {
376     return asChar(value, (char) 0);
377     }
378
379     /**
380      *
381      */

382     public static char asChar(Object JavaDoc value, char defaultValue) {
383     value = asType(Character JavaDoc.class, value);
384     if (value != null) {
385         return ((Character JavaDoc) value).charValue();
386     }
387     return defaultValue;
388     }
389
390     /**
391      *
392      */

393     public static boolean asBoolean(Object JavaDoc value) {
394     return asBoolean(value, false);
395     }
396
397     /**
398      *
399      */

400     public static boolean asBoolean(Object JavaDoc value, boolean defaultValue) {
401     value = asType(Boolean JavaDoc.class, value);
402     if (value != null) {
403         return ((Boolean JavaDoc) value).booleanValue();
404     }
405     return defaultValue;
406     }
407
408     /**
409      *
410      */

411     public static String JavaDoc asString(Object JavaDoc value) {
412     return (String JavaDoc) asType(String JavaDoc.class, value);
413     }
414
415     /**
416      *
417      */

418     public static String JavaDoc asString(Object JavaDoc value, String JavaDoc defaultValue) {
419     value = asType(String JavaDoc.class, value);
420     if (value != null) {
421         return (String JavaDoc) value;
422     }
423     return defaultValue;
424     }
425
426
427     /////////////////////////////////////////////////////////////////////////
428
// Inner classes
429
/////////////////////////////////////////////////////////////////////////
430

431     /**
432      *
433      */

434     public static class UnknownTypeConversion implements TypeConversion {
435
436     /**
437      *
438      */

439     public Object JavaDoc convertValue(Object JavaDoc value) {
440         return value;
441     }
442     }
443
444     /**
445      *
446      */

447     public static class StringTypeConversion implements TypeConversion {
448
449     /**
450      *
451      */

452     public Object JavaDoc convertValue(Object JavaDoc value) {
453         if (value == null) {
454         return null;
455         }
456
457         if (value.getClass().isArray()) {
458         // This is a byte array; we can convert it to a string
459
if (value.getClass().getComponentType() == Byte.TYPE) {
460             value = new String JavaDoc((byte[]) value);
461         } else if (value.getClass().getComponentType() == Character.TYPE) {
462             value = new String JavaDoc((char[]) value);
463         }
464         } else if (!(value instanceof String JavaDoc)) {
465         value = value.toString();
466         }
467         return value;
468     }
469     }
470
471     /**
472      *
473      */

474     public static class IntegerTypeConversion implements TypeConversion {
475
476     /**
477      *
478      */

479     public Object JavaDoc convertValue(Object JavaDoc value) {
480         if (value == null) {
481         return null;
482         }
483
484         if (!(value instanceof Integer JavaDoc)) {
485         String JavaDoc v = value.toString();
486         if (v.trim().length() == 0) {
487             value = null;
488         } else {
489             value = new Integer JavaDoc(v);
490         }
491         }
492
493         return value;
494     }
495     }
496
497     /**
498      *
499      */

500     public static class DoubleTypeConversion implements TypeConversion {
501
502     /**
503      *
504      */

505     public Object JavaDoc convertValue(Object JavaDoc value) {
506         if (value == null) {
507         return null;
508         }
509
510         if (!(value instanceof Double JavaDoc)) {
511         String JavaDoc v = value.toString();
512         if (v.trim().length() == 0) {
513             value = null;
514         } else {
515             value = new Double JavaDoc(v);
516         }
517         }
518
519         return value;
520     }
521     }
522
523     /**
524      *
525      */

526     public static class BooleanTypeConversion implements TypeConversion {
527
528     /**
529      *
530      */

531     public Object JavaDoc convertValue(Object JavaDoc value) {
532         if (value == null) {
533         return null;
534         }
535
536         if (!(value instanceof Boolean JavaDoc)) {
537         String JavaDoc v = value.toString();
538         if (v.trim().length() == 0) {
539             value = null;
540         } else {
541             value = Boolean.valueOf(v);
542         }
543         }
544
545         return value;
546     }
547     }
548
549     /**
550      *
551      */

552     public static class LongTypeConversion implements TypeConversion {
553
554     /**
555      *
556      */

557     public Object JavaDoc convertValue(Object JavaDoc value) {
558         if (value == null) {
559         return null;
560         }
561
562         if (!(value instanceof Long JavaDoc)) {
563         String JavaDoc v = value.toString();
564         if (v.trim().length() == 0) {
565             value = null;
566         } else {
567             value = new Long JavaDoc(v);
568         }
569         }
570
571         return value;
572     }
573     }
574
575     /**
576      *
577      */

578     public static class FloatTypeConversion implements TypeConversion {
579
580     /**
581      *
582      */

583     public Object JavaDoc convertValue(Object JavaDoc value) {
584         if (value == null) {
585         return null;
586         }
587
588         if (!(value instanceof Float JavaDoc)) {
589         String JavaDoc v = value.toString();
590         if (v.trim().length() == 0) {
591             value = null;
592         } else {
593             value = new Float JavaDoc(v);
594         }
595         }
596
597         return value;
598     }
599     }
600
601     /**
602      *
603      */

604     public static class ShortTypeConversion implements TypeConversion {
605
606     /**
607      *
608      */

609     public Object JavaDoc convertValue(Object JavaDoc value) {
610         if (value == null) {
611         return null;
612         }
613
614         if (!(value instanceof Short JavaDoc)) {
615         String JavaDoc v = value.toString();
616         if (v.trim().length() == 0) {
617             value = null;
618         } else {
619             value = new Short JavaDoc(v);
620         }
621         }
622
623         return value;
624     }
625     }
626
627     /**
628      *
629      */

630     public static class BigDecimalTypeConversion implements TypeConversion {
631
632     /**
633      *
634      */

635     public Object JavaDoc convertValue(Object JavaDoc value) {
636         if (value == null) {
637         return null;
638         }
639
640         if (!(value instanceof BigDecimal JavaDoc)) {
641         String JavaDoc v = value.toString();
642         if (v.trim().length() == 0) {
643             value = null;
644         } else {
645             value = new BigDecimal JavaDoc(v);
646         }
647         }
648
649         return value;
650     }
651     }
652
653     /**
654      *
655      */

656     public static class ByteTypeConversion implements TypeConversion {
657
658     /**
659      *
660      */

661     public Object JavaDoc convertValue(Object JavaDoc value) {
662         if (value == null) {
663         return null;
664         }
665
666         if (!(value instanceof Byte JavaDoc)) {
667         String JavaDoc v = value.toString();
668         if (v.trim().length() == 0) {
669             value = null;
670         } else {
671             value = new Byte JavaDoc(v);
672         }
673         }
674
675         return value;
676     }
677     }
678
679     /**
680      *
681      */

682     public static class CharacterTypeConversion implements TypeConversion {
683
684     /**
685      *
686      */

687     public Object JavaDoc convertValue(Object JavaDoc value) {
688         if (value == null) {
689         return null;
690         }
691
692         if (!(value instanceof Character JavaDoc)) {
693         String JavaDoc v = value.toString();
694         if (v.trim().length() == 0) {
695             value = null;
696         } else {
697             value = new Character JavaDoc(v.charAt(0));
698         }
699         }
700
701         return value;
702     }
703     }
704
705     /**
706      *
707      */

708     public static class SqlDateTypeConversion implements TypeConversion {
709     /**
710      *
711      */

712     public Object JavaDoc convertValue(Object JavaDoc value) {
713         if (value == null) {
714         return null;
715         }
716
717         if (!(value instanceof java.sql.Date JavaDoc)) {
718         String JavaDoc v = value.toString();
719         if (v.trim().length() == 0) {
720             value = null;
721         } else {
722             // Value must be in the "yyyy-mm-dd" format
723
value = java.sql.Date.valueOf(v);
724         }
725         }
726
727         return value;
728     }
729     }
730
731     /**
732      *
733      */

734     public static class SqlTimeTypeConversion implements TypeConversion {
735
736     /**
737      *
738      */

739     public Object JavaDoc convertValue(Object JavaDoc value) {
740         if (value == null) {
741         return null;
742         }
743
744         if (!(value instanceof java.sql.Time JavaDoc)) {
745         String JavaDoc v = value.toString();
746         if (v.trim().length() == 0) {
747             value = null;
748         } else {
749             // Value must be in the "hh:mm:ss" format
750
value = java.sql.Time.valueOf(v);
751         }
752         }
753
754         return value;
755     }
756     }
757
758
759     /**
760      *
761      */

762     public static class SqlTimestampTypeConversion implements TypeConversion {
763
764     /**
765      *
766      */

767     public Object JavaDoc convertValue(Object JavaDoc value) {
768         if (value == null) {
769         return null;
770         }
771
772         if (!(value instanceof java.sql.Timestamp JavaDoc)) {
773         String JavaDoc v = value.toString();
774         if (v.trim().length() == 0) {
775             value = null;
776         } else {
777             // Value must be in the "yyyy-mm-dd hh:mm:ss.fffffffff"
778
// format
779
value = java.sql.Timestamp.valueOf(v);
780         }
781         }
782
783         return value;
784     }
785     }
786
787
788     /**
789      *
790      */

791     public static class ObjectTypeConversion implements TypeConversion {
792     public Object JavaDoc convertValue(Object JavaDoc value) {
793 /*
794         if (value==null) {
795         return null;
796         }
797
798 // TODO: Decide if this is important functionality. For now just return the Object that is passed in.
799         if (value.getClass().isArray()) {
800         // This is a byte array; we can convert it to an object
801         if (value.getClass().getComponentType() == Byte.TYPE) {
802             ByteArrayInputStream bis =
803                 new ByteArrayInputStream((byte[])value);
804             ApplicationObjectInputStream ois = null;
805             try {
806             ois = new ApplicationObjectInputStream(bis);
807             value = ois.readObject();
808             } catch (Exception e) {
809             throw new WrapperRuntimeException(
810                 "Could not deserialize object",e);
811             } finally {
812             try {
813                 if (ois != null) {
814                 ois.close();
815                 }
816                 if (bis != null) {
817                 bis.close();
818                 }
819             } catch (IOException e) {
820                 // Ignore
821             }
822             }
823         } else {
824             // value is OK as is
825         }
826         }
827 */

828
829         return value;
830     }
831     }
832
833
834
835
836     /////////////////////////////////////////////////////////////////////////
837
// Test classes
838
/////////////////////////////////////////////////////////////////////////
839

840     private static class TestSuperclass extends Object JavaDoc {
841     }
842
843     private static class Test extends TestSuperclass {
844     public static void main(String JavaDoc[] args) {
845         if (!(TypeConverter.asString(new Integer JavaDoc(12)) instanceof String JavaDoc)) {
846         throw new Error JavaDoc();
847         }
848
849         if (!(TypeConverter.asType(Integer JavaDoc.class, "12") instanceof Integer JavaDoc)) {
850         throw new Error JavaDoc();
851         }
852
853         if (!(TypeConverter.asType(Long JavaDoc.class, "12") instanceof Long JavaDoc)) {
854         throw new Error JavaDoc();
855         }
856
857         if (!(TypeConverter.asType(Float JavaDoc.class, "12.0") instanceof Float JavaDoc)) {
858         throw new Error JavaDoc();
859         }
860
861         if (!(TypeConverter.asType(Double JavaDoc.class, "12.0") instanceof Double JavaDoc)) {
862         throw new Error JavaDoc();
863         }
864
865         if (!(TypeConverter.asType(Short JavaDoc.class, "12") instanceof Short JavaDoc)) {
866         throw new Error JavaDoc();
867         }
868
869         if (!(TypeConverter.asType(BigDecimal JavaDoc.class, "12") instanceof BigDecimal JavaDoc)) {
870         throw new Error JavaDoc();
871         }
872
873         if (!(TypeConverter.asType(Boolean JavaDoc.class, "true") instanceof Boolean JavaDoc)) {
874         throw new Error JavaDoc();
875         }
876
877         if (!(TypeConverter.asType(Byte JavaDoc.class, "12") instanceof Byte JavaDoc)) {
878         throw new Error JavaDoc();
879         }
880
881         if (!(TypeConverter.asType(Character JavaDoc.class, "1") instanceof Character JavaDoc)) {
882         throw new Error JavaDoc();
883         }
884
885         System.out.println("Test passed.");
886     }
887     }
888
889
890
891
892     /////////////////////////////////////////////////////////////////////////
893
// Class variables
894
/////////////////////////////////////////////////////////////////////////
895

896     private static final Map JavaDoc typeConversions = new HashMap JavaDoc();
897
898     /** Logical type name "null" */
899     public static final String JavaDoc TYPE_UNKNOWN = "null";
900
901     /** Logical type name "object" */
902     public static final String JavaDoc TYPE_OBJECT = "object";
903
904     /** Logical type name "string" */
905     public static final String JavaDoc TYPE_STRING = "string";
906
907     /** Logical type name "int" */
908     public static final String JavaDoc TYPE_INT = "int";
909
910     /** Logical type name "integer" */
911     public static final String JavaDoc TYPE_INTEGER = "integer";
912
913     /** Logical type name "long" */
914     public static final String JavaDoc TYPE_LONG = "long";
915
916     /** Logical type name "float" */
917     public static final String JavaDoc TYPE_FLOAT = "float";
918
919     /** Logical type name "double" */
920     public static final String JavaDoc TYPE_DOUBLE = "double";
921
922     /** Logical type name "short" */
923     public static final String JavaDoc TYPE_SHORT = "short";
924
925     /** Logical type name "boolean" */
926     public static final String JavaDoc TYPE_BOOLEAN = "boolean";
927
928     /** Logical type name "byte" */
929     public static final String JavaDoc TYPE_BYTE = "byte";
930
931     /** Logical type name "char" */
932     public static final String JavaDoc TYPE_CHAR = "char";
933
934     /** Logical type name "character" */
935     public static final String JavaDoc TYPE_CHARACTER = "character";
936
937     /** Logical type name "bigdecimal" */
938     public static final String JavaDoc TYPE_BIG_DECIMAL = "bigdecimal";
939
940     /** Logical type name "sqldate" */
941     public static final String JavaDoc TYPE_SQL_DATE = "sqldate";
942
943     /** Logical type name "sqltime" */
944     public static final String JavaDoc TYPE_SQL_TIME = "sqltime";
945
946     /** Logical type name "sqltimestamp" */
947     public static final String JavaDoc TYPE_SQL_TIMESTAMP = "sqltimestamp";
948
949     public static final TypeConversion UNKNOWN_TYPE_CONVERSION =
950         new UnknownTypeConversion();
951     public static final TypeConversion OBJECT_TYPE_CONVERSION =
952         new ObjectTypeConversion();
953     public static final TypeConversion STRING_TYPE_CONVERSION =
954         new StringTypeConversion();
955     public static final TypeConversion INTEGER_TYPE_CONVERSION =
956         new IntegerTypeConversion();
957     public static final TypeConversion DOUBLE_TYPE_CONVERSION =
958         new DoubleTypeConversion();
959     public static final TypeConversion BOOLEAN_TYPE_CONVERSION =
960         new BooleanTypeConversion();
961     public static final TypeConversion LONG_TYPE_CONVERSION =
962         new LongTypeConversion();
963     public static final TypeConversion FLOAT_TYPE_CONVERSION =
964         new FloatTypeConversion();
965     public static final TypeConversion SHORT_TYPE_CONVERSION =
966         new ShortTypeConversion();
967     public static final TypeConversion BIG_DECIMAL_TYPE_CONVERSION =
968         new BigDecimalTypeConversion();
969     public static final TypeConversion BYTE_TYPE_CONVERSION =
970         new ByteTypeConversion();
971     public static final TypeConversion CHARACTER_TYPE_CONVERSION =
972         new CharacterTypeConversion();
973     public static final TypeConversion SQL_DATE_TYPE_CONVERSION =
974         new SqlDateTypeConversion();
975     public static final TypeConversion SQL_TIME_TYPE_CONVERSION =
976         new SqlTimeTypeConversion();
977     public static final TypeConversion SQL_TIMESTAMP_TYPE_CONVERSION =
978         new SqlTimestampTypeConversion();
979
980
981
982
983     ////////////////////////////////////////////////////////////////////////////////
984
// Initializers
985
////////////////////////////////////////////////////////////////////////////////
986

987     static {
988     // Add type conversions by class
989
registerTypeConversion(Object JavaDoc.class, OBJECT_TYPE_CONVERSION);
990     registerTypeConversion(String JavaDoc.class, STRING_TYPE_CONVERSION);
991     registerTypeConversion(Integer JavaDoc.class, INTEGER_TYPE_CONVERSION);
992     registerTypeConversion(Integer.TYPE, INTEGER_TYPE_CONVERSION);
993     registerTypeConversion(Double JavaDoc.class, DOUBLE_TYPE_CONVERSION);
994     registerTypeConversion(Double.TYPE, DOUBLE_TYPE_CONVERSION);
995     registerTypeConversion(Boolean JavaDoc.class, BOOLEAN_TYPE_CONVERSION);
996     registerTypeConversion(Boolean.TYPE, BOOLEAN_TYPE_CONVERSION);
997     registerTypeConversion(Long JavaDoc.class, LONG_TYPE_CONVERSION);
998     registerTypeConversion(Long.TYPE, LONG_TYPE_CONVERSION);
999     registerTypeConversion(Float JavaDoc.class, FLOAT_TYPE_CONVERSION);
1000    registerTypeConversion(Float.TYPE, FLOAT_TYPE_CONVERSION);
1001    registerTypeConversion(Short JavaDoc.class, SHORT_TYPE_CONVERSION);
1002    registerTypeConversion(Short.TYPE, SHORT_TYPE_CONVERSION);
1003    registerTypeConversion(BigDecimal JavaDoc.class, BIG_DECIMAL_TYPE_CONVERSION);
1004    registerTypeConversion(Byte JavaDoc.class, BYTE_TYPE_CONVERSION);
1005    registerTypeConversion(Byte.TYPE, BYTE_TYPE_CONVERSION);
1006    registerTypeConversion(Character JavaDoc.class, CHARACTER_TYPE_CONVERSION);
1007    registerTypeConversion(Character.TYPE, CHARACTER_TYPE_CONVERSION);
1008    registerTypeConversion(java.sql.Date JavaDoc.class, SQL_DATE_TYPE_CONVERSION);
1009    registerTypeConversion(java.sql.Time JavaDoc.class, SQL_TIME_TYPE_CONVERSION);
1010    registerTypeConversion(java.sql.Timestamp JavaDoc.class, SQL_TIMESTAMP_TYPE_CONVERSION);
1011
1012    // Add type conversions by class name
1013
registerTypeConversion(Object JavaDoc.class.getName(), OBJECT_TYPE_CONVERSION);
1014    registerTypeConversion(String JavaDoc.class.getName(), STRING_TYPE_CONVERSION);
1015    registerTypeConversion(Integer JavaDoc.class.getName(), INTEGER_TYPE_CONVERSION);
1016    registerTypeConversion(Double JavaDoc.class.getName(), DOUBLE_TYPE_CONVERSION);
1017    registerTypeConversion(Boolean JavaDoc.class.getName(), BOOLEAN_TYPE_CONVERSION);
1018    registerTypeConversion(Long JavaDoc.class.getName(), LONG_TYPE_CONVERSION);
1019    registerTypeConversion(Float JavaDoc.class.getName(), FLOAT_TYPE_CONVERSION);
1020    registerTypeConversion(Short JavaDoc.class.getName(), SHORT_TYPE_CONVERSION);
1021    registerTypeConversion(BigDecimal JavaDoc.class.getName(), BIG_DECIMAL_TYPE_CONVERSION);
1022    registerTypeConversion(Byte JavaDoc.class.getName(), BYTE_TYPE_CONVERSION);
1023    registerTypeConversion(Character JavaDoc.class.getName(), CHARACTER_TYPE_CONVERSION);
1024    registerTypeConversion(java.sql.Date JavaDoc.class.getName(), SQL_DATE_TYPE_CONVERSION);
1025    registerTypeConversion(java.sql.Time JavaDoc.class.getName(), SQL_TIME_TYPE_CONVERSION);
1026    registerTypeConversion(java.sql.Timestamp JavaDoc.class.getName(), SQL_TIMESTAMP_TYPE_CONVERSION);
1027
1028    // Add type conversions by name
1029
registerTypeConversion(TYPE_UNKNOWN, UNKNOWN_TYPE_CONVERSION);
1030    registerTypeConversion(TYPE_OBJECT, OBJECT_TYPE_CONVERSION);
1031    registerTypeConversion(TYPE_STRING, STRING_TYPE_CONVERSION);
1032    registerTypeConversion(TYPE_INT, INTEGER_TYPE_CONVERSION);
1033    registerTypeConversion(TYPE_INTEGER, INTEGER_TYPE_CONVERSION);
1034    registerTypeConversion(TYPE_DOUBLE, DOUBLE_TYPE_CONVERSION);
1035    registerTypeConversion(TYPE_BOOLEAN, BOOLEAN_TYPE_CONVERSION);
1036    registerTypeConversion(TYPE_LONG, LONG_TYPE_CONVERSION);
1037    registerTypeConversion(TYPE_FLOAT, FLOAT_TYPE_CONVERSION);
1038    registerTypeConversion(TYPE_SHORT, SHORT_TYPE_CONVERSION);
1039    registerTypeConversion(TYPE_BIG_DECIMAL, BIG_DECIMAL_TYPE_CONVERSION);
1040    registerTypeConversion(TYPE_BYTE, BYTE_TYPE_CONVERSION);
1041    registerTypeConversion(TYPE_CHAR, CHARACTER_TYPE_CONVERSION);
1042    registerTypeConversion(TYPE_CHARACTER, CHARACTER_TYPE_CONVERSION);
1043    registerTypeConversion(TYPE_SQL_DATE, SQL_DATE_TYPE_CONVERSION);
1044    registerTypeConversion(TYPE_SQL_TIME, SQL_TIME_TYPE_CONVERSION);
1045    registerTypeConversion(TYPE_SQL_TIMESTAMP, SQL_TIMESTAMP_TYPE_CONVERSION);
1046    }
1047}
1048
Popular Tags