Made schedule methods more fluent, made execution path of main schedule method

more obvious to the compiler.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2597 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2009-07-20 22:28:05 +00:00
parent 6c69cd0b20
commit 9f56deec63
+45 -39
View File
@@ -123,19 +123,23 @@ public abstract class Interval
/** /**
* Schedules this interval to execute once at <code>when</code>. Supersedes any previous * Schedules this interval to execute once at <code>when</code>. Supersedes any previous
* schedule that this Interval may have had. * schedule that this Interval may have had.
*
* @return this for convenient method chaining.
*/ */
public final void schedule (Date when) public final Interval schedule (Date when)
{ {
schedule(when.getTime() - System.currentTimeMillis()); return schedule(when.getTime() - System.currentTimeMillis());
} }
/** /**
* Schedule the interval to execute once, after the specified delay. Supersedes any previous * Schedule the interval to execute once, after the specified delay. Supersedes any previous
* schedule that this Interval may have had. * schedule that this Interval may have had.
*
* @return this for convenient method chaining.
*/ */
public final void schedule (long delay) public final Interval schedule (long delay)
{ {
schedule(delay, 0L); return schedule(delay, 0L);
} }
/** /**
@@ -145,10 +149,12 @@ public abstract class Interval
* <p> Note: if a repeating interval is scheduled to post itself to a RunQueue and the target * <p> Note: if a repeating interval is scheduled to post itself to a RunQueue and the target
* RunQueue is shutdown when the interval expires, the interval will cancel itself and log a * RunQueue is shutdown when the interval expires, the interval will cancel itself and log a
* warning message. </p> * warning message. </p>
*
* @return this for convenient method chaining.
*/ */
public final void schedule (long delay, boolean repeat) public final Interval schedule (long delay, boolean repeat)
{ {
schedule(delay, repeat ? delay : 0L); return schedule(delay, repeat ? delay : 0L);
} }
/** /**
@@ -159,10 +165,12 @@ public abstract class Interval
* <p> Note: if a repeating interval is scheduled to post itself to a RunQueue and the target * <p> Note: if a repeating interval is scheduled to post itself to a RunQueue and the target
* RunQueue is shutdown when the interval expires, the interval will cancel itself and log a * RunQueue is shutdown when the interval expires, the interval will cancel itself and log a
* warning message. </p> * warning message. </p>
*
* @return this for convenient method chaining.
*/ */
public final void schedule (long initialDelay, long repeatDelay) public final Interval schedule (long initialDelay, long repeatDelay)
{ {
schedule(initialDelay, repeatDelay, true); return schedule(initialDelay, repeatDelay, true);
} }
/** /**
@@ -179,46 +187,33 @@ public abstract class Interval
* {@link Timer#schedule(TimerTask, long, long)} which ensures that there will be close to * {@link Timer#schedule(TimerTask, long, long)} which ensures that there will be close to
* <code>repeateDelay</code> milliseconds between expirations. * <code>repeateDelay</code> milliseconds between expirations.
* *
* @return this for convenient method chaining.
*
* @exception IllegalArgumentException if fixedRate is false and a RunQueue has been specified. * @exception IllegalArgumentException if fixedRate is false and a RunQueue has been specified.
* That doesn't make sense because the fixed delay cannot account for the time that the * That doesn't make sense because the fixed delay cannot account for the time that the
* RunBuddy sits on the RunQueue waiting to call expire(). * RunBuddy sits on the RunQueue waiting to call expire().
*/ */
public final void schedule (long initialDelay, long repeatDelay, boolean fixedRate) public final Interval schedule (long initialDelay, long repeatDelay, boolean fixedRate)
{ {
cancel(); cancel();
IntervalTask task = new IntervalTask(this); IntervalTask task = new IntervalTask(this);
_task = task; _task = task;
// try twice to schedule the task- see comment inside the catch // try twice to schedule the task- see comment inside the catch
for (int tryCount = 0; tryCount < 2; tryCount++) { try {
try { scheduleTask(initialDelay, repeatDelay, fixedRate);
if (repeatDelay == 0L) {
_timer.schedule(task, initialDelay);
} else if (fixedRate) {
_timer.scheduleAtFixedRate(task, initialDelay, repeatDelay);
} else if (_runQueue != null) {
throw new IllegalArgumentException(
"Cannot schedule at a fixed delay when using a RunQueue.");
} else {
_timer.schedule(task, initialDelay, repeatDelay);
}
return;
} catch (IllegalStateException ise) { } catch (IllegalStateException ise) {
// Timer.schedule will only throw this if the TimerThead was shut down. // Timer.schedule will only throw this if the TimerThead was shut down.
// This may happen automatically in Applets, so we need to create a new // This may happen automatically in Applets, so we need to create a new
// Timer now. Note that in a multithreaded environment it may be possible // Timer now. Note that in a multithreaded environment it may be possible
// to have more than one Timer after this happens. That would be slightly // to have more than one Timer after this happens. That would be slightly
// undesirable but should not break anything. // undesirable but should not break anything.
if (tryCount == 0) { _timer = createTimer();
_timer = createTimer(); scheduleTask(initialDelay, repeatDelay, fixedRate);
} else {
// the second time through? Throw it!
throw ise;
}
}
} }
return this;
} }
/** /**
@@ -234,9 +229,20 @@ public abstract class Interval
} }
} }
/** protected void scheduleTask (long initialDelay, long repeatDelay, boolean fixedRate)
* Safely expire the interval. {
*/ if (repeatDelay == 0L) {
_timer.schedule(_task, initialDelay);
} else if (fixedRate) {
_timer.scheduleAtFixedRate(_task, initialDelay, repeatDelay);
} else if (_runQueue != null) {
throw new IllegalArgumentException(
"Cannot schedule at a fixed delay when using a RunQueue.");
} else {
_timer.schedule(_task, initialDelay, repeatDelay);
}
}
protected final void safelyExpire (IntervalTask task) protected final void safelyExpire (IntervalTask task)
{ {
// only expire the interval if the task is still valid // only expire the interval if the task is still valid