KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > timer > EventTimerTask


1 /*
2  * $Id: EventTimerTask.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util.timer;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.TimerTask JavaDoc;
16
17 /**
18  * <code>EventTimerTask</code> is a task that causes TimeEvent to be fired to
19  * listening objects when a specific number of milliseconds have passed. This
20  * implementation is based on the java.util.TimerTask.
21  *
22  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
23  * @version $Revision: 3798 $
24  */

25 public class EventTimerTask extends TimerTask JavaDoc
26 {
27     /**
28      * A list of listeners on this task
29      */

30     private List JavaDoc listeners = null;
31
32     /**
33      * The name of the task
34      */

35     private String JavaDoc name = null;
36
37     /**
38      * Determines if the task has been started
39      */

40     private boolean started = true;
41
42     /**
43      * Constructs a EventTimeTask and registers a listener with it
44      *
45      * @param listener the listener to register
46      */

47     public EventTimerTask(TimeEventListener listener)
48     {
49         super();
50         addListener(listener);
51         this.name = "EventTimerTask." + hashCode();
52     }
53
54     /**
55      * Constructs a EventTimeTask and registers a listener with it
56      *
57      * @param listener the listener to register
58      * @param name the name for the task
59      */

60     public EventTimerTask(TimeEventListener listener, String JavaDoc name)
61     {
62         super();
63         addListener(listener);
64         this.name = name;
65     }
66
67     /**
68      * The action to be performed by this timer task. The fireTime event method is
69      * called.
70      */

71     public void run()
72     {
73
74         TimeEvent event = new TimeEvent(this, getName(), scheduledExecutionTime());
75         fireTimerEvent(event);
76     }
77
78     /**
79      * Gets the task name (this is also the timer thread name)
80      *
81      * @return the task name
82      */

83     public String JavaDoc getName()
84     {
85         return name;
86     }
87
88     public void removeListener(TimeEventListener listener)
89     {
90         if (listeners != null && listeners.contains(listener))
91         {
92             listeners.remove(listener);
93         }
94     }
95
96     public void removeAllListeners()
97     {
98         listeners = new ArrayList JavaDoc();
99     }
100
101     public void addListener(TimeEventListener listener)
102     {
103         if (listeners == null)
104         {
105             listeners = new ArrayList JavaDoc();
106             listeners.add(listener);
107         }
108         else if (!listeners.contains(listener))
109         {
110             listeners.add(listener);
111         }
112     }
113
114     protected void fireTimerEvent(TimeEvent event)
115     {
116         if (listeners != null && started)
117         {
118             int count = listeners.size();
119             for (int i = 0; i < count; i++)
120             {
121                 ((TimeEventListener)listeners.get(i)).timeExpired(event);
122             }
123         }
124     }
125
126     public void stop()
127     {
128         started = false;
129     }
130
131     public void start()
132     {
133         started = true;
134     }
135
136     public boolean isStarted()
137     {
138         return started;
139     }
140 }
141
Popular Tags