ReverseListIterator.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.Objects;

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

  22. /**
  23.  * Iterates backwards through a List, starting with the last element
  24.  * and continuing to the first. This is useful for looping around
  25.  * a list in reverse order without needing to actually reverse the list.
  26.  * <p>
  27.  * The first call to {@code next()} will return the last element
  28.  * from the list, and so on. The {@code hasNext()} method works
  29.  * in concert with the {@code next()} method as expected.
  30.  * However, the {@code nextIndex()} method returns the correct
  31.  * index in the list, thus it starts high and reduces as the iteration
  32.  * continues. The previous methods work similarly.
  33.  * </p>
  34.  *
  35.  * @param <E> the type of elements returned by this iterator.
  36.  * @since 3.2
  37.  */
  38. public class ReverseListIterator<E> implements ResettableListIterator<E> {

  39.     /** The list being wrapped. */
  40.     private final List<E> list;
  41.     /** The list iterator being wrapped. */
  42.     private ListIterator<E> iterator;
  43.     /** Flag to indicate if updating is possible at the moment. */
  44.     private boolean validForUpdate = true;

  45.     /**
  46.      * Constructor that wraps a list.
  47.      *
  48.      * @param list  the list to create a reversed iterator for
  49.      * @throws NullPointerException if the list is null
  50.      */
  51.     public ReverseListIterator(final List<E> list) {
  52.         this.list = Objects.requireNonNull(list, "list");
  53.         iterator = list.listIterator(list.size());
  54.     }

  55.     /**
  56.      * Adds a new element to the list between the next and previous elements.
  57.      *
  58.      * @param obj  the object to add
  59.      * @throws UnsupportedOperationException if the list is unmodifiable
  60.      * @throws IllegalStateException if the iterator is not in a valid state for set
  61.      */
  62.     @Override
  63.     public void add(final E obj) {
  64.         // the validForUpdate flag is needed as the necessary previous()
  65.         // method call re-enables remove and add
  66.         if (!validForUpdate) {
  67.             throw new IllegalStateException("Cannot add to list until next() or previous() called");
  68.         }
  69.         validForUpdate = false;
  70.         iterator.add(obj);
  71.         iterator.previous();
  72.     }

  73.     /**
  74.      * Checks whether there is another element.
  75.      *
  76.      * @return true if there is another element
  77.      */
  78.     @Override
  79.     public boolean hasNext() {
  80.         return iterator.hasPrevious();
  81.     }

  82.     /**
  83.      * Checks whether there is a previous element.
  84.      *
  85.      * @return true if there is a previous element
  86.      */
  87.     @Override
  88.     public boolean hasPrevious() {
  89.         return iterator.hasNext();
  90.     }

  91.     /**
  92.      * Gets the next element.
  93.      * The next element is the previous in the list.
  94.      *
  95.      * @return the next element in the iterator
  96.      */
  97.     @Override
  98.     public E next() {
  99.         final E obj = iterator.previous();
  100.         validForUpdate = true;
  101.         return obj;
  102.     }

  103.     /**
  104.      * Gets the index of the next element.
  105.      *
  106.      * @return the index of the next element in the iterator
  107.      */
  108.     @Override
  109.     public int nextIndex() {
  110.         return iterator.previousIndex();
  111.     }

  112.     /**
  113.      * Gets the previous element.
  114.      * The next element is the previous in the list.
  115.      *
  116.      * @return the previous element in the iterator
  117.      */
  118.     @Override
  119.     public E previous() {
  120.         final E obj = iterator.next();
  121.         validForUpdate = true;
  122.         return obj;
  123.     }

  124.     /**
  125.      * Gets the index of the previous element.
  126.      *
  127.      * @return the index of the previous element in the iterator
  128.      */
  129.     @Override
  130.     public int previousIndex() {
  131.         return iterator.nextIndex();
  132.     }

  133.     /**
  134.      * Removes the last returned element.
  135.      *
  136.      * @throws UnsupportedOperationException if the list is unmodifiable
  137.      * @throws IllegalStateException if there is no element to remove
  138.      */
  139.     @Override
  140.     public void remove() {
  141.         if (!validForUpdate) {
  142.             throw new IllegalStateException("Cannot remove from list until next() or previous() called");
  143.         }
  144.         iterator.remove();
  145.     }

  146.     /**
  147.      * Resets the iterator back to the start (which is the
  148.      * end of the list as this is a reversed iterator)
  149.      */
  150.     @Override
  151.     public void reset() {
  152.         iterator = list.listIterator(list.size());
  153.     }

  154.     /**
  155.      * Replaces the last returned element.
  156.      *
  157.      * @param obj  the object to set
  158.      * @throws UnsupportedOperationException if the list is unmodifiable
  159.      * @throws IllegalStateException if the iterator is not in a valid state for set
  160.      */
  161.     @Override
  162.     public void set(final E obj) {
  163.         if (!validForUpdate) {
  164.             throw new IllegalStateException("Cannot set to list until next() or previous() called");
  165.         }
  166.         iterator.set(obj);
  167.     }

  168. }