net.sf.morph.transform.copiers
Class ConditionalCopier

java.lang.Object
  extended by net.sf.morph.transform.transformers.BaseTransformer
      extended by net.sf.morph.transform.copiers.ConditionalCopier
All Implemented Interfaces:
net.sf.composite.Component, Converter, Copier, DecoratedConverter, DecoratedCopier, DecoratedTransformer, ExplicitTransformer, Transformer

public class ConditionalCopier
extends BaseTransformer
implements DecoratedConverter, DecoratedCopier, ExplicitTransformer

This copier uses an if Converter to convert an incoming object to a Boolean. If this results in a Boolean object corresponding to boolean true, the then Transformer is invoked; otherwise the optional else Transformer is invoked if present.

Since:
Morph 1.1
Author:
Matt Benson

Field Summary
 
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
 
Constructor Summary
ConditionalCopier()
           
 
Method Summary
protected  java.lang.Object convertImpl(java.lang.Class destinationClass, 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  java.lang.Class[] getDestinationClassesImpl()
          Transformer.getDestinationClasses() implementation template method.
 Transformer getElseTransformer()
          Get the elseTransformer.
 Converter getIfConverter()
          Get the ifConverter.
protected  java.lang.Class[] getSourceClassesImpl()
          Transformer.getSourceClasses() implementation template method.
 Transformer getThenTransformer()
          Get the thenTransformer.
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 isTransformableImpl(java.lang.Class destinationType, java.lang.Class sourceType)
          Default implementation for Transformer#isTransformable(Class, Class) that assumes that each source type can be converted into each destination type.
 void setDestinationClasses(java.lang.Class[] destinationClasses)
          Configures the destinationClasses property of this transformer.
 void setElseTransformer(Transformer elseTransformer)
          Set the elseTransformer.
 void setIfConverter(Converter ifConverter)
          Set the ifConverter.
 void setSourceClasses(java.lang.Class[] sourceClasses)
          Configures the sourceClasses property of this transformer.
 void setThenTransformer(Transformer thenTransformer)
          Set the thenTransformer.
protected  java.lang.Object transform(java.lang.Class destinationType, java.lang.Object destination, java.lang.Object source, java.util.Locale locale, java.lang.Integer preferredTransformationType)
          Do the transformation.
 
Methods inherited from class net.sf.morph.transform.transformers.BaseTransformer
clone, convert, convert, copy, copy, createDefaultReflector, createNewInstance, createNewInstanceImpl, createReusableSource, equals, equals, equalsUnidirectionalTest, getDestinationClasses, getInstantiatingReflector, getLocale, getLog, getNestedTransformer, getReflector, getReflector, getSourceClasses, getTransformableCallCache, getTransformerName, initialize, initializeImpl, isCachingIsTransformableCalls, isImpreciseTransformation, isImpreciseTransformationImpl, isInitialized, isPerformingLogging, isTransformable, isWrappingRuntimeExceptions, setCachingIsTransformableCalls, setInitialized, setLog, setNestedTransformer, setReflector, 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.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.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
 

Constructor Detail

ConditionalCopier

public ConditionalCopier()
Method Detail

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

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

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

isTransformableImpl

protected boolean isTransformableImpl(java.lang.Class destinationType,
                                      java.lang.Class sourceType)
                               throws java.lang.Exception
Default implementation for Transformer#isTransformable(Class, Class) that assumes that each source type can be converted into each destination type.

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

setDestinationClasses

public void setDestinationClasses(java.lang.Class[] destinationClasses)
Configures the destinationClasses property of this transformer. Note that this method should be called before the transformer is used. Otherwise, if another thread is in the middle of transforming an object graph and this method is called, the behavior of the transformer can change partway through the transformation.

Overrides:
setDestinationClasses in class BaseTransformer
Parameters:
destinationClasses - the new destinationClasses for this transformer

setSourceClasses

public void setSourceClasses(java.lang.Class[] sourceClasses)
Configures the sourceClasses property of this transformer. Note that this method should be called before the transformer is used. Otherwise, if another thread is in the middle of transforming an object graph and this method is called, the behavior of the transformer can change partway through the transformation.

Overrides:
setSourceClasses in class BaseTransformer
Parameters:
sourceClasses - the new sourceClasses for this transformer

convertImpl

protected java.lang.Object convertImpl(java.lang.Class destinationClass,
                                       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

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

transform

protected java.lang.Object transform(java.lang.Class destinationType,
                                     java.lang.Object destination,
                                     java.lang.Object source,
                                     java.util.Locale locale,
                                     java.lang.Integer preferredTransformationType)
                              throws TransformationException
Do the transformation.

Parameters:
destinationType -
destination -
source -
locale -
preferredTransformationType -
Returns:
Throws:
TransformationException

getIfConverter

public Converter getIfConverter()
Get the ifConverter.

Returns:
Converter.

setIfConverter

public void setIfConverter(Converter ifConverter)
Set the ifConverter. By default it will be an instance of DefaultToBooleanConverter.

Parameters:
ifConverter - the Converter ifConverter to set.

getThenTransformer

public Transformer getThenTransformer()
Get the thenTransformer.

Returns:
Transformer.

setThenTransformer

public void setThenTransformer(Transformer thenTransformer)
Set the thenTransformer.

Parameters:
thenTransformer - the Transformer thenTransformer to set.

getElseTransformer

public Transformer getElseTransformer()
Get the elseTransformer.

Returns:
Transformer.

setElseTransformer

public void setElseTransformer(Transformer elseTransformer)
Set the elseTransformer.

Parameters:
elseTransformer - the Transformer elseTransformer to set.


Copyright © 2004-2008.