KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > buffer > CircularFifoBuffer


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.buffer;
17
18 import java.util.Collection JavaDoc;
19
20 /**
21  * CircularFifoBuffer is a first in first out buffer with a fixed size that
22  * replaces its oldest element if full.
23  * <p>
24  * The removal order of a <code>CircularFifoBuffer</code> is based on the
25  * insertion order; elements are removed in the same order in which they
26  * were added. The iteration order is the same as the removal order.
27  * <p>
28  * The {@link #add(Object)}, {@link #remove()} and {@link #get()} operations
29  * all perform in constant time. All other operations perform in linear
30  * time or worse.
31  * <p>
32  * Note that this implementation is not synchronized. The following can be
33  * used to provide synchronized access to your <code>CircularFifoBuffer</code>:
34  * <pre>
35  * Buffer fifo = BufferUtils.synchronizedBuffer(new CircularFifoBuffer());
36  * </pre>
37  * <p>
38  * This buffer prevents null objects from being added.
39  * <p>
40  * This class is Serializable from Commons Collections 3.1.
41  *
42  * @since Commons Collections 3.0
43  * @version $Revision: 1.5 $ $Date: 2004/06/03 22:02:13 $
44  *
45  * @author Stefano Fornari
46  * @author Stephen Colebourne
47  */

48 public class CircularFifoBuffer extends BoundedFifoBuffer {
49
50     /** Serialization version */
51     private static final long serialVersionUID = -8423413834657610406L;
52
53     /**
54      * Constructor that creates a buffer with the default size of 32.
55      */

56     public CircularFifoBuffer() {
57         super(32);
58     }
59
60     /**
61      * Constructor that creates a buffer with the specified size.
62      *
63      * @param size the size of the buffer (cannot be changed)
64      * @throws IllegalArgumentException if the size is less than 1
65      */

66     public CircularFifoBuffer(int size) {
67         super(size);
68     }
69
70     /**
71      * Constructor that creates a buffer from the specified collection.
72      * The collection size also sets the buffer size
73      *
74      * @param coll the collection to copy into the buffer, may not be null
75      * @throws NullPointerException if the collection is null
76      */

77     public CircularFifoBuffer(Collection JavaDoc coll) {
78         super(coll);
79     }
80
81     /**
82      * If the buffer is full, the least recently added element is discarded so
83      * that a new element can be inserted.
84      *
85      * @param element the element to add
86      * @return true, always
87      */

88     public boolean add(Object JavaDoc element) {
89         if (isFull()) {
90             remove();
91         }
92         return super.add(element);
93     }
94     
95 }
96
Popular Tags