View Javadoc

1   /*
2    ORG Usurper is a random value object generator library 
3    Copyright (C) 2007  Pierre-Antoine Grégoire
4    
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9    
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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   * This is the mutable version of the class setting up the behaviour of the UsurperGenerator. Default behaviour is to fail if no handler is registered for a given property of the Bean.<br>
41   * Default type handlers are already registered (but can be overriden) for :<br>
42   * <ul>
43   * <li>primitive types (int, boolean,...etc)</li>
44   * <li>corresponding types(Integer,Boolean,...etc)</li>
45   * <li>some other types (java.lang.String,java.util.Date)</li>
46   * </ul>
47   * <br>
48   * Default ArrayHandler and EnumHandler are the one provided with Usurper. <b>You are not encouraged to modify these two handlers.</b><br>
49   * 
50   * Default collections count callback returns the default value: 10.<br>
51   * 
52   * @author pagregoire
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       * Instantiates a new mutable usurper generator setup using default configuration:<br>
67       * <ul>
68       *  <li>Using default property handlers for basic types (see default handlers' package), SQL types handlers ,and Collections handlers.
69       *  <li>OnMissingHandlers: FAIL
70       *  <li>PropertyWritingMechanism.USE_SETTERS
71       *  <li>CountCallback: always returns the default entry count defined in {@link UsurperGeneratorConstants}.
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       * Instantiates a new mutable usurper generator setup using the passed setup as a configuration.
92       * 
93       * @param immutableSetup the immutable setup
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      * @see org.org.usurper.setup.IUsurperGeneratorSetup#getPropertyTypeHandlersMap()
107      */
108     public Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> getPropertyTypeHandlersMap() {
109         return propertyTypeHandlersMap;
110     }
111 
112     /**
113      * Sets the property type handlers map.
114      * 
115      * @param propertyTypeHandlersMap the property type handlers map
116      */
117     public void setPropertyTypeHandlersMap(Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> propertyTypeHandlersMap) {
118         this.propertyTypeHandlersMap = propertyTypeHandlersMap;
119     }
120 
121     /**
122      * @see org.org.usurper.setup.IUsurperGeneratorSetup#getSpecificPropertyHandlersMap()
123      */
124     public Map<SpecificPropertyDefinition, AbstractSpecificPropertyHandler> getSpecificPropertyHandlersMap() {
125         return specificPropertyHandlersMap;
126     }
127 
128     /**
129      * This method allows the user to register a Handler for Properties of determinated java types.<br>
130      * Only one handler can be registered for a given java type at a time.<br>
131      * Registering a handler when one is already registered for a given type overrides the preceding one.<br>
132      * 
133      * @param typeHandler the handler to register (AbstractPropertyTypeHandler)
134      */
135     public void registerPropertyTypeHandler(AbstractPropertyTypeHandler typeHandler) {
136         for (PropertyTypeDefinition handledType : typeHandler.getHandledTypes()) {
137             propertyTypeHandlersMap.put(handledType, typeHandler);
138         }
139     }
140 
141     /**
142      * This method allows the user to register many Handlers for Properties of determinated java types.<br>
143      * Only one handler can be registered for a given java type at a time.<br>
144      * Registering a handler when one is already registered for a given type overrides the preceding one.<br>
145      * 
146      * @param typeHandlers the handler to register (Set&lt;AbstractPropertyTypeHandler&gt;)
147      */
148     public void registerPropertyTypeHandlers(Set<AbstractPropertyTypeHandler> typeHandlers) {
149         for (AbstractPropertyTypeHandler typeHandler : typeHandlers) {
150             registerPropertyTypeHandler(typeHandler);
151         }
152     }
153 
154     /**
155      * @see org.org.usurper.setup.IUsurperGeneratorSetup#hasPropertyTypeHandler(org.org.usurper.model.PropertyTypeDefinition)
156      */
157     public boolean hasPropertyTypeHandler(PropertyTypeDefinition type) {
158         return this.propertyTypeHandlersMap.containsKey(type);
159     }
160 
161 
162     /**
163      * @see org.org.usurper.setup.IUsurperGeneratorSetup#getPropertyTypeHandler(org.org.usurper.model.PropertyTypeDefinition)
164      */
165     public AbstractPropertyTypeHandler getPropertyTypeHandler(PropertyTypeDefinition handledType) {
166         return this.propertyTypeHandlersMap.get(handledType);
167     }
168 
169     /**
170      * This method allows the user to register a Handler for a given property.<br>
171      * Only one handler can be registered for a given property at a time.<br>
172      * Registering a handler when one is already registered for a given property overrides the preceding one.<br>
173      * 
174      * @param propertyHandler the handler to register (AbstractSpecificPropertyHandler)
175      */
176     public void registerSpecificPropertyHandler(AbstractSpecificPropertyHandler propertyHandler) {
177         this.specificPropertyHandlersMap.put(propertyHandler.getTargetProperty(), propertyHandler);
178     }
179 
180  
181     /**
182      * @see org.org.usurper.setup.IUsurperGeneratorSetup#getAllHandlers()
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      * Gets the immutable version of this mutable setup.
193      * 
194      * @return the immutable
195      */
196     public ImmutableUsurperGeneratorSetup getImmutable() {
197         return new ImmutableUsurperGeneratorSetup(propertyTypeHandlersMap, specificPropertyHandlersMap, arrayHandler, enumHandler, onMissingHandlers, propertyWritingMechanism, countCallback);
198     }
199 
200     /**
201      * @see org.org.usurper.setup.IUsurperGeneratorSetup#getArrayHandler()
202      */
203     public ArrayHandler getArrayHandler() {
204         return arrayHandler;
205     }
206 
207     /**
208      * Sets the array handler.
209      * 
210      * @param arrayHandler the new array handler
211      */
212     public void setArrayHandler(ArrayHandler arrayHandler) {
213         this.arrayHandler = arrayHandler;
214     }
215 
216     /**
217      * @see org.org.usurper.setup.IUsurperGeneratorSetup#getEnumHandler()
218      */
219     public EnumHandler getEnumHandler() {
220         return enumHandler;
221     }
222 
223     /**
224      * Sets the enum handler.
225      * 
226      * @param enumHandler the new enum handler
227      */
228     public void setEnumHandler(EnumHandler enumHandler) {
229         this.enumHandler = enumHandler;
230     }
231 
232     /**
233      * @see org.org.usurper.setup.IUsurperGeneratorSetup#getOnMissingHandlers()
234      */
235     public OnMissingHandlers getOnMissingHandlers() {
236         return onMissingHandlers;
237     }
238 
239     /**
240      * Sets On missing handlers behavior using the String values of the enum type.
241      * 
242      * @param onMissingHandlers the new on missing handlers
243      */
244     public void setOnMissingHandlers(String onMissingHandlers) {
245         this.onMissingHandlers = OnMissingHandlers.valueOf(onMissingHandlers);
246     }
247 
248     /**
249      * Sets On missing handlers behavior using the enum type.
250      * 
251      * @param onMissingHandlers the on missing handlers
252      */
253     public void onMissingHandlers(OnMissingHandlers onMissingHandlers) {
254         this.onMissingHandlers = onMissingHandlers;
255     }
256 
257     /**
258      * @see org.org.usurper.setup.IUsurperGeneratorSetup#getPropertyWritingMechanism()
259      */
260     public PropertyWritingMechanism getPropertyWritingMechanism() {
261         return propertyWritingMechanism;
262     }
263 
264     /**
265      * Sets the property writing mechanism using the String values of the enum type.
266      * 
267      * @param propertyWritingMechanism the new property writing mechanism
268      */
269     public void setPropertyWritingMechanism(String propertyWritingMechanism) {
270         this.propertyWritingMechanism = PropertyWritingMechanism.valueOf(propertyWritingMechanism);
271     }
272 
273     /**
274      * Sets the property writing mechanism using the enum type.
275      * 
276      * @param propertyWritingMechanism the property writing mechanism
277      */
278     public void usePropertyWritingMechanism(PropertyWritingMechanism propertyWritingMechanism) {
279         this.propertyWritingMechanism = propertyWritingMechanism;
280     }
281 
282     /**
283      * @see org.org.usurper.setup.IUsurperGeneratorSetup#getCountCallback()
284      */
285     public ICountCallback getCountCallback() {
286         return countCallback;
287     }
288 
289     /**
290      * Sets the count callback.
291      * 
292      * @param countCallback the new count callback
293      */
294     public void setCountCallback(ICountCallback countCallback) {
295         this.countCallback = countCallback;
296     }
297 
298     /**
299      * @see org.org.usurper.setup.IUsurperGeneratorSetup#hasSpecificPropertyHandler(org.org.usurper.model.SpecificPropertyDefinition)
300      */
301     public boolean hasSpecificPropertyHandler(SpecificPropertyDefinition specificPropertyDefinition) {
302         return this.specificPropertyHandlersMap.containsKey(specificPropertyDefinition);
303     }
304 
305     /**
306      * @see org.org.usurper.setup.IUsurperGeneratorSetup#getSpecificPropertyHandler(org.org.usurper.model.SpecificPropertyDefinition)
307      */
308     public AbstractSpecificPropertyHandler getSpecificPropertyHandler(SpecificPropertyDefinition specificPropertyDefinition) {
309         return this.specificPropertyHandlersMap.get(specificPropertyDefinition);
310     }
311     
312     /**
313      * Sets All the handlers at once.
314      * 
315      * @param handlers the handlers
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      * @see org.org.usurper.setup.IUsurperGeneratorSetup#toStringRepresentation()
340      */
341     public String toStringRepresentation() {
342         return UsurperGeneratorSetupUtils.buildStringRepresentation(this);
343     }
344 
345     /**
346      * @see java.lang.Object#toString()
347      */
348     public String toString() {
349         return toStringRepresentation();
350     }
351 }