KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_timer > TimerEvent


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): ____________________________________.
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: TimerEvent.java,v 1.10 2004/05/17 09:57:47 durieuxp Exp $
26  * --------------------------------------------------------------------------
27  */

28
29
30 package org.objectweb.jonas_timer;
31
32 import org.objectweb.util.monolog.api.BasicLevel;
33
34 public class TimerEvent {
35
36     private TimerEventListener listener = null;
37     private Object JavaDoc arg = null;
38     private long remaining; // millisec
39
private long startvalue; // millisec
40
private long createtime;
41     private boolean permanent = false;
42     private boolean stopped = false;
43
44     /**
45      * Constructor
46      * @param l Object that will be notified when the timer expire.
47      * @param timeout nb of milliseconds before the timer expires.
48      * @param a info passed with the timer
49      * @param p true if the timer is permanent.
50      */

51     public TimerEvent(TimerEventListener l, long timeout, Object JavaDoc a, boolean p) {
52         if (TraceTimer.isDebug())
53             TraceTimer.logger.log(BasicLevel.DEBUG,"listener = "+l+",timeout = "+timeout+" ,object = "+a+" ,permanent = "+p+")");
54         listener = l;
55         remaining = timeout;
56         startvalue = timeout;
57         createtime = System.currentTimeMillis();
58         arg = a;
59         permanent = p;
60     }
61
62
63     /**
64      * Update timer every second. Used by clock.
65      * - this must be called with the timerList monitor.
66      * @param ms clock period
67      */

68     public long update() {
69         remaining = startvalue + createtime - System.currentTimeMillis();
70         if (TraceTimer.isDebug() && remaining <= 10) {
71             TraceTimer.logger.log(BasicLevel.DEBUG,"listener = "+listener+" remaining = "+remaining);
72         }
73         return remaining;
74     }
75
76     /**
77      * Restart timer to its initial value
78      */

79     public long restart() {
80         stopped = false;
81         // remaining should be < 0 here.
82
createtime = System.currentTimeMillis() + remaining;
83         remaining += startvalue;
84         if (TraceTimer.isDebug())
85             TraceTimer.logger.log(BasicLevel.DEBUG,"listener = "+listener+" remaining = "+remaining);
86         return remaining;
87     }
88
89     /**
90      * Process the Timer
91      */

92     public void process() {
93         if (listener != null) {
94             if (TraceTimer.isDebug())
95                 TraceTimer.logger.log(BasicLevel.DEBUG,"listener = "+listener+" remaining = "+remaining);
96             listener.timeoutExpired(arg);
97         }
98     }
99
100     /**
101      * Change the Timer value
102      * @param timeout in milliseconds
103      */

104     public void change(long timeout, Object JavaDoc a) {
105         if (TraceTimer.isDebug())
106             TraceTimer.logger.log(BasicLevel.DEBUG,"listener = "+listener+" timeout = "+timeout+",object = "+a);
107         stopped = false;
108         startvalue = timeout;
109         remaining = startvalue;
110         arg = a;
111     }
112
113     /**
114      * Unvalidate the timer. It will be removed by the timer manager.
115      */

116     public void unset() {
117         if (TraceTimer.isDebug())
118             TraceTimer.logger.log(BasicLevel.DEBUG,"listener = "+listener);
119         // the timerlist is not locked.
120
remaining = 100;
121         arg = null;
122         listener = null;
123         permanent = false;
124         stopped = false;
125     }
126
127     /**
128      * stop the timer, but keep it for further reuse (See change())
129      */

130     public void stop() {
131         if (TraceTimer.isDebug())
132             TraceTimer.logger.log(BasicLevel.DEBUG,"listener = "+listener);
133         // the timerlist is not locked.
134
remaining = 1000000;
135         stopped = true;
136     }
137
138     /**
139      * Is this timer valid ?
140      */

141     public boolean valid() {
142         return (listener != null);
143     }
144
145     /**
146      * Is this timer permanent ?
147      */

148     public boolean ispermanent() {
149         return permanent;
150     }
151
152     /**
153      * Is this timer stopped ?
154      */

155     public boolean isStopped() {
156         return stopped;
157     }
158
159     /**
160      * @return remaining time in millisec
161      */

162     public long getRemaining() {
163         return remaining;
164     }
165 }
166
167
Popular Tags