net.sf.morph.transform.transformers
Class SimpleDelegatingTransformer

java.lang.Object
  extended by net.sf.morph.transform.transformers.BaseTransformer
      extended by net.sf.morph.transform.transformers.BaseCompositeTransformer
          extended by net.sf.morph.transform.transformers.SimpleDelegatingTransformer
All Implemented Interfaces:
java.lang.Cloneable, net.sf.composite.Component, net.sf.composite.Composite, net.sf.composite.SimpleComposite, net.sf.composite.SpecializableComposite, net.sf.composite.StrictlyTypedComposite, Converter, Copier, DecoratedConverter, DecoratedCopier, DecoratedTransformer, ExplicitTransformer, ImpreciseTransformer, Transformer
Direct Known Subclasses:
DefaultToBooleanConverter, DefaultToTextConverter, TypeChangingGraphTransformer

public class SimpleDelegatingTransformer
extends BaseCompositeTransformer
implements net.sf.composite.SpecializableComposite, ExplicitTransformer, Transformer, DecoratedCopier, DecoratedConverter, java.lang.Cloneable, ImpreciseTransformer

Delegates transformations to a list of transformers. The transformers are tried in the order in which they appear in the components property of this transformer.

By default this transformer is initialized with a set of transformers that will meet basic needs. This list of transformers is subject to change in future versions of Morph, but we do not anticipate removing transformers, only adding new ones or modifying existing ones. This means if a transformation works in an older version of Morph it will also work in a newer version. A transformation's behavior may in some cases change between releases, but we will avoid this whenever possible.

The default set of transformers includes both converters and copiers. For calls to copy methods, only the copiers will be used. For calls to convert methods, the converters and the copiers will be used, since all Copiers provided by the Morph framework are also Converters.

Any delegates which implement NodeCopier will automatically have this transformer marked as the parent transformer. This is important for performing deep copies of object graphs. Note that it is safe for a SimpleDelegatingTransformer to implement the NodeCopier interface only if all its components do so.

Since:
Dec 12, 2004
Author:
Matt Sgarlata

Field Summary
 
Fields inherited from class net.sf.morph.transform.transformers.BaseCompositeTransformer
components
 
Fields inherited from class net.sf.morph.transform.transformers.BaseTransformer
destinationClasses, log, sourceClasses
 
Fields inherited from interface net.sf.morph.transform.Transformer
TRANSFORMATION_TYPE_CONVERT, TRANSFORMATION_TYPE_COPY
 
Fields inherited from interface net.sf.morph.transform.Transformer
TRANSFORMATION_TYPE_CONVERT, TRANSFORMATION_TYPE_COPY
 
Fields inherited from interface net.sf.morph.transform.Transformer
TRANSFORMATION_TYPE_CONVERT, TRANSFORMATION_TYPE_COPY
 
Fields inherited from interface net.sf.morph.transform.Transformer
TRANSFORMATION_TYPE_CONVERT, TRANSFORMATION_TYPE_COPY
 
Fields inherited from interface net.sf.morph.transform.Transformer
TRANSFORMATION_TYPE_CONVERT, TRANSFORMATION_TYPE_COPY
 
Constructor Summary
SimpleDelegatingTransformer()
          Construct a new SimpleDelegatingTransformer.
SimpleDelegatingTransformer(Transformer[] components)
          Construct a new SimpleDelegatingTransformer.
SimpleDelegatingTransformer(Transformer[] components, boolean appendDefaultComponents)
          Construct a new SimpleDelegatingTransformer.
 
Method Summary
protected  void clearVisitedSourceToDestinationMapIfNecessary()
          If we have popped everybody off the stack, clear the cache.
 java.lang.Object clone()
          
protected  java.lang.Object convertImpl(java.lang.Class destinationType, java.lang.Object source, java.util.Locale locale)
          The implementation of the convert method, which may omit the invalid argument checks already performed by this base class.
protected  void copyImpl(java.lang.Object destination, java.lang.Object source, java.util.Locale locale, java.lang.Integer preferredTransformationType)
          Implementation of the copy method.
