Coverage Report - be.objectify.led.ObjectFactoryRegistry
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectFactoryRegistry
100%
46/46
100%
6/6
1.75
 
 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  1
     private static final Logger LOGGER = LoggerFactory.getLogger(ObjectFactoryRegistry.class);
 44  
 
 45  117
     private final Map<Class, ObjectFactory> objectFactories = new HashMap<Class, ObjectFactory>();
 46  
 
 47  1
     private static final Map<Class, Class> PRIMITIVE_TO_WRAPPER_MAP = new HashMap<Class, Class>();
 48  
 
 49  
     static
 50  
     {
 51  1
         PRIMITIVE_TO_WRAPPER_MAP.put(boolean.class,
 52  
                                      Boolean.class);
 53  1
         PRIMITIVE_TO_WRAPPER_MAP.put(byte.class,
 54  
                                      Byte.class);
 55  1
         PRIMITIVE_TO_WRAPPER_MAP.put(char.class,
 56  
                                      Character.class);
 57  1
         PRIMITIVE_TO_WRAPPER_MAP.put(double.class,
 58  
                                      Double.class);
 59  1
         PRIMITIVE_TO_WRAPPER_MAP.put(float.class,
 60  
                                      Float.class);
 61  1
         PRIMITIVE_TO_WRAPPER_MAP.put(int.class,
 62  
                                      Integer.class);
 63  1
         PRIMITIVE_TO_WRAPPER_MAP.put(long.class,
 64  
                                      Long.class);
 65  1
         PRIMITIVE_TO_WRAPPER_MAP.put(short.class,
 66  
                                      Short.class);
 67  1
     }
 68  
 
 69  
     /**
 70  
      * Initialises a new instance of this class by registering the default object factories.
 71  
      */
 72  
     public ObjectFactoryRegistry()
 73  117
     {
 74  117
         ObjectFactory<Boolean> booleanFactory = new BooleanFactory();
 75  117
         objectFactories.put(booleanFactory.getBoundClass(),
 76  
                             booleanFactory);
 77  117
         ObjectFactory<Byte> byteFactory = new ByteFactory();
 78  117
         objectFactories.put(byteFactory.getBoundClass(),
 79  
                             byteFactory);
 80  117
         ObjectFactory<Character> characterFactory = new CharacterFactory();
 81  117
         objectFactories.put(characterFactory.getBoundClass(),
 82  
                             characterFactory);
 83  117
         ObjectFactory<Double> doubleFactory = new DoubleFactory();
 84  117
         objectFactories.put(doubleFactory.getBoundClass(),
 85  
                             doubleFactory);
 86  117
         ObjectFactory<Float> floatFactory = new FloatFactory();
 87  117
         objectFactories.put(floatFactory.getBoundClass(),
 88  
                             floatFactory);
 89  117
         ObjectFactory<Integer> integerFactory = new IntegerFactory();
 90  117
         objectFactories.put(integerFactory.getBoundClass(),
 91  
                             integerFactory);
 92  117
         ObjectFactory<Long> longFactory = new LongFactory();
 93  117
         objectFactories.put(longFactory.getBoundClass(),
 94  
                             longFactory);
 95  117
         ObjectFactory<Short> shortFactory = new ShortFactory();
 96  117
         objectFactories.put(shortFactory.getBoundClass(),
 97  
                             shortFactory);
 98  117
         ObjectFactory<String> stringFactory = new StringFactory();
 99  117
         objectFactories.put(stringFactory.getBoundClass(),
 100  
                             stringFactory);
 101  117
     }
 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  3
         ContractUtils.nonNull(objectFactories,
 111  
                                    "objectFactories");
 112  
 
 113  2
         register(Arrays.asList(objectFactories));
 114  2
     }
 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  3
         ContractUtils.nonNull(objectFactories,
 124  
                                    "objectFactories");
 125  
 
 126  2
         for (ObjectFactory objectFactory : objectFactories)
 127  
         {
 128  2
             this.objectFactories.put(objectFactory.getBoundClass(),
 129  
                                      objectFactory);
 130  
         }
 131  2
     }
 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  140
         if (clazz.isPrimitive())
 142  
         {
 143  22
             clazz = PRIMITIVE_TO_WRAPPER_MAP.get(clazz);
 144  
         }
 145  
 
 146  140
         ObjectFactory factory = objectFactories.get(clazz);
 147  
 
 148  140
         if (factory != null)
 149  
         {
 150  119
             LOGGER.debug("Found object factory [{}] for class [{}]",
 151  
                          factory,
 152  
                          clazz.getCanonicalName());
 153  
         }
 154  
         else
 155  
         {
 156  21
             LOGGER.debug("Could not find object factory for {}",
 157  
                          clazz.getCanonicalName());
 158  
         }
 159  
 
 160  140
         return factory;
 161  
     }
 162  
 }