info.aduna.lang.reflect
Class KeyedObjectFactory<KEY,TYPE>

java.lang.Object
  extended by info.aduna.lang.reflect.KeyedObjectFactory<KEY,TYPE>

public class KeyedObjectFactory<KEY,TYPE>
extends Object

A factory for objects of a specific abstract type (or interface) where the actual (runtime) type of the object is specified by a key. The factory requires all registered types to have a public constructor whose parameters can be specified upon creation of the factory.


Constructor Summary
KeyedObjectFactory(Class<?>... paramTypes)
           
 
Method Summary
 void addType(KEY key, Class<? extends TYPE> type)
          Associates a type with a specific key.
 TYPE createInstance(KEY key, Object... initArgs)
          Creates a new instance of the type associated with the specified key.
 Class<? extends TYPE> getType(KEY key)
          Gets the type that is associated with the specified key.
 void removeType(KEY key)
          Removes the association between the specified key and the type that is currently associated with that key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KeyedObjectFactory

public KeyedObjectFactory(Class<?>... paramTypes)
Method Detail

addType

public void addType(KEY key,
                    Class<? extends TYPE> type)
             throws SecurityException
Associates a type with a specific key. The specified type must be a public non-abstract class and it must have a public constructor whose parameters match the parameters specified when this factory was created.

Parameters:
key - The key to be associated with the type.
type - The type class.
Throws:
IllegalArgumentException - If the specified type is an interface or abstract class, if it isn't public, or if it doesn't have the required public constructor.
SecurityException - If the factory is not allowed to access the type's constructor.

removeType

public void removeType(KEY key)
Removes the association between the specified key and the type that is currently associated with that key.

Parameters:
key - A key specifying which key-type association to remove.

getType

public Class<? extends TYPE> getType(KEY key)
Gets the type that is associated with the specified key.

Parameters:
key - The key to get the type for.
Returns:
The type that is associated with the specified key, or null if no such type exists.

createInstance

public TYPE createInstance(KEY key,
                           Object... initArgs)
                    throws NoSuchTypeException,
                           TypeInstantiationException
Creates a new instance of the type associated with the specified key.

Parameters:
key - A key indicating the type of the instance that should be created.
Throws:
NoSuchTypeException - If the specified key is not associated with any type.
TypeInstantiationException - If, for some reason, the factory failed to create an instance of the requested type.


Copyright © 1997-2008 Aduna. All Rights Reserved.