01: /** 
02:     A first-in, first-out bounded collection of objects. 
03: */ 
04: public class BoundedQueue<E>
05: { 
06:    /** 
07:        Constructs an empty queue. 
08:        @param capacity the maximum capacity of the queue 
09:    */ 
10:    public BoundedQueue(int capacity) 
11:    { 
12:       elements = new Object[capacity]; 
13:       head = 0; 
14:       tail = 0; 
15:       size = 0;
16:    } 
17: 
18:    /** 
19:        Removes the object at the head. 
20:        @return the object that has been removed from the queue
21:        @precondition !isEmpty()
22:    */ 
23:    public E remove() 
24:    { 
25:       if (debug) System.out.print("removeFirst");
26:       E r = (E) elements[head]; 
27:       if (debug) System.out.print(".");
28:       head++;
29:       if (debug) System.out.print(".");
30:       size--;
31:       if (head == elements.length) 
32:       {
33:          if (debug) System.out.print(".");
34:          head = 0; 
35:       }
36:       if (debug) 
37:          System.out.println("head=" + head + ",tail=" + tail 
38:             + ",size=" + size);
39:       return r; 
40:    } 
41: 
42:    /** 
43:        Appends an object at the tail. 
44:        @param newValue the object to be appended 
45:        @precondition !isFull();
46:    */ 
47:    public void add(E newValue) 
48:    { 
49:       if (debug) System.out.print("add");
50:       elements[tail] = newValue; 
51:       if (debug) System.out.print(".");
52:       tail++;
53:       if (debug) System.out.print(".");
54:       size++;
55:       if (tail == elements.length) 
56:       {
57:          if (debug) System.out.print(".");
58:          tail = 0; 
59:       }
60:       if (debug) 
61:          System.out.println("head=" + head + ",tail=" + tail 
62:             + ",size=" + size);
63:    } 
64: 
65:    public boolean isFull() 
66:    { 
67:       return size == elements.length;
68:    } 
69: 
70:    public boolean isEmpty() 
71:    { 
72:       return size == 0; 
73:    } 
74: 
75:    public void setDebug(boolean newValue)
76:    {
77:       debug = newValue;
78:    }
79: 
80:    private Object[] elements; 
81:    private int head; 
82:    private int tail; 
83:    private int size;
84:    private boolean debug;
85: }