From 8ac364d1d29dfac0c7549f70ee14005fc9ca8002 Mon Sep 17 00:00:00 2001 From: samskivert Date: Mon, 20 Jul 2009 22:07:29 +0000 Subject: [PATCH] Added methods for creating an interval that execute a Runnable when they expire. Whoa, composable software. Amazing. We should have done this eight years ago. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2595 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/Interval.java | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/java/com/samskivert/util/Interval.java b/src/java/com/samskivert/util/Interval.java index 4bbb7b71..60857775 100644 --- a/src/java/com/samskivert/util/Interval.java +++ b/src/java/com/samskivert/util/Interval.java @@ -46,6 +46,34 @@ public abstract class Interval public Interval getInterval (); } + /** + * Creates an interval that executes the supplied runnable when it expires. + */ + public static Interval create (final Runnable onExpired) + { + return new Interval() { + public void expired () { + onExpired.run(); + } + }; + } + + /** + * Creates an interval that executes the supplied runnable on the specified RunQueue when it + * expires. + */ + public static Interval create (RunQueue runQueue, final Runnable onExpired) + { + // we could probably avoid all the wacky machinations internal to Interval that do the + // runbuddy reposting and whatever and just create a non-runqueue interval that posts the + // supplied runnable to the runqueue when it expires, but we'll just punt on that for now + return new Interval(runQueue) { + public void expired () { + onExpired.run(); + } + }; + } + /** * This may be removed. *