Class EnumBranch<A,E extends Enum<E>>

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

@Generated(generator="eu.bandm.tools.expander", version="", timestamp="2026-01-24T11:39:10") public class EnumBranch<A,E extends Enum<E>> extends AbstractPattern<A>
Enum-indexed disjunctive combination of patterns that tracks which branch has been chosen.

Patterns of this class hold a map of keys of the enum type E to subpatterns. For each target, the subpatterns are tried disjunctively, as via Pattern.some(Iterable), in the natural order of their keys. For each match, the key associated with the matching subpattern is temporarily recorded, and may be retrieved with the getKey() method before the next call to #match or #matchAgain.

See Also:
  • Constructor Details

    • EnumBranch

      public EnumBranch(EnumMap<E,? extends Pattern<? super A>> branches)
      Creates a new instance with the given branches.

      The given map is copied for internal storage; later changes to the original map do not affect the resulting pattern.

      Parameters:
      branches - the map of branches
      Throws:
      NullPointerException - if branches is or contains null
    • EnumBranch

      public EnumBranch(Map<E,? extends Pattern<? super A>> branches)
      Creates a new instance with the given branches.

      The given map is copied for internal storage; later changes to the original map do not affect the resulting pattern.

      Parameters:
      branches - the map of branches
      Throws:
      NullPointerException - if branches is or contains null
      IllegalArgumentException - if branches is not an EnumMap and is empty
    • EnumBranch

      @Deprecated(forRemoval=true, since="1.0") public EnumBranch(Class<E> cls)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use EnumBranch.Builder instead.
      Creates a new instance with no branches.
      Parameters:
      cls - the enum class to use for keys
  • Method Details

    • put

      @Deprecated(forRemoval=true, since="1.0") public EnumBranch<A,E> put(E key, Pattern<? super A> pattern)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use EnumBranch.Builder instead.
      Adds a new branch to this disjunction.
      Parameters:
      key - the key for the new branch
      pattern - the subpattern for the new branch
      Returns:
      this
    • getKey

      public E getKey()
      Returns the key for the branch chosen for the most recent match.

      The most recent match has happened when a call to match or matchAgain has last returned true. The result is unspecified if neither has been called yet, or false has been returned, or clear has been called since.

      Returns:
      the key for the branch chosen for the most recent match
    • 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.

      Parameters:
      target - the object to match against
      Returns:
      true if the matching is successful, false if it fails. Which Variables are bound by a successful or failed match depends.
      See Also:
    • matchAgain

      public boolean matchAgain()
      Attempts to re-match this pattern against a previously matched object.

      If the class of this pattern imposes a specific order on matches, they are chosen in that order, beginning with the second one. The behaviour of this method is unspecified if the last matching attempt of this pattern has not been successful.

      Patterns classes that do not override this method are deterministic. Pattern classes that overwrite this method to provide additional matches should also override Pattern.isDeterministic().

      Returns:
      true if the matching is successful, false if it fails. Which Variables are bound by a successful match depends. The default implementation always returns false.
      See Also:
    • cut

      public void cut(boolean recursively)
      Disposes of all temporary internal state related to nondeterminism.

      The default implementation does nothing. Subclasses that override this method should also override Pattern.clear(boolean) and ensure that it implies the effect of cut(false).

      Parameters:
      recursively - true if all subpatterns should traversed, false otherwise.
    • 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.

      Parameters:
      recursively - true if all subpatterns should be traversed, false otherwise.
    • binds

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

      The default implementation returns false.

      Parameters:
      v - a pattern variable
      Returns:
      true if the variable is guaranteed to have a meaningful value when this pattern is open, false otherwise.
    • preserves

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

      The default implementation returns false.

      Parameters:
      v - a pattern variable
      success - specifies preservation on success or failure
      Returns:
      true if the variable value is guaranteed to be unaffected by a matching attempt with this pattern that returns a value equal to success.
    • preserves

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

      The default implementation returns false.

      Parameters:
      v - a pattern variable
      Returns:
      true if the variable value is guaranteed to be unaffected by any matching attempt with this pattern.
    • isDeterministic

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

      The default implementation returns false.

      Returns:
      true if this pattern is guaranteed to match any object at most once, false otherwise.
      See Also:
    • fork

      public Pattern<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 Variable.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.
      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