public class Count extends AbstractFaAction
counts matches and can then be used with If
to perform
conditional actions depending on whether a threshold count is
reached. To retrieve and store the current count,
the callback obtains a Map
from a MapProvider
that has to be in the DfaRun.clientData
field of
the calling DfaRun
. The value is stored in the
Map
with the key specified in the constructor. The
same key may be used in a Printf
callback's format like
"%(key)
to insert the value into the output stream.
Whenever invoke
is called for a
successfull match, the counter is incremented by one.
The counter can be tested against a threshold with the Verifier
returned by ge()
. Typically this verifier
will be passed to an instance of If
. An example is
Count cnt = new Count("no_of_fishes"); ... nfa.or("start", cnt.reset()) .or("fish", cnt) .or("end", new If(cnt.ge(2), a1, a2))
The callback of If
will call ok()
on the
Verifier
returned by cnt.ge(2)
and
perform either action a1
or a2
.
To add another increment then 1 to the counter, use the action
created by add(int)
.
Constructor and Description |
---|
Count(java.lang.String key)
create a counter callback which records the its value with
key . |
Modifier and Type | Method and Description |
---|---|
AbstractFaAction |
add(int incr)
returns an
FaAction which changes the counter
according to the
given increment incr . |
Verifier |
ge(int threshold)
returns a
Verifier which returns true if
the counter is greater than or equal to the given
threshold. |
int |
getValue(DfaRun r)
given the
DfaRun which has just the right
MapProvider in its clientData field,
this returns the current counter value. |
void |
invoke(java.lang.StringBuilder yytext,
int start,
DfaRun r)
is called by methods of
DfaRun in case of a
match. |
AbstractFaAction |
reset()
returns an
FaAction which resets the counter to
zero. |
mergeWith, setPriority
public Count(java.lang.String key)
create a counter callback which records the its value with
key
.
public void invoke(java.lang.StringBuilder yytext, int start, DfaRun r)
FaAction
is called by methods of DfaRun
in case of a
match. Parameter yytext
contains the matching text
from position start
onwards. The callback may change
the whole of yytext
, but the under most
circumstances, only the matching text should be
changed. Parameter runner
is the DfaRun
object which called this function. Of interest are its fields
DfaRun.clientData
and DfaRun.collect
.
public int getValue(DfaRun r)
given the DfaRun
which has just the right
MapProvider
in its clientData
field,
this returns the current counter value.
public AbstractFaAction reset()
returns an FaAction
which resets the counter to
zero.
public AbstractFaAction add(int incr)
FaAction
which changes the counter
according to the
given increment incr
. Example use:Count cnt = new Count(7); ... nfa.or("pattern with weight 2", cnt.add(2));