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