Package net.sf.ezmorph
Class MorpherRegistry
- java.lang.Object
-
- net.sf.ezmorph.MorpherRegistry
-
- All Implemented Interfaces:
java.io.Serializable
public class MorpherRegistry extends java.lang.Object implements java.io.Serializable
Convenient class that manages Morphers.
A MorpherRehistry manages a group of Morphers. A Morpher will always be associated with a target class, it is possible to have several Morphers registered for a target class, if this is the case, the first Morpher will be used when performing a conversion and no specific Morpher is selected in advance.
MorphUtils
may be used to register standard Morphers for primitive types and primitive wrappers, as well as arrays of those types.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map
morphers
private static long
serialVersionUID
-
Constructor Summary
Constructors Constructor Description MorpherRegistry()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Deregisters all morphers.void
clear(java.lang.Class type)
Deregister all Morphers of a type.void
deregisterMorpher(Morpher morpher)
Deregister the specified Morpher.
The registry will remove the targetClass
from the morphers Map if it has no other registered morphers.Morpher
getMorpherFor(java.lang.Class clazz)
Returns a morpher forclazz
.
If several morphers are found for that class, it returns the first.Morpher[]
getMorphersFor(java.lang.Class clazz)
Returns all morphers forclazz
.
If no Morphers are found it will return an array containing the IdentityObjectMorpher.java.lang.Object
morph(java.lang.Class target, java.lang.Object value)
Morphs and object to the specified target class.
This method uses reflection to invoke primitive Morphers and Morphers that do not implement ObjectMorpher.void
registerMorpher(Morpher morpher)
Register a Morpher for a targetClass
.
The target class is the class this Morpher morphs to.void
registerMorpher(Morpher morpher, boolean override)
Register a Morpher for a targetClass
.
The target class is the class this Morpher morphs to.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
morphers
private java.util.Map morphers
-
-
Method Detail
-
clear
public void clear()
Deregisters all morphers.
-
clear
public void clear(java.lang.Class type)
Deregister all Morphers of a type.- Parameters:
class
- the target type the Morphers morph to
-
deregisterMorpher
public void deregisterMorpher(Morpher morpher)
Deregister the specified Morpher.
The registry will remove the targetClass
from the morphers Map if it has no other registered morphers.- Parameters:
morpher
- the target Morpher to remove
-
getMorpherFor
public Morpher getMorpherFor(java.lang.Class clazz)
Returns a morpher forclazz
.
If several morphers are found for that class, it returns the first. If no Morpher is found it will return the IdentityObjectMorpher.- Parameters:
clazz
- the target class for which a Morpher may be associated
-
getMorphersFor
public Morpher[] getMorphersFor(java.lang.Class clazz)
Returns all morphers forclazz
.
If no Morphers are found it will return an array containing the IdentityObjectMorpher.- Parameters:
clazz
- the target class for which a Morpher or Morphers may be associated
-
morph
public java.lang.Object morph(java.lang.Class target, java.lang.Object value)
Morphs and object to the specified target class.
This method uses reflection to invoke primitive Morphers and Morphers that do not implement ObjectMorpher.- Parameters:
target
- the target class to morph tovalue
- the value to morph- Returns:
- an instance of the target class if a suitable Morpher was found
- Throws:
MorphException
- if an error occurs during the conversion
-
registerMorpher
public void registerMorpher(Morpher morpher)
Register a Morpher for a targetClass
.
The target class is the class this Morpher morphs to. If there are another morphers registered to that class, it will be appended to a List.- Parameters:
morpher
- a Morpher to register. The methodmorphsTo()
is used to associate the Morpher to a target Class
-
registerMorpher
public void registerMorpher(Morpher morpher, boolean override)
Register a Morpher for a targetClass
.
The target class is the class this Morpher morphs to. If there are another morphers registered to that class, it will be appended to a List.- Parameters:
morpher
- a Morpher to register. The methodmorphsTo()
is used to associate the Morpher to a target Classoverride
- if registering teh Morpher should override all previously registered morphers for the target type
-
-