Class CachedTransform<A,B>
- Type Parameters:
A- the target type of the containing patternB- the target type of the contained subpattern
- All Implemented Interfaces:
Pattern<A>,Serializable,Cloneable
If a subclass of RestrictedTransform has expensive computations
shared between isDomain(A) and apply(A), consider making it a
subclass of this class instead.
Subclasses must implement the tryApply(A) method.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classIndicates that a target can not be transformed.static interfaceThe functional interface for the methodtryApply(A). -
Field Summary
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCachedTransform(Pattern<? super B> body) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionTransforms a target to be matched into a target for the subpattern.voidclear(boolean recursively) Disposes of all temporary internal state related to results and nondeterminism.final booleanChecks whether a given target is supported by this transform.static <A,B> CachedTransform <A, B> of(CachedTransform.TryApply<? super A, ? extends B> fun, Pattern<? super B> body) Creates a new instance.static <A> Areject()static <A> Aprotected abstract BChecks whether a target can be transformed and transforms it.static <A,B> Motif <B, A> with(CachedTransform.TryApply<? super A, ? extends B> fun) Methods inherited from class eu.bandm.tools.paisley.RestrictedTransform
cut, isRange, match, matchAgain, ofUnlessNull, withUnlessNullMethods inherited from class eu.bandm.tools.paisley.Unary
binds, fork, getBody, isDeterministic, preserves, preserves, setBodyMethods inherited from class eu.bandm.tools.paisley.AbstractPattern
clone, doclone
-
Constructor Details
-
CachedTransform
Creates a new instance.- Parameters:
body- the subpattern
-
-
Method Details
-
isDomain
Checks whether a given target is supported by this transform.The default implementation always returns
true.Subclasses should override this method to restrict the supported targets.
This implementation calls
tryApply(A)and caches the result.- Overrides:
isDomainin classRestrictedTransform<A,B> - Parameters:
target- the target to match against- Returns:
trueiftargetis supported by this transform;falseotherwise
-
tryApply
Checks whether a target can be transformed and transforms it.- Parameters:
target- the object to be matched against.- Returns:
- a target for
Unary.bodyto be matched against. - Throws:
CachedTransform.ApplyException- if the target can not be transformed.
-
apply
Transforms a target to be matched into a target for the subpattern.This implementation returns the result cached by
isDomain(A). -
clear
public void clear(boolean recursively) Disposes of all temporary internal state related to results and nondeterminism.The default implementation just invokes
cut(false). Subclasses that override this method should either invoke this overridden implementation, or ensure by other means that the effect ofcut(false)is implied. The default implementation invokescut(false), and delegates toUnary.bodyifrecursivelyistrue.This implementation clears the cache.
-
of
public static <A,B> CachedTransform<A,B> of(CachedTransform.TryApply<? super A, ? extends B> fun, Pattern<? super B> body) Creates a new instance.- Type Parameters:
A- the target type of the containing patternB- the target type of the contained subpattern- Parameters:
fun- the function to implement thetryApply(A)methodbody- the subpattern- Returns:
- a pattern that transforms with
funand delegates tobody
-
with
-
reject
- Throws:
CachedTransform.ApplyException
-
reject
- Throws:
CachedTransform.ApplyException
-