jaroslav@557: /* jaroslav@557: * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. jaroslav@557: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@557: * jaroslav@557: * This code is free software; you can redistribute it and/or modify it jaroslav@557: * under the terms of the GNU General Public License version 2 only, as jaroslav@557: * published by the Free Software Foundation. Oracle designates this jaroslav@557: * particular file as subject to the "Classpath" exception as provided jaroslav@557: * by Oracle in the LICENSE file that accompanied this code. jaroslav@557: * jaroslav@557: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@557: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@557: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@557: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@557: * accompanied this code). jaroslav@557: * jaroslav@557: * You should have received a copy of the GNU General Public License version jaroslav@557: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@557: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@557: * jaroslav@557: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@557: * or visit www.oracle.com if you need additional information or have any jaroslav@557: * questions. jaroslav@557: */ jaroslav@557: jaroslav@557: package java.util; jaroslav@557: jaroslav@557: /** jaroslav@557: * An iterator for lists that allows the programmer jaroslav@557: * to traverse the list in either direction, modify jaroslav@557: * the list during iteration, and obtain the iterator's jaroslav@557: * current position in the list. A {@code ListIterator} jaroslav@557: * has no current element; its cursor position always jaroslav@557: * lies between the element that would be returned by a call jaroslav@557: * to {@code previous()} and the element that would be jaroslav@557: * returned by a call to {@code next()}. jaroslav@557: * An iterator for a list of length {@code n} has {@code n+1} possible jaroslav@557: * cursor positions, as illustrated by the carets ({@code ^}) below: jaroslav@557: *
jaroslav@557:  *                      Element(0)   Element(1)   Element(2)   ... Element(n-1)
jaroslav@557:  * cursor positions:  ^            ^            ^            ^                  ^
jaroslav@557:  * 
jaroslav@557: * Note that the {@link #remove} and {@link #set(Object)} methods are jaroslav@557: * not defined in terms of the cursor position; they are defined to jaroslav@557: * operate on the last element returned by a call to {@link #next} or jaroslav@557: * {@link #previous()}. jaroslav@557: * jaroslav@557: *

This interface is a member of the jaroslav@557: * jaroslav@557: * Java Collections Framework. jaroslav@557: * jaroslav@557: * @author Josh Bloch jaroslav@557: * @see Collection jaroslav@557: * @see List jaroslav@557: * @see Iterator jaroslav@557: * @see Enumeration jaroslav@557: * @see List#listIterator() jaroslav@557: * @since 1.2 jaroslav@557: */ jaroslav@557: public interface ListIterator extends Iterator { jaroslav@557: // Query Operations jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns {@code true} if this list iterator has more elements when jaroslav@557: * traversing the list in the forward direction. (In other words, jaroslav@557: * returns {@code true} if {@link #next} would return an element rather jaroslav@557: * than throwing an exception.) jaroslav@557: * jaroslav@557: * @return {@code true} if the list iterator has more elements when jaroslav@557: * traversing the list in the forward direction jaroslav@557: */ jaroslav@557: boolean hasNext(); jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns the next element in the list and advances the cursor position. jaroslav@557: * This method may be called repeatedly to iterate through the list, jaroslav@557: * or intermixed with calls to {@link #previous} to go back and forth. jaroslav@557: * (Note that alternating calls to {@code next} and {@code previous} jaroslav@557: * will return the same element repeatedly.) jaroslav@557: * jaroslav@557: * @return the next element in the list jaroslav@557: * @throws NoSuchElementException if the iteration has no next element jaroslav@557: */ jaroslav@557: E next(); jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns {@code true} if this list iterator has more elements when jaroslav@557: * traversing the list in the reverse direction. (In other words, jaroslav@557: * returns {@code true} if {@link #previous} would return an element jaroslav@557: * rather than throwing an exception.) jaroslav@557: * jaroslav@557: * @return {@code true} if the list iterator has more elements when jaroslav@557: * traversing the list in the reverse direction jaroslav@557: */ jaroslav@557: boolean hasPrevious(); jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns the previous element in the list and moves the cursor jaroslav@557: * position backwards. This method may be called repeatedly to jaroslav@557: * iterate through the list backwards, or intermixed with calls to jaroslav@557: * {@link #next} to go back and forth. (Note that alternating calls jaroslav@557: * to {@code next} and {@code previous} will return the same jaroslav@557: * element repeatedly.) jaroslav@557: * jaroslav@557: * @return the previous element in the list jaroslav@557: * @throws NoSuchElementException if the iteration has no previous jaroslav@557: * element jaroslav@557: */ jaroslav@557: E previous(); jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns the index of the element that would be returned by a jaroslav@557: * subsequent call to {@link #next}. (Returns list size if the list jaroslav@557: * iterator is at the end of the list.) jaroslav@557: * jaroslav@557: * @return the index of the element that would be returned by a jaroslav@557: * subsequent call to {@code next}, or list size if the list jaroslav@557: * iterator is at the end of the list jaroslav@557: */ jaroslav@557: int nextIndex(); jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns the index of the element that would be returned by a jaroslav@557: * subsequent call to {@link #previous}. (Returns -1 if the list jaroslav@557: * iterator is at the beginning of the list.) jaroslav@557: * jaroslav@557: * @return the index of the element that would be returned by a jaroslav@557: * subsequent call to {@code previous}, or -1 if the list jaroslav@557: * iterator is at the beginning of the list jaroslav@557: */ jaroslav@557: int previousIndex(); jaroslav@557: jaroslav@557: jaroslav@557: // Modification Operations jaroslav@557: jaroslav@557: /** jaroslav@557: * Removes from the list the last element that was returned by {@link jaroslav@557: * #next} or {@link #previous} (optional operation). This call can jaroslav@557: * only be made once per call to {@code next} or {@code previous}. jaroslav@557: * It can be made only if {@link #add} has not been jaroslav@557: * called after the last call to {@code next} or {@code previous}. jaroslav@557: * jaroslav@557: * @throws UnsupportedOperationException if the {@code remove} jaroslav@557: * operation is not supported by this list iterator jaroslav@557: * @throws IllegalStateException if neither {@code next} nor jaroslav@557: * {@code previous} have been called, or {@code remove} or jaroslav@557: * {@code add} have been called after the last call to jaroslav@557: * {@code next} or {@code previous} jaroslav@557: */ jaroslav@557: void remove(); jaroslav@557: jaroslav@557: /** jaroslav@557: * Replaces the last element returned by {@link #next} or jaroslav@557: * {@link #previous} with the specified element (optional operation). jaroslav@557: * This call can be made only if neither {@link #remove} nor {@link jaroslav@557: * #add} have been called after the last call to {@code next} or jaroslav@557: * {@code previous}. jaroslav@557: * jaroslav@557: * @param e the element with which to replace the last element returned by jaroslav@557: * {@code next} or {@code previous} jaroslav@557: * @throws UnsupportedOperationException if the {@code set} operation jaroslav@557: * is not supported by this list iterator jaroslav@557: * @throws ClassCastException if the class of the specified element jaroslav@557: * prevents it from being added to this list jaroslav@557: * @throws IllegalArgumentException if some aspect of the specified jaroslav@557: * element prevents it from being added to this list jaroslav@557: * @throws IllegalStateException if neither {@code next} nor jaroslav@557: * {@code previous} have been called, or {@code remove} or jaroslav@557: * {@code add} have been called after the last call to jaroslav@557: * {@code next} or {@code previous} jaroslav@557: */ jaroslav@557: void set(E e); jaroslav@557: jaroslav@557: /** jaroslav@557: * Inserts the specified element into the list (optional operation). jaroslav@557: * The element is inserted immediately before the element that jaroslav@557: * would be returned by {@link #next}, if any, and after the element jaroslav@557: * that would be returned by {@link #previous}, if any. (If the jaroslav@557: * list contains no elements, the new element becomes the sole element jaroslav@557: * on the list.) The new element is inserted before the implicit jaroslav@557: * cursor: a subsequent call to {@code next} would be unaffected, and a jaroslav@557: * subsequent call to {@code previous} would return the new element. jaroslav@557: * (This call increases by one the value that would be returned by a jaroslav@557: * call to {@code nextIndex} or {@code previousIndex}.) jaroslav@557: * jaroslav@557: * @param e the element to insert jaroslav@557: * @throws UnsupportedOperationException if the {@code add} method is jaroslav@557: * not supported by this list iterator jaroslav@557: * @throws ClassCastException if the class of the specified element jaroslav@557: * prevents it from being added to this list jaroslav@557: * @throws IllegalArgumentException if some aspect of this element jaroslav@557: * prevents it from being added to this list jaroslav@557: */ jaroslav@557: void add(E e); jaroslav@557: }