KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > model > TimeoutProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.tasklist.usertasks.model;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.ListIterator JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * The TimeoutProvider class implements a way to let object that implements
28  * the Timeout interface to be notified at a given time.
29  *
30  * @author Trond Norbye
31  */

32 public class TimeoutProvider implements Runnable JavaDoc {
33     /** An inner class used to stash away all of the data */
34     private class Entry {
35         /** Create a new Entry instance
36          * @param owner the owner of the timeout
37          * @param userRef should be sent back with the timeout
38          * @param timeout when the timeout will expire
39          */

40         public Entry(Timeout owner, Object JavaDoc userRef, long timeout) {
41             this.owner = owner;
42             this.userRef = userRef;
43             this.timeout = timeout;
44         }
45         /** The object to notify */
46         public Timeout owner;
47         /** An optional user reference */
48         public Object JavaDoc userRef;
49         /** When this entry should time out */
50         public long timeout;
51     }
52        
53     /**
54      * Private constructor for this Singleton class...
55      * Create the linked list, and start the thread...
56      */

57     private TimeoutProvider() {
58     alarms = new LinkedList JavaDoc<Entry>();
59         alarms.add(new Entry(null, null, Long.MAX_VALUE));
60
61     thread = new Thread JavaDoc(this, "TimeoutProvider"); // NOI18N
62
thread.start();
63     }
64     
65     /**
66      * Get the one and only instance of the TimeoutProvider
67      * @return the handle to the TimeoutProvider
68      */

69     public static TimeoutProvider getInstance() {
70     if (instance == null) {
71         instance = new TimeoutProvider();
72     }
73     return instance;
74     }
75
76     /**
77      * Cancel a timeout
78      * @param owner the owner of the timeout
79      * @param userRef the owners reference (null == all timeouts belonging to
80      * to this object)
81      */

82     public void cancel(Timeout owner, Object JavaDoc userRef) {
83     synchronized (alarms) {
84         ListIterator JavaDoc i = alarms.listIterator(0);
85
86         while (i.hasNext()) {
87                 Entry e = (Entry)i.next();
88                 
89         if (owner.equals(e.owner)) {
90                     if (userRef == null || e.userRef.equals(userRef)) {
91                         i.remove();
92                     }
93         }
94         }
95     }
96         
97     thread.interrupt();
98     }
99
100     /**
101      * Add a new object to supervision
102      * @param owner the object that owns the timeout
103      * @param userRef an object the user would like to get back with the timeout
104      * @param timeout the time this object should time out
105      */

106     public void add(Timeout owner, Object JavaDoc userRef, long timeout) {
107     synchronized (alarms) {
108         ListIterator JavaDoc i = alarms.listIterator(0);
109         boolean done = false;
110         int idx = 0;
111
112         while (!done && i.hasNext()) {
113                 Entry e = (Entry)i.next();
114
115         if (timeout < e.timeout) {
116             done = true;
117         } else {
118             ++idx;
119         }
120         }
121         
122         alarms.add(idx, new Entry(owner, userRef, timeout));
123     }
124
125     thread.interrupt();
126     }
127
128     /**
129      * The "message loop" of the TimeoutProvider.
130      * Wait for the next timeout and signal all of the items...
131      */

132     public void run() {
133
134         while (true) {
135             // Find the next timeout
136
long nextTimeout;
137         synchronized (alarms) {
138         nextTimeout = ((Entry)alarms.get(0)).timeout;
139         }
140         long currentTime = System.currentTimeMillis();
141         try {
142                 long sleeptime = nextTimeout - currentTime;
143                 if (sleeptime > 0) {
144                     Thread.sleep(sleeptime);
145                 }
146                 
147                 Vector JavaDoc<Entry> vec = new Vector JavaDoc<Entry>();
148                 synchronized (alarms) {
149                     ListIterator JavaDoc i = alarms.listIterator(0);
150                     boolean done = false;
151                     
152                     while (!done && i.hasNext()) {
153                         Entry e = (Entry)i.next();
154                         
155                         if (e.timeout <= nextTimeout) {
156                             // I cannot notify the object from this synchronized
157
// block since the callback might want to register
158
// a new callback...
159
vec.add(e);
160                             i.remove(); // Remove this timeout
161
} else {
162                             done = true;
163                         }
164                     }
165                 } // synchronized
166

167                 Iterator JavaDoc i = vec.iterator();
168                 while (i.hasNext()) {
169                     // I should perhaps do this in it's own thread so I don't
170
// block the TimeoutProvider....
171
Entry e = (Entry)i.next();
172                     e.owner.timeoutExpired(e.userRef);
173                 }
174             
175             } catch (InterruptedException JavaDoc e) {
176         // Do nothing, reevaluate next timeout!
177
}
178     }
179     }
180
181     /** The internal thread used for waiting and signaling the objects */
182     private Thread JavaDoc thread;
183     /** The one and only instance of the timeout provider */
184     private static TimeoutProvider instance;
185     /** A "sorted" linked list of objects to wait for */
186     private LinkedList JavaDoc<Entry> alarms;
187 }
188
Popular Tags