org/objectweb/proactive/ext/util/FutureList.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.ext.util;
00032 
00033 
00050 import org.objectweb.proactive.ProActive;
00051 
00052 
00053 public class FutureList {
00054     private java.util.Vector<Object> futureList;
00055 
00056     public FutureList() {
00057         futureList = new java.util.Vector<Object>();
00058     }
00059 
00064     public boolean add(Object o) {
00065         //      System.out.println("Adding future " + o);
00066         return this.futureList.add(o);
00067     }
00068 
00073     public boolean remove(Object o) {
00074         //      System.out.println("Trying to remove " + o);
00075         return this.futureList.remove(o);
00076     }
00077 
00081     public int size() {
00082         return this.futureList.size();
00083     }
00084 
00088     public Object get(int index) {
00089         return this.futureList.elementAt(index);
00090     }
00091 
00095     public boolean allAwaited() {
00096         boolean value = true;
00097         for (int i = 0; i < futureList.size(); i++) {
00098             value = value && ProActive.isAwaited(futureList.elementAt(i));
00099         }
00100         return value;
00101     }
00102 
00106     public boolean noneAwaited() {
00107         for (int i = 0; i < futureList.size(); i++) {
00108             if (ProActive.isAwaited(futureList.elementAt(i))) {
00109                 return false;
00110             }
00111         }
00112         return true;
00113     }
00114 
00118     public int countAwaited() {
00119         int count = 0;
00120         for (int i = 0; i < futureList.size(); i++) {
00121             if (ProActive.isAwaited(futureList.elementAt(i))) {
00122                 count++;
00123             }
00124         }
00125         return count;
00126     }
00127 
00132     public Object getOne() {
00133         if (this.countAwaited() == this.size()) {
00134             //System.out.println("still waiting " + this.countAwaited()+ " futures");
00135             //futurePool.waitForReply();
00136             return null;
00137         } else {
00138             Object temp;
00139             for (int i = 0; i < futureList.size(); i++) {
00140                 temp = futureList.elementAt(i);
00141                 if (!ProActive.isAwaited(temp)) {
00142                     return temp;
00143                 }
00144             }
00145             return null;
00146         }
00147     }
00148 
00153     public Object removeOne() {
00154         Object tmp;
00155         tmp = this.getOne();
00156         if (tmp != null) {
00157             //  System.out.println("Removing future "  + tmp);
00158             //System.out.println("Result is " + this.remove(tmp));
00159             this.remove(tmp);
00160         }
00161         return tmp;
00162     }
00163 
00164     public Object waitAndGetOne() {
00165         this.waitOne();
00166         return this.getOne();
00167     }
00168 
00169     public Object waitAndRemoveOne() {
00170         this.waitOne();
00171         return this.removeOne();
00172     }
00173 
00174     public void waitAll() {
00175         ProActive.waitForAll(futureList);
00176     }
00177 
00178     public void waitOne() {
00179         ProActive.waitForAny(futureList);
00180     }
00181 
00182     public void waitN(int n) {
00183         java.util.Vector<Object> temp = new java.util.Vector<Object>(futureList);
00184         for (int i = 0; i < n; i++) {
00185             int index = ProActive.waitForAny(temp);
00186             temp.remove(index);
00187         }
00188     }
00189 
00190     public void waitTheNth(int n) {
00191         ProActive.waitForTheNth(futureList, n);
00192     }
00193 
00194     public Object waitAndGetTheNth(int n) {
00195         ProActive.waitForTheNth(futureList, n);
00196         return this.futureList.elementAt(n);
00197     }
00198 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1