001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util;
027
028 /**
029 * This class provides a skeletal implementation of the <tt>List</tt>
030 * interface to minimize the effort required to implement this interface
031 * backed by a "sequential access" data store (such as a linked list). For
032 * random access data (such as an array), <tt>AbstractList</tt> should be used
033 * in preference to this class.<p>
034 *
035 * This class is the opposite of the <tt>AbstractList</tt> class in the sense
036 * that it implements the "random access" methods (<tt>get(int index)</tt>,
037 * <tt>set(int index, E element)</tt>, <tt>add(int index, E element)</tt> and
038 * <tt>remove(int index)</tt>) on top of the list's list iterator, instead of
039 * the other way around.<p>
040 *
041 * To implement a list the programmer needs only to extend this class and
042 * provide implementations for the <tt>listIterator</tt> and <tt>size</tt>
043 * methods. For an unmodifiable list, the programmer need only implement the
044 * list iterator's <tt>hasNext</tt>, <tt>next</tt>, <tt>hasPrevious</tt>,
045 * <tt>previous</tt> and <tt>index</tt> methods.<p>
046 *
047 * For a modifiable list the programmer should additionally implement the list
048 * iterator's <tt>set</tt> method. For a variable-size list the programmer
049 * should additionally implement the list iterator's <tt>remove</tt> and
050 * <tt>add</tt> methods.<p>
051 *
052 * The programmer should generally provide a void (no argument) and collection
053 * constructor, as per the recommendation in the <tt>Collection</tt> interface
054 * specification.<p>
055 *
056 * This class is a member of the
057 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
058 * Java Collections Framework</a>.
059 *
060 * @author Josh Bloch
061 * @author Neal Gafter
062 * @version 1.42, 05/05/07
063 * @see Collection
064 * @see List
065 * @see AbstractList
066 * @see AbstractCollection
067 * @since 1.2
068 */
069
070 public abstract class AbstractSequentialList<E> extends AbstractList<E> {
071 /**
072 * Sole constructor. (For invocation by subclass constructors, typically
073 * implicit.)
074 */
075 protected AbstractSequentialList() {
076 }
077
078 /**
079 * Returns the element at the specified position in this list.
080 *
081 * <p>This implementation first gets a list iterator pointing to the
082 * indexed element (with <tt>listIterator(index)</tt>). Then, it gets
083 * the element using <tt>ListIterator.next</tt> and returns it.
084 *
085 * @throws IndexOutOfBoundsException {@inheritDoc}
086 */
087 public E get(int index) {
088 try {
089 return listIterator(index).next();
090 } catch (NoSuchElementException exc) {
091 throw new IndexOutOfBoundsException("Index: " + index);
092 }
093 }
094
095 /**
096 * Replaces the element at the specified position in this list with the
097 * specified element (optional operation).
098 *
099 * <p>This implementation first gets a list iterator pointing to the
100 * indexed element (with <tt>listIterator(index)</tt>). Then, it gets
101 * the current element using <tt>ListIterator.next</tt> and replaces it
102 * with <tt>ListIterator.set</tt>.
103 *
104 * <p>Note that this implementation will throw an
105 * <tt>UnsupportedOperationException</tt> if the list iterator does not
106 * implement the <tt>set</tt> operation.
107 *
108 * @throws UnsupportedOperationException {@inheritDoc}
109 * @throws ClassCastException {@inheritDoc}
110 * @throws NullPointerException {@inheritDoc}
111 * @throws IllegalArgumentException {@inheritDoc}
112 * @throws IndexOutOfBoundsException {@inheritDoc}
113 */
114 public E set(int index, E element) {
115 try {
116 ListIterator<E> e = listIterator(index);
117 E oldVal = e.next();
118 e.set(element);
119 return oldVal;
120 } catch (NoSuchElementException exc) {
121 throw new IndexOutOfBoundsException("Index: " + index);
122 }
123 }
124
125 /**
126 * Inserts the specified element at the specified position in this list
127 * (optional operation). Shifts the element currently at that position
128 * (if any) and any subsequent elements to the right (adds one to their
129 * indices).
130 *
131 * <p>This implementation first gets a list iterator pointing to the
132 * indexed element (with <tt>listIterator(index)</tt>). Then, it
133 * inserts the specified element with <tt>ListIterator.add</tt>.
134 *
135 * <p>Note that this implementation will throw an
136 * <tt>UnsupportedOperationException</tt> if the list iterator does not
137 * implement the <tt>add</tt> operation.
138 *
139 * @throws UnsupportedOperationException {@inheritDoc}
140 * @throws ClassCastException {@inheritDoc}
141 * @throws NullPointerException {@inheritDoc}
142 * @throws IllegalArgumentException {@inheritDoc}
143 * @throws IndexOutOfBoundsException {@inheritDoc}
144 */
145 public void add(int index, E element) {
146 try {
147 listIterator(index).add(element);
148 } catch (NoSuchElementException exc) {
149 throw new IndexOutOfBoundsException("Index: " + index);
150 }
151 }
152
153 /**
154 * Removes the element at the specified position in this list (optional
155 * operation). Shifts any subsequent elements to the left (subtracts one
156 * from their indices). Returns the element that was removed from the
157 * list.
158 *
159 * <p>This implementation first gets a list iterator pointing to the
160 * indexed element (with <tt>listIterator(index)</tt>). Then, it removes
161 * the element with <tt>ListIterator.remove</tt>.
162 *
163 * <p>Note that this implementation will throw an
164 * <tt>UnsupportedOperationException</tt> if the list iterator does not
165 * implement the <tt>remove</tt> operation.
166 *
167 * @throws UnsupportedOperationException {@inheritDoc}
168 * @throws IndexOutOfBoundsException {@inheritDoc}
169 */
170 public E remove(int index) {
171 try {
172 ListIterator<E> e = listIterator(index);
173 E outCast = e.next();
174 e.remove();
175 return outCast;
176 } catch (NoSuchElementException exc) {
177 throw new IndexOutOfBoundsException("Index: " + index);
178 }
179 }
180
181 // Bulk Operations
182
183 /**
184 * Inserts all of the elements in the specified collection into this
185 * list at the specified position (optional operation). Shifts the
186 * element currently at that position (if any) and any subsequent
187 * elements to the right (increases their indices). The new elements
188 * will appear in this list in the order that they are returned by the
189 * specified collection's iterator. The behavior of this operation is
190 * undefined if the specified collection is modified while the
191 * operation is in progress. (Note that this will occur if the specified
192 * collection is this list, and it's nonempty.)
193 *
194 * <p>This implementation gets an iterator over the specified collection and
195 * a list iterator over this list pointing to the indexed element (with
196 * <tt>listIterator(index)</tt>). Then, it iterates over the specified
197 * collection, inserting the elements obtained from the iterator into this
198 * list, one at a time, using <tt>ListIterator.add</tt> followed by
199 * <tt>ListIterator.next</tt> (to skip over the added element).
200 *
201 * <p>Note that this implementation will throw an
202 * <tt>UnsupportedOperationException</tt> if the list iterator returned by
203 * the <tt>listIterator</tt> method does not implement the <tt>add</tt>
204 * operation.
205 *
206 * @throws UnsupportedOperationException {@inheritDoc}
207 * @throws ClassCastException {@inheritDoc}
208 * @throws NullPointerException {@inheritDoc}
209 * @throws IllegalArgumentException {@inheritDoc}
210 * @throws IndexOutOfBoundsException {@inheritDoc}
211 */
212 public boolean addAll(int index, Collection<? extends E> c) {
213 try {
214 boolean modified = false;
215 ListIterator<E> e1 = listIterator(index);
216 Iterator<? extends E> e2 = c.iterator();
217 while (e2.hasNext()) {
218 e1.add(e2.next());
219 modified = true;
220 }
221 return modified;
222 } catch (NoSuchElementException exc) {
223 throw new IndexOutOfBoundsException("Index: " + index);
224 }
225 }
226
227 // Iterators
228
229 /**
230 * Returns an iterator over the elements in this list (in proper
231 * 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 */
237 public Iterator<E> iterator() {
238 return listIterator();
239 }
240
241 /**
242 * Returns a list iterator over the elements in this list (in proper
243 * sequence).
244 *
245 * @param index index of first element to be returned from the list
246 * iterator (by a call to the <code>next</code> method)
247 * @return a list iterator over the elements in this list (in proper
248 * sequence)
249 * @throws IndexOutOfBoundsException {@inheritDoc}
250 */
251 public abstract ListIterator<E> listIterator(int index);
252 }
|