1 22 package info.monitorenter.util.collections; 23 24 import info.monitorenter.util.StringUtil; 25 26 import java.util.Iterator ; 27 import java.util.NoSuchElementException ; 28 29 30 51 public class RingBufferArrayFast implements Cloneable , IRingBuffer { 52 62 protected abstract class ARingBufferIterator implements Iterator { 63 67 protected int m_count; 68 69 70 protected int m_pos; 71 72 77 ARingBufferIterator() { 78 } 79 80 83 public boolean hasNext() { 84 return (this.m_count < RingBufferArrayFast.this.size()); 85 } 86 87 88 protected abstract void incPos(); 89 90 93 public Object next() { 94 if (!this.hasNext()) { 95 throw new NoSuchElementException (); 96 } 97 Object result = RingBufferArrayFast.this.m_buffer[this.m_pos]; 98 this.m_count++; 99 this.incPos(); 100 if (result == null) { 101 throw new NoSuchElementException ("RingBufferArrayFast.iteratorF2L returns null: pos:" 102 + this.m_pos + " count: " + this.m_count); 103 } 104 return result; 105 } 106 107 116 public void remove() throws UnsupportedOperationException { 117 throw new UnsupportedOperationException (); 118 } 119 120 } 121 122 126 public static final boolean DEBUG = false; 127 128 131 private static final long serialVersionUID = 3834590997991404595L; 132 133 134 protected Object [] m_buffer; 135 136 163 protected boolean m_empty = true; 164 165 169 protected int m_headpointer = 0; 170 171 178 protected int m_size; 179 180 183 protected int m_tailpointer = 0; 184 185 191 public RingBufferArrayFast(final int aSize) { 192 this.m_buffer = new Object [aSize]; 193 this.m_size = aSize - 1; 194 } 195 196 207 public Object add(final Object anObject) { 208 Object ret = null; 209 if (this.isFull()) { 210 ret = this.m_buffer[this.m_tailpointer]; 211 this.incTail(); 212 } 213 if (RingBufferArray.DEBUG) { 214 System.out.println("add: tailpointer: " + this.m_tailpointer + " headpointer: " 215 + this.m_headpointer + " size: " + this.size()); 216 } 217 this.m_buffer[this.m_headpointer] = anObject; 218 this.incHead(); 219 return ret; 220 } 221 222 229 public void clear() { 230 this.m_headpointer = 0; 231 this.m_tailpointer = 0; 232 this.m_empty = true; 233 } 234 235 238 public int getBufferSize() { 239 return this.m_size + 1; 240 } 241 242 245 public Object getOldest() throws RingBufferException { 246 if (this.isEmpty()) { 247 throw new IRingBuffer.RingBufferException("Buffer is empty."); 248 } 249 return this.m_buffer[this.m_tailpointer]; 250 } 251 252 256 public Object getYoungest() throws RingBufferException { 257 if (isEmpty()) { 258 throw new IRingBuffer.RingBufferException("Buffer is empty."); 259 } 260 int tmp = this.m_headpointer; 261 if (tmp == 0) { 262 tmp = this.m_size; 263 } else { 264 tmp--; 265 } 266 return this.m_buffer[tmp]; 267 } 268 269 274 private void incHead() { 275 if (this.m_headpointer == this.m_size) { 276 this.m_headpointer = 0; 277 } else { 278 this.m_headpointer++; 279 } 280 this.m_empty = false; 281 } 282 283 288 private void incTail() { 289 if (this.m_tailpointer == this.m_size) { 290 this.m_tailpointer = 0; 291 } else { 292 this.m_tailpointer++; 293 } 294 if (this.m_tailpointer == this.m_headpointer) { 295 this.m_empty = true; 296 } 297 } 298 299 302 public boolean isEmpty() { 303 if (RingBufferArray.DEBUG) { 304 System.out.println("isEmpty: " + this.m_empty + " head: " + this.m_headpointer + " tail: " 305 + this.m_tailpointer); 306 } 307 return this.m_empty; 308 } 309 310 313 public boolean isFull() { 314 boolean ret = (this.m_headpointer == this.m_tailpointer) && !this.m_empty; 315 if (RingBufferArray.DEBUG) { 316 System.out.println("isFull: " + ret + " head: " + this.m_headpointer + " tail: " 317 + this.m_tailpointer); 318 } 319 return ret; 320 } 321 322 335 336 public java.util.Iterator iteratorF2L() { 337 return new ARingBufferIterator() { 338 { 339 this.m_pos = (RingBufferArrayFast.this.m_headpointer == 0) ? RingBufferArrayFast.this 340 .size() - 1 : RingBufferArrayFast.this.m_headpointer - 1; 341 } 342 343 protected void incPos() { 344 if (this.m_pos == 0) { 345 this.m_pos = RingBufferArrayFast.this.m_size; 346 } else { 347 this.m_pos--; 348 } 349 } 350 }; 351 } 352 353 364 public java.util.Iterator iteratorL2F() { 365 return new ARingBufferIterator() { 366 { 367 this.m_pos = RingBufferArrayFast.this.m_tailpointer; 368 } 369 370 373 protected void incPos() { 374 if (this.m_pos == RingBufferArrayFast.this.m_size) { 375 this.m_pos = 0; 376 } else { 377 this.m_pos++; 378 } 379 } 380 }; 381 } 382 383 386 public Object remove() { 387 if (this.isEmpty()) { 388 throw new IRingBuffer.RingBufferException("Buffer is empty."); 389 } 390 Object ret = null; 391 ret = this.m_buffer[this.m_tailpointer]; 392 this.incTail(); 393 if (RingBufferArray.DEBUG) { 394 System.out.println("Removing element: " + ret + " head: " + this.m_headpointer + " tail: " 395 + this.m_tailpointer + " size: " + this.size()); 396 } 397 return ret; 398 } 399 400 403 public Object [] removeAll() { 404 Object [] ret = new Object [this.size()]; 405 if (RingBufferArray.DEBUG) { 406 System.out.println("removeAll()"); 407 } 408 for (int i = 0; i < ret.length; i++) { 409 ret[i] = this.remove(); 410 } 411 return ret; 412 } 413 414 428 public void setBufferSize(final int newSize) { 429 Object [] newbuffer = new Object [newSize]; 430 boolean emptyStore = this.m_empty; 431 int i = 0, j = 0; 432 if (RingBufferArray.DEBUG) { 433 System.out.println("setBufferSize(" + newSize + "): isEmpty(): " + this.isEmpty() + " tail: " 434 + this.m_tailpointer + " head: " + this.m_headpointer); 435 } 436 int oldSize = this.size(); 438 int stop = oldSize - newSize; 439 for (; i < stop && !this.isEmpty(); i++) { 440 this.remove(); 441 } 442 for (j = 0; j < newSize && !this.isEmpty(); j++) { 444 newbuffer[j] = this.remove(); 445 } 446 this.m_tailpointer = 0; 447 this.m_headpointer = j; 448 if (this.m_headpointer == newSize) { 449 this.m_headpointer = 0; 450 } 451 this.m_buffer = newbuffer; 452 this.m_size = newSize - 1; 453 this.m_empty = emptyStore || (newSize == 0); 454 } 455 456 459 public int size() { 460 if (this.m_empty) { 461 return 0; 462 } else if (this.m_headpointer == this.m_tailpointer) { 463 return this.m_size + 1; 464 } else if (this.m_headpointer > this.m_tailpointer) { 465 return this.m_headpointer - this.m_tailpointer; 466 } else { 467 return this.m_headpointer + this.m_size + 1 - this.m_tailpointer; 468 } 469 } 470 471 480 public String toString() { 481 if (this.isEmpty()) { 482 if (RingBufferArray.DEBUG) { 483 System.out.println("toString(): isEmpty: true"); 484 } 485 return "[]"; 486 } 487 Object [] actualcontent = new Object [this.size()]; 488 int tmp = this.m_tailpointer; 489 int i = 0; 490 for (; i < actualcontent.length; i++) { 491 actualcontent[i] = this.m_buffer[tmp]; 492 if (tmp == this.m_size) { 493 tmp = 0; 494 } else { 495 tmp++; 496 } 497 if (tmp == this.m_headpointer && this.m_empty) { 498 break; 499 } 500 } 501 return StringUtil.arrayToString(actualcontent); 502 } 503 } 504 | Popular Tags |