author | psandoz |
Fri, 09 Sep 2016 14:54:29 -0700 | |
changeset 40806 | 46132d430504 |
parent 32108 | aa5490a167ee |
child 44743 | f0bbd698c486 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util; |
|
27 |
||
28 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
29 |
* This class provides a skeletal implementation of the {@code List} |
2 | 30 |
* interface to minimize the effort required to implement this interface |
31 |
* backed by a "sequential access" data store (such as a linked list). For |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
32 |
* random access data (such as an array), {@code AbstractList} should be used |
2 | 33 |
* in preference to this class.<p> |
34 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
35 |
* This class is the opposite of the {@code AbstractList} class in the sense |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
36 |
* that it implements the "random access" methods ({@code get(int index)}, |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
37 |
* {@code set(int index, E element)}, {@code add(int index, E element)} and |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
38 |
* {@code remove(int index)}) on top of the list's list iterator, instead of |
2 | 39 |
* the other way around.<p> |
40 |
* |
|
41 |
* To implement a list the programmer needs only to extend this class and |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
42 |
* provide implementations for the {@code listIterator} and {@code size} |
2 | 43 |
* methods. For an unmodifiable list, the programmer need only implement the |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
44 |
* list iterator's {@code hasNext}, {@code next}, {@code hasPrevious}, |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
45 |
* {@code previous} and {@code index} methods.<p> |
2 | 46 |
* |
47 |
* For a modifiable list the programmer should additionally implement the list |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
48 |
* iterator's {@code set} method. For a variable-size list the programmer |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
49 |
* should additionally implement the list iterator's {@code remove} and |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
50 |
* {@code add} methods.<p> |
2 | 51 |
* |
52 |
* The programmer should generally provide a void (no argument) and collection |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
53 |
* constructor, as per the recommendation in the {@code Collection} interface |
2 | 54 |
* specification.<p> |
55 |
* |
|
56 |
* This class is a member of the |
|
57 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
58 |
* Java Collections Framework</a>. |
|
59 |
* |
|
60 |
* @author Josh Bloch |
|
61 |
* @author Neal Gafter |
|
62 |
* @see Collection |
|
63 |
* @see List |
|
64 |
* @see AbstractList |
|
65 |
* @see AbstractCollection |
|
66 |
* @since 1.2 |
|
67 |
*/ |
|
68 |
||
69 |
public abstract class AbstractSequentialList<E> extends AbstractList<E> { |
|
70 |
/** |
|
71 |
* Sole constructor. (For invocation by subclass constructors, typically |
|
72 |
* implicit.) |
|
73 |
*/ |
|
74 |
protected AbstractSequentialList() { |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Returns the element at the specified position in this list. |
|
79 |
* |
|
80 |
* <p>This implementation first gets a list iterator pointing to the |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
81 |
* indexed element (with {@code listIterator(index)}). Then, it gets |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
82 |
* the element using {@code ListIterator.next} and returns it. |
2 | 83 |
* |
84 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
85 |
*/ |
|
86 |
public E get(int index) { |
|
87 |
try { |
|
88 |
return listIterator(index).next(); |
|
89 |
} catch (NoSuchElementException exc) { |
|
90 |
throw new IndexOutOfBoundsException("Index: "+index); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Replaces the element at the specified position in this list with the |
|
96 |
* specified element (optional operation). |
|
97 |
* |
|
98 |
* <p>This implementation first gets a list iterator pointing to the |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
99 |
* indexed element (with {@code listIterator(index)}). Then, it gets |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
100 |
* the current element using {@code ListIterator.next} and replaces it |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
101 |
* with {@code ListIterator.set}. |
2 | 102 |
* |
103 |
* <p>Note that this implementation will throw an |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
104 |
* {@code UnsupportedOperationException} if the list iterator does not |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
105 |
* implement the {@code set} operation. |
2 | 106 |
* |
107 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
108 |
* @throws ClassCastException {@inheritDoc} |
|
109 |
* @throws NullPointerException {@inheritDoc} |
|
110 |
* @throws IllegalArgumentException {@inheritDoc} |
|
111 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
112 |
*/ |
|
113 |
public E set(int index, E element) { |
|
114 |
try { |
|
115 |
ListIterator<E> e = listIterator(index); |
|
116 |
E oldVal = e.next(); |
|
117 |
e.set(element); |
|
118 |
return oldVal; |
|
119 |
} catch (NoSuchElementException exc) { |
|
120 |
throw new IndexOutOfBoundsException("Index: "+index); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Inserts the specified element at the specified position in this list |
|
126 |
* (optional operation). Shifts the element currently at that position |
|
127 |
* (if any) and any subsequent elements to the right (adds one to their |
|
128 |
* indices). |
|
129 |
* |
|
130 |
* <p>This implementation first gets a list iterator pointing to the |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
131 |
* indexed element (with {@code listIterator(index)}). Then, it |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
132 |
* inserts the specified element with {@code ListIterator.add}. |
2 | 133 |
* |
134 |
* <p>Note that this implementation will throw an |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
135 |
* {@code UnsupportedOperationException} if the list iterator does not |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
136 |
* implement the {@code add} operation. |
2 | 137 |
* |
138 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
139 |
* @throws ClassCastException {@inheritDoc} |
|
140 |
* @throws NullPointerException {@inheritDoc} |
|
141 |
* @throws IllegalArgumentException {@inheritDoc} |
|
142 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
143 |
*/ |
|
144 |
public void add(int index, E element) { |
|
145 |
try { |
|
146 |
listIterator(index).add(element); |
|
147 |
} catch (NoSuchElementException exc) { |
|
148 |
throw new IndexOutOfBoundsException("Index: "+index); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Removes the element at the specified position in this list (optional |
|
154 |
* operation). Shifts any subsequent elements to the left (subtracts one |
|
155 |
* from their indices). Returns the element that was removed from the |
|
156 |
* list. |
|
157 |
* |
|
158 |
* <p>This implementation first gets a list iterator pointing to the |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
159 |
* indexed element (with {@code listIterator(index)}). Then, it removes |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
160 |
* the element with {@code ListIterator.remove}. |
2 | 161 |
* |
162 |
* <p>Note that this implementation will throw an |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
163 |
* {@code UnsupportedOperationException} if the list iterator does not |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
164 |
* implement the {@code remove} operation. |
2 | 165 |
* |
166 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
167 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
168 |
*/ |
|
169 |
public E remove(int index) { |
|
170 |
try { |
|
171 |
ListIterator<E> e = listIterator(index); |
|
172 |
E outCast = e.next(); |
|
173 |
e.remove(); |
|
174 |
return outCast; |
|
175 |
} catch (NoSuchElementException exc) { |
|
176 |
throw new IndexOutOfBoundsException("Index: "+index); |
|
177 |
} |
|
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 |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
196 |
* {@code listIterator(index)}). Then, it iterates over the specified |
2 | 197 |
* collection, inserting the elements obtained from the iterator into this |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
198 |
* list, one at a time, using {@code ListIterator.add} followed by |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
199 |
* {@code ListIterator.next} (to skip over the added element). |
2 | 200 |
* |
201 |
* <p>Note that this implementation will throw an |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
202 |
* {@code UnsupportedOperationException} if the list iterator returned by |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
203 |
* the {@code listIterator} method does not implement the {@code add} |
2 | 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); |
|
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
5506
diff
changeset
|
216 |
for (E e : c) { |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
5506
diff
changeset
|
217 |
e1.add(e); |
2 | 218 |
modified = true; |
219 |
} |
|
220 |
return modified; |
|
221 |
} catch (NoSuchElementException exc) { |
|
222 |
throw new IndexOutOfBoundsException("Index: "+index); |
|
223 |
} |
|
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 |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
246 |
* iterator (by a call to the {@code next} method) |
2 | 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 |
} |