be.objectify.led.ObjectFactoryRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of objectify-led Show documentation
Show all versions of objectify-led Show documentation
objectify-led is a small Java library for binding object or class properties at runtime using annotations, reducing boilerplate code.
Values can be taken from the System, from properties files, any arbitrary source in fact, and automatically set on properties.
/*
* Copyright 2009-2010 Steve Chaloner
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package be.objectify.led;
import be.objectify.led.factory.object.BooleanFactory;
import be.objectify.led.factory.object.ByteFactory;
import be.objectify.led.factory.object.CharacterFactory;
import be.objectify.led.factory.object.DoubleFactory;
import be.objectify.led.factory.object.FloatFactory;
import be.objectify.led.factory.object.IntegerFactory;
import be.objectify.led.factory.object.LongFactory;
import be.objectify.led.factory.object.ShortFactory;
import be.objectify.led.factory.object.StringFactory;
import be.objectify.led.util.ContractUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Registry of object factories for creating objects of the correct type based on the field.
*
* @author Steve Chaloner
*/
public class ObjectFactoryRegistry
{
private static final Logger LOGGER = LoggerFactory.getLogger(ObjectFactoryRegistry.class);
private final Map objectFactories = new HashMap();
private static final Map PRIMITIVE_TO_WRAPPER_MAP = new HashMap();
static
{
PRIMITIVE_TO_WRAPPER_MAP.put(boolean.class,
Boolean.class);
PRIMITIVE_TO_WRAPPER_MAP.put(byte.class,
Byte.class);
PRIMITIVE_TO_WRAPPER_MAP.put(char.class,
Character.class);
PRIMITIVE_TO_WRAPPER_MAP.put(double.class,
Double.class);
PRIMITIVE_TO_WRAPPER_MAP.put(float.class,
Float.class);
PRIMITIVE_TO_WRAPPER_MAP.put(int.class,
Integer.class);
PRIMITIVE_TO_WRAPPER_MAP.put(long.class,
Long.class);
PRIMITIVE_TO_WRAPPER_MAP.put(short.class,
Short.class);
}
/**
* Initialises a new instance of this class by registering the default object factories.
*/
public ObjectFactoryRegistry()
{
ObjectFactory booleanFactory = new BooleanFactory();
objectFactories.put(booleanFactory.getBoundClass(),
booleanFactory);
ObjectFactory byteFactory = new ByteFactory();
objectFactories.put(byteFactory.getBoundClass(),
byteFactory);
ObjectFactory characterFactory = new CharacterFactory();
objectFactories.put(characterFactory.getBoundClass(),
characterFactory);
ObjectFactory doubleFactory = new DoubleFactory();
objectFactories.put(doubleFactory.getBoundClass(),
doubleFactory);
ObjectFactory floatFactory = new FloatFactory();
objectFactories.put(floatFactory.getBoundClass(),
floatFactory);
ObjectFactory integerFactory = new IntegerFactory();
objectFactories.put(integerFactory.getBoundClass(),
integerFactory);
ObjectFactory longFactory = new LongFactory();
objectFactories.put(longFactory.getBoundClass(),
longFactory);
ObjectFactory shortFactory = new ShortFactory();
objectFactories.put(shortFactory.getBoundClass(),
shortFactory);
ObjectFactory stringFactory = new StringFactory();
objectFactories.put(stringFactory.getBoundClass(),
stringFactory);
}
/**
* Registers the object factories with the registry.
*
* @param objectFactories the object factories. None of the factories can be null.
*/
public void register(ObjectFactory... objectFactories)
{
ContractUtils.nonNull(objectFactories,
"objectFactories");
register(Arrays.asList(objectFactories));
}
/**
* Registers the object factories with the registry.
*
* @param objectFactories the object factories to register. Must not be null; none of contents
*/
public void register(List objectFactories)
{
ContractUtils.nonNull(objectFactories,
"objectFactories");
for (ObjectFactory objectFactory : objectFactories)
{
this.objectFactories.put(objectFactory.getBoundClass(),
objectFactory);
}
}
/**
* Gets the object factory for the given class.
*
* @param clazz the class of the required object
* @return the object factory, or null if no factory is registered for the class
*/
public ObjectFactory getFactory(Class clazz)
{
if (clazz.isPrimitive())
{
clazz = PRIMITIVE_TO_WRAPPER_MAP.get(clazz);
}
ObjectFactory factory = objectFactories.get(clazz);
if (factory != null)
{
LOGGER.debug("Found object factory [{}] for class [{}]",
factory,
clazz.getCanonicalName());
}
else
{
LOGGER.debug("Could not find object factory for {}",
clazz.getCanonicalName());
}
return factory;
}
}