1 package org.apache.jcs.utils.struct;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 /***
23 * This is a bounded queue. It only allows maxSize items.
24 * <p>
25 * @author Aaron Smuts
26 */
27 public class BoundedQueue
28 {
29 private int maxSize;
30
31 private DoubleLinkedList list = new DoubleLinkedList();
32
33 /***
34 * Initialize the bounded queue.
35 * <p>
36 * @param maxSize
37 */
38 public BoundedQueue( int maxSize )
39 {
40 this.maxSize = maxSize;
41 }
42
43 /***
44 * Adds an item to the end of the queue, which is the front of the list.
45 * <p>
46 * @param object
47 */
48 public void add( Object object )
49 {
50 if ( list.size() >= maxSize )
51 {
52 list.removeLast();
53 }
54 list.addFirst( new DoubleLinkedListNode( object ) );
55 }
56
57 /***
58 * Takes the last of the underlying double linked list.
59 * <p>
60 * @return null if it is epmpty.
61 */
62 public Object take()
63 {
64 DoubleLinkedListNode node = list.removeLast();
65 if ( node != null )
66 {
67 return node.getPayload();
68 }
69 return null;
70 }
71
72 /***
73 * Return the number of items in the queue.
74 * <p>
75 * @return size
76 */
77 public int size()
78 {
79 return list.size();
80 }
81
82 /***
83 * Return true if the size is <= 0;
84 * <p>
85 * @return true is size <= 0;
86 */
87 public boolean isEmpty()
88 {
89 return list.size() <= 0;
90 }
91 }