1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.hbql.mapping;
22
23 import org.apache.expreval.expr.node.BooleanValue;
24 import org.apache.expreval.expr.node.ByteValue;
25 import org.apache.expreval.expr.node.CharValue;
26 import org.apache.expreval.expr.node.DateValue;
27 import org.apache.expreval.expr.node.DoubleValue;
28 import org.apache.expreval.expr.node.FloatValue;
29 import org.apache.expreval.expr.node.GenericValue;
30 import org.apache.expreval.expr.node.IntegerValue;
31 import org.apache.expreval.expr.node.LongValue;
32 import org.apache.expreval.expr.node.ObjectValue;
33 import org.apache.expreval.expr.node.ShortValue;
34 import org.apache.expreval.expr.node.StringValue;
35 import org.apache.hadoop.hbase.hbql.client.HBqlException;
36 import org.apache.hadoop.hbase.hbql.util.Lists;
37 import org.apache.hadoop.hbase.util.Bytes;
38
39 import java.io.Serializable;
40 import java.lang.reflect.Field;
41 import java.util.Arrays;
42 import java.util.Date;
43 import java.util.List;
44
45 public enum FieldType implements Serializable {
46
47 KeyType(String.class, StringValue.class, -1, -1),
48
49 BooleanType(Boolean.TYPE, BooleanValue.class, 0, Bytes.SIZEOF_BOOLEAN, "BOOLEAN", "BOOL"),
50 ByteType(Byte.TYPE, ByteValue.class, 1, Bytes.SIZEOF_BYTE, "BYTE"),
51 CharType(Character.TYPE, CharValue.class, 1, Bytes.SIZEOF_CHAR, "CHAR"),
52
53 ShortType(Short.TYPE, ShortValue.class, 2, Bytes.SIZEOF_SHORT, "SHORT"),
54 IntegerType(Integer.TYPE, IntegerValue.class, 3, Bytes.SIZEOF_INT, "INTEGER", "INT"),
55 LongType(Long.TYPE, LongValue.class, 4, Bytes.SIZEOF_LONG, "LONG"),
56 FloatType(Float.TYPE, FloatValue.class, 5, Bytes.SIZEOF_FLOAT, "FLOAT"),
57 DoubleType(Double.TYPE, DoubleValue.class, 6, Bytes.SIZEOF_DOUBLE, "DOUBLE"),
58
59 StringType(String.class, StringValue.class, -1, -1, "STRING", "VARCHAR"),
60 DateType(Date.class, DateValue.class, -1, -1, "DATE", "DATETIME"),
61 ObjectType(Object.class, ObjectValue.class, -1, -1, "OBJECT", "OBJ");
62
63 private Class componentType;
64 private Class<? extends GenericValue> exprType;
65 private int typeRanking;
66 private int size;
67 private List<String> synonymList;
68
69 FieldType() {
70 }
71
72 FieldType(final Class componentType,
73 final Class<? extends GenericValue> exprType,
74 final int typeRanking,
75 final int size,
76 final String... synonyms) {
77 this.componentType = componentType;
78 this.exprType = exprType;
79 this.typeRanking = typeRanking;
80 this.size = size;
81 this.synonymList = Lists.newArrayList();
82 this.synonymList.addAll(Arrays.asList(synonyms));
83 }
84
85 public Class getComponentType() {
86 return this.componentType;
87 }
88
89 public int getTypeRanking() {
90 return this.typeRanking;
91 }
92
93 public int getSize() {
94 return this.size;
95 }
96
97 public Class<? extends GenericValue> getExprType() {
98 return this.exprType;
99 }
100
101 public static FieldType getFieldType(final Object obj) {
102 final Class fieldClass = obj.getClass();
103 return getFieldType(fieldClass);
104 }
105
106 public static FieldType getFieldType(final Field field) {
107 final Class fieldClass = field.getType();
108 return getFieldType(fieldClass);
109 }
110
111 public String getFirstSynonym() {
112 return this.getSynonymList().get(0);
113 }
114
115 private List<String> getSynonymList() {
116 return this.synonymList;
117 }
118
119 public static FieldType getFieldType(final Class fieldClass) {
120
121 final Class<?> clazz = fieldClass.isArray() ? fieldClass.getComponentType() : fieldClass;
122
123 if (!clazz.isPrimitive()) {
124 if (clazz.equals(String.class))
125 return StringType;
126 else if (clazz.equals(Date.class))
127 return DateType;
128 else
129 return ObjectType;
130 }
131 else {
132 for (final FieldType type : values()) {
133 final Class compType = type.getComponentType();
134 if (clazz.equals(compType))
135 return type;
136 }
137 }
138
139 throw new RuntimeException("Unknown type: " + clazz + " in FieldType.getFieldType()");
140 }
141
142 public static FieldType getFieldType(final String desc) throws HBqlException {
143
144 if (desc == null)
145 return null;
146
147 for (final FieldType type : values()) {
148 if (type.matchesSynonym(desc))
149 return type;
150 }
151
152 throw new HBqlException("Unknown type description: " + desc);
153 }
154
155 private boolean matchesSynonym(final String str) {
156 for (final String syn : this.getSynonymList())
157 if (str.equalsIgnoreCase(syn))
158 return true;
159 return false;
160 }
161 }