01: /*
02: * Copyright 2006 Ethan Nicholas. All rights reserved.
03: * Use is subject to license terms.
04: */
05: package jaxx.types;
06:
07: import java.util.*;
08:
09: public class TypeManager {
10: private static Map/*<Class, TypeConverter>*/converters = new HashMap/*<Class, TypeConverter>*/();
11:
12: private TypeManager() { /* not instantiable */
13: }
14:
15: public static void registerTypeConverter(Class type,
16: TypeConverter converter) {
17: converters.put(type, converter);
18: }
19:
20: public static TypeConverter getTypeConverter(Class type) {
21: return (TypeConverter) converters.get(type);
22: }
23:
24: public static String getJavaCode(Object object) {
25: if (object == null)
26: return "null";
27: else {
28: TypeConverter converter = getTypeConverter(object
29: .getClass());
30: if (converter == null)
31: throw new IllegalArgumentException("unsupported type: "
32: + object.getClass());
33: return converter.getJavaCode(object);
34: }
35: }
36:
37: public static Object convertFromString(String string, Class type) {
38: TypeConverter converter = getTypeConverter(type);
39: if (converter == null)
40: throw new IllegalArgumentException("unsupported type: "
41: + type);
42: return converter.convertFromString(string, type);
43: }
44: }
|