Add setDuration(long) method to CountdownTimer.
The purpose of this method is to allow initialization of the duration post-construction for convenience.
This commit is contained in:
@@ -33,7 +33,7 @@ public abstract class AbstractTimer implements Timer {
|
||||
@Override
|
||||
public void setInterval(long interval) {
|
||||
if (interval <= 0l) {
|
||||
throw new IllegalArgumentException("Tick interval must be positive.");
|
||||
throw new IllegalArgumentException("Tick interval must be positive: " + interval);
|
||||
}
|
||||
this.interval = interval;
|
||||
}
|
||||
|
||||
@@ -45,10 +45,7 @@ public class CountdownTimer extends AbstractTimer {
|
||||
public CountdownTimer(Plugin plugin, long duration, long interval, TimerCallback callback) {
|
||||
super(plugin, interval, callback);
|
||||
|
||||
if (duration < 0l) {
|
||||
throw new IllegalArgumentException("Duration must be non-negative.");
|
||||
}
|
||||
this.duration = duration;
|
||||
setDuration(duration);
|
||||
this.remaining = 0l;
|
||||
this.timer = null;
|
||||
}
|
||||
@@ -99,6 +96,22 @@ public class CountdownTimer extends AbstractTimer {
|
||||
this(plugin, duration, duration, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an uninitialized (0 duration) CountdownTimer.
|
||||
* <p>
|
||||
* This constructor leaves the timer in an inconsistent state until the
|
||||
* {@link #setCallback(TimerCallback)} method is called with a valid
|
||||
* callback object.
|
||||
* <p>
|
||||
* The CountdownTimer acts as a Null Object until a positive duration
|
||||
* is set via the {@link #setDuration(long)} method.
|
||||
*
|
||||
* @param plugin the plugin responsible for the timer
|
||||
*/
|
||||
public CountdownTimer(Plugin plugin) {
|
||||
this(plugin, 0, 1, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the timer.
|
||||
* <p>
|
||||
@@ -154,6 +167,24 @@ public class CountdownTimer extends AbstractTimer {
|
||||
return duration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the duration of the timer.
|
||||
* <p>
|
||||
* This method should only be used to set the duration post-construction
|
||||
* if it is inconvenient (or impossible) to set it during construction.
|
||||
* <p>
|
||||
* Changing the duration while the timer is running is not recommended,
|
||||
* because external classes may depend on it remaining constant.
|
||||
*
|
||||
* @param duration the duration of the timer; must be non-negative
|
||||
*/
|
||||
public synchronized void setDuration(long duration) {
|
||||
if (duration < 0l) {
|
||||
throw new IllegalArgumentException("Duration must be non-negative: " + duration);
|
||||
}
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the remaining number of ticks before this timer runs out.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user