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.hadoop.hbase.hbql.client.HBqlException;
24 import org.apache.hadoop.hbase.hbql.util.Maps;
25
26 import java.lang.reflect.Field;
27 import java.lang.reflect.Method;
28 import java.util.Map;
29 import java.util.NavigableMap;
30 import java.util.TreeMap;
31
32 public abstract class FieldAttrib extends ColumnAttrib {
33
34 private final transient Field field;
35
36 protected FieldAttrib(final String familyName,
37 final String columnName,
38 final Field field,
39 final FieldType fieldType,
40 final String getter,
41 final String setter) {
42 super(ColumnDefinition.newFieldAttribColumn(familyName, columnName, field, fieldType, getter, setter));
43 this.field = field;
44 setAccessible(this.getField());
45 }
46
47 public String toString() {
48 return this.getSimpleObjectQualifiedName() + " " + this.getFamilyQualifiedName();
49 }
50
51 public String getNameToUseInExceptions() {
52 return this.getObjectQualifiedName();
53 }
54
55 public String getObjectQualifiedName() {
56 return this.getEnclosingClassName() + "." + this.getField().getName();
57 }
58
59 public String getSimpleObjectQualifiedName() {
60 return this.getEnclosingClass().getSimpleName() + "." + this.getField().getName();
61 }
62
63 public static String getObjectQualifiedName(final Field field) {
64 return field.getDeclaringClass().getName() + "." + field.getName();
65 }
66
67 public String getEnclosingClassName() {
68 return this.getEnclosingClass().getName();
69 }
70
71 private Class getEnclosingClass() {
72 return this.getField().getDeclaringClass();
73 }
74
75 protected Method getMethod(final String methodName, final Class<?>... params) throws NoSuchMethodException {
76 return this.getEnclosingClass().getDeclaredMethod(methodName, params);
77 }
78
79 protected Class getComponentType() {
80 return this.getField().getType().getComponentType();
81 }
82
83 protected Field getField() {
84 return this.field;
85 }
86
87 public Object getCurrentValue(final Object obj) throws HBqlException {
88 try {
89 return this.getField().get(obj);
90 }
91 catch (IllegalAccessException e) {
92 throw new HBqlException("Error getting value of " + this.getObjectQualifiedName());
93 }
94 }
95
96 public void setCurrentValue(final Object obj, final long timestamp, final Object val) {
97 try {
98 this.getField().set(obj, val);
99 }
100 catch (IllegalAccessException e) {
101 throw new RuntimeException("Error setting value of " + this.getObjectQualifiedName());
102 }
103 }
104
105 public void setUnMappedCurrentValue(final Object obj,
106 final String name,
107 final byte[] val) throws HBqlException {
108
109 Map<String, byte[]> mapVal = (Map<String, byte[]>)this.getCurrentValue(obj);
110
111 if (mapVal == null) {
112 mapVal = Maps.newHashMap();
113 this.setCurrentValue(obj, 0, mapVal);
114 }
115
116 mapVal.put(name, val);
117 }
118
119 public void setUnMappedVersionMap(final Object obj,
120 final String name,
121 final NavigableMap<Long, byte[]> timeStampMap) throws HBqlException {
122
123 Map<String, NavigableMap<Long, byte[]>> mapVal = (Map<String, NavigableMap<Long, byte[]>>)this.getCurrentValue(obj);
124
125 if (mapVal == null) {
126 mapVal = Maps.newHashMap();
127 this.setCurrentValue(obj, 0, mapVal);
128 }
129
130 mapVal.put(name, timeStampMap);
131 }
132
133 public Map<Long, Object> getVersionMap(final Object obj) throws HBqlException {
134
135 if (!this.isAVersionValue())
136 throw new HBqlException(this.getFamilyQualifiedName() + " not marked with @ColumnVersionMap");
137
138
139 Map<Long, Object> mapVal = (Map<Long, Object>)this.getCurrentValue(obj);
140
141 if (mapVal == null) {
142 mapVal = new TreeMap<Long, Object>();
143 this.setCurrentValue(obj, 0, mapVal);
144 }
145
146 return mapVal;
147 }
148
149 protected static void setAccessible(final Field field) {
150
151 if (!field.isAccessible())
152 field.setAccessible(true);
153 }
154
155 public void resetDefaultValue() {
156
157 }
158
159 public Object getDefaultValue() throws HBqlException {
160 return null;
161 }
162
163 public boolean hasDefaultArg() throws HBqlException {
164 return false;
165 }
166 }