protected  Transformer[] createDefaultComponents()
          Create the default set of Transformer components.
protected  java.lang.Object createReusableSource(java.lang.Class destinationClass, java.lang.Object source)
          NodeCopier.createReusableSource(Class, Object)
protected  void decrementStackDepth()
          Decrement the depth of the nested copy stack.
protected  java.lang.Object getCachedResult(java.lang.Object source, java.lang.Class destinationType)
          Get the cached object in the process of being transformed
 java.lang.Object[] getComponents()
          
protected  Copier getCopier(java.lang.Class destinationClass, java.lang.Class sourceClass)
          Finds a Copier that is capable of transforming sourceClass to destinationClass.
protected  java.lang.Class[] getDestinationClassesImpl()
          Transformer.getDestinationClasses() implementation template method.
protected  java.lang.Class[] getSourceClassesImpl()
          Transformer.getSourceClasses() implementation template method.
 net.sf.composite.specialize.Specializer getSpecializer()
          Get the Specializer for this SDT.
protected  Transformer getTransformer(java.lang.Class destinationClass, java.lang.Class sourceClass)
          Finds a Transformer that is capable of transforming sourceClass to destinationClass.
protected  Transformer getTransformer(java.util.Map registry, java.lang.Class transformerType, java.lang.Class destinationClass, java.lang.Class sourceClass)
          Finds a transformer of type transformerType that is capable of transforming sourceClass to destinationClass.
 Transformer[] getTransformers()
          Get our components as a typesafe Transformer array.
protected  java.util.Map getVisitedSourceToDestinationMap()
          Gets a Map of all the nodes in the object graph that have been transformed so far by this transformer.
protected  boolean hasVisited(java.lang.Object source, java.lang.Class destinationType)
          Avoid recursion by checking whether we have already begun a conversion of the specified source object to the specified destination class.
protected  boolean hasVisitedDestination(java.lang.Object source, java.lang.Object destination)
          Avoid recursion by checking whether we have already begun a copy of the specified source object to the specified destination object.
protected  void incrementStackDepth()
          Increment the depth of the nested copy stack.
protected  void initializeImpl()
          Gives subclasses a chance to perform any computations needed to initialize the transformer.
protected  boolean isAutomaticallyHandlingNulls()
          Indicates whether null values will automatically be converted to null by this base class before even calling the subclass's BaseTransformer.convertImpl(Class, Object, Locale) method.
protected  boolean isImpreciseTransformationImpl(java.lang.Class destinationClass, java.lang.Class sourceClass)
          Implementation of isImpreciseTransformation
protected  boolean isPerformingLogging()
          Let the delegate do the logging
 boolean isPreferPreciseTransformers()
          Get the preferPreciseTransformers.
 boolean isSpecializable(java.lang.Class type)
          
protected  boolean isTransformableImpl(java.lang.Class destinationType, java.lang.Class sourceType)
          Determines if one of the delegate transformers is capable of performing the given transformation.
protected  void recordVisit(java.lang.Object source, java.lang.Class destinationType, java.lang.Object destination)
          Cache destination object for this source/destination class combination.
 void setComponents(java.lang.Object[] components)
          
 void setPreferPreciseTransformers(boolean preferPreciseTransformers)
          Set the preferPreciseTransformers.
 void setSpecializer(net.sf.composite.specialize.Specializer specializer)
          Set the Specializer for this SDT.
 java.lang.Object specialize(java.lang.Class compositeType)
          
 
Methods inherited from class net.sf.morph.transform.transformers.BaseCompositeTransformer
getComponentType, getComponentValidator, isNarrowingComponentArray, isWrappingRuntimeExceptions, setComponentValidator, setNestedTransformer, updateNestedTransformerComponents
 
