001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.iterators; 018 019import java.util.List; 020import java.util.ListIterator; 021import java.util.Objects; 022 023import org.apache.commons.collections4.ResettableListIterator; 024 025/** 026 * Iterates backwards through a List, starting with the last element 027 * and continuing to the first. This is useful for looping around 028 * a list in reverse order without needing to actually reverse the list. 029 * <p> 030 * The first call to {@code next()} will return the last element 031 * from the list, and so on. The {@code hasNext()} method works 032 * in concert with the {@code next()} method as expected. 033 * However, the {@code nextIndex()} method returns the correct 034 * index in the list, thus it starts high and reduces as the iteration 035 * continues. The previous methods work similarly. 036 * 037 * @param <E> the type of elements returned by this iterator. 038 * @since 3.2 039 */ 040public class ReverseListIterator<E> implements ResettableListIterator<E> { 041 042 /** The list being wrapped. */ 043 private final List<E> list; 044 /** The list iterator being wrapped. */ 045 private ListIterator<E> iterator; 046 /** Flag to indicate if updating is possible at the moment. */ 047 private boolean validForUpdate = true; 048 049 /** 050 * Constructor that wraps a list. 051 * 052 * @param list the list to create a reversed iterator for 053 * @throws NullPointerException if the list is null 054 */ 055 public ReverseListIterator(final List<E> list) { 056 this.list = Objects.requireNonNull(list, "list"); 057 iterator = list.listIterator(list.size()); 058 } 059 060 /** 061 * Adds a new element to the list between the next and previous elements. 062 * 063 * @param obj the object to add 064 * @throws UnsupportedOperationException if the list is unmodifiable 065 * @throws IllegalStateException if the iterator is not in a valid state for set 066 */ 067 @Override 068 public void add(final E obj) { 069 // the validForUpdate flag is needed as the necessary previous() 070 // method call re-enables remove and add 071 if (!validForUpdate) { 072 throw new IllegalStateException("Cannot add to list until next() or previous() called"); 073 } 074 validForUpdate = false; 075 iterator.add(obj); 076 iterator.previous(); 077 } 078 079 /** 080 * Checks whether there is another element. 081 * 082 * @return true if there is another element 083 */ 084 @Override 085 public boolean hasNext() { 086 return iterator.hasPrevious(); 087 } 088 089 /** 090 * Checks whether there is a previous element. 091 * 092 * @return true if there is a previous element 093 */ 094 @Override 095 public boolean hasPrevious() { 096 return iterator.hasNext(); 097 } 098 099 /** 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}