|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.mapper.util.ObjectFactory
public class ObjectFactory
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.
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 |
---|
protected Map nameMap
Constructor Detail |
---|
public ObjectFactory(Map map)
map
- A map of logical names to fully qualified class names.Method Detail |
---|
public static Object construct(String className)
className
- The fully qualified class name of the object to be
created.
IllegalArgumentException
- if the Class for the given name could
not be found.public Object create(String name)
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.public Map getMap()
Map
this factory is using to
construct instances of classes.
public boolean equals(Object o)
equals
in class Object
public Object clone()
clone
in class Object
public int hashCode()
hashCode
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |