Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/util/AbstractSequentialList.java
38829 views
/*1* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util;2627/**28* This class provides a skeletal implementation of the <tt>List</tt>29* interface to minimize the effort required to implement this interface30* backed by a "sequential access" data store (such as a linked list). For31* random access data (such as an array), <tt>AbstractList</tt> should be used32* in preference to this class.<p>33*34* This class is the opposite of the <tt>AbstractList</tt> class in the sense35* that it implements the "random access" methods (<tt>get(int index)</tt>,36* <tt>set(int index, E element)</tt>, <tt>add(int index, E element)</tt> and37* <tt>remove(int index)</tt>) on top of the list's list iterator, instead of38* the other way around.<p>39*40* To implement a list the programmer needs only to extend this class and41* provide implementations for the <tt>listIterator</tt> and <tt>size</tt>42* methods. For an unmodifiable list, the programmer need only implement the43* list iterator's <tt>hasNext</tt>, <tt>next</tt>, <tt>hasPrevious</tt>,44* <tt>previous</tt> and <tt>index</tt> methods.<p>45*46* For a modifiable list the programmer should additionally implement the list47* iterator's <tt>set</tt> method. For a variable-size list the programmer48* should additionally implement the list iterator's <tt>remove</tt> and49* <tt>add</tt> methods.<p>50*51* The programmer should generally provide a void (no argument) and collection52* constructor, as per the recommendation in the <tt>Collection</tt> interface53* specification.<p>54*55* This class is a member of the56* <a href="{@docRoot}/../technotes/guides/collections/index.html">57* Java Collections Framework</a>.58*59* @author Josh Bloch60* @author Neal Gafter61* @see Collection62* @see List63* @see AbstractList64* @see AbstractCollection65* @since 1.266*/6768public abstract class AbstractSequentialList<E> extends AbstractList<E> {69/**70* Sole constructor. (For invocation by subclass constructors, typically71* implicit.)72*/73protected AbstractSequentialList() {74}7576/**77* Returns the element at the specified position in this list.78*79* <p>This implementation first gets a list iterator pointing to the80* indexed element (with <tt>listIterator(index)</tt>). Then, it gets81* the element using <tt>ListIterator.next</tt> and returns it.82*83* @throws IndexOutOfBoundsException {@inheritDoc}84*/85public E get(int index) {86try {87return listIterator(index).next();88} catch (NoSuchElementException exc) {89throw new IndexOutOfBoundsException("Index: "+index);90}91}9293/**94* Replaces the element at the specified position in this list with the95* specified element (optional operation).96*97* <p>This implementation first gets a list iterator pointing to the98* indexed element (with <tt>listIterator(index)</tt>). Then, it gets99* the current element using <tt>ListIterator.next</tt> and replaces it100* with <tt>ListIterator.set</tt>.101*102* <p>Note that this implementation will throw an103* <tt>UnsupportedOperationException</tt> if the list iterator does not104* implement the <tt>set</tt> operation.105*106* @throws UnsupportedOperationException {@inheritDoc}107* @throws ClassCastException {@inheritDoc}108* @throws NullPointerException {@inheritDoc}109* @throws IllegalArgumentException {@inheritDoc}110* @throws IndexOutOfBoundsException {@inheritDoc}111*/112public E set(int index, E element) {113try {114ListIterator<E> e = listIterator(index);115E oldVal = e.next();116e.set(element);117return oldVal;118} catch (NoSuchElementException exc) {119throw new IndexOutOfBoundsException("Index: "+index);120}121}122123/**124* Inserts the specified element at the specified position in this list125* (optional operation). Shifts the element currently at that position126* (if any) and any subsequent elements to the right (adds one to their127* indices).128*129* <p>This implementation first gets a list iterator pointing to the130* indexed element (with <tt>listIterator(index)</tt>). Then, it131* inserts the specified element with <tt>ListIterator.add</tt>.132*133* <p>Note that this implementation will throw an134* <tt>UnsupportedOperationException</tt> if the list iterator does not135* implement the <tt>add</tt> operation.136*137* @throws UnsupportedOperationException {@inheritDoc}138* @throws ClassCastException {@inheritDoc}139* @throws NullPointerException {@inheritDoc}140* @throws IllegalArgumentException {@inheritDoc}141* @throws IndexOutOfBoundsException {@inheritDoc}142*/143public void add(int index, E element) {144try {145listIterator(index).add(element);146} catch (NoSuchElementException exc) {147throw new IndexOutOfBoundsException("Index: "+index);148}149}150151/**152* Removes the element at the specified position in this list (optional153* operation). Shifts any subsequent elements to the left (subtracts one154* from their indices). Returns the element that was removed from the155* list.156*157* <p>This implementation first gets a list iterator pointing to the158* indexed element (with <tt>listIterator(index)</tt>). Then, it removes159* the element with <tt>ListIterator.remove</tt>.160*161* <p>Note that this implementation will throw an162* <tt>UnsupportedOperationException</tt> if the list iterator does not163* implement the <tt>remove</tt> operation.164*165* @throws UnsupportedOperationException {@inheritDoc}166* @throws IndexOutOfBoundsException {@inheritDoc}167*/168public E remove(int index) {169try {170ListIterator<E> e = listIterator(index);171E outCast = e.next();172e.remove();173return outCast;174} catch (NoSuchElementException exc) {175throw new IndexOutOfBoundsException("Index: "+index);176}177}178179180// Bulk Operations181182/**183* Inserts all of the elements in the specified collection into this184* list at the specified position (optional operation). Shifts the185* element currently at that position (if any) and any subsequent186* elements to the right (increases their indices). The new elements187* will appear in this list in the order that they are returned by the188* specified collection's iterator. The behavior of this operation is189* undefined if the specified collection is modified while the190* operation is in progress. (Note that this will occur if the specified191* collection is this list, and it's nonempty.)192*193* <p>This implementation gets an iterator over the specified collection and194* a list iterator over this list pointing to the indexed element (with195* <tt>listIterator(index)</tt>). Then, it iterates over the specified196* collection, inserting the elements obtained from the iterator into this197* list, one at a time, using <tt>ListIterator.add</tt> followed by198* <tt>ListIterator.next</tt> (to skip over the added element).199*200* <p>Note that this implementation will throw an201* <tt>UnsupportedOperationException</tt> if the list iterator returned by202* the <tt>listIterator</tt> method does not implement the <tt>add</tt>203* operation.204*205* @throws UnsupportedOperationException {@inheritDoc}206* @throws ClassCastException {@inheritDoc}207* @throws NullPointerException {@inheritDoc}208* @throws IllegalArgumentException {@inheritDoc}209* @throws IndexOutOfBoundsException {@inheritDoc}210*/211public boolean addAll(int index, Collection<? extends E> c) {212try {213boolean modified = false;214ListIterator<E> e1 = listIterator(index);215Iterator<? extends E> e2 = c.iterator();216while (e2.hasNext()) {217e1.add(e2.next());218modified = true;219}220return modified;221} catch (NoSuchElementException exc) {222throw new IndexOutOfBoundsException("Index: "+index);223}224}225226227// Iterators228229/**230* Returns an iterator over the elements in this list (in proper231* sequence).<p>232*233* This implementation merely returns a list iterator over the list.234*235* @return an iterator over the elements in this list (in proper sequence)236*/237public Iterator<E> iterator() {238return listIterator();239}240241/**242* Returns a list iterator over the elements in this list (in proper243* sequence).244*245* @param index index of first element to be returned from the list246* iterator (by a call to the <code>next</code> method)247* @return a list iterator over the elements in this list (in proper248* sequence)249* @throws IndexOutOfBoundsException {@inheritDoc}250*/251public abstract ListIterator<E> listIterator(int index);252}253254255