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. import java.util.Collection;
  18. /**
  19. * CircularFifoBuffer is a first in first out buffer with a fixed size that
  20. * replaces its oldest element if full.
  21. * <p>
  22. * The removal order of a <code>CircularFifoBuffer</code> is based on the
  23. * insertion order; elements are removed in the same order in which they
  24. * were added. The iteration order is the same as the removal order.
  25. * <p>
  26. * The {@link #add(Object)}, {@link #remove()} and {@link #get()} operations
  27. * all perform in constant time. All other operations perform in linear
  28. * time or worse.
  29. * <p>
  30. * Note that this implementation is not synchronized. The following can be
  31. * used to provide synchronized access to your <code>CircularFifoBuffer</code>:
  32. * <pre>
  33. * Buffer fifo = BufferUtils.synchronizedBuffer(new CircularFifoBuffer());
  34. * </pre>
  35. * <p>
  36. * This buffer prevents null objects from being added.
  37. * <p>
  38. * This class is Serializable from Commons Collections 3.1.
  39. *
  40. * @since Commons Collections 3.0
  41. * @version $Revision: 1.5 $ $Date: 2004/06/03 22:02:13 $
  42. *
  43. * @author Stefano Fornari
  44. * @author Stephen Colebourne
  45. */
  46. public class CircularFifoBuffer extends BoundedFifoBuffer {
  47. /** Serialization version */
  48. private static final long serialVersionUID = -8423413834657610406L;
  49. /**
  50. * Constructor that creates a buffer with the default size of 32.
  51. */
  52. public CircularFifoBuffer() {
  53. super(32);
  54. }
  55. /**
  56. * Constructor that creates a buffer with the specified size.
  57. *
  58. * @param size the size of the buffer (cannot be changed)
  59. * @throws IllegalArgumentException if the size is less than 1
  60. */
  61. public CircularFifoBuffer(int size) {
  62. super(size);
  63. }
  64. /**
  65. * Constructor that creates a buffer from the specified collection.
  66. * The collection size also sets the buffer size
  67. *
  68. * @param coll the collection to copy into the buffer, may not be null
  69. * @throws NullPointerException if the collection is null
  70. */
  71. public CircularFifoBuffer(Collection coll) {
  72. super(coll);
  73. }
  74. /**
  75. * If the buffer is full, the least recently added element is discarded so
  76. * that a new element can be inserted.
  77. *
  78. * @param element the element to add
  79. * @return true, always
  80. */
  81. public boolean add(Object element) {
  82. if (isFull()) {
  83. remove();
  84. }
  85. return super.add(element);
  86. }
  87. }