All Packages Class Hierarchy This Package Previous Next Index
Class oplan.util.match.MatchTable
java.lang.Object
|
+----oplan.util.match.MatchTable
- public class MatchTable
- extends Object
A MatchTable acts as a kind of pattern-matching case statement.
The various cases are represented by MatchCase objects. Each
MatchCase contains a pattern and provides methods tryMatch and
ifSelected. tryMatch is given an object to match against the pattern,
and ifSelected is called if the match succeeds. ifSelected is given
two arguments: the object that was matched and the result from
tryMatch.
A MatchTable also contains a default MatchCase that can be set by
setDefault. If none of the ordinary cases matches, and there is
a default MatchCase, the default's ifSelected method is called with
the object to match as its first argument and null as its second.
Otherwise, the result is null.
The table and the cases it contains can cooperate in the matching
process, with some work done by each. The patterns in the cases are
normally accessible to table. This allows the table to do clever
indexing or even "compilation" of part of the match. For instance,
if all of the patterns were strings, and the table was asked to
match a non-string against those patterns, it might know that the
match was guaranteed to fail; and so it could just return null
right away, without bothering to do any further matching.
This ability of the table to exploit information obtained by
examining all of the cases is an important reason for using a
table object rather than an if-statement.
Moreover, the table contains MatchCase objects, rather than just
patterns, because that allows some things to be done when the
case object is created, rather than being repeated each time a
match is tried. For instance, patterns that are regular expressions
might be translated into descriptions of finite-state machines.
For instances of the MatchTable class itself, all of the matching
is done by the cases. The MatchTable's match method simply calls
each case's tryMatch in turn. However, a subclass might provide
something more interesting, even to the point where all the work
was done by the table without calling the tryMatch methods at all.
It might be asked what happens if the table does something that's
incompatible with the matching the cases expect to perform. The
answer is that it's up to the programmer to select compatible
table and case classes when constructing a table, so that such
things do not happen. This need for compatibility should also be
kept in mind when designing new table and case classes. Some
classes might even check various compatibility issues when they're
used.
- See Also:
- MatchCase, MatchEnv
-
cases
- The MatchCases in this MatchTable.
-
defaultCase
- This table's default case.
-
MatchTable()
-
-
addCase(MatchCase)
- addCase adds a new MatchCase after those that were
added earlier.
-
match(Object)
- match tries to find a MatchCase in the MatchTable that matches
an object.
-
matchCases(Object, Vector)
- A variant of match that can be used internally to match a subset
of the cases that has been selected in some way (such as by indexing
on a "key" in the data).
-
matchDefault(Object)
- Applies the default case, if there is one.
-
setDefault(MatchCase)
- setDefault assigns a MatchCase as the table's default case.
cases
protected Vector cases
- The MatchCases in this MatchTable.
defaultCase
protected MatchCase defaultCase
- This table's default case.
MatchTable
public MatchTable()
addCase
public void addCase(MatchCase c)
- addCase adds a new MatchCase after those that were
added earlier.
setDefault
public void setDefault(MatchCase c)
- setDefault assigns a MatchCase as the table's default case.
match
public Object match(Object data)
- match tries to find a MatchCase in the MatchTable that matches
an object. It calls the tryMatch method of each MatchCase in turn
until one returns a non-null result. Then match returns the result
of calling the same MatchCase's ifSelected method, passing it the
object that was matched and the non-null result. If all the
tryMatch calls return null, then the default case is examined.
If the default is a MatchCase, its ifSelected method is is called
on the object the table's been trying to match and null. If
the default is null, match returns null.
matchCases
protected Object matchCases(Object data,
Vector selectedCases)
- A variant of match that can be used internally to match a subset
of the cases that has been selected in some way (such as by indexing
on a "key" in the data). The MatchTable's default case is not
considered. Instead, if there's no match among the selected cases,
matchCases returns null.
matchDefault
protected Object matchDefault(Object data)
- Applies the default case, if there is one. matchDefault is
typically used with matchCases.
All Packages Class Hierarchy This Package Previous Next Index