Methods inherited from class net.sf.morph.transform.transformers.BaseTransformer
convert, convert, copy, copy, createDefaultReflector, createNewInstance, createNewInstanceImpl, equals, equals, equalsUnidirectionalTest, getDestinationClasses, getInstantiatingReflector, getLocale, getLog, getNestedTransformer, getReflector, getReflector, getSourceClasses, getTransformableCallCache, getTransformerName, initialize, isCachingIsTransformableCalls, isImpreciseTransformation, isInitialized, isTransformable, setCachingIsTransformableCalls, setDestinationClasses, setInitialized, setLog, setReflector, setSourceClasses, setTransformableCallCache, setTransformerName, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.morph.transform.ExplicitTransformer
isTransformable
 
Methods inherited from interface net.sf.morph.transform.Transformer
getDestinationClasses, getSourceClasses
 
Methods inherited from interface net.sf.morph.transform.DecoratedCopier
copy
 
Methods inherited from interface net.sf.morph.transform.Copier
copy
 
Methods inherited from interface net.sf.morph.transform.Transformer
getDestinationClasses, getSourceClasses
 
Methods inherited from interface net.sf.morph.transform.Transformer
getDestinationClasses, getSourceClasses
 
Methods inherited from interface net.sf.morph.transform.ExplicitTransformer
isTransformable
 
Methods inherited from interface net.sf.morph.transform.DecoratedConverter
convert, equals, equals
 
Methods inherited from interface net.sf.morph.transform.Converter
convert
 
Methods inherited from interface net.sf.morph.transform.Transformer
getDestinationClasses, getSourceClasses
 
Methods inherited from interface net.sf.morph.transform.Transformer
getDestinationClasses, getSourceClasses
 
Methods inherited from interface net.sf.morph.transform.ExplicitTransformer
isTransformable
 
Methods inherited from interface net.sf.morph.transform.ImpreciseTransformer
isImpreciseTransformation
 

Constructor Detail

SimpleDelegatingTransformer

public SimpleDelegatingTransformer()
Construct a new SimpleDelegatingTransformer.


SimpleDelegatingTransformer

public SimpleDelegatingTransformer(Transformer[] components)
Construct a new SimpleDelegatingTransformer.

Parameters:
components -

SimpleDelegatingTransformer

public SimpleDelegatingTransformer(Transformer[] components,
                                   boolean appendDefaultComponents)
Construct a new SimpleDelegatingTransformer.

Parameters:
components -
appendDefaultComponents -
Method Detail

createDefaultComponents

protected Transformer[] createDefaultComponents()
Create the default set of Transformer components.

Returns:
Transformer[]

initializeImpl

protected void initializeImpl()
                       throws java.lang.Exception
Gives subclasses a chance to perform any computations needed to initialize the transformer.

Overrides:
initializeImpl in class BaseCompositeTransformer
Throws:
java.lang.Exception
See Also:
BaseCompositeTransformer.initializeImpl()

isTransformableImpl

protected boolean isTransformableImpl(java.lang.Class destinationType,
                                      java.lang.Class sourceType)
                               throws java.lang.Exception
Determines if one of the delegate transformers is capable of performing the given transformation. This method is necessary because otherwise the delegating transformer would be too eager in what it says it can convert. For example, if a delegate transformer was capable of transforming A to B and another delegate was capable of transforming C to D, this transformer would incorrectly state that transforming A to D and C to B was possible even though they are not.

Overrides:
isTransformableImpl in class BaseTransformer
Parameters:
destinationType - the destination type to test
sourceType - the source type to test
Returns:
whether the destination type is transformable to the source type
Throws:
TransformationException - if it could not be determined if sourceType is transformable into destinationType
java.lang.Exception

isImpreciseTransformationImpl

protected boolean isImpreciseTransformationImpl(java.lang.Class destinationClass,
                                                java.lang.Class sourceClass)
Implementation of isImpreciseTransformation

Overrides:
isImpreciseTransformationImpl in class BaseTransformer
Returns:
boolean

getSourceClassesImpl

protected java.lang.Class[] getSourceClassesImpl()
                                          throws java.lang.Exception
Transformer.getSourceClasses() implementation template method.

Specified by:
getSourceClassesImpl in class BaseTransformer
Returns:
Class[]
Throws:
java.lang.Exception
See Also:
BaseTransformer.getSourceClassesImpl()

getDestinationClassesImpl

