Class CollectionsExt.NonModifyingCyclicIterator<E>

  • Type Parameters:
    E - the class of the elements to iterate over.
    All Implemented Interfaces:
    CyclicIterator<E>, Iterator<E>
    Enclosing class:
    CollectionsExt<E>

    public static final class CollectionsExt.NonModifyingCyclicIterator<E>
    extends 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 with CollectionsExt.ImmutableCyclicList.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(E obj)
      Inserts the specified element into the underlying cyclic list (optional operation).
      void addAll​(List<? extends E> list)
      Inserts the specified list into the underlying cyclic list (optional operation).
      double dist​(CyclicIterator<E> other)  
      boolean equals​(Object other)
      Returns false if other is not an instance of CyclicIterator.
      CyclicList<E> getCyclicList()
      Returns the cyclic list to which this iterator points.
      int getFirstIndex()
      Returns the cursor of this iterator immediately after it has been created (if not modified since then which is currently not possible.).
      int getIndex()
      Returns the current cursor of this iterator.
      int getNextIndexOf​(E obj)
      Returns the (non-negative) index of the next object returned by next which equals the given one, if possible; otherwise returns -1.
      int hashCode()  
      boolean hasNext()
      Returns true if the iteration has more elements.
      boolean hasPrev()
      Returns true if this iterator has more elements when traversing the cyclic list in the reverse direction.
      E next()
      Returns the next element in the interation.
      E previous()
      Returns the previous element in the cyclic list.
      void refresh()
      Reinitialize this iterator without changing the cursor but such that all elements of the corresponding cyclic list may be accessed successively through CyclicIterator.next().
      void remove()
      Removes from the underlying CyclicList the last element returned by next or previous (optional operation).
      boolean retEquals​(CyclicIterator<?> other)  
      void set​(E obj)
      Replaces the last element returned by next or previous with the specified element (optional operation).
      void setIndex​(int index)
      Sets the given index as cursor of this iterator.
    • Constructor Detail

      • NonModifyingCyclicIterator

        NonModifyingCyclicIterator​(CyclicIterator<E> wrapped)
    • Method Detail

      • getFirstIndex

        public int getFirstIndex()
        Description copied from interface: CyclicIterator
        Returns the cursor of this iterator immediately after it has been created (if not modified since then which is currently not possible.).
        Specified by:
        getFirstIndex in interface CyclicIterator<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: CyclicIterator
        Returns the current cursor of this iterator.
        Specified by:
        getIndex in interface CyclicIterator<E>
        Returns:
        the current cursor of this iterator.
      • getCyclicList

        public CyclicList<E> getCyclicList()
        Description copied from interface: CyclicIterator
        Returns the cyclic list to which this iterator points. Contract: cyclicList.cyclicIterator(int).getCyclicList() == cyclicList again.
        Specified by:
        getCyclicList in interface CyclicIterator<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: CyclicIterator
        Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)
        Specified by:
        hasNext in interface CyclicIterator<E>
        Specified by:
        hasNext in interface Iterator<E>
        Returns:
        true if the iterator has more elements.
      • next

        public E next()
        Description copied from interface: CyclicIterator
        Returns the next element in the interation. This method may be called repeatedly to iterate through the list, or intermixed with calls to previous to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)
        Specified by:
        next in interface CyclicIterator<E>
        Specified by:
        next in interface Iterator<E>
        Returns:
        the next element in the interation.
      • hasPrev

        public boolean hasPrev()
        Description copied from interface: CyclicIterator
        Returns true if this iterator has more elements when traversing the cyclic list in the reverse direction. (In other words, returns true if previous would return an element rather than throwing an exception.)
        Specified by:
        hasPrev in interface CyclicIterator<E>
        Returns:
        true if the list iterator has more elements when traversing the list in the reverse direction.
      • previous

        public E previous()
        Description copied from interface: CyclicIterator
        Returns the previous element in the cyclic list. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to next to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)
        Specified by:
        previous in interface CyclicIterator<E>
        Returns:
        the previous element in the list.
      • getNextIndexOf

        public int getNextIndexOf​(E obj)
        Description copied from interface: CyclicIterator
        Returns the (non-negative) index of the next object returned by next which equals the given one, if possible; otherwise returns -1.
        Specified by:
        getNextIndexOf in interface CyclicIterator<E>
        Parameters:
        obj - an object.
        Returns:
        • the index minimal index ind in {0,...,this.list.size()-1} satisfying obj.equals(this.list.get(ind)) if possible;
        • -1 if there is no such index.
      • setIndex

        public void setIndex​(int index)
        Description copied from interface: CyclicIterator
        Sets the given index as cursor of this iterator. Consider the case first that the underlying list CyclicIterator.getCyclicList() is not empty. Then it.setIndex(index); return it.getIndex(); returns index again up to it.getCyclicList().size(). For it.getCyclicList().isEmpty(), this method does not modify this iterator.
        Specified by:
        setIndex in interface CyclicIterator<E>
        Parameters:
        index - an arbitrary int value, which may also be negative.
      • add

        public void add​(E obj)
        Description copied from interface: CyclicIterator
        Inserts the specified element into the underlying cyclic list (optional operation). The element is inserted immediately before the next element that would be returned by next, if any, and after the next element that would be returned by previous, 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 next would be unaffected, and a subsequent call to previous would return the new element. (This call increases by one the value that would be returned by a call to nextIndex or previousIndex.)

        Specified by:
        add in interface CyclicIterator<E>
        Parameters:
        obj - the element to be inserted.
        See Also:
        CyclicIterator.addAll(java.util.List<? extends E>)
      • addAll

        public void addAll​(List<? extends E> list)
        Description copied from interface: CyclicIterator
        Inserts the specified list into the underlying cyclic list (optional operation). The list is inserted immediately before the next element that would be returned by next, if any, and after the next element that would be returned by previous, 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 next would be unaffected, and a subsequent call to previous would return the given list in reversed order. (This call increases by list.size() the value that would be returned by a call to nextIndex or previousIndex.)

        If list.size() contains a single element e, addAll(list) is equivalent with add(e).

        Specified by:
        addAll in interface CyclicIterator<E>
        Parameters:
        list - the list to be inserted.
        See Also:
        CyclicIterator.add(E)
      • set

        public void set​(E obj)
        Description copied from interface: CyclicIterator
        Replaces the last element returned by next or previous with the specified element (optional operation). This call can be made only if neither ListIterator.remove nor add have been called after the last call to next or previous.
        Specified by:
        set in interface CyclicIterator<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: CyclicIterator
        Removes from the underlying CyclicList the last element returned by next or previous (optional operation). This method can be called only once per call to next or previous. It can be made only if add has not been called after the last call to next or previous.
        Specified by:
        remove in interface CyclicIterator<E>
        Specified by:
        remove in interface Iterator<E>
      • equals

        public boolean equals​(Object other)
        Description copied from interface: CyclicIterator
        Returns false if other is not an instance of CyclicIterator. The implementation of this interface should not play a role.
        Specified by:
        equals in interface CyclicIterator<E>
        Overrides:
        equals in class Object
        Parameters:
        other - another Object.
        Returns:
        false if other is not an instance of CyclicIterator. The implementation of this interface should not play a role.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object