Class Variable<A>

java.lang.Object
eu.bandm.tools.paisley.AbstractPattern<A>
eu.bandm.tools.paisley.Variable<A>
Type Parameters:
A - the target type of objects to match against
All Implemented Interfaces:
Extractor<A>, Pattern<A>, Serializable, Cloneable

@Generated(generator="eu.bandm.tools.expander", version="", timestamp="2026-01-24T11:39:10") public final class Variable<A> extends AbstractPattern<A> implements Extractor<A>
Class of pattern variables.

A pattern variable is deterministic, matches any object and binds itself.

See Also:
  • Constructor Details

    • Variable

      public Variable()
      Creates a new anonymous variable.
    • Variable

      public Variable(@Opt @Opt String name)
      Creates a new named variable. Variable names are for user-level annotation purposes only, they have no effect on matching behaviour.
      Parameters:
      name - the variable name.
  • Method Details

    • getName

      @Opt public @Opt String getName()
      Returns the name of this variable.
      Returns:
      the name of this variable, or null if this variable is anonymous.
    • getValue

      public A getValue()
      Returns the current value bound to this variable. The value is initially null. It is set to a meaningful value by a successful match with a pattern that guarantees to bind the variable. It may be set to an unspecified value by a match with a pattern that does not guarantee to preserve the variable.
      Specified by:
      getValue in interface Extractor<A>
      Returns:
      the current value bound to this variable.
      See Also:
    • 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. This implementation resets the variable value to null.

      Specified by:
      clear in interface Pattern<A>
      Parameters:
      recursively - true if all subpatterns should be traversed, false otherwise.
    • match

      public boolean match(A target)
      Attempts to match this pattern against a given object.

      If the class of this pattern imposes a specific order on matches, the first one is chosen.

      The null target is generally allowed. This implementation sets the variable value to target.

      Specified by:
      match in interface Pattern<A>
      Parameters:
      target - the object to match against
      Returns:
      true
      See Also:
    • binds

      public boolean binds(Variable<?> variable)
      Checks whether a variable is bound by this pattern.

      The default implementation returns false.

      Specified by:
      binds in interface Pattern<A>
      Parameters:
      variable - a pattern variable
      Returns:
      true if this variable is identical to the given variable, false otherwise.
    • preserves

      public boolean preserves(Variable<?> variable, boolean success)
      Checks whether a variable is preserved by this pattern, conditionally on success or failure.

      The default implementation returns false.

      Specified by:
      preserves in interface Pattern<A>
      Parameters:
      variable - a pattern variable
      success - specifies preservation on success or failure
      Returns:
      true if success is false or this variable is distinct from the given variable, false otherwise.
    • preserves

      public boolean preserves(Variable<?> variable)
      Checks whether a variable is preserved by this pattern unconditionally.

      The default implementation returns false.

      Specified by:
      preserves in interface Pattern<A>
      Parameters:
      variable - a pattern variable
      Returns:
      true if this variable is distinct from the given variable, false otherwise.
    • isDeterministic

      public boolean isDeterministic()
      Checks whether this pattern is deterministic.

      The default implementation returns false.

      Specified by:
      isDeterministic in interface Pattern<A>
      Returns:
      true
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • fork

      public final Variable<A> fork()
      Makes a copy of this pattern with independent internal state.

      Subclasses must override this method if necessary to honour the following rules for all transitively reachable patterns:

      • Subpatterns that are instances of Variable must be shared. This implies that fork() must return this.
      • Fields that do not refer to subpatterns, but to matching state (changed by invocations of Pattern.match(A), Pattern.matchAgain(), Pattern.cut() or Pattern.clear()) must be duplicated. If matching state changes are effected by modification of other objects, these must be duplicated. In summary, matching operations on this pattern and its copy must not interfere, with the exception of variable bindings.
      • Fields neither referring to subpatterns nor to matching state may be either shared or duplicated. The usage of such fields in a modifiable way (such that the distinction is relevant) is strongly deprecated.
      • Patterns whose fields need not be duplicated need not be duplicated themselves, but may return this.

      Subpatterns that are instances of Variable must be shared. This implies that fork() must return this.

      Specified by:
      fork in interface Pattern<A>
      Overrides:
      fork in class AbstractPattern<A>
      Returns:
      a copy of this pattern that has independent internal state, except for variables
      See Also:
    • lambda

      public <B> Motif<A,B> lambda(Pattern<B> body)
      Returns a motif that has the occurrences of this variable in the given body pattern as the hole.
      Type Parameters:
      B - the target type of the body pattern
      Parameters:
      body - the body pattern
      Returns:
      a motif that has the occurrences of this in body body pattern as the hole
      Throws:
      NullPointerException - if body is null.
    • plus

      public Pattern<A> plus(Pattern<? super A> root)
      Returns a pattern that is equivalent to one or more iterations of the given root pattern nested via this variable.

      Conceptually, x.plus(p) is equivalent to the pattern arising from p.fork() by substituting x.star(p) for x. Note that this cannot be used naively for implementation, since the ensuing recursive substitution would not terminate. The actual implementation guarantees to produce only as many new subpatterns as dynamically necessary.

      Parameters:
      root - the root pattern, which should bind this variable
      Returns:
      a pattern that is equivalent to the infinite disjunction of root, bind(root, root), bind(bind(root, root), root), etc.
      See Also:
    • star

      public Pattern<A> star(Pattern<? super A> root)
      Returns a pattern that is equivalent to zero or more iterations of the given root pattern nested via this variable.

      Conceptually, x.star(p) is equivalent to either(x, x.plus(p)).

      Parameters:
      root - the root pattern, which should bind this variable
      Returns:
      a pattern that is equivalent to the infinite disjunction of this, root, bind(root, root), bind(bind(root, root), root), etc.
      See Also: