1 16 package org.apache.commons.collections.buffer; 17 18 import java.io.IOException ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.io.Serializable ; 22 import java.util.AbstractCollection ; 23 import java.util.Iterator ; 24 import java.util.NoSuchElementException ; 25 26 import org.apache.commons.collections.Buffer; 27 import org.apache.commons.collections.BufferUnderflowException; 28 29 61 public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, Serializable { 62 63 64 private static final long serialVersionUID = -3482960336579541419L; 65 66 67 protected transient Object [] buffer; 68 69 protected transient int head; 70 71 protected transient int tail; 72 73 81 public UnboundedFifoBuffer() { 82 this(32); 83 } 84 85 92 public UnboundedFifoBuffer(int initialSize) { 93 if (initialSize <= 0) { 94 throw new IllegalArgumentException ("The size must be greater than 0"); 95 } 96 buffer = new Object [initialSize + 1]; 97 head = 0; 98 tail = 0; 99 } 100 101 108 private void writeObject(ObjectOutputStream out) throws IOException { 109 out.defaultWriteObject(); 110 out.writeInt(size()); 111 for (Iterator it = iterator(); it.hasNext();) { 112 out.writeObject(it.next()); 113 } 114 } 115 116 123 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 124 in.defaultReadObject(); 125 int size = in.readInt(); 126 buffer = new Object [size]; 127 for (int i = 0; i < size; i++) { 128 buffer[i] = in.readObject(); 129 } 130 head = 0; 131 tail = size; 132 } 133 134 140 public int size() { 141 int size = 0; 142 143 if (tail < head) { 144 size = buffer.length - head + tail; 145 } else { 146 size = tail - head; 147 } 148 149 return size; 150 } 151 152 157 public boolean isEmpty() { 158 return (size() == 0); 159 } 160 161 168 public boolean add(final Object obj) { 169 if (obj == null) { 170 throw new NullPointerException ("Attempted to add null object to buffer"); 171 } 172 173 if (size() + 1 >= buffer.length) { 174 Object [] tmp = new Object [((buffer.length - 1) * 2) + 1]; 175 176 int j = 0; 177 for (int i = head; i != tail;) { 178 tmp[j] = buffer[i]; 179 buffer[i] = null; 180 181 j++; 182 i++; 183 if (i == buffer.length) { 184 i = 0; 185 } 186 } 187 188 buffer = tmp; 189 head = 0; 190 tail = j; 191 } 192 193 buffer[tail] = obj; 194 tail++; 195 if (tail >= buffer.length) { 196 tail = 0; 197 } 198 return true; 199 } 200 201 207 public Object get() { 208 if (isEmpty()) { 209 throw new BufferUnderflowException("The buffer is already empty"); 210 } 211 212 return buffer[head]; 213 } 214 215 221 public Object remove() { 222 if (isEmpty()) { 223 throw new BufferUnderflowException("The buffer is already empty"); 224 } 225 226 Object element = buffer[head]; 227 228 if (null != element) { 229 buffer[head] = null; 230 231 head++; 232 if (head >= buffer.length) { 233 head = 0; 234 } 235 } 236 237 return element; 238 } 239 240 246 private int increment(int index) { 247 index++; 248 if (index >= buffer.length) { 249 index = 0; 250 } 251 return index; 252 } 253 254 260 private int decrement(int index) { 261 index--; 262 if (index < 0) { 263 index = buffer.length - 1; 264 } 265 return index; 266 } 267 268 273 public Iterator iterator() { 274 return new Iterator () { 275 276 private int index = head; 277 private int lastReturnedIndex = -1; 278 279 public boolean hasNext() { 280 return index != tail; 281 282 } 283 284 public Object next() { 285 if (!hasNext()) { 286 throw new NoSuchElementException (); 287 } 288 lastReturnedIndex = index; 289 index = increment(index); 290 return buffer[lastReturnedIndex]; 291 } 292 293 public void remove() { 294 if (lastReturnedIndex == -1) { 295 throw new IllegalStateException (); 296 } 297 298 if (lastReturnedIndex == head) { 300 UnboundedFifoBuffer.this.remove(); 301 lastReturnedIndex = -1; 302 return; 303 } 304 305 int i = lastReturnedIndex + 1; 307 while (i != tail) { 308 if (i >= buffer.length) { 309 buffer[i - 1] = buffer[0]; 310 i = 0; 311 } else { 312 buffer[i - 1] = buffer[i]; 313 i++; 314 } 315 } 316 317 lastReturnedIndex = -1; 318 tail = decrement(tail); 319 buffer[tail] = null; 320 index = decrement(index); 321 } 322 323 }; 324 } 325 326 } 327 | Popular Tags |