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 * https://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.ListIterator; 020import java.util.NoSuchElementException; 021 022import org.apache.commons.collections4.ResettableListIterator; 023 024/** 025 * Implements a {@link ListIterator} over an array of objects. 026 * <p> 027 * This iterator does not support {@link #add} or {@link #remove}, as the object array 028 * cannot be structurally modified. The {@link #set} method is supported however. 029 * </p> 030 * <p> 031 * The iterator implements a {@link #reset} method, allowing the reset of the iterator 032 * back to the start if required. 033 * </p> 034 * 035 * @param <E> The type of elements returned by this iterator. 036 * @see org.apache.commons.collections4.iterators.ObjectArrayIterator 037 * @see java.util.Iterator 038 * @see java.util.ListIterator 039 * @since 3.0 040 */ 041public class ObjectArrayListIterator<E> extends ObjectArrayIterator<E> 042 implements ResettableListIterator<E> { 043 044 /** 045 * Holds the index of the last item returned by a call to {@code next()} 046 * or {@code previous()}. This is set to {@code -1} if neither method 047 * has yet been invoked. {@code lastItemIndex} is used to implement the 048 * {@link #set} method. 049 */ 050 private int lastItemIndex = -1; 051 052 /** 053 * Constructs an ObjectArrayListIterator that will iterate over the values in the 054 * specified array. 055 * 056 * @param array The array to iterate over 057 * @throws NullPointerException if {@code array} is {@code null} 058 */ 059 public ObjectArrayListIterator(final E... array) { 060 super(array); 061 } 062 063 /** 064 * Constructs an ObjectArrayListIterator that will iterate over the values in the 065 * specified array from a specific start index. 066 * 067 * @param array The array to iterate over 068 * @param start The index to start iterating at 069 * @throws NullPointerException if {@code array} is {@code null} 070 * @throws IndexOutOfBoundsException if the start index is out of bounds 071 */ 072 public ObjectArrayListIterator(final E[] array, final int start) { 073 super(array, start); 074 } 075 076 /** 077 * Constructs an ObjectArrayListIterator that will iterate over a range of values 078 * in the specified array. 079 * 080 * @param array The array to iterate over 081 * @param start The index to start iterating at 082 * @param end The index (exclusive) to finish iterating at 083 * @throws IndexOutOfBoundsException if the start or end index is out of bounds 084 * @throws IllegalArgumentException if end index is before the start 085 * @throws NullPointerException if {@code array} is {@code null} 086 */ 087 public ObjectArrayListIterator(final E[] array, final int start, final int end) { 088 super(array, start, end); 089 } 090 091 /** 092 * Always throws {@link UnsupportedOperationException}. 093 * 094 * @param obj Ignored. 095 * @throws UnsupportedOperationException Always thrown. 096 */ 097 @Override 098 public void add(final E obj) { 099 throw new UnsupportedOperationException("add() method is not supported"); 100 } 101 102 /** 103 * Returns true if there are previous elements to return from the array. 104 * 105 * @return true if there is a previous element to return 106 */ 107 @Override 108 public boolean hasPrevious() { 109 return index > getStartIndex(); 110 } 111 112 /** 113 * Gets the next element from the array. 114 * 115 * @return The next element 116 * @throws NoSuchElementException if there is no next element 117 */ 118 @Override 119 public E next() { 120 if (!hasNext()) { 121 throw new NoSuchElementException(); 122 } 123 lastItemIndex = index; 124 return array[index++]; 125 } 126 127 /** 128 * Gets the next index to be retrieved. 129 * 130 * @return The index of the item to be retrieved next 131 */ 132 @Override 133 public int nextIndex() { 134 return index - getStartIndex(); 135 } 136 137 /** 138 * Gets the previous element from the array. 139 * 140 * @return The previous element 141 * @throws NoSuchElementException if there is no previous element 142 */ 143 @Override 144 public E previous() { 145 if (!hasPrevious()) { 146 throw new NoSuchElementException(); 147 } 148 lastItemIndex = --index; 149 return array[index]; 150 } 151 152 /** 153 * Gets the index of the item to be retrieved if {@link #previous()} is called. 154 * 155 * @return The index of the item to be retrieved next 156 */ 157 @Override 158 public int previousIndex() { 159 return index - getStartIndex() - 1; 160 } 161 162 /** 163 * Resets the iterator back to the start index. 164 */ 165 @Override 166 public void reset() { 167 super.reset(); 168 lastItemIndex = -1; 169 } 170 171 /** 172 * Sets the element under the cursor. 173 * <p> 174 * This method sets the element that was returned by the last call 175 * to {@link #next()} of {@link #previous()}. 176 * </p> 177 * <p> 178 * <strong>Note:</strong> {@link ListIterator} implementations that support {@code add()} 179 * and {@code remove()} only allow {@code set()} to be called once per call 180 * to {@code next()} or {@code previous} (see the {@link ListIterator} 181 * Javadoc for more details). Since this implementation does not support 182 * {@code add()} or {@code remove()}, {@code set()} may be 183 * called as often as desired. 184 * </p> 185 * 186 * @param obj The object to set into the array 187 * @throws IllegalStateException if next() has not yet been called. 188 * @throws ClassCastException if the object type is unsuitable for the array 189 */ 190 @Override 191 public void set(final E obj) { 192 if (lastItemIndex == -1) { 193 throw new IllegalStateException("must call next() or previous() before a call to set()"); 194 } 195 array[lastItemIndex] = obj; 196 } 197 198}