Class CachedTransform<A,B>

Type Parameters:
A - the target type of the containing pattern
B - the target type of the contained subpattern
All Implemented Interfaces:
Pattern<A>, Serializable, Cloneable

@Generated(generator="eu.bandm.tools.expander", version="", timestamp="2026-01-24T11:39:08") public abstract class CachedTransform<A,B> extends RestrictedTransform<A,B>
Abstract base class for patterns that delegate expensively transformed targets to a subpattern.

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:
  • Constructor Details

    • CachedTransform

      protected CachedTransform(Pattern<? super B> body)
      Creates a new instance.
      Parameters:
      body - the subpattern
  • Method Details

    • isDomain

      public final boolean isDomain(A target)
      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:
      isDomain in class RestrictedTransform<A,B>
      Parameters:
      target - the target to match against
      Returns:
      true if target is supported by this transform; false otherwise
    • tryApply

      protected abstract B tryApply(A target) throws CachedTransform.ApplyException
      Checks whether a target can be transformed and transforms it.
      Parameters:
      target - the object to be matched against.
      Returns:
      a target for Unary.body to be matched against.
      Throws:
      CachedTransform.ApplyException - if the target can not be transformed.
    • apply

      public B apply(A target)
      Transforms a target to be matched into a target for the subpattern.

      This implementation returns the result cached by isDomain(A).

      Specified by:
      apply in class Transform<A,B>
      Parameters:
      target - the target for this pattern to be matched against.
      Returns:
      a target for Unary.body to be matched against.
    • 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 of cut(false) is implied. The default implementation invokes cut(false), and delegates to Unary.body if recursively is true.

      This implementation clears the cache.

      Specified by:
      clear in interface Pattern<A>
      Overrides:
      clear in class Unary<B,A>
      Parameters:
      recursively - true if all subpatterns should be traversed, false otherwise.
    • 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 pattern
      B - the target type of the contained subpattern
      Parameters:
      fun - the function to implement the tryApply(A) method
      body - the subpattern
      Returns:
      a pattern that transforms with fun and delegates to body
    • with

      public static <A, B> Motif<B,A> with(CachedTransform.TryApply<? super A,? extends B> fun)
    • reject

      public static <A> A reject() throws CachedTransform.ApplyException
      Throws:
      CachedTransform.ApplyException
    • reject

      public static <A> A reject(Throwable cause) throws CachedTransform.ApplyException
      Throws:
      CachedTransform.ApplyException