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;
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 /*** The interval, in msec, between timeout messages */
40 private long _interval;
41 /*** The initial delay, in msec, before the first timeout will be sent */
42 private long _delay;
43 /*** True if the timer should continue after the first message */
44 private boolean _repeat;
45 /*** The underlying Java timer. All the egg timers are timer tasks exeucted by this timer. */
46 private static final java.util.Timer TIMER = new java.util.Timer(true /*isDaemon*/);
47 /*** The timer task */
48 private MyTimerTask _task;
49
50 /***
51 * Create a timer that will deliver timeout messages periodically.
52 * @param l the listener for the timeout messages.
53 * @param period the interval, in milliseconds, between timeout messages.
54 * @return new timer.
55 */
56 public static final Timer createRepeatingTimer(TimeoutListener l, long period)
57 {
58 return new Timer(l, period, period, true);
59 }
60
61 /***
62 * Create a timer that will deliver timeout messages periodically following
63 * the initial specified delay.
64 * @param l the listener for the timeout messages.
65 * @param period the interval, in milliseconds, between timeout messages.
66 * @param delay the initial delay before the first timeout message is delivered to the listener.
67 * @return new timer.
68 */
69 public static final Timer createRepeatingTimer(TimeoutListener l, long period, long delay)
70 {
71 return new Timer(l, period, delay, true);
72 }
73
74 /***
75 * Create a timer that will deliver a single timeout message.
76 * @param l the listener for the timeout message.
77 * @param delay the number of milliseconds to wait before the timeout
78 * message is sent.
79 * @return new timer.
80 */
81 public static final Timer createSingleShotTimer(TimeoutListener l, long delay)
82 {
83 return new Timer(l, delay, delay, false);
84 }
85
86 /***
87 * Construct a timer that will deliver a timeout message periodically.
88 * @param tl the listener to deliver the timeout message to.
89 * @param msec the period of the deliveries.
90 * @param delay_msec the initial delay before the first message is delivered.
91 * @param r if false, only one message will be delivered.
92 */
93 private Timer(TimeoutListener tl, long msec, long delay_msec, boolean r)
94 {
95 _interval = msec;
96 _delay = delay_msec;
97 _repeat = r;
98 _task = new MyTimerTask(tl, this, _repeat);
99 TIMER.schedule( _task, _delay, _interval);
100 }
101
102 /***
103 * Return the delivery period of this timer, which is the number of
104 * milliseconds between timeouts.
105 * @return number of milliseconds between timeouts.
106 */
107 public final long getInterval()
108 {
109 return _interval;
110 }
111
112 /***
113 * Return the initial delay before the timeouts start.
114 * @return number of milliseconds to wait before sending the first timeout.
115 */
116 public final long getDelay()
117 {
118 return _delay;
119 }
120
121 /***
122 * Return whether or not this timer will deliver multiple timeouts or just
123 * a single timeout.
124 * @return true if the timer will deliver multiple timeouts.
125 */
126 public final boolean isRepeating()
127 {
128 return _repeat;
129 }
130
131 /***
132 * Stop the timer.
133 *
134 */
135 public final void cancel()
136 {
137 _task.cancel();
138 }
139
140 /***
141 * The timer task that is executed by the java timer, and which delivers
142 * the timeout message to the listener.
143 */
144 private final class MyTimerTask extends TimerTask
145 {
146 private TimeoutListener _tl;
147 private Timer _t;
148 private boolean _r;
149 public MyTimerTask(TimeoutListener tl, Timer t, boolean isRepeating)
150 {
151 _tl = tl;
152 _t = t;
153 _r = isRepeating;
154 }
155 public void run()
156 {
157 _tl.timeout(_t);
158 if (!_r)
159 {
160 cancel();
161 }
162 }
163 }
164 }
This page was automatically generated by Maven