java.lang.Object
page.codeberg.friedolyn.util.Countdown
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAn object that is interested in thecountdown's progressand that will be notified about any changes to it.classDecreases thecountdownby one second, if the current value is greater than 1 second.static enumIndicates what a countdown is currently doing or what it has done. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate @NonNull DurationThe time remaining until the countdown reaches zero.private @NonNull Countdown.CountdownTaskDecreases thecountdownby one second, if the current value is greater than 1 second.private final @NonNull DurationThe time interval that the countdown started with.private LocalDateTimeUsed byresume()to prevent the countdown from being fast-forwarded accidentally.private @NonNull Set<@NonNull Countdown.CountdownListener> All objects that are interested in the countdown's progress.private final @NonNull ReentrantLockprivate LocalDateTimeUsed byresume()to prevent the countdown from being fast-forwarded accidentally.private @NonNull Countdown.StatusIndicates what the countdown is currently doing or what it has done. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Aborts the countdown and sets it to zero.@NonNull Countdowncopy()voidfinish()Prematurely sets the countdown to zero andnotifiesall listeners about it.@NonNull Durationget()voidpause()Temporarily halts the countdown at the current value untilresume()is called.voidregisterListener(@NonNull Countdown.CountdownListener listener) Adds aCountdown.CountdownListenerto thelist of objectsthat are interested in the countdown's progress.booleanremoveListener(@NonNull Countdown.CountdownListener listener) Deletes an already registeredCountdown.CountdownListenerfrom thelist of objectsthat are interested in the countdown's progress.voidreset()Sets thecountdownback to theinitial time.voidrestart()Sets thecountdownback to theinitial timeand immediately launches the countdown again.voidresume()Continues the countdown from where it waspaused.voidstart()Launches the timer that decreases thecountdownby one second every second.private voidLaunches the timer that decreases thecountdownby one second every second, after waiting for the specified delay.
-
Field Details
-
countdown
The time remaining until the countdown reaches zero. -
initialTime
The time interval that the countdown started with. -
lastUpdate
Used byresume()to prevent the countdown from being fast-forwarded accidentally. TL;DR: When pausing the countdown, that never happens precisely at a whole second, resulting in a small delay that we must wait for when resuming the countdown. -
pausedAt
Used byresume()to prevent the countdown from being fast-forwarded accidentally. TL;DR: When pausing the countdown, that never happens precisely at a whole second, resulting in a small delay that we must wait for when resuming the countdown. -
LOCK
-
status
Indicates what the countdown is currently doing or what it has done. -
listeners
All objects that are interested in the countdown's progress. Whenever the countdown is updated, all listeners are notified viaCountdown.CountdownListener.onUpdate(Duration). Once the countdown reaches zero, all listeners are notified viaCountdown.CountdownListener.onFinished(). -
countdownTask
Decreases thecountdownby one second, if the current value is greater than 1 second.
-
-
Constructor Details
-
Countdown
Creates a new countdown with the specified initial time.- Parameters:
duration- The time that should be counted down until it reaches zero. Must not be negative.- Throws:
IllegalArgumentException- If the given duration is negative.
-
-
Method Details
-
registerListener
Adds aCountdown.CountdownListenerto thelist of objectsthat are interested in the countdown's progress. Whenever the countdown is updated, all listeners are notified viaCountdown.CountdownListener.onUpdate(Duration). Once the countdown reaches zero, all listeners are notified viaCountdown.CountdownListener.onFinished().- Parameters:
listener- The new listener that will be notified about the countdown's progress.
-
removeListener
Deletes an already registeredCountdown.CountdownListenerfrom thelist of objectsthat are interested in the countdown's progress. Use the exact same reference that was passed toregisterListener(CountdownListener)previously.- Parameters:
listener- The currently registered listener that will be removed from the list of listeners.- Returns:
trueif the listener was removed,falseif the listener could not be found.
-
get
-
start
Launches the timer that decreases thecountdownby one second every second.Notifiesalllistenersabout the start.- Throws:
IllegalStateException- If the countdown has already been started.- Implementation Note:
- Relies on
start(Duration)tonotifythe listeners and actually start the countdown; i.e. this method does neither of these things itself. This means that the listeners will not be notified twice, and the countdown will not be started twice.
-
start
Launches the timer that decreases thecountdownby one second every second, after waiting for the specified delay.Notifiesalllistenersabout the start.- Parameters:
delay- The time to wait before starting the countdown. Must not be negative.- Throws:
IllegalArgumentException- If the delay is negative.IllegalStateException- If the countdown has already been started.
-
pause
-
resume
- Throws:
IllegalStateException- If the countdown has already been resumed or if it had not been paused before.- Implementation Note:
- When the countdown is
paused, this happens a fraction of a second before the next update of thecountdownwould have been made (i.e. we always pause the countdown a little bit too early). That means, when resuming the countdown, we must wait for that fraction of a second before continuing the countdown. (Otherwise, the user could fast-forward the countdown by rapidly pausing and resuming it in quick succession.)
-
restart
public void restart()Sets thecountdownback to theinitial timeand immediately launches the countdown again.Notifiesalllistenersabout the reset.- See Also:
-
reset
public void reset()Sets thecountdownback to theinitial time. Will not automatically start the countdown again.- See Also:
-
cancel
-
finish
-
copy
-