org.apache.commons.mapper.util
Class ObjectFactory

java.lang.Object
  extended by org.apache.commons.mapper.util.ObjectFactory
All Implemented Interfaces:
Serializable, Cloneable

public class ObjectFactory
extends Object
implements Cloneable, Serializable

ObjectFactory maps string names to classes to be instantiated. Given a name it will construct a new object of the mapped type using reflection. Pass in a Map with entries in the form "logical name"="fully qualified class name". Both key and value must be Strings.

For quick instantiation from a String, use the class method ObjectFactory.construct(String).

The classes to be constructed must have a public no-arg constructor. An Exception thrown during the loading of a Class or creation of an Object is considered a programmer error and is converted to an IllegalArgumentException. This greatly simplifies client code and indicates a misconfiguration of the Map passed to this factory. This class is thread-safe.

Example:

 Map map = new HashMap();
 map.put("list", "java.util.ArrayList");
 map.put("set", "java.util.HashSet");
 
 ObjectFactory factory = new ObjectFactory(map);
 Set mySet = (Set) factory.construct("set");
 

Using ObjectFactory without a map:
Set mySet = (Set) ObjectFactory.construct("java.util.HashSet");

This class is useful as a backing class for higher level factory classes. The higher level class can use ObjectFactory to do the mapping and creation work while it implements factory specific semantics and caching.

See Also:
Serialized Form

Field Summary
protected  Map nameMap
          Stores "name"="qualified class name".
 
Constructor Summary
ObjectFactory(Map map)
          Create an ObjectFactory with the given mapping of names to fully qualified class names.
 
Method Summary
 Object clone()
          Returns a cloned instance of this ObjectFactory.
static Object construct(String className)
          Convenience method that creates an object of the class given as a string.
 Object create(String name)
          Returns an object of the class associated with the given command.
 boolean equals(Object o)
          Returns true if the ObjectFactory's maps are equal.
 Map getMap()
          Gets a copy of the Map this factory is using to construct instances of classes.
 int hashCode()
          ObjectFactory's hashcode is based on its underlying Map's hashcodes.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nameMap

protected Map nameMap
Stores "name"="qualified class name".

Constructor Detail

ObjectFactory

public ObjectFactory(Map map)
Create an ObjectFactory with the given mapping of names to fully qualified class names.

Parameters:
map - A map of logical names to fully qualified class names.
Method Detail

construct

public static Object construct(String className)
Convenience method that creates an object of the class given as a string.

Parameters:
className - The fully qualified class name of the object to be created.
Throws:
IllegalArgumentException - if the Class for the given name could not be found.

create

public Object create(String name)
Returns an object of the class associated with the given command.

Throws:
IllegalArgumentException - if given name was not found in the map, the Class for the name could not be found, or the Class is missing a no-arg constructor.

getMap

public Map getMap()
Gets a copy of the Map this factory is using to construct instances of classes.


equals

public boolean equals(Object o)
Returns true if the ObjectFactory's maps are equal.

Overrides:
equals in class Object

clone

public Object clone()
Returns a cloned instance of this ObjectFactory.

Overrides:
clone in class Object

hashCode

public int hashCode()
ObjectFactory's hashcode is based on its underlying Map's hashcodes.

Overrides:
hashCode in class Object


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.