1. /*
  2. * Copyright 2002-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;
  17. import java.util.AbstractCollection;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.Iterator;
  21. import java.util.NoSuchElementException;
  22. /**
  23. * The BoundedFifoBuffer is a very efficient implementation of
  24. * Buffer that does not alter the size of the buffer at runtime.
  25. * <p>
  26. * The removal order of a <code>BoundedFifoBuffer</code> is based on the
  27. * insertion order; elements are removed in the same order in which they
  28. * were added. The iteration order is the same as the removal order.
  29. * <p>
  30. * The {@link #add(Object)}, {@link #remove()} and {@link #get()} operations
  31. * all perform in constant time. All other operations perform in linear
  32. * time or worse.
  33. * <p>
  34. * Note that this implementation is not synchronized. The following can be
  35. * used to provide synchronized access to your <code>BoundedFifoBuffer</code>:
  36. * <pre>
  37. * Buffer fifo = BufferUtils.synchronizedBuffer(new BoundedFifoBuffer());
  38. * </pre>
  39. * <p>
  40. * This buffer prevents null objects from being added.
  41. *
  42. * @deprecated Moved to buffer subpackage. Due to be removed in v4.0.
  43. * @since 2.1
  44. * @version $Revision: 1.16 $ $Date: 2004/02/18 01:15:43 $
  45. *
  46. * @author Avalon
  47. * @author Berin Loritsch
  48. * @author Paul Jack
  49. * @author Stephen Colebourne
  50. * @author Herve Quiroz
  51. */
  52. public class BoundedFifoBuffer extends AbstractCollection
  53. implements Buffer, BoundedCollection {
  54. private final Object[] m_elements;
  55. private int m_start = 0;
  56. private int m_end = 0;
  57. private boolean m_full = false;
  58. private final int maxElements;
  59. /**
  60. * Constructs a new <code>BoundedFifoBuffer</code> big enough to hold
  61. * 32 elements.
  62. */
  63. public BoundedFifoBuffer() {
  64. this(32);
  65. }
  66. /**
  67. * Constructs a new <code>BoundedFifoBuffer</code> big enough to hold
  68. * the specified number of elements.
  69. *
  70. * @param size the maximum number of elements for this fifo
  71. * @throws IllegalArgumentException if the size is less than 1
  72. */
  73. public BoundedFifoBuffer(int size) {
  74. if (size <= 0) {
  75. throw new IllegalArgumentException("The size must be greater than 0");
  76. }
  77. m_elements = new Object[size];
  78. maxElements = m_elements.length;
  79. }
  80. /**
  81. * Constructs a new <code>BoundedFifoBuffer</code> big enough to hold all
  82. * of the elements in the specified collection. That collection's
  83. * elements will also be added to the buffer.
  84. *
  85. * @param coll the collection whose elements to add, may not be null
  86. * @throws NullPointerException if the collection is null
  87. */
  88. public BoundedFifoBuffer(Collection coll) {
  89. this(coll.size());
  90. addAll(coll);
  91. }
  92. /**
  93. * Returns the number of elements stored in the buffer.
  94. *
  95. * @return this buffer's size
  96. */
  97. public int size() {
  98. int size = 0;
  99. if (m_end < m_start) {
  100. size = maxElements - m_start + m_end;
  101. } else if (m_end == m_start) {
  102. size = (m_full ? maxElements : 0);
  103. } else {
  104. size = m_end - m_start;
  105. }
  106. return size;
  107. }
  108. /**
  109. * Returns true if this buffer is empty; false otherwise.
  110. *
  111. * @return true if this buffer is empty
  112. */
  113. public boolean isEmpty() {
  114. return size() == 0;
  115. }
  116. /**
  117. * Returns true if this collection is full and no new elements can be added.
  118. *
  119. * @return <code>true</code> if the collection is full
  120. */
  121. public boolean isFull() {
  122. return size() == maxElements;
  123. }
  124. /**
  125. * Gets the maximum size of the collection (the bound).
  126. *
  127. * @return the maximum number of elements the collection can hold
  128. */
  129. public int maxSize() {
  130. return maxElements;
  131. }
  132. /**
  133. * Clears this buffer.
  134. */
  135. public void clear() {
  136. m_full = false;
  137. m_start = 0;
  138. m_end = 0;
  139. Arrays.fill(m_elements, null);
  140. }
  141. /**
  142. * Adds the given element to this buffer.
  143. *
  144. * @param element the element to add
  145. * @return true, always
  146. * @throws NullPointerException if the given element is null
  147. * @throws BufferOverflowException if this buffer is full
  148. */
  149. public boolean add(Object element) {
  150. if (null == element) {
  151. throw new NullPointerException("Attempted to add null object to buffer");
  152. }
  153. if (m_full) {
  154. throw new BufferOverflowException("The buffer cannot hold more than " + maxElements + " objects.");
  155. }
  156. m_elements[m_end++] = element;
  157. if (m_end >= maxElements) {
  158. m_end = 0;
  159. }
  160. if (m_end == m_start) {
  161. m_full = true;
  162. }
  163. return true;
  164. }
  165. /**
  166. * Returns the least recently inserted element in this buffer.
  167. *
  168. * @return the least recently inserted element
  169. * @throws BufferUnderflowException if the buffer is empty
  170. */
  171. public Object get() {
  172. if (isEmpty()) {
  173. throw new BufferUnderflowException("The buffer is already empty");
  174. }
  175. return m_elements[m_start];
  176. }
  177. /**
  178. * Removes the least recently inserted element from this buffer.
  179. *
  180. * @return the least recently inserted element
  181. * @throws BufferUnderflowException if the buffer is empty
  182. */
  183. public Object remove() {
  184. if (isEmpty()) {
  185. throw new BufferUnderflowException("The buffer is already empty");
  186. }
  187. Object element = m_elements[m_start];
  188. if (null != element) {
  189. m_elements[m_start++] = null;
  190. if (m_start >= maxElements) {
  191. m_start = 0;
  192. }
  193. m_full = false;
  194. }
  195. return element;
  196. }
  197. /**
  198. * Increments the internal index.
  199. *
  200. * @param index the index to increment
  201. * @return the updated index
  202. */
  203. private int increment(int index) {
  204. index++;
  205. if (index >= maxElements) {
  206. index = 0;
  207. }
  208. return index;
  209. }
  210. /**
  211. * Decrements the internal index.
  212. *
  213. * @param index the index to decrement
  214. * @return the updated index
  215. */
  216. private int decrement(int index) {
  217. index--;
  218. if (index < 0) {
  219. index = maxElements - 1;
  220. }
  221. return index;
  222. }
  223. /**
  224. * Returns an iterator over this buffer's elements.
  225. *
  226. * @return an iterator over this buffer's elements
  227. */
  228. public Iterator iterator() {
  229. return new Iterator() {
  230. private int index = m_start;
  231. private int lastReturnedIndex = -1;
  232. private boolean isFirst = m_full;
  233. public boolean hasNext() {
  234. return isFirst || (index != m_end);
  235. }
  236. public Object next() {
  237. if (!hasNext()) throw new NoSuchElementException();
  238. isFirst = false;
  239. lastReturnedIndex = index;
  240. index = increment(index);
  241. return m_elements[lastReturnedIndex];
  242. }
  243. public void remove() {
  244. if (lastReturnedIndex == -1) throw new IllegalStateException();
  245. // First element can be removed quickly
  246. if (lastReturnedIndex == m_start) {
  247. BoundedFifoBuffer.this.remove();
  248. lastReturnedIndex = -1;
  249. return;
  250. }
  251. // Other elements require us to shift the subsequent elements
  252. int i = lastReturnedIndex + 1;
  253. while (i != m_end) {
  254. if (i >= maxElements) {
  255. m_elements[i - 1] = m_elements[0];
  256. i = 0;
  257. } else {
  258. m_elements[i - 1] = m_elements[i];
  259. i++;
  260. }
  261. }
  262. lastReturnedIndex = -1;
  263. m_end = decrement(m_end);
  264. m_elements[m_end] = null;
  265. m_full = false;
  266. index = decrement(index);
  267. }
  268. };
  269. }
  270. }