Package eu.simuline.util
Class CollectionsExt.NonModifyingCyclicIterator<E>
- java.lang.Object
-
- eu.simuline.util.CollectionsExt.NonModifyingCyclicIterator<E>
-
- Type Parameters:
E- the class of the elements to iterate over.
- All Implemented Interfaces:
CyclicIterator<E>,java.util.Iterator<E>
- Enclosing class:
- CollectionsExt<E>
public static final class CollectionsExt.NonModifyingCyclicIterator<E> extends java.lang.Object implements CyclicIterator<E>
An interator which prevents modification on the underlying list, by throwing an exception for the modifying methods. This is used in conjunction withCollectionsExt.ImmutableCyclicList.
-
-
Field Summary
Fields Modifier and Type Field Description private CyclicIterator<E>wrapped
-
Constructor Summary
Constructors Constructor Description NonModifyingCyclicIterator(CyclicIterator<E> wrapped)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(E obj)Inserts the specified element into the underlying cyclic list (optional operation).voidaddAll(java.util.List<? extends E> list)Inserts the specified list into the underlying cyclic list (optional operation).doubledist(CyclicIterator<E> other)booleanequals(java.lang.Object other)Returnsfalseifotheris not an instance ofCyclicIterator.CyclicList<E>getCyclicList()Returns the cyclic list to which this iterator points.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 cursor 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()Returnstrueif the iteration has more elements.booleanhasPrev()Returnstrueif this iterator has more elements when traversing the cyclic list in the reverse direction.Enext()Returns the next element in the interation.Eprevious()Returns the previous element in the cyclic list.voidrefresh()Reinitialize this iterator without changing the cursor but such that all elements of the corresponding cyclic list may be accessed successively throughCyclicIterator.next().voidremove()Removes from the underlyingCyclicListthe last element returned bynextorprevious(optional operation).booleanretEquals(CyclicIterator<?> other)voidset(E obj)Replaces the last element returned bynextorpreviouswith the specified element (optional operation).voidsetIndex(int index)Sets the given index as cursor of this iterator.
-
-
-
Field Detail
-
wrapped
private final CyclicIterator<E> wrapped
-
-
Constructor Detail
-
NonModifyingCyclicIterator
NonModifyingCyclicIterator(CyclicIterator<E> wrapped)
-
-
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)
-
getIndex
public int getIndex()
Description copied from interface:CyclicIteratorReturns the current cursor of this iterator.- Specified by:
getIndexin interfaceCyclicIterator<E>- Returns:
- the current cursor of this iterator.
-
getCyclicList
public CyclicList<E> getCyclicList()
Description copied from interface:CyclicIteratorReturns the cyclic list to which this iterator points. Contract:cyclicList.cyclicIterator(int).getCyclicList() == cyclicListagain.- Specified by:
getCyclicListin interfaceCyclicIterator<E>- Returns:
- the cyclic list to which this iterator points.
This may be empty but it may not be
null. - See Also:
CyclicList.cyclicIterator(int)
-
hasNext
public boolean hasNext()
Description copied from interface:CyclicIteratorReturnstrueif the iteration has more elements. (In other words, returnstrueifnextwould return an element rather than throwing an exception.)- Specified by:
hasNextin interfaceCyclicIterator<E>- Specified by:
hasNextin interfacejava.util.Iterator<E>- Returns:
trueif the iterator has more elements.
-
next
public E next()
Description copied from interface:CyclicIteratorReturns 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.
-
hasPrev
public boolean hasPrev()
Description copied from interface:CyclicIteratorReturnstrueif this iterator has more elements when traversing the cyclic list in the reverse direction. (In other words, returnstrueifpreviouswould return an element rather than throwing an exception.)- Specified by:
hasPrevin interfaceCyclicIterator<E>- Returns:
trueif the list iterator has more elements when traversing the list in the reverse direction.
-
previous
public E previous()
Description copied from interface:CyclicIteratorReturns 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.
-
getNextIndexOf
public int getNextIndexOf(E obj)
Description copied from interface:CyclicIteratorReturns 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.list.size()-1}satisfyingobj.equals(this.list.get(ind))if possible; -
-1if there is no such index.
-
the index minimal index
-
setIndex
public void setIndex(int index)
Description copied from interface:CyclicIteratorSets the given index as cursor of this iterator. Consider the case first that the underlying listCyclicIterator.getCyclicList()is not empty. Thenit.setIndex(index); return it.getIndex();returnsindexagain up toit.getCyclicList().size(). Forit.getCyclicList().isEmpty(), this method does not modify this iterator.- Specified by:
setIndexin interfaceCyclicIterator<E>- Parameters:
index- an arbitraryintvalue, which may also be negative.
-
add
public void add(E obj)
Description copied from interface:CyclicIteratorInserts the specified element into the underlying cyclic list (optional operation). 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 tonextIndexorpreviousIndex.)- Specified by:
addin interfaceCyclicIterator<E>- Parameters:
obj- the element to be inserted.- See Also:
CyclicIterator.addAll(java.util.List<? extends E>)
-
addAll
public void addAll(java.util.List<? extends E> list)
Description copied from interface:CyclicIteratorInserts the specified list into the underlying cyclic list (optional operation). 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:
list- the list to be inserted.- See Also:
CyclicIterator.add(E)
-
set
public void set(E obj)
Description copied from interface:CyclicIteratorReplaces 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.
-
remove
public void remove()
Description copied from interface:CyclicIteratorRemoves from the underlyingCyclicListthe last element returned bynextorprevious(optional operation). This method can be called only once per call tonextorprevious. It can be made only ifaddhas not been called after the last call tonextorprevious.- Specified by:
removein interfaceCyclicIterator<E>- Specified by:
removein interfacejava.util.Iterator<E>
-
refresh
public void refresh()
Description copied from interface:CyclicIteratorReinitialize this iterator without changing the cursor but such that all elements of the corresponding cyclic list may be accessed successively throughCyclicIterator.next(). On the other hand,CyclicIterator.previous()throws an exception.- Specified by:
refreshin interfaceCyclicIterator<E>
-
retEquals
public boolean retEquals(CyclicIterator<?> other)
- Specified by:
retEqualsin interfaceCyclicIterator<E>
-
equals
public boolean equals(java.lang.Object other)
Description copied from interface:CyclicIteratorReturnsfalseifotheris not an instance ofCyclicIterator. The implementation of this interface should not play a role.- Specified by:
equalsin interfaceCyclicIterator<E>- Overrides:
equalsin classjava.lang.Object- Parameters:
other- anotherObject.- Returns:
falseifotheris not an instance ofCyclicIterator. The implementation of this interface should not play a role.
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
dist
public double dist(CyclicIterator<E> other)
- Specified by:
distin interfaceCyclicIterator<E>
-
-