View Javadoc

1   package net.sourceforge.jparam.typename;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import net.sourceforge.jparam.JParamException;
7   import net.sourceforge.jparam.conversion.creators.CreatorRegistry;
8   
9   /***
10   * This registry holds a mapping between JParam type names and Java types
11   *  
12   * @author ron_sidi
13   * 
14   */
15  public class TypeNameRegistry {
16  	private static TypeNameRegistry registry = null;
17  
18  	private Map jparamToJavaMap = new HashMap();
19  
20  	private Map javaToJParamMap = new HashMap();
21  
22  	private TypeNameRegistry() {
23  	}
24  
25  	public static TypeNameRegistry getInstance() {
26  		if (registry == null) {
27  			registry = new TypeNameRegistry();
28  		}
29  		return registry;
30  	}
31  
32  	/***
33  	 * Return the Java <code>Class</code> represented by the given JParam type
34  	 * name
35  	 * 
36  	 * All const, '*', '&' and template modifiers are ignored since they have no
37  	 * meaning in Java
38  	 * 
39  	 * @param _jparamTypeName
40  	 * @return @throws
41  	 *         JParamException
42  	 */
43  	public Class getJavaType(String _jparamTypeName) throws JParamException {
44  		String jparamTypeName = _jparamTypeName;
45  		if (jparamTypeName.indexOf('*') > 0) {
46  			jparamTypeName = jparamTypeName.substring(0, jparamTypeName
47  					.indexOf('*'));
48  		}
49  		if (jparamTypeName.indexOf('&') > 0) {
50  			jparamTypeName = jparamTypeName.substring(0, jparamTypeName
51  					.indexOf('&'));
52  		}
53  		if (jparamTypeName.startsWith("const")) {
54  			jparamTypeName = jparamTypeName.substring("const".length()).trim();
55  		}
56  		Class obj = (Class) jparamToJavaMap.get(jparamTypeName);
57  		if (obj != null) {
58  			return obj;
59  		}
60  
61  		int templateArgIndex = jparamTypeName.indexOf('<');
62  		if (templateArgIndex != -1) {
63  			String baseType = jparamTypeName.substring(0, templateArgIndex);
64  			return getJavaType(baseType);
65  		}
66  
67  		throw new JParamException("No Java type registered for JParam type: "
68  				+ jparamTypeName);
69  
70  	}
71  
72  	/***
73  	 * Get the JParam type name for the given Java <code>Class</code>
74  	 * 
75  	 * @param type
76  	 * @return @throws
77  	 *         JParamException
78  	 */
79  	public String getJParamTypeName(Class type) throws JParamException {
80  		Object obj = javaToJParamMap.get(type);
81  		if (obj != null) {
82  			return (String) obj;
83  		}
84  
85  		throw new JParamException("No typename registered for: "
86  				+ type.getName());
87  	}
88  
89  	/***
90  	 * Register a bidirectional mapping between the given Java
91  	 * <code>Class</code> and the given JParam type name
92  	 * 
93  	 * @param t
94  	 * @param jparamTypeName
95  	 */
96  	public void registerJParamAndJava(Class t, String jparamTypeName) {
97  		registerJParamToJava(jparamTypeName, t);
98  		registerJavaToJParam(t, jparamTypeName);
99  	}
100 
101 	/***
102 	 * Register a mapping from the given JParam type name to the given Java
103 	 * <code>Class</code>, this is needed in order to support multi-mapping
104 	 * for instance for registering both "long" and "int" as the
105 	 * <code>Integer</code> Java <code>Class</code>
106 	 * 
107 	 * @param jparamTypeName
108 	 * @param t
109 	 */
110 	public void registerJParamToJava(String jparamTypeName, Class t) {
111 		jparamToJavaMap.put(jparamTypeName, t);
112 	}
113 
114 	/***
115 	 * Register the given Java <code>Class</code> to the given type name,
116 	 * again this is needed for multi mapping like the case of registering
117 	 * internal Java types such as Object and Number that have no JParam
118 	 * representation
119 	 * 
120 	 * @param t
121 	 * @param jparamTypeName
122 	 */
123 	public void registerJavaToJParam(Class t, String jparamTypeName) {
124 		javaToJParamMap.put(t, jparamTypeName);
125 		CreatorRegistry.getInstance().registerClass(t);
126 	}
127 
128 	public static void clearSingleton() {
129 		registry = null;
130 	}
131 }