| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 38 | |
|
| 39 | |
|
| 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 | |
|
| 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 | |
|
| 105 | |
|
| 106 | |
|
| 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 | |
|
| 118 | |
|
| 119 | |
|
| 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 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 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 | |
} |