Class 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 a CyclicList. CyclicIterator corresponds with CyclicLists as Iterators or ListIterators does with Lists. Nevertheless, CyclicIterator does not implement java.util.Iterator.
    Version:
    1.0
    Author:
    Ernst Reissner
    See Also:
    CyclicList
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private CyclicArrayList<E> cal  
      private CyclicArrayList.StateIter calledLast
      Indicates the last method invoked.
      private int index
      A non-negative index which points, modulo list.size()-1, to the 0,...
      private int startIndex
      Points to the beginning of this cyclic list: this.hasPrev() == false iff index == startIndex.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(E obj)
      Inserts the specified element into the cyclic list.
      void addAll​(java.util.List<? extends E> addList)
      Inserts the specified list into the underlying cyclic list.
      double dist​(CyclicIterator<E> other)  
      boolean equals​(java.lang.Object other)
      Returns whether other is also an instance of CyclicIterator and, if so, whether the underlying list and the indices coincide.
      CyclicList<E> getCyclicList()
      Returns the CyclicList this iterator points to.
      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 index 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 whether a subsequent call to next() would return an element rather than throwing an exception.
      boolean hasPrev()
      Returns whether a subsequent call to previous() does not throw an exception.
      E next()
      Returns the next element in the interation.
      E previous()
      Returns the previous element in the cyclic list.
      void refresh()
      Reinitializes this iterator without changing the cursor (i.e. modulo list.size()) but such that all elements of the corresponding cyclic list may be accessed successively through next().
      void remove()
      Removes from the underlying CyclicList the last element returned by the iterator.
      boolean retEquals​(CyclicIterator<?> other)
      Returns whether the two cyclic iterators given return the same elements in the same order if method next() is invoked sequentially.
      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 pointer to the given index modulo the length of the list.
      java.lang.String toString()
      Returns a string representation consisting of the cyclic list corresponding with this iterator .
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Field Detail

      • index

        private int index
        A non-negative index which points, modulo list.size()-1, to the 0,...,list.size()-1th element of list provided CyclicArrayList.list is not empty; if it is empty, index == -1.
      • startIndex

        private int startIndex
        Points to the beginning of this cyclic list: this.hasPrev() == false iff index == startIndex. It also determines the end of this list implicitly: this.hasNext() == false iff this.index < this.startIndex+this.list.size(). This is true also for CyclicArrayList.list.isEmpty().
        See Also:
        hasNext(), hasPrev(), index
    • Constructor Detail

      • CyclicArrayIterator

        public CyclicArrayIterator​(CyclicArrayList<E> cal,
                                   int index)
        Creates a new CyclicIterator for the given list, pointing to the element with the position given. This position is modulo list.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 modulo list.size(), provided list is not empty. In the latter case, index is ignored
      • CyclicArrayIterator

        public CyclicArrayIterator​(CyclicArrayList<E> cal,
                                   CyclicArrayList.CyclicArrayIterator<E> iter)
        Creates a fresh CyclicPtIterator with the same list and the same pointer as the CyclicPtIterator given.

        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 - some CyclicPtItererator.
    • 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)
      • refresh

        public void refresh()
        Reinitializes this iterator without changing the cursor (i.e. modulo list.size()) but such that all elements of the corresponding cyclic list may be accessed successively through next(). On the other hand, previous() throws an exception.
        Specified by:
        refresh in interface CyclicIterator<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:
        setIndex in interface CyclicIterator<E>
        Parameters:
        index - an int representing a pointer on the underlying list. This may also be negative.
      • getIndex

        public int getIndex()
        Returns the current index of this iterator.
        Specified by:
        getIndex in interface CyclicIterator<E>
        Returns:
        an int value
      • hasNext

        public boolean hasNext()
        Returns whether a subsequent call to next() would return an element rather than throwing an exception.
        Specified by:
        hasNext in interface CyclicIterator<E>
        Specified by:
        hasNext in interface java.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 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 java.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 to previous() does not throw an exception.
        Specified by:
        hasPrev in interface CyclicIterator<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 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.
        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 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.cal.size()-1} satisfying obj.equals(this.cal.get(ind)) if possible;
        • -1 if there is no such 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 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 to previousIndex.)

        Specified by:
        add in interface CyclicIterator<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 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:
        addList - the list to be inserted.
        See Also:
        CyclicIterator.add(E)
      • set

        public void set​(E obj)
        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.
        Throws:
        java.lang.IllegalStateException - if neither next nor previous have been called, or remove or add have been called after the last call to next or previous.
      • remove

        public void remove()
        Removes from the underlying CyclicList the last element returned by the iterator. This method can be called only once per call to next or to previous. 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:
        remove in interface CyclicIterator<E>
        Specified by:
        remove in interface java.util.Iterator<E>
        Throws:
        java.lang.IllegalStateException - if the next method has not yet been called, or the remove method has already been called after the last call to the next method.
      • equals

        public boolean equals​(java.lang.Object other)
        Returns whether other is also an instance of CyclicIterator and, if so, whether the underlying list and the indices coincide.
        Specified by:
        equals in interface CyclicIterator<E>
        Overrides:
        equals in class java.lang.Object
        Parameters:
        other - another Object; possibly null.
        Returns:
        Returns false if other is not an instance of CyclicIterator. Otherwise returns true if all of the following methods return equal values: getIndex(), getFirstIndex() and getCyclicList().
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • retEquals

        public boolean retEquals​(CyclicIterator<?> other)
        Returns whether the two cyclic iterators given return the same elements in the same order if method next() is invoked sequentially.
        Specified by:
        retEquals in interface CyclicIterator<E>
        Parameters:
        other - another CyclicIterator.
        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 be null.
      • 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:
        toString in class java.lang.Object
        Returns:
        a String representing this iterator.