Path: blob/master/src/java.base/share/classes/java/util/AbstractSequentialList.java
41152 views
/*1* Copyright (c) 1997, 2018, 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 {@code List}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), {@code AbstractList} should be used32* in preference to this class.<p>33*34* This class is the opposite of the {@code AbstractList} class in the sense35* that it implements the "random access" methods ({@code get(int index)},36* {@code set(int index, E element)}, {@code add(int index, E element)} and37* {@code remove(int index)}) 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 {@code listIterator} and {@code size}42* methods. For an unmodifiable list, the programmer need only implement the43* list iterator's {@code hasNext}, {@code next}, {@code hasPrevious},44* {@code previous} and {@code index} methods.<p>45*46* For a modifiable list the programmer should additionally implement the list47* iterator's {@code set} method. For a variable-size list the programmer48* should additionally implement the list iterator's {@code remove} and49* {@code add} methods.<p>50*51* The programmer should generally provide a void (no argument) and collection52* constructor, as per the recommendation in the {@code Collection} interface53* specification.<p>54*55* This class is a member of the56* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">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 {@code listIterator(index)}). Then, it gets81* the element using {@code ListIterator.next} 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 {@code listIterator(index)}). Then, it gets99* the current element using {@code ListIterator.next} and replaces it100* with {@code ListIterator.set}.101*102* <p>Note that this implementation will throw an103* {@code UnsupportedOperationException} if the list iterator does not104* implement the {@code set} 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 {@code listIterator(index)}). Then, it131* inserts the specified element with {@code ListIterator.add}.132*133* <p>Note that this implementation will throw an134* {@code UnsupportedOperationException} if the list iterator does not135* implement the {@code add} 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 {@code listIterator(index)}). Then, it removes159* the element with {@code ListIterator.remove}.160*161* <p>Note that this implementation will throw an162* {@code UnsupportedOperationException} if the list iterator does not163* implement the {@code remove} 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* {@code listIterator(index)}). Then, it iterates over the specified196* collection, inserting the elements obtained from the iterator into this197* list, one at a time, using {@code ListIterator.add} followed by198* {@code ListIterator.next} (to skip over the added element).199*200* <p>Note that this implementation will throw an201* {@code UnsupportedOperationException} if the list iterator returned by202* the {@code listIterator} method does not implement the {@code add}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);215for (E e : c) {216e1.add(e);217modified = true;218}219return modified;220} catch (NoSuchElementException exc) {221throw new IndexOutOfBoundsException("Index: "+index);222}223}224225226// Iterators227228/**229* Returns an iterator over the elements in this list (in proper230* sequence).<p>231*232* This implementation merely returns a list iterator over the list.233*234* @return an iterator over the elements in this list (in proper sequence)235*/236public Iterator<E> iterator() {237return listIterator();238}239240/**241* Returns a list iterator over the elements in this list (in proper242* sequence).243*244* @param index index of first element to be returned from the list245* iterator (by a call to the {@code next} method)246* @return a list iterator over the elements in this list (in proper247* sequence)248* @throws IndexOutOfBoundsException {@inheritDoc}249*/250public abstract ListIterator<E> listIterator(int index);251}252253254