1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.org.usurper.setup;
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.org.usurper.handlers.IHandler;
26 import org.org.usurper.handlers.basic.AbstractPropertyTypeHandler;
27 import org.org.usurper.handlers.basic.AbstractSpecificPropertyHandler;
28 import org.org.usurper.handlers.basic.ArrayHandler;
29 import org.org.usurper.handlers.basic.EnumHandler;
30 import org.org.usurper.model.IHandledEntity;
31 import org.org.usurper.model.ITargetDefinition;
32 import org.org.usurper.model.PropertyTypeDefinition;
33 import org.org.usurper.model.SpecificPropertyDefinition;
34 import org.org.usurper.setup.constants.OnMissingHandlers;
35 import org.org.usurper.setup.constants.PropertyWritingMechanism;
36 import org.org.usurper.setup.constants.UsurperGeneratorConstants;
37 import org.org.usurper.utils.UsurperGeneratorSetupUtils;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class UsurperGeneratorSetup implements IUsurperGeneratorSetup {
56
57 Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> propertyTypeHandlersMap;
58 Map<SpecificPropertyDefinition, AbstractSpecificPropertyHandler> specificPropertyHandlersMap;
59 private ArrayHandler arrayHandler;
60 private EnumHandler enumHandler;
61 private OnMissingHandlers onMissingHandlers;
62 private PropertyWritingMechanism propertyWritingMechanism;
63 private ICountCallback countCallback;
64
65
66
67
68
69
70
71
72
73 public UsurperGeneratorSetup() {
74 this.propertyTypeHandlersMap = new HashMap<PropertyTypeDefinition, AbstractPropertyTypeHandler>(10);
75 this.specificPropertyHandlersMap = new HashMap<SpecificPropertyDefinition, AbstractSpecificPropertyHandler>();
76 this.arrayHandler = new ArrayHandler();
77 this.enumHandler = new EnumHandler();
78 this.onMissingHandlers = OnMissingHandlers.FAIL;
79 this.propertyWritingMechanism = PropertyWritingMechanism.USE_SETTERS;
80 this.registerPropertyTypeHandlers(UsurperGeneratorConstants.DEFAULT_PROPERTY_HANDLERS);
81 this.countCallback = new ICountCallback() {
82
83 public Integer determineCount(IHandledEntity handledEntity) {
84 return UsurperGeneratorConstants.DEFAULT_ENTRIES_COUNT;
85 }
86
87 };
88 }
89
90
91
92
93
94
95 public UsurperGeneratorSetup(IUsurperGeneratorSetup immutableSetup) {
96 this.propertyTypeHandlersMap = new HashMap<PropertyTypeDefinition, AbstractPropertyTypeHandler>(immutableSetup.getPropertyTypeHandlersMap());
97 this.specificPropertyHandlersMap = new HashMap<SpecificPropertyDefinition, AbstractSpecificPropertyHandler>(immutableSetup.getSpecificPropertyHandlersMap());
98 this.arrayHandler = immutableSetup.getArrayHandler();
99 this.enumHandler = immutableSetup.getEnumHandler();
100 this.onMissingHandlers = immutableSetup.getOnMissingHandlers();
101 this.propertyWritingMechanism = immutableSetup.getPropertyWritingMechanism();
102 this.countCallback = immutableSetup.getCountCallback();
103 }
104
105
106
107
108 public Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> getPropertyTypeHandlersMap() {
109 return propertyTypeHandlersMap;
110 }
111
112
113
114
115
116
117 public void setPropertyTypeHandlersMap(Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> propertyTypeHandlersMap) {
118 this.propertyTypeHandlersMap = propertyTypeHandlersMap;
119 }
120
121
122
123
124 public Map<SpecificPropertyDefinition, AbstractSpecificPropertyHandler> getSpecificPropertyHandlersMap() {
125 return specificPropertyHandlersMap;
126 }
127
128
129
130
131
132
133
134
135 public void registerPropertyTypeHandler(AbstractPropertyTypeHandler typeHandler) {
136 for (PropertyTypeDefinition handledType : typeHandler.getHandledTypes()) {
137 propertyTypeHandlersMap.put(handledType, typeHandler);
138 }
139 }
140
141
142
143
144
145
146
147
148 public void registerPropertyTypeHandlers(Set<AbstractPropertyTypeHandler> typeHandlers) {
149 for (AbstractPropertyTypeHandler typeHandler : typeHandlers) {
150 registerPropertyTypeHandler(typeHandler);
151 }
152 }
153
154
155
156
157 public boolean hasPropertyTypeHandler(PropertyTypeDefinition type) {
158 return this.propertyTypeHandlersMap.containsKey(type);
159 }
160
161
162
163
164
165 public AbstractPropertyTypeHandler getPropertyTypeHandler(PropertyTypeDefinition handledType) {
166 return this.propertyTypeHandlersMap.get(handledType);
167 }
168
169
170
171
172
173
174
175
176 public void registerSpecificPropertyHandler(AbstractSpecificPropertyHandler propertyHandler) {
177 this.specificPropertyHandlersMap.put(propertyHandler.getTargetProperty(), propertyHandler);
178 }
179
180
181
182
183
184 public Map<ITargetDefinition, IHandler> getAllHandlers() {
185 Map<ITargetDefinition, IHandler> result = new HashMap<ITargetDefinition, IHandler>();
186 result.putAll(specificPropertyHandlersMap);
187 result.putAll(propertyTypeHandlersMap);
188 return result;
189 }
190
191
192
193
194
195
196 public ImmutableUsurperGeneratorSetup getImmutable() {
197 return new ImmutableUsurperGeneratorSetup(propertyTypeHandlersMap, specificPropertyHandlersMap, arrayHandler, enumHandler, onMissingHandlers, propertyWritingMechanism, countCallback);
198 }
199
200
201
202
203 public ArrayHandler getArrayHandler() {
204 return arrayHandler;
205 }
206
207
208
209
210
211
212 public void setArrayHandler(ArrayHandler arrayHandler) {
213 this.arrayHandler = arrayHandler;
214 }
215
216
217
218
219 public EnumHandler getEnumHandler() {
220 return enumHandler;
221 }
222
223
224
225
226
227
228 public void setEnumHandler(EnumHandler enumHandler) {
229 this.enumHandler = enumHandler;
230 }
231
232
233
234
235 public OnMissingHandlers getOnMissingHandlers() {
236 return onMissingHandlers;
237 }
238
239
240
241
242
243
244 public void setOnMissingHandlers(String onMissingHandlers) {
245 this.onMissingHandlers = OnMissingHandlers.valueOf(onMissingHandlers);
246 }
247
248
249
250
251
252
253 public void onMissingHandlers(OnMissingHandlers onMissingHandlers) {
254 this.onMissingHandlers = onMissingHandlers;
255 }
256
257
258
259
260 public PropertyWritingMechanism getPropertyWritingMechanism() {
261 return propertyWritingMechanism;
262 }
263
264
265
266
267
268
269 public void setPropertyWritingMechanism(String propertyWritingMechanism) {
270 this.propertyWritingMechanism = PropertyWritingMechanism.valueOf(propertyWritingMechanism);
271 }
272
273
274
275
276
277
278 public void usePropertyWritingMechanism(PropertyWritingMechanism propertyWritingMechanism) {
279 this.propertyWritingMechanism = propertyWritingMechanism;
280 }
281
282
283
284
285 public ICountCallback getCountCallback() {
286 return countCallback;
287 }
288
289
290
291
292
293
294 public void setCountCallback(ICountCallback countCallback) {
295 this.countCallback = countCallback;
296 }
297
298
299
300
301 public boolean hasSpecificPropertyHandler(SpecificPropertyDefinition specificPropertyDefinition) {
302 return this.specificPropertyHandlersMap.containsKey(specificPropertyDefinition);
303 }
304
305
306
307
308 public AbstractSpecificPropertyHandler getSpecificPropertyHandler(SpecificPropertyDefinition specificPropertyDefinition) {
309 return this.specificPropertyHandlersMap.get(specificPropertyDefinition);
310 }
311
312
313
314
315
316
317 public void setAllHandlers(Map<ITargetDefinition, IHandler> handlers) {
318 for (ITargetDefinition key : handlers.keySet()) {
319 IHandler handler = handlers.get(key);
320 if (key instanceof SpecificPropertyDefinition) {
321 if (handler instanceof AbstractSpecificPropertyHandler) {
322 specificPropertyHandlersMap.put((SpecificPropertyDefinition) key, (AbstractSpecificPropertyHandler) handler);
323 } else {
324 throw new ClassCastException("With SpecificPropertyDefinition key :<" + key + "> a " + AbstractSpecificPropertyHandler.class.getName() + " implementation should be defined");
325 }
326 } else if (key instanceof PropertyTypeDefinition) {
327 if (handler instanceof AbstractPropertyTypeHandler) {
328 propertyTypeHandlersMap.put((PropertyTypeDefinition) key, (AbstractPropertyTypeHandler) handler);
329 } else {
330 throw new ClassCastException("With PropertyTypeDefinition key :<" + key + "> a " + AbstractPropertyTypeHandler.class.getName() + " implementation should be defined");
331 }
332 } else {
333 throw new ClassCastException("key should be a " + SpecificPropertyDefinition.class.getName() + " or a " + PropertyTypeDefinition.class.getName() + ", not a" + key.getClass().getName());
334 }
335 }
336 }
337
338
339
340
341 public String toStringRepresentation() {
342 return UsurperGeneratorSetupUtils.buildStringRepresentation(this);
343 }
344
345
346
347
348 public String toString() {
349 return toStringRepresentation();
350 }
351 }