View Javadoc

1   /*
2    * Copyright 2009-2010 Steve Chaloner
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package be.objectify.led;
17  
18  import be.objectify.led.factory.object.BooleanFactory;
19  import be.objectify.led.factory.object.ByteFactory;
20  import be.objectify.led.factory.object.CharacterFactory;
21  import be.objectify.led.factory.object.DoubleFactory;
22  import be.objectify.led.factory.object.FloatFactory;
23  import be.objectify.led.factory.object.IntegerFactory;
24  import be.objectify.led.factory.object.LongFactory;
25  import be.objectify.led.factory.object.ShortFactory;
26  import be.objectify.led.factory.object.StringFactory;
27  import be.objectify.led.util.ContractUtils;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import java.util.Arrays;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * Registry of object factories for creating objects of the correct type based on the field.
38   *
39   * @author Steve Chaloner
40   */
41  public class ObjectFactoryRegistry
42  {
43      private static final Logger LOGGER = LoggerFactory.getLogger(ObjectFactoryRegistry.class);
44  
45      private final Map<Class, ObjectFactory> objectFactories = new HashMap<Class, ObjectFactory>();
46  
47      private static final Map<Class, Class> PRIMITIVE_TO_WRAPPER_MAP = new HashMap<Class, Class>();
48  
49      static
50      {
51          PRIMITIVE_TO_WRAPPER_MAP.put(boolean.class,
52                                       Boolean.class);
53          PRIMITIVE_TO_WRAPPER_MAP.put(byte.class,
54                                       Byte.class);
55          PRIMITIVE_TO_WRAPPER_MAP.put(char.class,
56                                       Character.class);
57          PRIMITIVE_TO_WRAPPER_MAP.put(double.class,
58                                       Double.class);
59          PRIMITIVE_TO_WRAPPER_MAP.put(float.class,
60                                       Float.class);
61          PRIMITIVE_TO_WRAPPER_MAP.put(int.class,
62                                       Integer.class);
63          PRIMITIVE_TO_WRAPPER_MAP.put(long.class,
64                                       Long.class);
65          PRIMITIVE_TO_WRAPPER_MAP.put(short.class,
66                                       Short.class);
67      }
68  
69      /**
70       * Initialises a new instance of this class by registering the default object factories.
71       */
72      public ObjectFactoryRegistry()
73      {
74          ObjectFactory<Boolean> booleanFactory = new BooleanFactory();
75          objectFactories.put(booleanFactory.getBoundClass(),
76                              booleanFactory);
77          ObjectFactory<Byte> byteFactory = new ByteFactory();
78          objectFactories.put(byteFactory.getBoundClass(),
79                              byteFactory);
80          ObjectFactory<Character> characterFactory = new CharacterFactory();
81          objectFactories.put(characterFactory.getBoundClass(),
82                              characterFactory);
83          ObjectFactory<Double> doubleFactory = new DoubleFactory();
84          objectFactories.put(doubleFactory.getBoundClass(),
85                              doubleFactory);
86          ObjectFactory<Float> floatFactory = new FloatFactory();
87          objectFactories.put(floatFactory.getBoundClass(),
88                              floatFactory);
89          ObjectFactory<Integer> integerFactory = new IntegerFactory();
90          objectFactories.put(integerFactory.getBoundClass(),
91                              integerFactory);
92          ObjectFactory<Long> longFactory = new LongFactory();
93          objectFactories.put(longFactory.getBoundClass(),
94                              longFactory);
95          ObjectFactory<Short> shortFactory = new ShortFactory();
96          objectFactories.put(shortFactory.getBoundClass(),
97                              shortFactory);
98          ObjectFactory<String> stringFactory = new StringFactory();
99          objectFactories.put(stringFactory.getBoundClass(),
100                             stringFactory);
101     }
102 
103     /**
104      * Registers the object factories with the registry.
105      *
106      * @param objectFactories the object factories.  None of the factories can be null.
107      */
108     public void register(ObjectFactory... objectFactories)
109     {
110         ContractUtils.nonNull(objectFactories,
111                                    "objectFactories");
112 
113         register(Arrays.asList(objectFactories));
114     }
115 
116     /**
117      * Registers the object factories with the registry.
118      *
119      * @param objectFactories the object factories to register.  Must not be null; none of contents
120      */
121     public void register(List<ObjectFactory> objectFactories)
122     {
123         ContractUtils.nonNull(objectFactories,
124                                    "objectFactories");
125 
126         for (ObjectFactory objectFactory : objectFactories)
127         {
128             this.objectFactories.put(objectFactory.getBoundClass(),
129                                      objectFactory);
130         }
131     }
132 
133     /**
134      * Gets the object factory for the given class.
135      *
136      * @param clazz the class of the required object
137      * @return the object factory, or null if no factory is registered for the class
138      */
139     public ObjectFactory getFactory(Class clazz)
140     {
141         if (clazz.isPrimitive())
142         {
143             clazz = PRIMITIVE_TO_WRAPPER_MAP.get(clazz);
144         }
145 
146         ObjectFactory factory = objectFactories.get(clazz);
147 
148         if (factory != null)
149         {
150             LOGGER.debug("Found object factory [{}] for class [{}]",
151                          factory,
152                          clazz.getCanonicalName());
153         }
154         else
155         {
156             LOGGER.debug("Could not find object factory for {}",
157                          clazz.getCanonicalName());
158         }
159 
160         return factory;
161     }
162 }