public class RepeatGuard
extends java.lang.Object
A reduction of the number of executions can be useful if that code is resource intensive (e.g. DB access, remote calls, logging) or has other unwanted side effects if executed too often (e.g. annoying the user with too frequent error or confirmation dialogues). Of course this only makes sense if the nature of the problem allows skipping some of these executions.
This class is so general that it does not actually execute any code; it simply keeps track of counters and timer.
It can be used directly to manage decisions about skipping or executing some code.
For example, the application can instantiate a RepeatGuard with the timer set to one second.
Then whenever the repetitive action would be called, the application first calls check();
for every timer interval, only the first call to check() will return true,
while the subsequent calls will return false, and the application should skip the repeated block of code.
Alternatively this class can be extended or wrapped to become easier to use for specific purposes.
Then typically the code to be executed (e.g. logging a message) becomes part of the specialized repeat guard class,
as in RepeatGuardLogger.
The concept of repeat guards in ACS is discussed at http://almasw.hq.eso.org/almasw/bin/view/ACS/LoggingRepetitionControl
Repetitions can be reduced using
OR combination of the above: either enough attempts were made or enough time has passed, whatever happens first
AND combination: enough skipped execution attempts, and enough time passed.
RepeatGuard.Logic enum.| Modifier and Type | Class and Description |
|---|---|
static class |
RepeatGuard.Logic
Evaluation logic: TIMER (time based guard), COUNTER (count based guard), AND/OR (conjunction/disjunction or both)
|
| Constructor and Description |
|---|
RepeatGuard(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions)
Constructor, convenience for the above, using
Logic.OR evaluation method
if both interval and maxRepetitions are positive values,
otherwise or to make sure that only the respective parameter with a positive value gets used. |
RepeatGuard(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions,
RepeatGuard.Logic logic)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
check()
This method checks if the guarded activity is due for execution, or if it should be skipped instead.
|
boolean |
checkAndIncrement()
Increments the counter and checks (see
check()). |
int |
counter()
Get current counter value.
|
int |
counterAtLastExecution()
Gets the value of the counter that it had when the
check() or
checkAndIncrement() method returned true the last time,
which corresponds to the number of times the activity was skipped before it got executed. |
void |
increment()
Increase counter value.
|
void |
reset()
Resets this guard without changing the configuration for timer, counter and logic.
|
void |
reset(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions)
Resets and reconfigures this guard using the given interval, maxRepetitions, and
Logic.OR logic. |
void |
reset(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions,
RepeatGuard.Logic logic)
Resets and reconfigures logic of guard.
|
public RepeatGuard(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions,
RepeatGuard.Logic logic)
interval - Time interval (in timeUnit units).timeUnit - Time unit of interval parameter.maxRepetitions - Maximum number of repetitions.logic - Evaluation logic for interval and maxRepetitions.
The logic will be "reduced" automatically if interval or maxRepetitions
have a value <= 0, so as to be based only on the other positive value.java.lang.IllegalArgumentException - if maxRepetitions <= 0 && interval <= 0public RepeatGuard(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions)
Logic.OR evaluation method
if both interval and maxRepetitions are positive values,
otherwise or to make sure that only the respective parameter with a positive value gets used.interval - Time interval (in timeUnit units).timeUnit - Time unit of interval parameter.maxRepetitions - Maximum number of repetitions.java.lang.IllegalArgumentException - if maxRepetitions <= 0 && interval <= 0public boolean check()
For the first call, it always returns true.
Later it returns true if the last call to check()was longer ago than the interval
given in the constructor or in the reset methods,
and/or if the internal counter has been incremented more than maxRepetitions
by calls to increment() or checkAndIncrement().
true if guarded activity should be run, false if it should be skipped.public boolean checkAndIncrement()
check()).true if OK, false if should be guardedcheck()public void reset(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions)
Logic.OR logic.interval - Time interval (in timeUnit units).timeUnit - Time unit of interval parameter.maxRepetitions - Maximum number of skipped repetitions.java.lang.IllegalArgumentException - if maxRepetitions <= 0 && interval <= 0RepeatGuard(long, TimeUnit, int, Logic)public void reset(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions,
RepeatGuard.Logic logic)
interval - Time interval (in timeUnit units).timeUnit - Time unit of interval parameter.maxRepetitions - Maximum number of skipped repetitions.logic - Evaluation logic for interval and maxRepetitions.
The logic will be "reduced" automatically if interval or maxRepetitions
have a value <= 0, so as to be based only on the other positive value.java.lang.IllegalArgumentException - if maxRepetitions <= 0 && interval <= 0 or required arg == nullpublic void reset()
public void increment()
public int counter()
public int counterAtLastExecution()
check() or
checkAndIncrement() method returned true the last time,
which corresponds to the number of times the activity was skipped before it got executed.
Calling this method does not make sense if only timer logic was used (no counters configured nor incremented)