KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > util > Queue


1 package org.sapia.ubik.util;
2
3 import java.util.LinkedList JavaDoc;
4
5 /**
6  * A basic FIFO datastructure.
7  *
8  * @author Yanick Duchesne
9  *
10  * <dl>
11  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
12  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
13  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
14  * </dl>
15  */

16 public class Queue {
17   protected LinkedList JavaDoc _items = new LinkedList JavaDoc();
18   private boolean _added;
19   
20   /**
21    * Creates an instance of this class with no maximum size.
22    */

23   public Queue(){
24   }
25   
26   /**
27    * @param o the <code>Object</code> to add to this queue.
28    * @param notifyAll if <code>true</code>, all threads waiting on this queue will be
29    * internally notified.
30    */

31   public synchronized void add(Object JavaDoc o, boolean notifyAll){
32     
33     _items.add(o);
34     _added = true;
35     if(notifyAll){
36       notifyAll();
37     }
38     else{
39       notify();
40     }
41   }
42   
43   /**
44    * This method retrusn the first object in this queue, or blocks until an object is added.
45    *
46    * @return the first <code>Object</code> in the queue - which is internally removed.
47    * @throws InterruptedException
48    */

49   public synchronized Object JavaDoc remove() throws InterruptedException JavaDoc{
50     while(_items.size() == 0){
51       wait();
52     }
53     _added = false;
54     return _items.removeFirst();
55   }
56   
57   /**
58    * @return the number of items in this queue.
59    */

60   public int size(){
61     return _items.size();
62   }
63   
64   /**
65    * @return <code>true</code> if an item was just added to this instance.
66    */

67   public boolean wasItemAdded(){
68     return _added;
69   }
70   
71   protected void resetAddedFlag(){
72     _added = false;
73   }
74 }
75
Popular Tags