public abstract class Pattern<A> extends Object
A pattern can be matched against a given object, resulting in zero or more successful matches. As a side effect of a successful match, pattern variables may be bound. They retain their bound values until the next matching attempt. The value of variables is unspecified before a match has been attempted, and after a failed attempt. Which variables are bound by a successful match is specific to the class of the pattern.
The first successful match is retrieved using the
method. All subsequent matches on the same object are retrieved
using the matchAgain()
method. Any call to these methods
invalidates the variables bound by previous calls. Patterns may be
reused arbitrarily. Behaviour is unspecified if usage times
overlap, either concurrently or recursively.
A class of patterns that yield multiple successful matches for an object may either guarantee a certain order in which the matches are produced, or leave the order unspecified. A pattern class is ordered if such an order exists for all matched objects, otherwise unordered.
A class of patterns may also guarantee that there is at most one successful match for any object. A pattern class is deterministic if there is at most one match, otherwise nondeterministic. Nondeterministic patterns typically require internal memory to keep track of subsequent matches. That memory can be discarded explicitly, forfeiting remaining matches for the same object, and potentially allowing some heap space to be reclaimed.
A pattern is always in one of three states:
Modifier and Type | Class and Description |
---|---|
static class |
Pattern.Atomic<A> |
static class |
Pattern.Binary<A,B,C> |
static class |
Pattern.Contravariant<A> |
class |
Pattern.Proxy |
static class |
Pattern.Transform<A,B> |
static class |
Pattern.Unary<A,B> |
static class |
Pattern.Variable<A>
Pattern variable.
|
Modifier and Type | Field and Description |
---|---|
static Pattern.Contravariant<Object> |
any |
static Pattern.Contravariant<Object> |
none |
static Pattern.Contravariant<Object> |
p_null |
Modifier | Constructor and Description |
---|---|
protected |
Pattern()
Base constructor with no
toString representation. |
Modifier and Type | Method and Description |
---|---|
Pattern.Contravariant<A> |
and(Pattern<A> p) |
static <A> Pattern.Contravariant<A> |
any()
Return a pattern that matches anything.
|
abstract boolean |
bindsVariable(Pattern.Variable x) |
static <A> Pattern.Contravariant<A> |
both(Pattern<? super A> p,
Pattern<? super A> q)
Combine two patterns conjunctively.
|
void |
clear(boolean recursively) |
void |
cut(boolean recursively) |
abstract boolean |
disturbsVariable(Pattern.Variable x) |
static <A> Pattern.Contravariant<A> |
either(Pattern<? super A> p,
Pattern<? super A> q)
Combine two patterns disjunctively.
|
static <A> Pattern.Contravariant<A> |
eq(A a)
Return a pattern that matches objects equal to a given
reference.
|
static <A> Pattern<Iterable<? extends A>> |
first(Pattern<? super A> elem) |
Pattern<Object> |
forInstancesOf(Class<? extends A> cls) |
abstract boolean |
isDeterministic() |
Pattern<A> |
limit(int n) |
abstract boolean |
match(A x)
Attempt to match this pattern against a given object.
|
boolean |
matchAgain()
Attempt to re-match this pattern against a previously matched
object.
|
boolean |
matchOnce(A x) |
static <A> Pattern.Contravariant<A> |
none()
Return a pattern that matches nothing.
|
Pattern.Contravariant<A> |
or(Pattern<A> p) |
static <A> Pattern.Contravariant<A> |
p_null()
Return a pattern that matches only the
null reference. |
abstract boolean |
savesVariable(Pattern.Variable x) |
Pattern<Iterable<? extends A>> |
somewhere()
Return a pattern that attempts to match this pattern
successively to all elements of an
Iterable . |
<B> Pattern<B> |
transform(Function<? super B,? extends A> fun) |
Pattern<A> |
uniquely()
Returns a pattern that matches if and only if this pattern has a
unique match.
|
static <A> Pattern.Variable<A> |
variable()
Creates a new anonymous pattern variable.
|
static <A> Pattern.Variable<A> |
variable(String name)
Creates a new named pattern variable.
|
public static final Pattern.Contravariant<Object> any
public static final Pattern.Contravariant<Object> none
public static final Pattern.Contravariant<Object> p_null
protected Pattern()
toString
representation.
Creates an inactive pattern.public abstract boolean match(A x)
public boolean matchAgain()
Patterns classes that do not overwrite this method guarantee
public void cut(boolean recursively)
public void clear(boolean recursively)
public boolean matchOnce(A x)
public abstract boolean bindsVariable(Pattern.Variable x)
public abstract boolean disturbsVariable(Pattern.Variable x)
public abstract boolean savesVariable(Pattern.Variable x)
public abstract boolean isDeterministic()
public static <A> Pattern.Variable<A> variable()
public static <A> Pattern.Variable<A> variable(String name)
public final Pattern<A> uniquely()
A patterns of this class is deterministic.
public static final <A> Pattern.Contravariant<A> any()
public static final <A> Pattern.Contravariant<A> none()
A pattern returned by this method is vacuously deterministic and binds all variables.
public static <A> Pattern.Contravariant<A> eq(A a)
Object.equals(java.lang.Object)
if an object
is given, or by ==
if null
is given instead.
A pattern returned by this method is deterministic and bings no variables.
a
- an object to compare with or null
.public static <A> Pattern.Contravariant<A> p_null()
null
reference.
A pattern returned by this method is deterministic and binds no variables.
public Pattern.Contravariant<A> and(Pattern<A> p)
public Pattern.Contravariant<A> or(Pattern<A> p)
public static final <A> Pattern.Contravariant<A> both(Pattern<? super A> p, Pattern<? super A> q)
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.
p
- an argument patternq
- another argument patternpublic static final <A> Pattern.Contravariant<A> either(Pattern<? super A> p, Pattern<? super A> q)
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.
p
- an argument patternq
- another argument patternpublic Pattern<Iterable<? extends A>> somewhere()
Iterable
.
A pattern returned by this method is generally nondeterministic. It is ordered if the underlying pattern is ordered. It binds all variables bound by the underlying pattern.
see also the complete user documentation .