jaroslav@597: /* jaroslav@597: * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. jaroslav@597: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@597: * jaroslav@597: * This code is free software; you can redistribute it and/or modify it jaroslav@597: * under the terms of the GNU General Public License version 2 only, as jaroslav@597: * published by the Free Software Foundation. Oracle designates this jaroslav@597: * particular file as subject to the "Classpath" exception as provided jaroslav@597: * by Oracle in the LICENSE file that accompanied this code. jaroslav@597: * jaroslav@597: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@597: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@597: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@597: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@597: * accompanied this code). jaroslav@597: * jaroslav@597: * You should have received a copy of the GNU General Public License version jaroslav@597: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@597: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@597: * jaroslav@597: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@597: * or visit www.oracle.com if you need additional information or have any jaroslav@597: * questions. jaroslav@597: */ jaroslav@597: jaroslav@597: package java.util; jaroslav@597: jaroslav@597: /** jaroslav@597: * This class provides a skeletal implementation of the List jaroslav@597: * interface to minimize the effort required to implement this interface jaroslav@597: * backed by a "sequential access" data store (such as a linked list). For jaroslav@597: * random access data (such as an array), AbstractList should be used jaroslav@597: * in preference to this class.

jaroslav@597: * jaroslav@597: * This class is the opposite of the AbstractList class in the sense jaroslav@597: * that it implements the "random access" methods (get(int index), jaroslav@597: * set(int index, E element), add(int index, E element) and jaroslav@597: * remove(int index)) on top of the list's list iterator, instead of jaroslav@597: * the other way around.

jaroslav@597: * jaroslav@597: * To implement a list the programmer needs only to extend this class and jaroslav@597: * provide implementations for the listIterator and size jaroslav@597: * methods. For an unmodifiable list, the programmer need only implement the jaroslav@597: * list iterator's hasNext, next, hasPrevious, jaroslav@597: * previous and index methods.

jaroslav@597: * jaroslav@597: * For a modifiable list the programmer should additionally implement the list jaroslav@597: * iterator's set method. For a variable-size list the programmer jaroslav@597: * should additionally implement the list iterator's remove and jaroslav@597: * add methods.

jaroslav@597: * jaroslav@597: * The programmer should generally provide a void (no argument) and collection jaroslav@597: * constructor, as per the recommendation in the Collection interface jaroslav@597: * specification.

jaroslav@597: * jaroslav@597: * This class is a member of the jaroslav@597: * jaroslav@597: * Java Collections Framework. jaroslav@597: * jaroslav@597: * @author Josh Bloch jaroslav@597: * @author Neal Gafter jaroslav@597: * @see Collection jaroslav@597: * @see List jaroslav@597: * @see AbstractList jaroslav@597: * @see AbstractCollection jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: jaroslav@597: public abstract class AbstractSequentialList extends AbstractList { jaroslav@597: /** jaroslav@597: * Sole constructor. (For invocation by subclass constructors, typically jaroslav@597: * implicit.) jaroslav@597: */ jaroslav@597: protected AbstractSequentialList() { jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the element at the specified position in this list. jaroslav@597: * jaroslav@597: *

This implementation first gets a list iterator pointing to the jaroslav@597: * indexed element (with listIterator(index)). Then, it gets jaroslav@597: * the element using ListIterator.next and returns it. jaroslav@597: * jaroslav@597: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@597: */ jaroslav@597: public E get(int index) { jaroslav@597: try { jaroslav@597: return listIterator(index).next(); jaroslav@597: } catch (NoSuchElementException exc) { jaroslav@597: throw new IndexOutOfBoundsException("Index: "+index); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Replaces the element at the specified position in this list with the jaroslav@597: * specified element (optional operation). jaroslav@597: * jaroslav@597: *

This implementation first gets a list iterator pointing to the jaroslav@597: * indexed element (with listIterator(index)). Then, it gets jaroslav@597: * the current element using ListIterator.next and replaces it jaroslav@597: * with ListIterator.set. jaroslav@597: * jaroslav@597: *

Note that this implementation will throw an jaroslav@597: * UnsupportedOperationException if the list iterator does not jaroslav@597: * implement the set operation. jaroslav@597: * jaroslav@597: * @throws UnsupportedOperationException {@inheritDoc} jaroslav@597: * @throws ClassCastException {@inheritDoc} jaroslav@597: * @throws NullPointerException {@inheritDoc} jaroslav@597: * @throws IllegalArgumentException {@inheritDoc} jaroslav@597: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@597: */ jaroslav@597: public E set(int index, E element) { jaroslav@597: try { jaroslav@597: ListIterator e = listIterator(index); jaroslav@597: E oldVal = e.next(); jaroslav@597: e.set(element); jaroslav@597: return oldVal; jaroslav@597: } catch (NoSuchElementException exc) { jaroslav@597: throw new IndexOutOfBoundsException("Index: "+index); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Inserts the specified element at the specified position in this list jaroslav@597: * (optional operation). Shifts the element currently at that position jaroslav@597: * (if any) and any subsequent elements to the right (adds one to their jaroslav@597: * indices). jaroslav@597: * jaroslav@597: *

This implementation first gets a list iterator pointing to the jaroslav@597: * indexed element (with listIterator(index)). Then, it jaroslav@597: * inserts the specified element with ListIterator.add. jaroslav@597: * jaroslav@597: *

Note that this implementation will throw an jaroslav@597: * UnsupportedOperationException if the list iterator does not jaroslav@597: * implement the add operation. jaroslav@597: * jaroslav@597: * @throws UnsupportedOperationException {@inheritDoc} jaroslav@597: * @throws ClassCastException {@inheritDoc} jaroslav@597: * @throws NullPointerException {@inheritDoc} jaroslav@597: * @throws IllegalArgumentException {@inheritDoc} jaroslav@597: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@597: */ jaroslav@597: public void add(int index, E element) { jaroslav@597: try { jaroslav@597: listIterator(index).add(element); jaroslav@597: } catch (NoSuchElementException exc) { jaroslav@597: throw new IndexOutOfBoundsException("Index: "+index); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Removes the element at the specified position in this list (optional jaroslav@597: * operation). Shifts any subsequent elements to the left (subtracts one jaroslav@597: * from their indices). Returns the element that was removed from the jaroslav@597: * list. jaroslav@597: * jaroslav@597: *

This implementation first gets a list iterator pointing to the jaroslav@597: * indexed element (with listIterator(index)). Then, it removes jaroslav@597: * the element with ListIterator.remove. jaroslav@597: * jaroslav@597: *

Note that this implementation will throw an jaroslav@597: * UnsupportedOperationException if the list iterator does not jaroslav@597: * implement the remove operation. jaroslav@597: * jaroslav@597: * @throws UnsupportedOperationException {@inheritDoc} jaroslav@597: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@597: */ jaroslav@597: public E remove(int index) { jaroslav@597: try { jaroslav@597: ListIterator e = listIterator(index); jaroslav@597: E outCast = e.next(); jaroslav@597: e.remove(); jaroslav@597: return outCast; jaroslav@597: } catch (NoSuchElementException exc) { jaroslav@597: throw new IndexOutOfBoundsException("Index: "+index); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: jaroslav@597: // Bulk Operations jaroslav@597: jaroslav@597: /** jaroslav@597: * Inserts all of the elements in the specified collection into this jaroslav@597: * list at the specified position (optional operation). Shifts the jaroslav@597: * element currently at that position (if any) and any subsequent jaroslav@597: * elements to the right (increases their indices). The new elements jaroslav@597: * will appear in this list in the order that they are returned by the jaroslav@597: * specified collection's iterator. The behavior of this operation is jaroslav@597: * undefined if the specified collection is modified while the jaroslav@597: * operation is in progress. (Note that this will occur if the specified jaroslav@597: * collection is this list, and it's nonempty.) jaroslav@597: * jaroslav@597: *

This implementation gets an iterator over the specified collection and jaroslav@597: * a list iterator over this list pointing to the indexed element (with jaroslav@597: * listIterator(index)). Then, it iterates over the specified jaroslav@597: * collection, inserting the elements obtained from the iterator into this jaroslav@597: * list, one at a time, using ListIterator.add followed by jaroslav@597: * ListIterator.next (to skip over the added element). jaroslav@597: * jaroslav@597: *

Note that this implementation will throw an jaroslav@597: * UnsupportedOperationException if the list iterator returned by jaroslav@597: * the listIterator method does not implement the add jaroslav@597: * operation. jaroslav@597: * jaroslav@597: * @throws UnsupportedOperationException {@inheritDoc} jaroslav@597: * @throws ClassCastException {@inheritDoc} jaroslav@597: * @throws NullPointerException {@inheritDoc} jaroslav@597: * @throws IllegalArgumentException {@inheritDoc} jaroslav@597: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@597: */ jaroslav@597: public boolean addAll(int index, Collection c) { jaroslav@597: try { jaroslav@597: boolean modified = false; jaroslav@597: ListIterator e1 = listIterator(index); jaroslav@597: Iterator e2 = c.iterator(); jaroslav@597: while (e2.hasNext()) { jaroslav@597: e1.add(e2.next()); jaroslav@597: modified = true; jaroslav@597: } jaroslav@597: return modified; jaroslav@597: } catch (NoSuchElementException exc) { jaroslav@597: throw new IndexOutOfBoundsException("Index: "+index); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: jaroslav@597: // Iterators jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns an iterator over the elements in this list (in proper jaroslav@597: * sequence).

jaroslav@597: * jaroslav@597: * This implementation merely returns a list iterator over the list. jaroslav@597: * jaroslav@597: * @return an iterator over the elements in this list (in proper sequence) jaroslav@597: */ jaroslav@597: public Iterator iterator() { jaroslav@597: return listIterator(); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a list iterator over the elements in this list (in proper jaroslav@597: * sequence). jaroslav@597: * jaroslav@597: * @param index index of first element to be returned from the list jaroslav@597: * iterator (by a call to the next method) jaroslav@597: * @return a list iterator over the elements in this list (in proper jaroslav@597: * sequence) jaroslav@597: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@597: */ jaroslav@597: public abstract ListIterator listIterator(int index); jaroslav@597: }