View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.util;
25  
26  import java.util.*;
27  
28  public class PlainQueue {
29     protected Vector[] data = null;
30  
31     public PlainQueue() {
32        this(1);
33     }
34  
35     public PlainQueue(int levels) {
36        Assert.notFalse(levels > 0);
37        data = new Vector[levels];
38        for(int i = 0; i < data.length; i++ )
39           data[i] = new Vector();
40     }
41  
42     public synchronized Enumeration elements() {
43        Vector out = new Vector();
44        for(int i = 0; i < data.length; i++ )
45           for(int j = 0; j < data[i].size(); j++ )
46              out.addElement(data[i].elementAt(j));
47        return out.elements();
48     }
49  
50     public synchronized boolean remove(Object elem) {
51        for(int i = 0; i < data.length; i++ ) {
52           for(int j = 0; j < data[i].size(); j++ ) {
53              if ( data[i].contains(elem) ) {
54                 data[i].removeElement(elem);
55                 return true;
56              }
57           }
58        }
59        return false;
60     }
61  
62     public synchronized boolean remove(Object elem, int level) {
63        for(int j = 0; j < data[level].size(); j++ ) {
64           if ( data[level].contains(elem) ) {
65              data[level].removeElement(elem);
66              return true;
67           }
68        }
69        return false;
70     }
71  
72     public synchronized void enqueue(Object elem) {
73        enqueue(elem, 0);
74     }
75  
76     public synchronized void enqueue(Object elem, int level) {
77        Assert.notFalse(level >= 0 && level < data.length);
78        data[level].addElement(elem);
79     }
80  
81     public synchronized Object dequeue() {
82        for(int i = 0; i < data.length; i++ ) {
83           if ( !data[i].isEmpty() ) {
84              Object elem = data[i].firstElement();
85              data[i].removeElement(elem);
86              return elem;
87           }
88        }
89        return null;
90     }
91  
92  
93     public synchronized Object peek() {
94        for(int i = 0; i < data.length; i++ ) {
95           if ( !data[i].isEmpty() )
96              return data[i].firstElement();
97        }
98        return null;
99     }
100 
101    public synchronized boolean isEmpty() {
102       for(int i = 0; i < data.length; i++ )
103          if ( !data[i].isEmpty() )
104             return false;
105       return true;
106    }
107    
108    public synchronized void clear() {
109       for(int i = 0; i < data.length; i++ )
110          data[i].removeAllElements();
111    }
112 
113 }