org.apache.commons.mapper.util
Class ObjectFactory

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

public class ObjectFactory
extends java.lang.Object
implements java.lang.Cloneable, java.io.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  java.util.Map nameMap
          Stores "name"="qualified class name".
 
Constructor Summary
ObjectFactory(java.util.Map map)
          Create an ObjectFactory with the given mapping of names to fully qualified class names.
 
Method Summary
 java.lang.Object clone()
          Returns a cloned instance of this ObjectFactory.
static java.lang.Object construct(java.lang.String className)
          Convenience method that creates an object of the class given as a string.
 java.lang.Object create(java.lang.String name)
          Returns an object of the class associated with the given command.
 boolean equals(java.lang.Object o)
          Returns true if the ObjectFactory's maps are equal.
 java.util.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 java.util.Map nameMap
Stores "name"="qualified class name".

Constructor Detail

ObjectFactory

public ObjectFactory(java.util.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 java.lang.Object construct(java.lang.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:
java.lang.IllegalArgumentException - if the Class for the given name could not be found.

create

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

Throws:
java.lang.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 java.util.Map getMap()
Gets a copy of the Map this factory is using to construct instances of classes.


equals

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


clone

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


hashCode

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



Copyright © 2003-2005 The Apache Software Foundation. All Rights Reserved.