bb.util
Class ListWithIndex

java.lang.Object
  extended by bb.util.ListWithIndex

public class ListWithIndex
extends Object

Contains an internal List, as well as keeps track of the current position (index) in the List. Elements are gotten, added, or removed from the current index. The index is -1 if the List has size 0.

This component is a Java Bean, and supports "index" as a bound property. See the Sun tutorial for details. Implementation note: must always use the setIndex method to change the index field to guarantee proper index event firing.

This class is not multithread safe.


Field Summary
private  PropertyChangeSupport changes
           
private  int index
           
private  List<Object> list
           
 
Constructor Summary
ListWithIndex()
           
 
Method Summary
 void add(Object obj)
          Inserts obj into the List right after the element that index currently points to.
 void addPropertyChangeListener(PropertyChangeListener listener)
           
 void decrementIndex()
          If hasPrevious() returns true, then this method decrements index.
 Object get()
          Returns the element at index.
 Object getAtOffset(int offset)
          Returns the element at index + offset.
 int getIndex()
          Returns the current value of index.
 List<Object> getList()
          Returns a new List instance which contains all the elements currently in the internal List.
 boolean hasAtOffset(int offset)
          Reports whether or not there is an element at index + offset.
 boolean hasNext()
          Reports whether or not there is an element after the current value of index.
 boolean hasPrevious()
          Reports whether or not there is an element before the current value of index.
 void incrementIndex()
          If hasNext() returns true, then this method increments index.
 void offsetIndex(int offset)
          Changes index to index + offset.
 Object remove()
          Removes the element at index from the list.
 void removePropertyChangeListener(PropertyChangeListener listener)
           
 void setIndex(int index)
          Assigns the current value of index.
 int size()
          Returns the size of the internal List.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

list

private final List<Object> list

changes

private final PropertyChangeSupport changes

index

private int index
Constructor Detail

ListWithIndex

public ListWithIndex()
Method Detail

get

public Object get()
           throws IllegalStateException
Returns the element at index. There are no side effects of calling this method (i.e. index is left unchanged).

Throws:
IllegalStateException - if list.size() == 0

getAtOffset

public Object getAtOffset(int offset)
                   throws IllegalStateException
Returns the element at index + offset. There are no side effects of calling this method (i.e. index is left unchanged).

Throws:
IllegalStateException - if hasAtOffset(offset) returns false

add

public void add(Object obj)
Inserts obj into the List right after the element that index currently points to. Side effect: index is incremented (so that it now points to obj). Contract: index will never become an illegal value by calling this method.

Parameters:
obj - any Object, including null

remove

public Object remove()
              throws IllegalStateException
Removes the element at index from the list. Side effect: if, after removal of the element, index now points outside the List (i.e. it has the value list.size()), it will be decremented. Contract: index will never become an illegal value by calling this method.

Returns:
the element that was removed from the list
Throws:
IllegalStateException - if list.size() == 0

size

public int size()
Returns the size of the internal List.


getList

public List<Object> getList()
Returns a new List instance which contains all the elements currently in the internal List.


getIndex

public int getIndex()
Returns the current value of index. Will be -1 if list.size() == 0.


setIndex

public void setIndex(int index)
              throws IllegalStateException
Assigns the current value of index.

Throws:
IllegalStateException - if list.size() == 0 && index != -1; list.size() > 0 && index < 0; index >= list.size()

offsetIndex

public void offsetIndex(int offset)
                 throws IllegalStateException
Changes index to index + offset.

Throws:
IllegalStateException - if setIndex objects to index + offset as a new index

incrementIndex

public void incrementIndex()
If hasNext() returns true, then this method increments index. Otherwise this method leaves index unchanged. Contract: index will never become an illegal value by calling this method.


decrementIndex

public void decrementIndex()
If hasPrevious() returns true, then this method decrements index. Otherwise this method leaves index unchanged. Contract: index will never become an illegal value by calling this method.


hasPrevious

public boolean hasPrevious()
Reports whether or not there is an element before the current value of index.


hasNext

public boolean hasNext()
Reports whether or not there is an element after the current value of index.


hasAtOffset

public boolean hasAtOffset(int offset)
Reports whether or not there is an element at index + offset.


addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
                               throws IllegalArgumentException
Throws:
IllegalArgumentException

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
                                  throws IllegalArgumentException
Throws:
IllegalArgumentException