1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
7 /** A simple synchronized queue, implemented as an array */
10 public Queue(int initiallength) { vec = new Object[initiallength]; }
15 /** The index of the first node in the queue */
16 private int first = 0;
18 /** The number of elements in the queue; INVARAINT: size <= vec.length */
21 /** Grow the queue, if needed */
22 private void grow(int newlength) {
23 Object[] newvec = new Object[newlength];
24 if (first + size > vec.length) {
25 System.arraycopy(vec, first, newvec, 0, vec.length - first);
26 System.arraycopy(vec, 0, newvec, vec.length - first, size - (vec.length - first));
28 System.arraycopy(vec, first, newvec, 0, size);
34 /** The number of elements in the queue */
35 public int size() { return size; }
37 /** Empties the queue */
38 public synchronized void flush() {
41 for(int i=0; i<vec.length; i++) vec[i] = null;
44 /** Add an element to the front of the queue */
45 public synchronized void prepend(Object o) {
46 if (size == vec.length) grow(vec.length * 2);
48 if (first < 0) first += vec.length;
51 if (size == 1) notify();
54 /** Add an element to the back of the queue */
55 public synchronized void append(Object o) {
56 if (size == vec.length) grow(vec.length * 2);
57 if (first + size >= vec.length) vec[first + size - vec.length] = o;
58 else vec[first + size] = o;
60 if (size == 1) notify();
63 /** Remove and return and element from the queue, blocking if empty. */
64 public Object remove() { return remove(true); }
66 /** Remove and return an element from the queue, blocking if
67 <tt>block</tt> is true and the queue is empty. */
68 public synchronized Object remove(boolean block) {
70 while (size == 0 && block) {
71 try { wait(); } catch (InterruptedException e) { }
74 if (!block && size == 0) return null;
75 Object ret = vec[first];
78 if (first >= vec.length) first = 0;
82 /** Returns the top element in the queue without removing it */
83 public synchronized Object peek() {
84 if (size == 0) return null;