- Type Parameters:
T- The type of the value captured by this capture variable
public final class Capture<T> extends Pattern<T>
<T> is a pattern that matches everything and, when
matched, stores the value that it matched. Within a single pattern match,
the capture must not be matched twice. (It is, however, possible to have the
same capture occur in mutually exclusive parts of the pattern, see, e.g.,
Patterns.Or(de.unruh.javapatterns.Pattern<? super T>...).)
The value that was assigned to the capture
x can be read using
x.. This is possible both in the action of a pattern match
Case) and in the pattern itself. This makes it possible to make
parts of the pattern depend on values that have been captured already. (E.g.,
arrays with two identical entries.)
Patterns.Array(de.unruh.javapatterns.Pattern<? super T>...)(x,
Captures need to be declared before the pattern match with a given type, e.g.,
Capture<String> x =However, the value of the capture is local to one case of the pattern match. (So, logically, a capture is a local variable within the case.) A capture can be used in several cases in the same match, or in different matches, but it is not thread-safe (i.e., not in concurrent matches).
Capture("x"); ... match(value, ... patterns using x ...)
Modifier and Type Method Description
apply(@NotNull MatchManager mgr, T value)Performs the pattern match.
isAssigned()Returns whether this capture has been assigned.
toString()Should give a human readable representation of this pattern.
v()The current value of the capture.
public java.lang.String toString()Description copied from class:
PatternShould give a human readable representation of this pattern. Used in error messages.
public boolean isAssigned()Returns whether this capture has been assigned.
@Contract(pure=true) public T v()The current value of the capture.
InvalidPatternMatch- if the capture has not been assigned in the current pattern
Description copied from class:
PatternPerforms the pattern match. See the
Must only be invoked from withing an
apply. (I.e., a pattern may invoke
applyon its subpatterns from within its own