Class IntervalChecker
java.lang.Object
de.arstwo.twotil.IntervalChecker
A utility class designed for managing and checking time intervals in a thread-safe manner.
This class simplifies the boilerplate code necessary to handle interval checking.
Usage example:
IntervalChecker everyMinute = IntervalChecker.every(1, ChronoUnit.MINUTES);
if (everyMinute.updateIfDue()) {
// Do the task...
}
// alternatively:
everyMinute.executeIfDue(myTask::run);
Timer checks are performed atomically to ensure that if multiple threads attempt to reset the timer, only one will succeed and trigger due task execution.
-
Method Summary
Modifier and TypeMethodDescriptionstatic IntervalCheckerevery(long amount, TemporalUnit unit) Creates a new IntervalChecker with the specified time interval.static IntervalCheckerCreates a new IntervalChecker with the specified time interval.booleanexecute()Checks (and possibly resets) the timer, then performs the previously set tasks (if any) accordingly.booleanExecutes the previously set task if the interval has expired.booleanExecutes the previously set task if the interval has not expired.booleanexecuteTaskIfDue(Runnable task) Executes the specified operation if the interval has expired.booleanexecuteTaskIfNotDue(Runnable task) Checks the timer and executes the given task if not expired, otherwise execute the due task (if set) and resets the timer.booleanexecuteTasks(Runnable onDue, Runnable onNotDue) If the timer is due it is reset and ifDue is executed, otherwise ifNotDue is executed without a timer reset.forceDue()Forces the timer to indicate that the interval time has expired at the next check.booleanisDue()Checks whether or not this timer is due right now without excecuting any tasks.Forces a new time interval without calling any tasks.booleanupdate()Checks the interval and atomically resets the timer if the time has expired.Specifies a fixed task to run on execute..whenNotDue(Runnable task) Specifies a fixed task to run on execute...
-
Method Details
-
every
Creates a new IntervalChecker with the specified time interval. The returned timer is marked as "due".- Parameters:
amount- The amount of the specified unit.unit- The time unit, usuallyChronoUnit.- Returns:
- An IntervalChecker set to the specified time interval.
- See Also:
-
every
Creates a new IntervalChecker with the specified time interval. The returned timer is marked as "due".- Parameters:
duration- interval between actions.- Returns:
- An IntervalChecker set to the specified time interval.
- See Also:
-
update
public boolean update()Checks the interval and atomically resets the timer if the time has expired. Will not execute any set tasks.- Returns:
- true if the time had expired, otherwise false.
-
executeTasks
If the timer is due it is reset and ifDue is executed, otherwise ifNotDue is executed without a timer reset.- Parameters:
onDue- task to execute if due, or null.onNotDue- task to execute if not due, or null.- Returns:
- true if the timer was due, otherwise false.
-
execute
public boolean execute()Checks (and possibly resets) the timer, then performs the previously set tasks (if any) accordingly.- Returns:
- true if the interval had expired, otherwise false.
-
whenDue
Specifies a fixed task to run on execute.. checks if the timer is due.- Parameters:
task- any runnable.- Returns:
- this, for convenience.
-
whenNotDue
Specifies a fixed task to run on execute... checks if the timer is not due.- Parameters:
task- any runnable.- Returns:
- this, for convenience.
-
executeIfDue
public boolean executeIfDue()Executes the previously set task if the interval has expired. In that case, also resets the timer. Will not run any not-due task.- Returns:
- true if the interval had expired, otherwise false.
-
executeTaskIfDue
Executes the specified operation if the interval has expired. In that case, also resets the timer. Will not run any not-due task.- Parameters:
task- The task to be performed.- Returns:
- true if the interval had expired, otherwise false.
-
executeIfNotDue
public boolean executeIfNotDue()Executes the previously set task if the interval has not expired. Will not run any due task.- Returns:
- true if the interval had expired, otherwise false.
-
executeTaskIfNotDue
Checks the timer and executes the given task if not expired, otherwise execute the due task (if set) and resets the timer. Will not run any due task.- Parameters:
task- The task to be performed if not due.- Returns:
- true if the interval had expired, otherwise false.
-
isDue
public boolean isDue()Checks whether or not this timer is due right now without excecuting any tasks.- Returns:
- true if it is due right now, otherwise false.
-
restartTimer
Forces a new time interval without calling any tasks.- Returns:
- this, for convenience.
-
forceDue
Forces the timer to indicate that the interval time has expired at the next check.- Returns:
- this, for convenience.
-