1 package org.apache.helix;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.Constructor;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30
31
32
33 public class HelixProperty
34 {
35 public enum HelixPropertyAttribute
36 {
37 BUCKET_SIZE,
38 BATCH_MESSAGE_MODE
39 }
40
41 protected final ZNRecord _record;
42
43 public HelixProperty(String id)
44 {
45 _record = new ZNRecord(id);
46 }
47
48 public HelixProperty(ZNRecord record)
49 {
50 _record = new ZNRecord(record);
51 }
52
53 public final String getId()
54 {
55 return _record.getId();
56 }
57
58 public final ZNRecord getRecord()
59 {
60 return _record;
61 }
62
63 public final void setDeltaList(List<ZNRecordDelta> deltaList)
64 {
65 _record.setDeltaList(deltaList);
66 }
67
68 @Override
69 public String toString()
70 {
71 return _record.toString();
72 }
73
74 public int getBucketSize()
75 {
76 String bucketSizeStr =
77 _record.getSimpleField(HelixPropertyAttribute.BUCKET_SIZE.toString());
78 int bucketSize = 0;
79 if (bucketSizeStr != null)
80 {
81 try
82 {
83 bucketSize = Integer.parseInt(bucketSizeStr);
84 }
85 catch (NumberFormatException e)
86 {
87
88 }
89 }
90 return bucketSize;
91 }
92
93 public void setBucketSize(int bucketSize)
94 {
95 if (bucketSize <= 0)
96 bucketSize = 0;
97
98 _record.setSimpleField(HelixPropertyAttribute.BUCKET_SIZE.toString(), "" + bucketSize);
99 }
100
101
102
103
104
105
106
107
108 public static <T extends HelixProperty> T convertToTypedInstance(Class<T> clazz,
109 ZNRecord record)
110 {
111 if (record == null)
112 {
113 return null;
114 }
115
116 try
117 {
118 Constructor<T> constructor = clazz.getConstructor(new Class[] { ZNRecord.class });
119 return constructor.newInstance(record);
120 }
121 catch (Exception e)
122 {
123
124 e.printStackTrace();
125 }
126
127 return null;
128 }
129
130 public static <T extends HelixProperty> List<T> convertToTypedList(Class<T> clazz,
131 Collection<ZNRecord> records)
132 {
133 if (records == null)
134 {
135 return null;
136 }
137
138 List<T> decorators = new ArrayList<T>();
139 for (ZNRecord record : records)
140 {
141 T decorator = HelixProperty.convertToTypedInstance(clazz, record);
142 if (decorator != null)
143 {
144 decorators.add(decorator);
145 }
146 }
147 return decorators;
148 }
149
150 public static <T extends HelixProperty> Map<String, T> convertListToMap(List<T> records)
151 {
152 if (records == null)
153 {
154 return Collections.emptyMap();
155 }
156
157 Map<String, T> decorators = new HashMap<String, T>();
158 for (T record : records)
159 {
160 decorators.put(record.getId(), record);
161 }
162 return decorators;
163 }
164
165 public static <T extends HelixProperty> List<ZNRecord> convertToList(List<T> typedInstances)
166 {
167 if (typedInstances == null)
168 {
169 return Collections.emptyList();
170 }
171
172 List<ZNRecord> records = new ArrayList<ZNRecord>();
173 for (T typedInstance : typedInstances)
174 {
175 records.add(typedInstance.getRecord());
176 }
177
178 return records;
179 }
180
181 public void setBatchMessageMode(boolean enable)
182 {
183 _record.setSimpleField(HelixPropertyAttribute.BATCH_MESSAGE_MODE.toString(), ""
184 + enable);
185 }
186
187 public boolean getBatchMessageMode()
188 {
189 String enableStr =
190 _record.getSimpleField(HelixPropertyAttribute.BATCH_MESSAGE_MODE.toString());
191 if (enableStr == null)
192 {
193 return false;
194 }
195
196 try
197 {
198 return Boolean.parseBoolean(enableStr.toLowerCase());
199 }
200 catch (Exception e)
201 {
202 return false;
203 }
204 }
205
206 public boolean isValid()
207 {
208 return true;
209 }
210
211 @Override
212 public boolean equals(Object obj)
213 {
214 if (obj == null)
215 {
216 return false;
217 }
218 if (obj instanceof HelixProperty)
219 {
220 HelixProperty that = (HelixProperty) obj;
221 if (that.getRecord() != null)
222 {
223 return that.getRecord().equals(this.getRecord());
224 }
225 }
226 return false;
227 }
228 }