protected java.lang.Class[] getDestinationClassesImpl()
                                               throws java.lang.Exception
Transformer.getDestinationClasses() implementation template method.

Specified by:
getDestinationClassesImpl in class BaseTransformer
Returns:
Class[]
Throws:
java.lang.Exception
See Also:
BaseTransformer.getDestinationClassesImpl()

copyImpl

protected void copyImpl(java.lang.Object destination,
                        java.lang.Object source,
                        java.util.Locale locale,
                        java.lang.Integer preferredTransformationType)
                 throws java.lang.Exception
Implementation of the copy method. By default, this method throws UnsupportedOperationException.

Overrides:
copyImpl in class BaseTransformer
Throws:
java.lang.Exception
See Also:
BaseTransformer.copyImpl(java.lang.Object, java.lang.Object, java.util.Locale, java.lang.Integer)

convertImpl

protected java.lang.Object convertImpl(java.lang.Class destinationType,
                                       java.lang.Object source,
                                       java.util.Locale locale)
                                throws java.lang.Exception
The implementation of the convert method, which may omit the invalid argument checks already performed by this base class. By default, this method creates a new instance of the destinationClass and copies information from the source to the destination. This implementation should be fine as-is for Copiers, but Converters will need to implement this method since they will not be implementing the copy method.

Overrides:
convertImpl in class BaseTransformer
locale - the locale in which the conversion should take place. for converters that are not locale-aware, the local argument can simply be ignored
Throws:
java.lang.Exception
See Also:
BaseTransformer.convertImpl(java.lang.Class, java.lang.Object, java.util.Locale)

incrementStackDepth

protected void incrementStackDepth()
Increment the depth of the nested copy stack.


decrementStackDepth

protected void decrementStackDepth()
Decrement the depth of the nested copy stack.


clearVisitedSourceToDestinationMapIfNecessary

protected void clearVisitedSourceToDestinationMapIfNecessary()
If we have popped everybody off the stack, clear the cache.


recordVisit

protected void recordVisit(java.lang.Object source,
                           java.lang.Class destinationType,
                           java.lang.Object destination)
Cache destination object for this source/destination class combination.

Parameters:
source -
destinationType -
destination -

hasVisited

protected boolean hasVisited(java.lang.Object source,
                             java.lang.Class destinationType)
Avoid recursion by checking whether we have already begun a conversion of the specified source object to the specified destination class.

Parameters:
source -
destinationType -
Returns:
boolean

hasVisitedDestination

protected boolean hasVisitedDestination(java.lang.Object source,
                                        java.lang.Object destination)
Avoid recursion by checking whether we have already begun a copy of the specified source object to the specified destination object.

Parameters:
source -
destination -
Returns:
boolean

getCachedResult

protected java.lang.Object getCachedResult(java.lang.Object source,
                                           java.lang.Class destinationType)
Get the cached object in the process of being transformed

Parameters:
source -
destinationType -
Returns:

specialize

public java.lang.Object specialize(java.lang.Class compositeType)

Specified by:
specialize in interface net.sf.composite.SpecializableComposite
See Also:
SpecializableComposite.specialize(java.lang.Class)

isSpecializable

public boolean isSpecializable(java.lang.Class type)
                        throws net.sf.composite.CompositeException

Specified by:
isSpecializable in interface net.sf.composite.SpecializableComposite
Throws:
net.sf.composite.CompositeException
See Also:
SpecializableComposite.isSpecializable(java.lang.Class)

getTransformer

protected Transformer getTransformer(java.util.Map registry,
                                     java.lang.Class transformerType,
                                     java.lang.Class destinationClass,
                                     java.lang.Class sourceClass)
Finds a transformer of type transformerType that is capable of transforming sourceClass to destinationClass. Caches results in the registry.

Parameters:
registry - a cache that remembers which transformers can be used for which transformations
transformerType - the type of the returned transformer
destinationClass - the destinationClass of the transformation
sourceClass - the sourceClass of the transformation
Returns:
the transformer of the requested type capable of performing the requested transformation
Throws:
TransformationException - if no suitable transformer could be found

getCopier

