diff --git a/src/com/garbagemule/MobArena/util/timer/AbstractTimer.java b/src/com/garbagemule/MobArena/util/timer/AbstractTimer.java index 6fa3d07..dc37722 100644 --- a/src/com/garbagemule/MobArena/util/timer/AbstractTimer.java +++ b/src/com/garbagemule/MobArena/util/timer/AbstractTimer.java @@ -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; } diff --git a/src/com/garbagemule/MobArena/util/timer/CountdownTimer.java b/src/com/garbagemule/MobArena/util/timer/CountdownTimer.java index 5ddb6aa..678fa42 100644 --- a/src/com/garbagemule/MobArena/util/timer/CountdownTimer.java +++ b/src/com/garbagemule/MobArena/util/timer/CountdownTimer.java @@ -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. + *

+ * This constructor leaves the timer in an inconsistent state until the + * {@link #setCallback(TimerCallback)} method is called with a valid + * callback object. + *

+ * 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. *

@@ -154,6 +167,24 @@ public class CountdownTimer extends AbstractTimer { return duration; } + /** + * Set the duration of the timer. + *

+ * This method should only be used to set the duration post-construction + * if it is inconvenient (or impossible) to set it during construction. + *

+ * 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. *