fr/dyade/aaa/util/Queue.java

00001 /*
00002  * Copyright (C) 2001 - 2008 ScalAgent Distributed Technologies
00003  * Copyright (C) 1996 - 2000 BULL
00004  * Copyright (C) 1996 - 2000 INRIA
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA.
00020  */
00021 package fr.dyade.aaa.util;
00022 
00023 import java.util.Vector;
00024 
00037 public class Queue extends Vector {
00039   private static final long serialVersionUID = 1L;
00040   
00045   private boolean stopping;
00046 
00050   public Queue() {
00051     super();
00052     start();
00053   }
00054 
00061   public synchronized void push(Object item) {
00062     if (stopping)
00063       throw new StoppedQueueException();
00064 
00065     addElement(item);
00066     notify();
00067   }
00068 
00075   public synchronized Object get() throws InterruptedException {
00076     while (size() == 0)
00077       wait();
00078 
00079     return elementAt(0);
00080   }
00081 
00088   public synchronized Object pop() {
00089     if (size() == 0)
00090       throw new EmptyQueueException();
00091     
00092     Object obj = elementAt(0);
00093     removeElementAt(0);
00094 
00095     if (stopping && size() == 0)
00096       notify();
00097 
00098     return obj;
00099   }
00100   
00107   public synchronized Object getAndPop() throws InterruptedException {
00108     while (size() == 0)
00109       wait();
00110 
00111     Object obj = elementAt(0);
00112     removeElementAt(0);
00113 
00114     if (stopping && size() == 0)
00115       notify();
00116 
00117     return obj;
00118   }
00119 
00121   public void start() {
00122     stopping = false;
00123   }
00124 
00129   public synchronized void stop() throws InterruptedException {
00130     stopping = true;
00131     if (size() != 0) wait();
00132   }
00133 }

Generated on Tue Sep 16 16:14:24 2008 for joram by  doxygen 1.5.0