LoopingListIterator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.iterators;

  18. import java.util.List;
  19. import java.util.ListIterator;
  20. import java.util.NoSuchElementException;
  21. import java.util.Objects;

  22. import org.apache.commons.collections4.ResettableListIterator;

  23. /**
  24.  * A ListIterator that restarts when it reaches the end or when it
  25.  * reaches the beginning.
  26.  * <p>
  27.  * The iterator will loop continuously around the provided list,
  28.  * unless there are no elements in the collection to begin with, or
  29.  * all of the elements have been {@link #remove removed}.
  30.  * </p>
  31.  * <p>
  32.  * Concurrent modifications are not directly supported, and for most
  33.  * collection implementations will throw a
  34.  * ConcurrentModificationException.
  35.  * </p>
  36.  *
  37.  * @param <E> the type of elements returned by this iterator.
  38.  * @since 3.2
  39.  */
  40. public class LoopingListIterator<E> implements ResettableListIterator<E> {

  41.     /** The list to base the iterator on */
  42.     private final List<E> list;
  43.     /** The current list iterator */
  44.     private ListIterator<E> iterator;

  45.     /**
  46.      * Constructor that wraps a list.
  47.      * <p>
  48.      * There is no way to reset a ListIterator instance without
  49.      * recreating it from the original source, so the List must be
  50.      * passed in and a reference to it held.
  51.      * </p>
  52.      *
  53.      * @param list the list to wrap
  54.      * @throws NullPointerException if the list is null
  55.      */
  56.     public LoopingListIterator(final List<E> list) {
  57.         this.list = Objects.requireNonNull(list, "collection");
  58.         init();
  59.     }

  60.     /**
  61.      * Inserts the specified element into the underlying list.
  62.      * <p>
  63.      * The element is inserted before the next element that would be
  64.      * returned by {@link #next}, if any, and after the next element
  65.      * that would be returned by {@link #previous}, if any.
  66.      * </p>
  67.      * <p>
  68.      * This feature is only supported if the underlying list's
  69.      * {@link List#listIterator} method returns an implementation
  70.      * that supports it.
  71.      * </p>
  72.      *
  73.      * @param obj  the element to insert
  74.      * @throws UnsupportedOperationException if the add method is not
  75.      *  supported by the iterator implementation of the underlying list
  76.      */
  77.     @Override
  78.     public void add(final E obj) {
  79.         iterator.add(obj);
  80.     }

  81.     /**
  82.      * Returns whether this iterator has any more elements.
  83.      * <p>
  84.      * Returns false only if the list originally had zero elements, or
  85.      * all elements have been {@link #remove removed}.
  86.      * </p>
  87.      *
  88.      * @return {@code true} if there are more elements
  89.      */
  90.     @Override
  91.     public boolean hasNext() {
  92.         return !list.isEmpty();
  93.     }

  94.     /**
  95.      * Returns whether this iterator has any more previous elements.
  96.      * <p>
  97.      * Returns false only if the list originally had zero elements, or
  98.      * all elements have been {@link #remove removed}.
  99.      * </p>
  100.      *
  101.      * @return {@code true} if there are more elements
  102.      */
  103.     @Override
  104.     public boolean hasPrevious() {
  105.         return !list.isEmpty();
  106.     }

  107.     private void init() {
  108.         iterator = list.listIterator();
  109.     }

  110.     /**
  111.      * Returns the next object in the list.
  112.      * <p>
  113.      * If at the end of the list, returns the first element.
  114.      * </p>
  115.      *
  116.      * @return the object after the last element returned
  117.      * @throws NoSuchElementException if there are no elements in the list
  118.      */
  119.     @Override
  120.     public E next() {
  121.         if (list.isEmpty()) {
  122.             throw new NoSuchElementException(
  123.                 "There are no elements for this iterator to loop on");
  124.         }
  125.         if (!iterator.hasNext()) {
  126.             reset();
  127.         }
  128.         return iterator.next();
  129.     }

  130.     /**
  131.      * Returns the index of the element that would be returned by a
  132.      * subsequent call to {@link #next}.
  133.      * <p>
  134.      * As would be expected, if the iterator is at the physical end of
  135.      * the underlying list, 0 is returned, signifying the beginning of
  136.      * the list.
  137.      * </p>
  138.      *
  139.      * @return the index of the element that would be returned if next() were called
  140.      * @throws NoSuchElementException if there are no elements in the list
  141.      */
  142.     @Override
  143.     public int nextIndex() {
  144.         if (list.isEmpty()) {
  145.             throw new NoSuchElementException(
  146.                 "There are no elements for this iterator to loop on");
  147.         }
  148.         if (!iterator.hasNext()) {
  149.             return 0;
  150.         }
  151.         return iterator.nextIndex();
  152.     }

  153.     /**
  154.      * Returns the previous object in the list.
  155.      * <p>
  156.      * If at the beginning of the list, return the last element. Note
  157.      * that in this case, traversal to find that element takes linear time.
  158.      * </p>
  159.      *
  160.      * @return the object before the last element returned
  161.      * @throws NoSuchElementException if there are no elements in the list
  162.      */
  163.     @Override
  164.     public E previous() {
  165.         if (list.isEmpty()) {
  166.             throw new NoSuchElementException(
  167.                 "There are no elements for this iterator to loop on");
  168.         }
  169.         if (!iterator.hasPrevious()) {
  170.             E result = null;
  171.             while (iterator.hasNext()) {
  172.                 result = iterator.next();
  173.             }
  174.             iterator.previous();
  175.             return result;
  176.         }
  177.         return iterator.previous();
  178.     }

  179.     /**
  180.      * Returns the index of the element that would be returned by a
  181.      * subsequent call to {@link #previous}.
  182.      * <p>
  183.      * As would be expected, if at the iterator is at the physical
  184.      * beginning of the underlying list, the list's size minus one is
  185.      * returned, signifying the end of the list.
  186.      * </p>
  187.      *
  188.      * @return the index of the element that would be returned if previous() were called
  189.      * @throws NoSuchElementException if there are no elements in the list
  190.      */
  191.     @Override
  192.     public int previousIndex() {
  193.         if (list.isEmpty()) {
  194.             throw new NoSuchElementException(
  195.                 "There are no elements for this iterator to loop on");
  196.         }
  197.         if (!iterator.hasPrevious()) {
  198.             return list.size() - 1;
  199.         }
  200.         return iterator.previousIndex();
  201.     }

  202.     /**
  203.      * Removes the previously retrieved item from the underlying list.
  204.      * <p>
  205.      * This feature is only supported if the underlying list's
  206.      * {@link List#iterator()} method returns an implementation
  207.      * that supports it.
  208.      * </p>
  209.      * <p>
  210.      * This method can only be called after at least one {@link #next}
  211.      * or {@link #previous} method call. After a removal, the remove
  212.      * method may not be called again until another {@link #next} or
  213.      * {@link #previous} has been performed. If the {@link #reset} is
  214.      * called, then remove may not be called until {@link #next} or
  215.      * {@link #previous} is called again.
  216.      * </p>
  217.      *
  218.      * @throws UnsupportedOperationException if the remove method is
  219.      * not supported by the iterator implementation of the underlying
  220.      * list
  221.      */
  222.     @Override
  223.     public void remove() {
  224.         iterator.remove();
  225.     }

  226.     /**
  227.      * Resets the iterator back to the start of the list.
  228.      */
  229.     @Override
  230.     public void reset() {
  231.         init();
  232.     }

  233.     /**
  234.      * Replaces the last element that was returned by {@link #next} or
  235.      * {@link #previous}.
  236.      * <p>
  237.      * This feature is only supported if the underlying list's
  238.      * {@link List#listIterator} method returns an implementation
  239.      * that supports it.
  240.      * </p>
  241.      *
  242.      * @param obj  the element with which to replace the last element returned
  243.      * @throws UnsupportedOperationException if the set method is not
  244.      *  supported by the iterator implementation of the underlying list
  245.      */
  246.     @Override
  247.     public void set(final E obj) {
  248.         iterator.set(obj);
  249.     }

  250.     /**
  251.      * Gets the size of the list underlying the iterator.
  252.      *
  253.      * @return the current list size
  254.      */
  255.     public int size() {
  256.         return list.size();
  257.     }

  258. }