protected Copier getCopier(java.lang.Class destinationClass,
                           java.lang.Class sourceClass)
Finds a Copier that is capable of transforming sourceClass to destinationClass.

Parameters:
destinationClass - the destinationClass of the transformation
sourceClass - the sourceClass of the transformation
Returns:
the transformer of the requested type capable of performing the requested transformation
Throws:
TransformationException - if no suitable copier could be found

getTransformer

protected Transformer getTransformer(java.lang.Class destinationClass,
                                     java.lang.Class sourceClass)
Finds a Transformer that is capable of transforming sourceClass to destinationClass.

Parameters:
destinationClass - the destinationClass of the transformation
sourceClass - the sourceClass of the transformation
Returns:
the transformer of the requested type capable of performing the requested transformation
Throws:
TransformationException - if no suitable transformer could be found

getTransformers

public Transformer[] getTransformers()
Get our components as a typesafe Transformer array.

Returns:
Transformer[]

getComponents

public java.lang.Object[] getComponents()
Description copied from class: BaseCompositeTransformer

Specified by:
getComponents in interface net.sf.composite.SimpleComposite
Overrides:
getComponents in class BaseCompositeTransformer
See Also:
SimpleComposite.getComponents()

setComponents

public void setComponents(java.lang.Object[] components)
Description copied from class: BaseCompositeTransformer

Specified by:
setComponents in interface net.sf.composite.SimpleComposite
Overrides:
setComponents in class BaseCompositeTransformer
See Also:
SimpleComposite.setComponents(java.lang.Object[])

isPerformingLogging

protected boolean isPerformingLogging()
Let the delegate do the logging

Overrides:
isPerformingLogging in class BaseTransformer
Returns:
boolean

isAutomaticallyHandlingNulls

protected boolean isAutomaticallyHandlingNulls()
Indicates whether null values will automatically be converted to null by this base class before even calling the subclass's BaseTransformer.convertImpl(Class, Object, Locale) method. Subclasses which depend on this behavior (which is all subclasses, by default) should include null as one of their source and destination classes so that the actual behavior of the transformer is consistent with the values that are returned by the BaseTransformer.isTransformable(Class, Class) method. The conversions will happen automatically even if the source and destination classes don't contain null, but for the sake of consistency the nulls should be included.

Overrides:
isAutomaticallyHandlingNulls in class BaseTransformer
Returns:
whether null values will automatically be converted to null by this base class before even calling the subclass's BaseTransformer.convertImpl(Class, Object, Locale) method
See Also:
BaseTransformer.isAutomaticallyHandlingNulls()

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException

Overrides:
clone in class BaseTransformer
Throws:
java.lang.CloneNotSupportedException
See Also:
BaseTransformer.clone()

getVisitedSourceToDestinationMap

protected java.util.Map getVisitedSourceToDestinationMap()
Gets a Map of all the nodes in the object graph that have been transformed so far by this transformer. The keys in the Map are the visited source nodes, and the values are the converted representation of the node.

Returns:
a Map of all the nodes in the object graph that have been transformed so far by this transformer

getSpecializer

public net.sf.composite.specialize.Specializer getSpecializer()
Get the Specializer for this SDT.

Returns:
Specializer

setSpecializer

public void setSpecializer(net.sf.composite.specialize.Specializer specializer)
Set the Specializer for this SDT.

Parameters:
specializer -

createReusableSource

protected java.lang.Object createReusableSource(java.lang.Class destinationClass,
                                                java.lang.Object source)
NodeCopier.createReusableSource(Class, Object)

Overrides:
createReusableSource in class BaseTransformer
Returns:
See Also:
BaseTransformer.createReusableSource(java.lang.Class, java.lang.Object)

isPreferPreciseTransformers

public boolean isPreferPreciseTransformers()
Get the preferPreciseTransformers.

Returns:
boolean

setPreferPreciseTransformers

public void setPreferPreciseTransformers(boolean preferPreciseTransformers)
Set the preferPreciseTransformers. Default false.

Parameters:
preferPreciseTransformers - the boolean to set


Copyright © 2004-2008.