textbender.g.util
Class SequenceCircler

java.lang.Object
  extended by SequenceCircler

public final class SequenceCircler
extends Object

Circular indexing for a fixed-length sequence.

Thread safe, except where marked.


Constructor Summary
SequenceCircler(int s)
          Creates a SequenceCircler.
 
Method Summary
 int getCursor()
          Returns the index of the cursor.
 int indexOf(int offset)
          Returns the circular (wrapped) index of an offset from the cursor.
 void setCursor(int c)
          Sets the index of the cursor.
 int size()
          Returns the size of the sequence.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SequenceCircler

public SequenceCircler(int s)
Creates a SequenceCircler.

Parameters:
s - size per size()
Method Detail

getCursor

public int getCursor()
Returns the index of the cursor.


setCursor

public void setCursor(int c)
Sets the index of the cursor.

Thread-safe iff setters sync (and getters touch-sync) on SequenceCircler.this for cross-thread visibility/ordering of writes.

Parameters:
c - new index of the cursor
Throws:
ArrayIndexOutOfBoundsException - if c < 0 or c >= size()

indexOf

public int indexOf(int offset)
Returns the circular (wrapped) index of an offset from the cursor.

Parameters:
offset - (positive or negative) from the cursor
Returns:
index in sequence of cursor + offset, wrapped as necessary around the ends of the sequence
Throws:
ArrayIndexOutOfBoundsException - if |offset| > size(), which would wrap past the cursor

size

public int size()
Returns the size of the sequence.