View Javadoc

1   package org.apache.jcs.utils.struct;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  }