Class CyclicArrayList.CyclicArrayIterator<E>
- java.lang.Object
-
- eu.simuline.util.CyclicArrayList.CyclicArrayIterator<E>
-
- Type Parameters:
E- the class of the elements to be iterated over.
- All Implemented Interfaces:
CyclicIterator<E>,java.util.Iterator<E>
- Enclosing class:
- CyclicArrayList<E>
public static final class CyclicArrayList.CyclicArrayIterator<E> extends java.lang.Object implements CyclicIterator<E>
An iterator over aCyclicList.CyclicIteratorcorresponds withCyclicLists asIterators orListIterators does withLists. Nevertheless,CyclicIteratordoes not implementjava.util.Iterator.- Version:
- 1.0
- Author:
- Ernst Reissner
- See Also:
CyclicList
-
-
Field Summary
Fields Modifier and Type Field Description private CyclicArrayList<E>calprivate CyclicArrayList.StateItercalledLastIndicates the last method invoked.private intindexA non-negative index which points, modulolist.size()-1, to the0,...private intstartIndexPoints to the beginning of this cyclic list:this.hasPrev() == falseiffindex == startIndex.
-
Constructor Summary
Constructors Constructor Description CyclicArrayIterator(CyclicArrayList<E> cal, int index)Creates a newCyclicIteratorfor the given list, pointing to the element with the position given.CyclicArrayIterator(CyclicArrayList<E> cal, CyclicArrayList.CyclicArrayIterator<E> iter)Creates a freshCyclicPtIteratorwith the same list and the same pointer as theCyclicPtIteratorgiven.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(E obj)Inserts the specified element into the cyclic list.voidaddAll(java.util.List<? extends E> addList)Inserts the specified list into the underlying cyclic list.doubledist(CyclicIterator<E> other)booleanequals(java.lang.Object other)Returns whetherotheris also an instance ofCyclicIteratorand, if so, whether the underlying list and the indices coincide.CyclicList<E>getCyclicList()Returns theCyclicListthis iterator points to.intgetFirstIndex()Returns the cursor of this iterator immediately after it has been created (if not modified since then which is currently not possible.).intgetIndex()Returns the current index of this iterator.intgetNextIndexOf(E obj)Returns the (non-negative) index of the next object returned bynextwhich equals the given one, if possible; otherwise returns-1.inthashCode()booleanhasNext()Returns whether a subsequent call tonext()would return an element rather than throwing an exception.booleanhasPrev()Returns whether a subsequent call toprevious()does not throw an exception.Enext()Returns the next element in the interation.Eprevious()Returns the previous element in the cyclic list.voidrefresh()Reinitializes this iterator without changing the cursor (i.e. modulolist.size()) but such that all elements of the corresponding cyclic list may be accessed successively throughnext().voidremove()Removes from the underlyingCyclicListthe last element returned by the iterator.booleanretEquals(CyclicIterator<?> other)Returns whether the two cyclic iterators given return the same elements in the same order if methodnext()is invoked sequentially.voidset(E obj)Replaces the last element returned bynextorpreviouswith the specified element (optional operation).voidsetIndex(int index)Sets the pointer to the given index modulo the length of the list.java.lang.StringtoString()Returns a string representation consisting of the cyclic list corresponding with this iterator .
-
-
-
Field Detail
-
calledLast
private CyclicArrayList.StateIter calledLast
Indicates the last method invoked.- See Also:
refresh()
-
index
private int index
A non-negative index which points, modulolist.size()-1, to the0,...,list.size()-1th element oflistprovidedCyclicArrayList.listis not empty; if it is empty,index == -1.
-
startIndex
private int startIndex
Points to the beginning of this cyclic list:this.hasPrev() == falseiffindex == startIndex. It also determines the end of this list implicitly:this.hasNext() == falseiffthis.index < this.startIndex+this.list.size(). This is true also for.CyclicArrayList.list.isEmpty()
-
cal
private CyclicArrayList<E> cal
-
-
Constructor Detail
-
CyclicArrayIterator
public CyclicArrayIterator(CyclicArrayList<E> cal, int index)
Creates a newCyclicIteratorfor the given list, pointing to the element with the position given. This position is modulolist.size()and may also be negative.Note that this list may be empty (in which case the indices are -1) but by construction it may not be
null.- Parameters:
index- an index modulolist.size(), providedlistis not empty. In the latter case,indexis ignored
-
CyclicArrayIterator
public CyclicArrayIterator(CyclicArrayList<E> cal, CyclicArrayList.CyclicArrayIterator<E> iter)
Creates a freshCyclicPtIteratorwith the same list and the same pointer as theCyclicPtIteratorgiven.An iterator is called "fresh" if it delivers all elements of its list. Equivalently, it is called fresh, if after having created it, no getNext-method has been invoked.
- Parameters:
iter- someCyclicPtItererator.
-
-
Method Detail
-
getFirstIndex
public int getFirstIndex()
Description copied from interface:CyclicIteratorReturns the cursor of this iterator immediately after it has been created (if not modified since then which is currently not possible.).- Specified by:
getFirstIndexin interfaceCyclicIterator<E>- Returns:
- the cursor of this iterator immediately after it has been created (if not modified since then).
- See Also:
CyclicList.cyclicIterator(int)
-
refresh
public void refresh()
Reinitializes this iterator without changing the cursor (i.e. modulolist.size()) but such that all elements of the corresponding cyclic list may be accessed successively throughnext(). On the other hand,previous()throws an exception.- Specified by:
refreshin interfaceCyclicIterator<E>
-
setIndex
public void setIndex(int index)
Sets the pointer to the given index modulo the length of the list. For empty list: no change.- Specified by:
setIndexin interfaceCyclicIterator<E>- Parameters:
index- anintrepresenting a pointer on the underlying list. This may also be negative.
-
getIndex
public int getIndex()
Returns the current index of this iterator.- Specified by:
getIndexin interfaceCyclicIterator<E>- Returns:
- an
intvalue
-
getCyclicList
public CyclicList<E> getCyclicList()
Returns theCyclicListthis iterator points to.- Specified by:
getCyclicListin interfaceCyclicIterator<E>- Returns:
- the
CyclicListthis iterator points to. - See Also:
CyclicList.cyclicIterator(int)
-
hasNext
public boolean hasNext()
Returns whether a subsequent call tonext()would return an element rather than throwing an exception.- Specified by:
hasNextin interfaceCyclicIterator<E>- Specified by:
hasNextin interfacejava.util.Iterator<E>- Returns:
- whether a subsequent call to
next()would return an element rather than throwing an exception.
-
next
public E next() throws java.util.NoSuchElementException
Returns the next element in the interation. This method may be called repeatedly to iterate through the list, or intermixed with calls topreviousto go back and forth. (Note that alternating calls tonextandpreviouswill return the same element repeatedly.)- Specified by:
nextin interfaceCyclicIterator<E>- Specified by:
nextin interfacejava.util.Iterator<E>- Returns:
- the next element in the interation.
- Throws:
java.util.NoSuchElementException- iteration has no more elements.
-
hasPrev
public boolean hasPrev()
Returns whether a subsequent call toprevious()does not throw an exception.- Specified by:
hasPrevin interfaceCyclicIterator<E>- Returns:
- Returns whether a subsequent call to
previous()does not throw an exception.
-
previous
public E previous() throws java.util.NoSuchElementException
Returns the previous element in the cyclic list. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls tonextto go back and forth. (Note that alternating calls tonextandpreviouswill return the same element repeatedly.)- Specified by:
previousin interfaceCyclicIterator<E>- Returns:
- the previous element in the list.
- Throws:
java.util.NoSuchElementException- if the iteration has no previous element.
-
getNextIndexOf
public int getNextIndexOf(E obj)
Returns the (non-negative) index of the next object returned bynextwhich equals the given one, if possible; otherwise returns-1.- Specified by:
getNextIndexOfin interfaceCyclicIterator<E>- Parameters:
obj- an object.- Returns:
-
the index minimal index
ind in {0,...,this.cal.size()-1}satisfyingobj.equals(this.cal.get(ind))if possible; -
-1if there is no such index.
-
the index minimal index
-
add
public void add(E obj)
Inserts the specified element into the cyclic list. The element is inserted immediately before the next element that would be returned bynext, if any, and after the next element that would be returned byprevious, if any. (If the cyclic list is empty, the new element becomes the sole element on the cyclic list.)The new element is inserted before the implicit cursor: a subsequent call to
nextwould be unaffected, and a subsequent call topreviouswould return the new element. (This call increases by one the value that would be returned by a call tonextIndexor topreviousIndex.)- Specified by:
addin interfaceCyclicIterator<E>- Parameters:
obj- An object to be inserted in the list this iterator points to.- See Also:
CyclicIterator.addAll(java.util.List<? extends E>)
-
addAll
public void addAll(java.util.List<? extends E> addList)
Inserts the specified list into the underlying cyclic list. The list is inserted immediately before the next element that would be returned bynext, if any, and after the next element that would be returned byprevious, if any. (If the cyclic list is empty, the new cyclic list comprises the given list.)The given list is inserted before the implicit cursor: a subsequent call to
nextwould be unaffected, and a subsequent call topreviouswould return the given list in reversed order. (This call increases bylist.size()the value that would be returned by a call tonextIndexorpreviousIndex.)If
list.size()contains a single elemente,addAll(list)is equivalent withadd(e).- Specified by:
addAllin interfaceCyclicIterator<E>- Parameters:
addList- the list to be inserted.- See Also:
CyclicIterator.add(E)
-
set
public void set(E obj)
Replaces the last element returned bynextorpreviouswith the specified element (optional operation). This call can be made only if neitherListIterator.removenoraddhave been called after the last call tonextorprevious.- Specified by:
setin interfaceCyclicIterator<E>- Parameters:
obj- the element with which to replace the last element returned by next or previous.- Throws:
java.lang.IllegalStateException- if neithernextnorprevioushave been called, orremoveoraddhave been called after the last call tonextorprevious.
-
remove
public void remove()
Removes from the underlyingCyclicListthe last element returned by the iterator. This method can be called only once per call tonextor toprevious. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.- Specified by:
removein interfaceCyclicIterator<E>- Specified by:
removein interfacejava.util.Iterator<E>- Throws:
java.lang.IllegalStateException- if thenextmethod has not yet been called, or theremovemethod has already been called after the last call to thenextmethod.
-
equals
public boolean equals(java.lang.Object other)
Returns whetherotheris also an instance ofCyclicIteratorand, if so, whether the underlying list and the indices coincide.- Specified by:
equalsin interfaceCyclicIterator<E>- Overrides:
equalsin classjava.lang.Object- Parameters:
other- anotherObject; possiblynull.- Returns:
- Returns
falseifotheris not an instance ofCyclicIterator. Otherwise returnstrueif all of the following methods return equal values:getIndex(),getFirstIndex()andgetCyclicList().
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
retEquals
public boolean retEquals(CyclicIterator<?> other)
Returns whether the two cyclic iterators given return the same elements in the same order if methodnext()is invoked sequentially.- Specified by:
retEqualsin interfaceCyclicIterator<E>- Parameters:
other- anotherCyclicIterator.- Returns:
- whether the two cyclic iterators given
return the same elements in the same order
if method
next()is invoked sequentially as long as possible. This imposes that the lengths of the sequences coincide. The elements in the sequence may well benull.
-
dist
public double dist(CyclicIterator<E> other)
- Specified by:
distin interfaceCyclicIterator<E>
-
toString
public java.lang.String toString()
Returns a string representation consisting of- the cyclic list corresponding with this iterator .
- The current pointer.
- The first index i of this iterator. and the last one (which is i+size()-1). ******* empty list?!?
- Overrides:
toStringin classjava.lang.Object- Returns:
- a
Stringrepresenting this iterator.
-
-