I'd like to modify the org.gnu.glib.Timer API before more people start
using it. The problem is that it uses the Runnable interface which
defines a void method, while GLib's timeout facility uses a function
that returns a boolean. This boolean return makes it very easy to
define a one-shot timer, and for a timeout function to stop itself
cleanly -- just return false instead of true. With a Runnable
interface, one has to jump through hoops to achieve the same result.
Since you can't overload a method's return value and since timers are
traditionally thought to fire rather than run anyway, I'd like to define
the following to replace Timer's use of Runnable:
package org.gnu.glib;
public interface Fireable {
/**
* This method executes when "fired" by a subsystem, such as by
* a timer.
*
* @return true if the timer should continue executing, or false
* if this is the last invocation of this method.
*/
boolean fire();
}
One option is to make Fireable an inner interface of Timer. Timer's
constructor would change to:
public Timer(int interval, Fireable target) {}
and Timer._run() can then be deleted.
Any objections? One nice side-benefit is that an invokeLater method
becomes trivial to implement by using a one-shot timer.
Tom
|