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="1.3.1", timestamp="2026-02-18T13:05:42") 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
      Throws:
      NullPointerException - if body is null
  • 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)
      Returns a motif that acts like an instance of this class, with the given partial transform and a hole in place of the subpattern.
      Type Parameters:
      A - the subtarget type of objects to bind (match the hole against)
      B - the target type of objects to match the entire pattern against
      Parameters:
      fun - the transform to attempt to apply to target objects
      Returns:
      a motif that matches the hole against successfully transformed target objects, or fails if the target cannot be transformed.
      Throws:
      NullPointerException - if fun is null
    • reject

      public static <A> A reject() throws CachedTransform.ApplyException
      Signals that the current target cannot be transformed.

      This method is for use in implementations of CachedTransform.TryApply.

      Type Parameters:
      A - the target type
      Returns:
      this method never returns
      Throws:
      CachedTransform.ApplyException - always
    • reject

      public static <A> A reject(Throwable cause) throws CachedTransform.ApplyException
      Signals that the current target cannot be transformed due to a known cause.

      This method is for use in implementations of CachedTransform.TryApply.

      Type Parameters:
      A - the target type
      Parameters:
      cause - the cause for rejection
      Returns:
      this method never returns
      Throws:
      CachedTransform.ApplyException - always