java.lang.Object
page.codeberg.friedolyn.util.Countdown
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
An object that is interested in thecountdown's progress
and that will be notified about any changes to it.class
Decreases thecountdown
by one second, if the current value is greater than 1 second.static enum
Indicates what a countdown is currently doing or what it has done. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate @NonNull Duration
The time remaining until the countdown reaches zero.private @NonNull Countdown.CountdownTask
Decreases thecountdown
by one second, if the current value is greater than 1 second.private final @NonNull Duration
The time interval that the countdown started with.private LocalDateTime
Used 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 ReentrantLock
private LocalDateTime
Used byresume()
to prevent the countdown from being fast-forwarded accidentally.private @NonNull Countdown.Status
Indicates what the countdown is currently doing or what it has done. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
cancel()
Aborts the countdown and sets it to zero.@NonNull Countdown
copy()
void
finish()
Prematurely sets the countdown to zero andnotifies
all listeners about it.@NonNull Duration
get()
void
pause()
Temporarily halts the countdown at the current value untilresume()
is called.void
registerListener
(@NonNull Countdown.CountdownListener listener) Adds aCountdown.CountdownListener
to thelist of objects
that are interested in the countdown's progress.boolean
removeListener
(@NonNull Countdown.CountdownListener listener) Deletes an already registeredCountdown.CountdownListener
from thelist of objects
that are interested in the countdown's progress.void
reset()
Sets thecountdown
back to theinitial time
.void
restart()
Sets thecountdown
back to theinitial time
and immediately launches the countdown again.void
resume()
Continues the countdown from where it waspaused
.void
start()
Launches the timer that decreases thecountdown
by one second every second.private void
Launches the timer that decreases thecountdown
by 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 thecountdown
by 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.CountdownListener
to thelist of 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()
.- Parameters:
listener
- The new listener that will be notified about the countdown's progress.
-
removeListener
Deletes an already registeredCountdown.CountdownListener
from thelist of objects
that 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:
true
if the listener was removed,false
if the listener could not be found.
-
get
-
start
Launches the timer that decreases thecountdown
by one second every second.Notifies
alllisteners
about the start.- Throws:
IllegalStateException
- If the countdown has already been started.- Implementation Note:
- Relies on
start(Duration)
tonotify
the 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 thecountdown
by one second every second, after waiting for the specified delay.Notifies
alllisteners
about 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 thecountdown
would 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 thecountdown
back to theinitial time
and immediately launches the countdown again.Notifies
alllisteners
about the reset.- See Also:
-
reset
public void reset()Sets thecountdown
back to theinitial time
. Will not automatically start the countdown again.- See Also:
-
cancel
-
finish
-
copy
-