KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jegg > timer > Timer


1 /*
2  * Copyright (c) 2004, Bruce Lowery
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * - Neither the name of JEGG nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without
15  * specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */

29 package jegg.timer;
30
31 import java.util.TimerTask JavaDoc;
32
33 /**
34  * A timer that can be used to periodically deliver timeout messages to
35  * a listener.
36  */

37 public final class Timer
38 {
39     // TODO add a 'reset' method to allow a repeating timer to
40
// restart timing from the instant the reset was invoked.
41

42     /** The interval, in msec, between timeout messages */
43     private long _interval;
44     /** The initial delay, in msec, before the first timeout will be sent */
45     private long _delay;
46     /** True if the timer should continue after the first message */
47     private boolean _repeat;
48     /** The underlying Java timer. All the egg timers are timer tasks exeucted by this timer. */
49     private static final java.util.Timer JavaDoc TIMER = new java.util.Timer JavaDoc(true /*isDaemon*/);
50     /** The timer task */
51     private MyTimerTask _task;
52     
53     /**
54      * Create a timer that will deliver timeout messages periodically.
55      * @param l the listener for the timeout messages.
56      * @param period the interval, in milliseconds, between timeout messages.
57      * @return new timer.
58      */

59     public static final Timer createRepeatingTimer(TimeoutListener l, long period)
60     {
61         return new Timer(l, period, period, true);
62     }
63
64     /**
65      * Create a timer that will deliver timeout messages periodically following
66      * the initial specified delay.
67      * @param l the listener for the timeout messages.
68      * @param period the interval, in milliseconds, between timeout messages.
69      * @param delay the initial delay before the first timeout message is delivered to the listener.
70      * @return new timer.
71      */

72     public static final Timer createRepeatingTimer(TimeoutListener l, long period, long delay)
73     {
74         return new Timer(l, period, delay, true);
75     }
76
77     /**
78      * Create a timer that will deliver a single timeout message.
79      * @param l the listener for the timeout message.
80      * @param delay the number of milliseconds to wait before the timeout
81      * message is sent.
82      * @return new timer.
83      */

84     public static final Timer createSingleShotTimer(TimeoutListener l, long delay)
85     {
86         return new Timer(l, delay, delay, false);
87     }
88
89     /**
90      * Construct a timer that will deliver a timeout message periodically.
91      * @param tl the listener to deliver the timeout message to.
92      * @param msec the period of the deliveries.
93      * @param delay_msec the initial delay before the first message is delivered.
94      * @param r if false, only one message will be delivered.
95      */

96     private Timer(TimeoutListener tl, long msec, long delay_msec, boolean r)
97     {
98         _interval = msec;
99         _delay = delay_msec;
100         _repeat = r;
101         _task = new MyTimerTask(tl, this, _repeat);
102         TIMER.schedule( _task, _delay, _interval);
103     }
104     
105     /**
106      * Return the delivery period of this timer, which is the number of
107      * milliseconds between timeouts.
108      * @return number of milliseconds between timeouts.
109      */

110     public final long getInterval()
111     {
112         return _interval;
113     }
114     
115     /**
116      * Return the initial delay before the timeouts start.
117      * @return number of milliseconds to wait before sending the first timeout.
118      */

119     public final long getDelay()
120     {
121         return _delay;
122     }
123     
124     /**
125      * Return whether or not this timer will deliver multiple timeouts or just
126      * a single timeout.
127      * @return true if the timer will deliver multiple timeouts.
128      */

129     public final boolean isRepeating()
130     {
131         return _repeat;
132     }
133
134     /**
135      * Stop the timer.
136      *
137      */

138     public final void cancel()
139     {
140         _task.cancel();
141     }
142     
143     /**
144      * The timer task that is executed by the java timer, and which delivers
145      * the timeout message to the listener.
146      */

147     private final class MyTimerTask extends TimerTask JavaDoc
148     {
149         private TimeoutListener _tl;
150         private Timer _t;
151         private boolean _r;
152         public MyTimerTask(TimeoutListener tl, Timer t, boolean isRepeating)
153         {
154             _tl = tl;
155             _t = t;
156             _r = isRepeating;
157         }
158         public void run()
159         {
160             _tl.timeout(_t);
161             if (!_r)
162             {
163                 cancel();
164             }
165         }
166     }
167 }
168
Popular Tags