Class Pattern<A>
- Type Parameters:
A- the target type (of objects to match against)
- All Implemented Interfaces:
Serializable,Cloneable
- Direct Known Subclasses:
Adaptive,Atomic,Binary,EnumBranch,IntBranch,Observable,Search,Unary,Variable
Subclasses must implement the match(A) method.
Nondeterministic subclasses must override the matchAgain()
method. Subclasses with local matching state and/or subpatterns
must override the clone() method.
Subclasses should override the isDeterministic(), binds(Variable) and preserves(Variable,boolean) methods if appropriate to improve
runtime information on pattern behavior.
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <A> Pattern<A> Combines many patterns conjunctively.static <A> Pattern<A> Combines many patterns conjunctively.static <A> Pattern<A> Combines many patterns conjunctively.static <A> Pattern<A> Combines many patterns conjunctively.static <A> Pattern<A> Combines this patterns conjunctively with another pattern.static final <A> Pattern<A> any()Returns a pattern that matches anything.static <A> Pattern<A> booleanChecks whether a variable is bound by this pattern.static final <A> Pattern<A> Combines two patterns conjunctively.final voidclear()Disposes of all internal state related to results and nondeterminism.voidclear(boolean recursively) Disposes of all internal state related to results and nondeterminism.clone()Makes a matching-state copy if this pattern.compile()protected voidcompileClear(CompilationContext context) protected voidcompileClear(CompilationContext context, boolean recursively) protected voidcompileCut(CompilationContext context) protected voidcompileCut(CompilationContext context, boolean recursively) protected voidcompileIsDeterministic(CompilationContext context) protected voidcompileMatch(CompilationContext context) protected voidcompileMatchAgain(CompilationContext context) protected VariableContext.VariablecompileSubPattern(CompilationContext context) protected VariableContext.VariablecompileThis(CompilationContext context) compileUpTo(Variable<B> hole) final voidcut()Disposes of all internal state related to nondeterminism.voidcut(boolean recursively) Disposes of all internal state related to nondeterminism.voidstatic final <A> Pattern<A> Combines two patterns disjunctively.static <A> Pattern<A> booleanChecks whether this pattern is deterministic.limit(int n) Returns a pattern that stops after a given number of matches.static voidabstract booleanAttempts to match this pattern against a given object.booleanAttempts to re-match this pattern against a previously matched object.final booleanAttempts to match this pattern at most once against a given object.booleannarrow()Returns an equivalent pattern with narrower type parameter.static final <A> Pattern<A> noMatch()Returns a pattern that matches if and only if this pattern has no match.static final <A> Pattern<A> none()Returns a pattern that matches nothing.static <A> Pattern<A> static <A> Pattern<A> Deprecated, for removal: This API element is subject to removal in a future version.Combines this patterns disjunctively with another pattern.booleanChecks whether a variable is preserved by this pattern, unconditionally.booleanChecks whether a variable is preserved by this pattern, conditionally on success or failure.static final <A> Pattern<A> repeat()static <A> Pattern<A> Combines many patterns disjunctively.static <A> Pattern<A> Combines many patterns disjunctively.static <A> Pattern<A> Combines many patterns disjunctively.static <A> Pattern<A> Combines many patterns disjunctively.protected voidstartCompile(CompilationContext context) uniquely()Returns a pattern that matches if and only if this pattern has a unique match.static <A> Variable<A> variable()Creates a new anonymous pattern variable.static <A> Variable<A> Creates a new named pattern variable.
-
Field Details
-
none
A pattern that matches nothing. This pattern may be shared and reused concurrently. -
_left_matched
- See Also:
-
_target_save
- See Also:
-
-
Constructor Details
-
Pattern
protected Pattern()Creates a new pattern.
-
-
Method Details
-
match
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.- Parameters:
target- the object to match against- Returns:
trueif the matching is successful,falseif it fails. WhichVariables 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
isDeterministic().- Returns:
trueif the matching is successful,falseif it fails. WhichVariables are bound by a successful match depends. The default implementation always returnsfalse.- See Also:
-
matchVar
-
cut
public final void cut()Disposes of all internal state related to nondeterminism. Subsequent matching attempts withmatchAgain()should fail. Invokescut(true). -
clear
public final void clear()Disposes of all internal state related to results and nondeterminism. Subsequent matching attempts withmatchAgain()should fail. Bound variables are reset to initial values. Invokesclear(true). -
cut
public void cut(boolean recursively) Disposes of all internal state related to nondeterminism.The default implementation does nothing. Subclasses that override this method should also override
clear(boolean)and ensure that it implies the effect ofcut(false).- Parameters:
recursively-trueif all subpatterns should be deactivated,falseotherwise.
-
clear
public void clear(boolean recursively) Disposes of all internal state related to results and nondeterminism.The default implementation does nothing.
- Parameters:
recursively-trueif all subpatterns should be deactivated,falseotherwise.
-
matchOnce
Attempts to match this pattern at most once against a given object.Closes the pattern immediately by invoking
cut(). -
binds
Checks whether a variable is bound by this pattern.The default implementation returns
false.- Parameters:
variable- a pattern variable- Returns:
trueif the variable is guaranteed to have a meaningful value when this pattern is open,falseotherwise.
-
preserves
Checks whether a variable is preserved by this pattern, conditionally on success or failure.The default implementation returns
false.- Parameters:
variable- a pattern variablesuccess- specifies preservation on success or failure- Returns:
trueif the variable value is guaranteed to be unaffected by a matching attempt with this pattern that returns a value equal tosuccess.
-
preserves
Checks whether a variable is preserved by this pattern, unconditionally.The default implementation returns
false.- Parameters:
variable- a pattern variable- Returns:
trueif 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
true.- Returns:
trueif this pattern is guaranteed to match any object at most once,falseotherwise.- See Also:
-
narrow
Returns an equivalent pattern with narrower type parameter. The default implementation returns this pattern and relies on the class invariants for dynamic type safety.Note that, by contrast, casting
Variable<A>toVariable<B>, whereBis a subtype ofA, is not type safe. -
clone
Makes a matching-state copy if this pattern.Subclasses must override this method if necessary to honour the following rules for all transitively reachable patterns:
- Subpatterns that are instances of
Variablemust be shared. This implies thatVariable.clone()must returnthis. - Fields that do not refer to
subpatterns, but to matching state (changed by invocations of
match(A),matchAgain(),cut()orclear()) 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
Creates a new anonymous pattern variable. -
variable
Creates a new named pattern variable. -
once
Deprecated, for removal: This API element is subject to removal in a future version.Returns a pattern which matches once by evaluating the given function object.- Type Parameters:
A- the target type- Parameters:
test- the functional object that implements the match
-
uniquely
Returns a pattern that matches if and only if this pattern has a unique match.Note that the resulting pattern invokes this pattern twice, since determining that there is no second match may clobber the state of the first match. Hence nesting such patterns is strongly discouraged.
A pattern of this class is deterministic by construction.
-
limit
Returns a pattern that stops after a given number of matches.- Parameters:
n- the maximal number of matches.- Throws:
IllegalArgumentException- ifn < 0.
-
any
Returns a pattern that matches anything. A pattern returned by this method is deterministic and binds no variables. -
none
Returns a pattern that matches nothing.A pattern returned by this method is vacuously deterministic and binds all variables.
-
repeat
-
all
Combines many patterns conjunctively. Equivalent to folding in the monoid ofboth(Pattern, Pattern)andany(). -
all
-
all
Combines many patterns conjunctively. Equivalent to folding in the monoid ofboth(Pattern, Pattern)andany(). -
all
public static <A> Pattern<A> all(Pattern<? super A> p1, Pattern<? super A> p2, Pattern<? super A> p3) Combines many patterns conjunctively. Equivalent to folding in the monoid ofboth(Pattern, Pattern)andany(). -
all
public static <A> Pattern<A> all(Pattern<? super A> p1, Pattern<? super A> p2, Pattern<? super A> p3, Pattern<? super A> p4) Combines many patterns conjunctively. Equivalent to folding in the monoid ofboth(Pattern, Pattern)andany(). -
some
Combines many patterns disjunctively. Equivalent to folding in the monoid ofeither(Pattern, Pattern)andnone(). -
some
Combines many patterns disjunctively. Equivalent to folding in the monoid ofeither(Pattern, Pattern)andnone(). -
some
public static <A> Pattern<A> some(Pattern<? super A> p1, Pattern<? super A> p2, Pattern<? super A> p3) Combines many patterns disjunctively. Equivalent to folding in the monoid ofeither(Pattern, Pattern)andnone(). -
some
public static <A> Pattern<A> some(Pattern<? super A> p1, Pattern<? super A> p2, Pattern<? super A> p3, Pattern<? super A> p4) Combines many patterns disjunctively. Equivalent to folding in the monoid ofeither(Pattern, Pattern)andnone(). -
and
Combines this patterns conjunctively with another pattern. Callsboth(this, p). -
or
Combines this patterns disjunctively with another pattern. Callseither(this, p). -
both
Combines two patterns conjunctively.A pattern returned by this method is deterministic if and only if both argument patterns are deterministic. A pattern returned by this method is ordered if and only if both argument patterns are ordered. It binds all variables bound by either argument pattern.
Since the matches of the second argument pattern are repeated for each successful match of the first, it is more efficient to give arguments in decreasing order of matching costs.
- Parameters:
left- an argument patternright- another argument pattern- Returns:
- a pattern that has a successful match for each combination of successful matches of the argument patterns. If both argument patterns are ordered, the resulting order is the lexical order of the pairs.
-
either
Combines two patterns disjunctively.A pattern returned by this method is deterministic if and only if both argument patterns are deterministic and mutually exclusive. A pattern returned by this method is ordered if and only if both argument patterns are ordered. It binds all variables bound by both argument patterns.
- Parameters:
p- an argument patternq- another argument pattern- Returns:
- a pattern that has a successful match for each successful match of either argument pattern. If both argument patterns are ordered, the resulting order is the concatenation.
-
noMatch
Returns a pattern that matches if and only if this pattern has no match.A pattern returned by this method is deterministic and binds no variables.
-
someMatch
-
enPassant
-
newAll
-
flatten
-
aside
-
andThen
-
andThen
-
orElse
-
orElse
-
startCompile
-
compileThis
-
compileMatch
-
compileMatchAgain
-
compileCut
-
compileCut
-
compileClear
-
compileClear
-
compileIsDeterministic
-
compile
-
compileUpTo
-
compileSubPattern
-
main
-
DEBUG
-
of
-
FunctionPatterns.test(java.util.function.Predicate<? super A>)instead.