1 16 package org.apache.commons.collections; 17 18 import java.util.AbstractCollection ; 19 import java.util.Arrays ; 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 import java.util.NoSuchElementException ; 23 24 54 public class BoundedFifoBuffer extends AbstractCollection 55 implements Buffer, BoundedCollection { 56 57 private final Object [] m_elements; 58 private int m_start = 0; 59 private int m_end = 0; 60 private boolean m_full = false; 61 private final int maxElements; 62 63 67 public BoundedFifoBuffer() { 68 this(32); 69 } 70 71 78 public BoundedFifoBuffer(int size) { 79 if (size <= 0) { 80 throw new IllegalArgumentException ("The size must be greater than 0"); 81 } 82 m_elements = new Object [size]; 83 maxElements = m_elements.length; 84 } 85 86 94 public BoundedFifoBuffer(Collection coll) { 95 this(coll.size()); 96 addAll(coll); 97 } 98 99 104 public int size() { 105 int size = 0; 106 107 if (m_end < m_start) { 108 size = maxElements - m_start + m_end; 109 } else if (m_end == m_start) { 110 size = (m_full ? maxElements : 0); 111 } else { 112 size = m_end - m_start; 113 } 114 115 return size; 116 } 117 118 123 public boolean isEmpty() { 124 return size() == 0; 125 } 126 127 132 public boolean isFull() { 133 return size() == maxElements; 134 } 135 136 141 public int maxSize() { 142 return maxElements; 143 } 144 145 148 public void clear() { 149 m_full = false; 150 m_start = 0; 151 m_end = 0; 152 Arrays.fill(m_elements, null); 153 } 154 155 163 public boolean add(Object element) { 164 if (null == element) { 165 throw new NullPointerException ("Attempted to add null object to buffer"); 166 } 167 168 if (m_full) { 169 throw new BufferOverflowException("The buffer cannot hold more than " + maxElements + " objects."); 170 } 171 172 m_elements[m_end++] = element; 173 174 if (m_end >= maxElements) { 175 m_end = 0; 176 } 177 178 if (m_end == m_start) { 179 m_full = true; 180 } 181 182 return true; 183 } 184 185 191 public Object get() { 192 if (isEmpty()) { 193 throw new BufferUnderflowException("The buffer is already empty"); 194 } 195 196 return m_elements[m_start]; 197 } 198 199 205 public Object remove() { 206 if (isEmpty()) { 207 throw new BufferUnderflowException("The buffer is already empty"); 208 } 209 210 Object element = m_elements[m_start]; 211 212 if (null != element) { 213 m_elements[m_start++] = null; 214 215 if (m_start >= maxElements) { 216 m_start = 0; 217 } 218 219 m_full = false; 220 } 221 222 return element; 223 } 224 225 231 private int increment(int index) { 232 index++; 233 if (index >= maxElements) { 234 index = 0; 235 } 236 return index; 237 } 238 239 245 private int decrement(int index) { 246 index--; 247 if (index < 0) { 248 index = maxElements - 1; 249 } 250 return index; 251 } 252 253 258 public Iterator iterator() { 259 return new Iterator () { 260 261 private int index = m_start; 262 private int lastReturnedIndex = -1; 263 private boolean isFirst = m_full; 264 265 public boolean hasNext() { 266 return isFirst || (index != m_end); 267 268 } 269 270 public Object next() { 271 if (!hasNext()) throw new NoSuchElementException (); 272 isFirst = false; 273 lastReturnedIndex = index; 274 index = increment(index); 275 return m_elements[lastReturnedIndex]; 276 } 277 278 public void remove() { 279 if (lastReturnedIndex == -1) throw new IllegalStateException (); 280 281 if (lastReturnedIndex == m_start) { 283 BoundedFifoBuffer.this.remove(); 284 lastReturnedIndex = -1; 285 return; 286 } 287 288 int i = lastReturnedIndex + 1; 290 while (i != m_end) { 291 if (i >= maxElements) { 292 m_elements[i - 1] = m_elements[0]; 293 i = 0; 294 } else { 295 m_elements[i - 1] = m_elements[i]; 296 i++; 297 } 298 } 299 300 lastReturnedIndex = -1; 301 m_end = decrement(m_end); 302 m_elements[m_end] = null; 303 m_full = false; 304 index = decrement(index); 305 } 306 307 }; 308 } 309 310 } 311 | Popular Tags |