net.sf.molae.pipe.trafo
Class CachedTransformer<S,T>

java.lang.Object
  extended by net.sf.molae.pipe.trafo.CachedTransformer<S,T>
All Implemented Interfaces:
TwoWayTransformer<S,T>

public class CachedTransformer<S,T>
extends Object
implements TwoWayTransformer<S,T>

A cached transformer. All transformation results are stored in a cache and used the next time the transformation is called with an equal input parameter.

Since:
1.1

Constructor Summary
CachedTransformer(Map<S,T> cache, Map<T,S> inverted, TwoWayTransformer<S,T> base)
          Constructs a CachedTransformer on the base of the specified parameters.
CachedTransformer(TwoWayTransformer<S,T> base)
          Constructs an empty CachedTransformer.
 
Method Summary
 Map<S,T> getCache()
          Returns the cache.
 Map<T,S> getInverted()
          Returns the retransformation cache.
 boolean isReadOnly()
          Returns true if this object is marked as read only.
 boolean isRetrievable(T input)
          Indicates if the origin of the specified object can be calculated.
 S retransform(T input)
          Calculates the transformation origin of the specified object.
 void setReadOnly()
          Marks this CachedTransformer object as "read only".
 T transform(S input)
          Returns the value to which the base map maps the specified key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachedTransformer

public CachedTransformer(Map<S,T> cache,
                         Map<T,S> inverted,
                         TwoWayTransformer<S,T> base)
Constructs a CachedTransformer on the base of the specified parameters.

Parameters:
cache - Initial values of the cache.
inverted - Initial values of the retransformation cache.
base - the default transformer.

CachedTransformer

public CachedTransformer(TwoWayTransformer<S,T> base)
Constructs an empty CachedTransformer.

Parameters:
base - the default transformer.
Method Detail

getCache

public Map<S,T> getCache()
Returns the cache.

Returns:
the cache.

getInverted

public Map<T,S> getInverted()
Returns the retransformation cache.

Returns:
the retransformation cache.

setReadOnly

public void setReadOnly()
Marks this CachedTransformer object as "read only". If this transformer is "read only" only the cache is used, but not the transformer.

See Also:
isReadOnly()

isReadOnly

public boolean isReadOnly()
Returns true if this object is marked as read only. By default, the object is not read only. It can be set to read only by a call to setReadOnly().

Returns:
true if this object is marked as readonly, false otherwise.

transform

public T transform(S input)
Returns the value to which the base map maps the specified key. If no value exists in the base map and this object is not read only, a new entry is created and stored in the base map.

Specified by:
transform in interface TwoWayTransformer<S,T>
Parameters:
input - key whose associated value is to be returned
Returns:
the value to which the base map maps the specified key.

retransform

public S retransform(T input)
Calculates the transformation origin of the specified object.

Specified by:
retransform in interface TwoWayTransformer<S,T>
Parameters:
input - the object for which the transformation origin is searched.
Returns:
the transformation origin of the specified object.
Throws:
IllegalArgumentException - depending on the base transformer.
ClassCastException - depending on the base transformer.
UnsupportedOperationException - depending on the base transformer.

isRetrievable

public boolean isRetrievable(T input)
Description copied from interface: TwoWayTransformer
Indicates if the origin of the specified object can be calculated. This requires that:
  1. retransform is implemented for this object and does not throw an exception.
  2. The object x returned by retransform is unique, or more formally: transform(o1).equals(transform(o2)) &rArr o1.equals(o2)
This implies that
 retransform(transform(input)).equals(input)
 
for all objects where the retransformation is defined.

Specified by:
isRetrievable in interface TwoWayTransformer<S,T>
Parameters:
input - the object for which the transformation origin is searched.
Returns:
true if the origin of the specified object can be calculated.