View Javadoc

1   /*
2    * Created on 23 Dec 2007
3    *
4    */
5   package org.sourceforge.jemm.database;
6   
7   import java.util.HashMap;
8   import java.util.Map;
9   
10  import org.sourceforge.jemm.util.JEMMObject;
11  
12  /**
13   * The valid field types within JEMM
14   * 
15   * @author Rory Graves
16   */
17  public enum FieldType {
18      /** A boolean field. */
19      BOOLEAN,
20      /** A byte field. */
21      BYTE,
22      /** A char field. */
23      CHAR,
24      /** A char field. */
25      SHORT,
26      /** An int field. */
27      INT,
28      /** A float field. */
29      FLOAT,
30      /** A long field. */
31      LONG,
32      /** A double field. */
33      DOUBLE,
34      /** An immutable, serializable value type (e.g. String) */
35      SERIALIZABLE,    
36      /** An reference field. */
37      OBJECT,
38      /** boolean array type */
39      BOOLEAN_ARRAY,
40      /** byte array type */
41      BYTE_ARRAY,
42      /** char array type */
43      CHAR_ARRAY,
44      /** short array type */
45      SHORT_ARRAY,
46      /** int array type */
47      INT_ARRAY,
48      /** float array type */
49      FLOAT_ARRAY,
50      /** long array type */
51      LONG_ARRAY,
52      /** double array type */
53      DOUBLE_ARRAY,
54      /** object array type */
55      OBJECT_ARRAY;
56      
57      private static final Map<Class<?>, FieldType> ARRAY_TYPES = new HashMap<Class<?>, FieldType>();
58  	private static final Map<Class<?>, FieldType> BASIC_TYPES = new HashMap<Class<?>, FieldType>();
59  	
60  	private static final Map<Class<?>, FieldType> PRIMTIVE_OBJECT_TYPES = new HashMap<Class<?>, FieldType>();
61      
62      static {
63      	ARRAY_TYPES.put(int.class, INT_ARRAY);
64      	ARRAY_TYPES.put(float.class, FLOAT_ARRAY);
65      	ARRAY_TYPES.put(boolean.class, BOOLEAN_ARRAY);
66      	ARRAY_TYPES.put(byte.class, BYTE_ARRAY);
67      	ARRAY_TYPES.put(char.class, CHAR_ARRAY);
68      	ARRAY_TYPES.put(short.class, SHORT_ARRAY);
69      	ARRAY_TYPES.put(long.class, LONG_ARRAY);
70      	ARRAY_TYPES.put(double.class, DOUBLE_ARRAY);
71      	
72      	BASIC_TYPES.put(int.class, INT);
73      	BASIC_TYPES.put(float.class, FLOAT);
74      	BASIC_TYPES.put(boolean.class, BOOLEAN);
75      	BASIC_TYPES.put(byte.class, BYTE);
76      	BASIC_TYPES.put(char.class, CHAR);
77      	BASIC_TYPES.put(short.class, SHORT);
78      	BASIC_TYPES.put(long.class, LONG);
79      	BASIC_TYPES.put(double.class, DOUBLE);
80      	BASIC_TYPES.put(String.class, SERIALIZABLE);
81      	
82      	PRIMTIVE_OBJECT_TYPES.put(Integer.class, INT);
83      	PRIMTIVE_OBJECT_TYPES.put(Float.class, FLOAT);
84      	PRIMTIVE_OBJECT_TYPES.put(Boolean.class, BOOLEAN);
85      	PRIMTIVE_OBJECT_TYPES.put(Byte.class, BYTE);
86      	PRIMTIVE_OBJECT_TYPES.put(Character.class, CHAR);
87      	PRIMTIVE_OBJECT_TYPES.put(Short.class, SHORT);
88      	PRIMTIVE_OBJECT_TYPES.put(Long.class, LONG);
89      	PRIMTIVE_OBJECT_TYPES.put(Double.class, DOUBLE);
90      	PRIMTIVE_OBJECT_TYPES.put(String.class, SERIALIZABLE);
91      	
92      	BASIC_TYPES.putAll(PRIMTIVE_OBJECT_TYPES);
93      }
94      
95      
96      public static FieldType convert(Class<?> clazz) {
97      	if (clazz.isArray()) {
98  			Class<?> underlying = clazz.getComponentType();
99  			
100 			if (ARRAY_TYPES.containsKey(underlying))
101 				return ARRAY_TYPES.get(underlying);
102 			
103 	    	return OBJECT_ARRAY;
104 		}    	
105     	
106     	if (BASIC_TYPES.containsKey(clazz))
107 			return BASIC_TYPES.get(clazz);
108 
109     	if(JEMMObject.class.isAssignableFrom(clazz))
110     		return OBJECT;
111     	else
112     		throw new IllegalArgumentException("Given class " + clazz + " cannot be mapped to a storage type");
113 	}
114     
115     public static boolean isPrimitive(FieldType type) {
116     	return BOOLEAN.equals(type) || BYTE.equals(type ) || 
117     		CHAR.equals(type) || SHORT.equals(type) ||
118     		INT.equals(type) || FLOAT.equals(type) || 
119     		LONG.equals(type) || DOUBLE.equals(type) || 
120     		SERIALIZABLE.equals(type);
121     }
122     
123     public static boolean isObjectPrimitive(Class<?> clazz) {
124     	return PRIMTIVE_OBJECT_TYPES.containsKey(clazz);
125     }
126 }