View Javadoc
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  
19  import java.util.List;
20  import java.util.ListIterator;
21  import java.util.Objects;
22  
23  import org.apache.commons.collections4.ResettableListIterator;
24  
25  /**
26   * Iterates backwards through a List, starting with the last element
27   * and continuing to the first. This is useful for looping around
28   * a list in reverse order without needing to actually reverse the list.
29   * <p>
30   * The first call to {@code next()} will return the last element
31   * from the list, and so on. The {@code hasNext()} method works
32   * in concert with the {@code next()} method as expected.
33   * However, the {@code nextIndex()} method returns the correct
34   * index in the list, thus it starts high and reduces as the iteration
35   * continues. The previous methods work similarly.
36   *
37   * @param <E> the type of elements returned by this iterator.
38   * @since 3.2
39   */
40  public class ReverseListIterator<E> implements ResettableListIterator<E> {
41  
42      /** The list being wrapped. */
43      private final List<E> list;
44      /** The list iterator being wrapped. */
45      private ListIterator<E> iterator;
46      /** Flag to indicate if updating is possible at the moment. */
47      private boolean validForUpdate = true;
48  
49      /**
50       * Constructor that wraps a list.
51       *
52       * @param list  the list to create a reversed iterator for
53       * @throws NullPointerException if the list is null
54       */
55      public ReverseListIterator(final List<E> list) {
56          this.list = Objects.requireNonNull(list, "list");
57          iterator = list.listIterator(list.size());
58      }
59  
60      /**
61       * Adds a new element to the list between the next and previous elements.
62       *
63       * @param obj  the object to add
64       * @throws UnsupportedOperationException if the list is unmodifiable
65       * @throws IllegalStateException if the iterator is not in a valid state for set
66       */
67      @Override
68      public void add(final E obj) {
69          // the validForUpdate flag is needed as the necessary previous()
70          // method call re-enables remove and add
71          if (!validForUpdate) {
72              throw new IllegalStateException("Cannot add to list until next() or previous() called");
73          }
74          validForUpdate = false;
75          iterator.add(obj);
76          iterator.previous();
77      }
78  
79      /**
80       * Checks whether there is another element.
81       *
82       * @return true if there is another element
83       */
84      @Override
85      public boolean hasNext() {
86          return iterator.hasPrevious();
87      }
88  
89      /**
90       * Checks whether there is a previous element.
91       *
92       * @return true if there is a previous element
93       */
94      @Override
95      public boolean hasPrevious() {
96          return iterator.hasNext();
97      }
98  
99      /**
100      * Gets the next element.
101      * The next element is the previous in the list.
102      *
103      * @return the next element in the iterator
104      */
105     @Override
106     public E next() {
107         final E obj = iterator.previous();
108         validForUpdate = true;
109         return obj;
110     }
111 
112     /**
113      * Gets the index of the next element.
114      *
115      * @return the index of the next element in the iterator
116      */
117     @Override
118     public int nextIndex() {
119         return iterator.previousIndex();
120     }
121 
122     /**
123      * Gets the previous element.
124      * The next element is the previous in the list.
125      *
126      * @return the previous element in the iterator
127      */
128     @Override
129     public E previous() {
130         final E obj = iterator.next();
131         validForUpdate = true;
132         return obj;
133     }
134 
135     /**
136      * Gets the index of the previous element.
137      *
138      * @return the index of the previous element in the iterator
139      */
140     @Override
141     public int previousIndex() {
142         return iterator.nextIndex();
143     }
144 
145     /**
146      * Removes the last returned element.
147      *
148      * @throws UnsupportedOperationException if the list is unmodifiable
149      * @throws IllegalStateException if there is no element to remove
150      */
151     @Override
152     public void remove() {
153         if (!validForUpdate) {
154             throw new IllegalStateException("Cannot remove from list until next() or previous() called");
155         }
156         iterator.remove();
157     }
158 
159     /**
160      * Resets the iterator back to the start (which is the
161      * end of the list as this is a reversed iterator)
162      */
163     @Override
164     public void reset() {
165         iterator = list.listIterator(list.size());
166     }
167 
168     /**
169      * Replaces the last returned element.
170      *
171      * @param obj  the object to set
172      * @throws UnsupportedOperationException if the list is unmodifiable
173      * @throws IllegalStateException if the iterator is not in a valid state for set
174      */
175     @Override
176     public void set(final E obj) {
177         if (!validForUpdate) {
178             throw new IllegalStateException("Cannot set to list until next() or previous() called");
179         }
180         iterator.set(obj);
181     }
182 
183 }