KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > util > FutureList


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31
32 package org.objectweb.proactive.ext.util;
33
34 /**
35  * <p>
36  * <code>FutureList</code> is an object used to monitor a subset of all the
37  * futures waited by an active object. A user can simply add or remove
38  * <code>Future</code> objects from this list and then call methods to test for
39  * their availability.
40  * </p><p>
41  * Future Objects to be watched after are added and removed to this list by the user.
42  * This class is not thread safe
43  * </p>
44  *
45  * @author ProActive Team
46  * @version 1.0, 2002/09/25
47  * @since ProActive 0.9
48  *
49  */

50 import org.objectweb.proactive.ProActive;
51
52 public class FutureList {
53   private java.util.Vector JavaDoc futureList;
54
55
56   public FutureList() {
57     futureList = new java.util.Vector JavaDoc();
58   }
59
60
61   /**
62    * Add the future to the futureList
63    * This method does not test if the future is already in the list.
64    */

65   public boolean add(Object JavaDoc o) {
66     // System.out.println("Adding future " + o);
67
return this.futureList.add(o);
68   }
69
70
71   /**
72    * Remove the object from the FutureList
73    * Return true if successfull
74    */

75   public boolean remove(Object JavaDoc o) {
76     // System.out.println("Trying to remove " + o);
77
return this.futureList.remove(o);
78   }
79
80
81   /**
82    * Return the number of future in the List
83    */

84   public int size() {
85     return this.futureList.size();
86   }
87
88
89   /**
90    * Return the element at the specified position in this List
91    */

92   public Object JavaDoc get(int index) {
93     return this.futureList.elementAt(index);
94   }
95
96
97   /**
98    * Return true if all the futures in the current list are awaited
99    */

100   public boolean allAwaited() {
101     boolean value = true;
102       for (int i = 0; i < futureList.size(); i++) {
103         value = value && ProActive.isAwaited(futureList.elementAt(i));
104       }
105     return value;
106   }
107
108
109   /**
110    * Return true if none of the futures in the current list are awaited
111    */

112   public boolean noneAwaited() {
113     for (int i = 0; i < futureList.size(); i++) {
114       if (ProActive.isAwaited(futureList.elementAt(i))) {
115         return false;
116       }
117     }
118     return true;
119   }
120
121
122
123   /**
124    * Return the number of currently awaited futures in this list
125    */

126   public int countAwaited() {
127     int count = 0;
128       for (int i = 0; i < futureList.size(); i++) {
129         if (ProActive.isAwaited(futureList.elementAt(i))) {
130           count++;
131         }
132       }
133     return count;
134   }
135
136
137   /**
138    * Returns a future available in this list.
139    * Returns null if none is available.
140    */

141   public Object JavaDoc getOne() {
142       if (this.countAwaited() == this.size()) {
143         //System.out.println("still waiting " + this.countAwaited()+ " futures");
144
//futurePool.waitForReply();
145
return null;
146       } else {
147         Object JavaDoc temp;
148         for (int i = 0; i < futureList.size(); i++) {
149           temp = futureList.elementAt(i);
150           if (! ProActive.isAwaited(temp)) {
151             return temp;
152           }
153         }
154         return null;
155       }
156   }
157
158
159   /**
160    * Removes and returns a future available this list.
161    * Returns null if none is available.
162    */

163   public Object JavaDoc removeOne() {
164       Object JavaDoc tmp;
165       tmp = this.getOne();
166       if (tmp != null) {
167         // System.out.println("Removing future " + tmp);
168
//System.out.println("Result is " + this.remove(tmp));
169
this.remove(tmp);
170       }
171       return tmp;
172   }
173
174
175   public Object JavaDoc waitAndGetOne() {
176       this.waitOne();
177       return this.getOne();
178   }
179
180
181   public Object JavaDoc waitAndRemoveOne() {
182       this.waitOne();
183       return this.removeOne();
184   }
185
186
187   public void waitAll() {
188       ProActive.waitForAll(futureList);
189   }
190
191
192   public void waitOne() {
193       ProActive.waitForAny(futureList);
194   }
195   
196   public void waitN(int n) {
197     java.util.Vector JavaDoc temp = new java.util.Vector JavaDoc(futureList);
198     for (int i = 0; i < n ; i++) {
199         int index = ProActive.waitForAny(temp);
200         temp.remove(index);
201     }
202   }
203   
204   public void waitTheNth (int n) {
205     ProActive.waitForTheNth(futureList,n);
206   }
207   
208   public Object JavaDoc waitAndGetTheNth(int n) {
209         ProActive.waitForTheNth(futureList,n);
210         return this.futureList.elementAt(n);
211     }
212   
213 }
214
Popular Tags