70
71 public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
72 /**
73 * Sole constructor. (For invocation by subclass constructors, typically
74 * implicit.)
75 */
76 protected AbstractList() {
77 }
78
79 /**
80 * Appends the specified element to the end of this list (optional
81 * operation).
82 *
83 * <p>Lists that support this operation may place limitations on what
84 * elements may be added to this list. In particular, some
85 * lists will refuse to add null elements, and others will impose
86 * restrictions on the type of elements that may be added. List
87 * classes should clearly specify in their documentation any restrictions
88 * on what elements may be added.
89 *
90 * <p>This implementation calls {@code add(size(), e)}.
91 *
92 * <p>Note that this implementation throws an
93 * {@code UnsupportedOperationException} unless
94 * {@link #add(int, Object) add(int, E)} is overridden.
95 *
96 * @param e element to be appended to this list
97 * @return {@code true} (as specified by {@link Collection#add})
98 * @throws UnsupportedOperationException if the {@code add} operation
99 * is not supported by this list
100 * @throws ClassCastException if the class of the specified element
101 * prevents it from being added to this list
102 * @throws NullPointerException if the specified element is null and this
103 * list does not permit null elements
104 * @throws IllegalArgumentException if some property of this element
105 * prevents it from being added to this list
106 */
107 public boolean add(E e) {
108 add(size(), e);
109 return true;
110 }
111
112 /**
113 * {@inheritDoc}
114 *
115 * @throws IndexOutOfBoundsException {@inheritDoc}
116 */
117 abstract public E get(int index);
118
119 /**
120 * {@inheritDoc}
121 *
122 * <p>This implementation always throws an
123 * {@code UnsupportedOperationException}.
124 *
125 * @throws UnsupportedOperationException {@inheritDoc}
126 * @throws ClassCastException {@inheritDoc}
127 * @throws NullPointerException {@inheritDoc}
128 * @throws IllegalArgumentException {@inheritDoc}
129 * @throws IndexOutOfBoundsException {@inheritDoc}
130 */
131 public E set(int index, E element) {
132 throw new UnsupportedOperationException();
133 }
134
135 /**
136 * {@inheritDoc}
137 *
138 * <p>This implementation always throws an
139 * {@code UnsupportedOperationException}.
140 *
141 * @throws UnsupportedOperationException {@inheritDoc}
142 * @throws ClassCastException {@inheritDoc}
143 * @throws NullPointerException {@inheritDoc}
144 * @throws IllegalArgumentException {@inheritDoc}
145 * @throws IndexOutOfBoundsException {@inheritDoc}
146 */
147 public void add(int index, E element) {
148 throw new UnsupportedOperationException();
149 }
150
151 /**
152 * {@inheritDoc}
153 *
154 * <p>This implementation always throws an
155 * {@code UnsupportedOperationException}.
156 *
157 * @throws UnsupportedOperationException {@inheritDoc}
158 * @throws IndexOutOfBoundsException {@inheritDoc}
159 */
160 public E remove(int index) {
161 throw new UnsupportedOperationException();
162 }
163
164
165 // Search Operations
166
167 /**
168 * {@inheritDoc}
169 *
170 * <p>This implementation first gets a list iterator (with
171 * {@code listIterator()}). Then, it iterates over the list until the
172 * specified element is found or the end of the list is reached.
173 *
174 * @throws ClassCastException {@inheritDoc}
175 * @throws NullPointerException {@inheritDoc}
176 */
177 public int indexOf(Object o) {
178 ListIterator<E> it = listIterator();
179 if (o==null) {
180 while (it.hasNext())
181 if (it.next()==null)
182 return it.previousIndex();
183 } else {
184 while (it.hasNext())
185 if (o.equals(it.next()))
186 return it.previousIndex();
187 }
188 return -1;
189 }
190
191 /**
192 * {@inheritDoc}
193 *
194 * <p>This implementation first gets a list iterator that points to the end
195 * of the list (with {@code listIterator(size())}). Then, it iterates
196 * backwards over the list until the specified element is found, or the
197 * beginning of the list is reached.
198 *
199 * @throws ClassCastException {@inheritDoc}
200 * @throws NullPointerException {@inheritDoc}
201 */
202 public int lastIndexOf(Object o) {
203 ListIterator<E> it = listIterator(size());
204 if (o==null) {
205 while (it.hasPrevious())
206 if (it.previous()==null)
207 return it.nextIndex();
208 } else {
209 while (it.hasPrevious())
210 if (o.equals(it.previous()))
211 return it.nextIndex();
212 }
213 return -1;
214 }
215
216
217 // Bulk Operations
218
219 /**
220 * Removes all of the elements from this list (optional operation).
221 * The list will be empty after this call returns.
222 *
223 * <p>This implementation calls {@code removeRange(0, size())}.
224 *
225 * <p>Note that this implementation throws an
226 * {@code UnsupportedOperationException} unless {@code remove(int
227 * index)} or {@code removeRange(int fromIndex, int toIndex)} is
228 * overridden.
229 *
230 * @throws UnsupportedOperationException if the {@code clear} operation
231 * is not supported by this list
232 */
233 public void clear() {
234 removeRange(0, size());
235 }
236
237 /**
238 * {@inheritDoc}
239 *
240 * <p>This implementation gets an iterator over the specified collection
241 * and iterates over it, inserting the elements obtained from the
242 * iterator into this list at the appropriate position, one at a time,
243 * using {@code add(int, E)}.
244 * Many implementations will override this method for efficiency.
245 *
246 * <p>Note that this implementation throws an
247 * {@code UnsupportedOperationException} unless
248 * {@link #add(int, Object) add(int, E)} is overridden.
249 *
250 * @throws UnsupportedOperationException {@inheritDoc}
251 * @throws ClassCastException {@inheritDoc}
252 * @throws NullPointerException {@inheritDoc}
253 * @throws IllegalArgumentException {@inheritDoc}
254 * @throws IndexOutOfBoundsException {@inheritDoc}
255 */
256 public boolean addAll(int index, Collection<? extends E> c) {
257 rangeCheckForAdd(index);
258 boolean modified = false;
259 for (E e : c) {
260 add(index++, e);
261 modified = true;
262 }
263 return modified;
264 }
265
266
267 // Iterators
268
269 /**
270 * Returns an iterator over the elements in this list in proper sequence.
271 *
272 * <p>This implementation returns a straightforward implementation of the
273 * iterator interface, relying on the backing list's {@code size()},
274 * {@code get(int)}, and {@code remove(int)} methods.
275 *
276 * <p>Note that the iterator returned by this method will throw an
277 * {@link UnsupportedOperationException} in response to its
278 * {@code remove} method unless the list's {@code remove(int)} method is
279 * overridden.
280 *
281 * <p>This implementation can be made to throw runtime exceptions in the
282 * face of concurrent modification, as described in the specification
283 * for the (protected) {@link #modCount} field.
284 *
285 * @return an iterator over the elements in this list in proper sequence
286 */
287 public Iterator<E> iterator() {
288 return new Itr();
289 }
290
291 /**
292 * {@inheritDoc}
293 *
294 * <p>This implementation returns {@code listIterator(0)}.
295 *
296 * @see #listIterator(int)
297 */
298 public ListIterator<E> listIterator() {
299 return listIterator(0);
300 }
301
302 /**
303 * {@inheritDoc}
304 *
305 * <p>This implementation returns a straightforward implementation of the
306 * {@code ListIterator} interface that extends the implementation of the
307 * {@code Iterator} interface returned by the {@code iterator()} method.
308 * The {@code ListIterator} implementation relies on the backing list's
309 * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
310 * and {@code remove(int)} methods.
311 *
312 * <p>Note that the list iterator returned by this implementation will
313 * throw an {@link UnsupportedOperationException} in response to its
314 * {@code remove}, {@code set} and {@code add} methods unless the
315 * list's {@code remove(int)}, {@code set(int, E)}, and
316 * {@code add(int, E)} methods are overridden.
317 *
318 * <p>This implementation can be made to throw runtime exceptions in the
319 * face of concurrent modification, as described in the specification for
320 * the (protected) {@link #modCount} field.
321 *
322 * @throws IndexOutOfBoundsException {@inheritDoc}
323 */
324 public ListIterator<E> listIterator(final int index) {
325 rangeCheckForAdd(index);
431 }
432
433 public void add(E e) {
434 checkForComodification();
435
436 try {
437 int i = cursor;
438 AbstractList.this.add(i, e);
439 lastRet = -1;
440 cursor = i + 1;
441 expectedModCount = modCount;
442 } catch (IndexOutOfBoundsException ex) {
443 throw new ConcurrentModificationException();
444 }
445 }
446 }
447
448 /**
449 * {@inheritDoc}
450 *
451 * <p>This implementation returns a list that subclasses
452 * {@code AbstractList}. The subclass stores, in private fields, the
453 * offset of the subList within the backing list, the size of the subList
454 * (which can change over its lifetime), and the expected
455 * {@code modCount} value of the backing list. There are two variants
456 * of the subclass, one of which implements {@code RandomAccess}.
457 * If this list implements {@code RandomAccess} the returned list will
458 * be an instance of the subclass that implements {@code RandomAccess}.
459 *
460 * <p>The subclass's {@code set(int, E)}, {@code get(int)},
461 * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
462 * Collection)} and {@code removeRange(int, int)} methods all
463 * delegate to the corresponding methods on the backing abstract list,
464 * after bounds-checking the index and adjusting for the offset. The
465 * {@code addAll(Collection c)} method merely returns {@code addAll(size,
466 * c)}.
467 *
468 * <p>The {@code listIterator(int)} method returns a "wrapper object"
469 * over a list iterator on the backing list, which is created with the
470 * corresponding method on the backing list. The {@code iterator} method
471 * merely returns {@code listIterator()}, and the {@code size} method
480 * @throws IllegalArgumentException if the endpoint indices are out of order
481 * {@code (fromIndex > toIndex)}
482 */
483 public List<E> subList(int fromIndex, int toIndex) {
484 return (this instanceof RandomAccess ?
485 new RandomAccessSubList<>(this, fromIndex, toIndex) :
486 new SubList<>(this, fromIndex, toIndex));
487 }
488
489 // Comparison and hashing
490
491 /**
492 * Compares the specified object with this list for equality. Returns
493 * {@code true} if and only if the specified object is also a list, both
494 * lists have the same size, and all corresponding pairs of elements in
495 * the two lists are <i>equal</i>. (Two elements {@code e1} and
496 * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
497 * e1.equals(e2))}.) In other words, two lists are defined to be
498 * equal if they contain the same elements in the same order.<p>
499 *
500 * This implementation first checks if the specified object is this
501 * list. If so, it returns {@code true}; if not, it checks if the
502 * specified object is a list. If not, it returns {@code false}; if so,
503 * it iterates over both lists, comparing corresponding pairs of elements.
504 * If any comparison returns {@code false}, this method returns
505 * {@code false}. If either iterator runs out of elements before the
506 * other it returns {@code false} (as the lists are of unequal length);
507 * otherwise it returns {@code true} when the iterations complete.
508 *
509 * @param o the object to be compared for equality with this list
510 * @return {@code true} if the specified object is equal to this list
511 */
512 public boolean equals(Object o) {
513 if (o == this)
514 return true;
515 if (!(o instanceof List))
516 return false;
517
518 ListIterator<E> e1 = listIterator();
519 ListIterator<?> e2 = ((List<?>) o).listIterator();
520 while (e1.hasNext() && e2.hasNext()) {
521 E o1 = e1.next();
522 Object o2 = e2.next();
523 if (!(o1==null ? o2==null : o1.equals(o2)))
524 return false;
525 }
526 return !(e1.hasNext() || e2.hasNext());
527 }
528
529 /**
530 * Returns the hash code value for this list.
531 *
532 * <p>This implementation uses exactly the code that is used to define the
533 * list hash function in the documentation for the {@link List#hashCode}
534 * method.
535 *
536 * @return the hash code value for this list
537 */
538 public int hashCode() {
539 int hashCode = 1;
540 for (E e : this)
541 hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
542 return hashCode;
543 }
544
545 /**
546 * Removes from this list all of the elements whose index is between
547 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
548 * Shifts any succeeding elements to the left (reduces their index).
549 * This call shortens the list by {@code (toIndex - fromIndex)} elements.
550 * (If {@code toIndex==fromIndex}, this operation has no effect.)
551 *
552 * <p>This method is called by the {@code clear} operation on this list
553 * and its subLists. Overriding this method to take advantage of
554 * the internals of the list implementation can <i>substantially</i>
555 * improve the performance of the {@code clear} operation on this list
556 * and its subLists.
557 *
558 * <p>This implementation gets a list iterator positioned before
559 * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
560 * followed by {@code ListIterator.remove} until the entire range has
561 * been removed. <b>Note: if {@code ListIterator.remove} requires linear
562 * time, this implementation requires quadratic time.</b>
563 *
564 * @param fromIndex index of first element to be removed
565 * @param toIndex index after last element to be removed
566 */
567 protected void removeRange(int fromIndex, int toIndex) {
568 ListIterator<E> it = listIterator(fromIndex);
569 for (int i=0, n=toIndex-fromIndex; i<n; i++) {
570 it.next();
571 it.remove();
572 }
573 }
574
575 /**
576 * The number of times this list has been <i>structurally modified</i>.
577 * Structural modifications are those that change the size of the
578 * list, or otherwise perturb it in such a fashion that iterations in
|
70
71 public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
72 /**
73 * Sole constructor. (For invocation by subclass constructors, typically
74 * implicit.)
75 */
76 protected AbstractList() {
77 }
78
79 /**
80 * Appends the specified element to the end of this list (optional
81 * operation).
82 *
83 * <p>Lists that support this operation may place limitations on what
84 * elements may be added to this list. In particular, some
85 * lists will refuse to add null elements, and others will impose
86 * restrictions on the type of elements that may be added. List
87 * classes should clearly specify in their documentation any restrictions
88 * on what elements may be added.
89 *
90 * @implSpec
91 * This implementation calls {@code add(size(), e)}.
92 *
93 * <p>Note that this implementation throws an
94 * {@code UnsupportedOperationException} unless
95 * {@link #add(int, Object) add(int, E)} is overridden.
96 *
97 * @param e element to be appended to this list
98 * @return {@code true} (as specified by {@link Collection#add})
99 * @throws UnsupportedOperationException if the {@code add} operation
100 * is not supported by this list
101 * @throws ClassCastException if the class of the specified element
102 * prevents it from being added to this list
103 * @throws NullPointerException if the specified element is null and this
104 * list does not permit null elements
105 * @throws IllegalArgumentException if some property of this element
106 * prevents it from being added to this list
107 */
108 public boolean add(E e) {
109 add(size(), e);
110 return true;
111 }
112
113 /**
114 * {@inheritDoc}
115 *
116 * @throws IndexOutOfBoundsException {@inheritDoc}
117 */
118 abstract public E get(int index);
119
120 /**
121 * {@inheritDoc}
122 *
123 * @implSpec
124 * This implementation always throws an
125 * {@code UnsupportedOperationException}.
126 *
127 * @throws UnsupportedOperationException {@inheritDoc}
128 * @throws ClassCastException {@inheritDoc}
129 * @throws NullPointerException {@inheritDoc}
130 * @throws IllegalArgumentException {@inheritDoc}
131 * @throws IndexOutOfBoundsException {@inheritDoc}
132 */
133 public E set(int index, E element) {
134 throw new UnsupportedOperationException();
135 }
136
137 /**
138 * {@inheritDoc}
139 *
140 * @implSpec
141 * This implementation always throws an
142 * {@code UnsupportedOperationException}.
143 *
144 * @throws UnsupportedOperationException {@inheritDoc}
145 * @throws ClassCastException {@inheritDoc}
146 * @throws NullPointerException {@inheritDoc}
147 * @throws IllegalArgumentException {@inheritDoc}
148 * @throws IndexOutOfBoundsException {@inheritDoc}
149 */
150 public void add(int index, E element) {
151 throw new UnsupportedOperationException();
152 }
153
154 /**
155 * {@inheritDoc}
156 *
157 * @implSpec
158 * This implementation always throws an
159 * {@code UnsupportedOperationException}.
160 *
161 * @throws UnsupportedOperationException {@inheritDoc}
162 * @throws IndexOutOfBoundsException {@inheritDoc}
163 */
164 public E remove(int index) {
165 throw new UnsupportedOperationException();
166 }
167
168
169 // Search Operations
170
171 /**
172 * {@inheritDoc}
173 *
174 * @implSpec
175 * This implementation first gets a list iterator (with
176 * {@code listIterator()}). Then, it iterates over the list until the
177 * specified element is found or the end of the list is reached.
178 *
179 * @throws ClassCastException {@inheritDoc}
180 * @throws NullPointerException {@inheritDoc}
181 */
182 public int indexOf(Object o) {
183 ListIterator<E> it = listIterator();
184 if (o==null) {
185 while (it.hasNext())
186 if (it.next()==null)
187 return it.previousIndex();
188 } else {
189 while (it.hasNext())
190 if (o.equals(it.next()))
191 return it.previousIndex();
192 }
193 return -1;
194 }
195
196 /**
197 * {@inheritDoc}
198 *
199 * @implSpec
200 * This implementation first gets a list iterator that points to the end
201 * of the list (with {@code listIterator(size())}). Then, it iterates
202 * backwards over the list until the specified element is found, or the
203 * beginning of the list is reached.
204 *
205 * @throws ClassCastException {@inheritDoc}
206 * @throws NullPointerException {@inheritDoc}
207 */
208 public int lastIndexOf(Object o) {
209 ListIterator<E> it = listIterator(size());
210 if (o==null) {
211 while (it.hasPrevious())
212 if (it.previous()==null)
213 return it.nextIndex();
214 } else {
215 while (it.hasPrevious())
216 if (o.equals(it.previous()))
217 return it.nextIndex();
218 }
219 return -1;
220 }
221
222
223 // Bulk Operations
224
225 /**
226 * Removes all of the elements from this list (optional operation).
227 * The list will be empty after this call returns.
228 *
229 * @implSpec
230 * This implementation calls {@code removeRange(0, size())}.
231 *
232 * <p>Note that this implementation throws an
233 * {@code UnsupportedOperationException} unless {@code remove(int
234 * index)} or {@code removeRange(int fromIndex, int toIndex)} is
235 * overridden.
236 *
237 * @throws UnsupportedOperationException if the {@code clear} operation
238 * is not supported by this list
239 */
240 public void clear() {
241 removeRange(0, size());
242 }
243
244 /**
245 * {@inheritDoc}
246 *
247 * @implSpec
248 * This implementation gets an iterator over the specified collection
249 * and iterates over it, inserting the elements obtained from the
250 * iterator into this list at the appropriate position, one at a time,
251 * using {@code add(int, E)}.
252 * Many implementations will override this method for efficiency.
253 *
254 * <p>Note that this implementation throws an
255 * {@code UnsupportedOperationException} unless
256 * {@link #add(int, Object) add(int, E)} is overridden.
257 *
258 * @throws UnsupportedOperationException {@inheritDoc}
259 * @throws ClassCastException {@inheritDoc}
260 * @throws NullPointerException {@inheritDoc}
261 * @throws IllegalArgumentException {@inheritDoc}
262 * @throws IndexOutOfBoundsException {@inheritDoc}
263 */
264 public boolean addAll(int index, Collection<? extends E> c) {
265 rangeCheckForAdd(index);
266 boolean modified = false;
267 for (E e : c) {
268 add(index++, e);
269 modified = true;
270 }
271 return modified;
272 }
273
274
275 // Iterators
276
277 /**
278 * Returns an iterator over the elements in this list in proper sequence.
279 *
280 * @implSpec
281 * This implementation returns a straightforward implementation of the
282 * iterator interface, relying on the backing list's {@code size()},
283 * {@code get(int)}, and {@code remove(int)} methods.
284 *
285 * <p>Note that the iterator returned by this method will throw an
286 * {@link UnsupportedOperationException} in response to its
287 * {@code remove} method unless the list's {@code remove(int)} method is
288 * overridden.
289 *
290 * <p>This implementation can be made to throw runtime exceptions in the
291 * face of concurrent modification, as described in the specification
292 * for the (protected) {@link #modCount} field.
293 *
294 * @return an iterator over the elements in this list in proper sequence
295 */
296 public Iterator<E> iterator() {
297 return new Itr();
298 }
299
300 /**
301 * {@inheritDoc}
302 *
303 * @implSpec
304 * This implementation returns {@code listIterator(0)}.
305 *
306 * @see #listIterator(int)
307 */
308 public ListIterator<E> listIterator() {
309 return listIterator(0);
310 }
311
312 /**
313 * {@inheritDoc}
314 *
315 * @implSpec
316 * This implementation returns a straightforward implementation of the
317 * {@code ListIterator} interface that extends the implementation of the
318 * {@code Iterator} interface returned by the {@code iterator()} method.
319 * The {@code ListIterator} implementation relies on the backing list's
320 * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
321 * and {@code remove(int)} methods.
322 *
323 * <p>Note that the list iterator returned by this implementation will
324 * throw an {@link UnsupportedOperationException} in response to its
325 * {@code remove}, {@code set} and {@code add} methods unless the
326 * list's {@code remove(int)}, {@code set(int, E)}, and
327 * {@code add(int, E)} methods are overridden.
328 *
329 * <p>This implementation can be made to throw runtime exceptions in the
330 * face of concurrent modification, as described in the specification for
331 * the (protected) {@link #modCount} field.
332 *
333 * @throws IndexOutOfBoundsException {@inheritDoc}
334 */
335 public ListIterator<E> listIterator(final int index) {
336 rangeCheckForAdd(index);
442 }
443
444 public void add(E e) {
445 checkForComodification();
446
447 try {
448 int i = cursor;
449 AbstractList.this.add(i, e);
450 lastRet = -1;
451 cursor = i + 1;
452 expectedModCount = modCount;
453 } catch (IndexOutOfBoundsException ex) {
454 throw new ConcurrentModificationException();
455 }
456 }
457 }
458
459 /**
460 * {@inheritDoc}
461 *
462 * @implSpec
463 * This implementation returns a list that subclasses
464 * {@code AbstractList}. The subclass stores, in private fields, the
465 * offset of the subList within the backing list, the size of the subList
466 * (which can change over its lifetime), and the expected
467 * {@code modCount} value of the backing list. There are two variants
468 * of the subclass, one of which implements {@code RandomAccess}.
469 * If this list implements {@code RandomAccess} the returned list will
470 * be an instance of the subclass that implements {@code RandomAccess}.
471 *
472 * <p>The subclass's {@code set(int, E)}, {@code get(int)},
473 * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
474 * Collection)} and {@code removeRange(int, int)} methods all
475 * delegate to the corresponding methods on the backing abstract list,
476 * after bounds-checking the index and adjusting for the offset. The
477 * {@code addAll(Collection c)} method merely returns {@code addAll(size,
478 * c)}.
479 *
480 * <p>The {@code listIterator(int)} method returns a "wrapper object"
481 * over a list iterator on the backing list, which is created with the
482 * corresponding method on the backing list. The {@code iterator} method
483 * merely returns {@code listIterator()}, and the {@code size} method
492 * @throws IllegalArgumentException if the endpoint indices are out of order
493 * {@code (fromIndex > toIndex)}
494 */
495 public List<E> subList(int fromIndex, int toIndex) {
496 return (this instanceof RandomAccess ?
497 new RandomAccessSubList<>(this, fromIndex, toIndex) :
498 new SubList<>(this, fromIndex, toIndex));
499 }
500
501 // Comparison and hashing
502
503 /**
504 * Compares the specified object with this list for equality. Returns
505 * {@code true} if and only if the specified object is also a list, both
506 * lists have the same size, and all corresponding pairs of elements in
507 * the two lists are <i>equal</i>. (Two elements {@code e1} and
508 * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
509 * e1.equals(e2))}.) In other words, two lists are defined to be
510 * equal if they contain the same elements in the same order.<p>
511 *
512 * @implSpec
513 * This implementation first checks if the specified object is this
514 * list. If so, it returns {@code true}; if not, it checks if the
515 * specified object is a list. If not, it returns {@code false}; if so,
516 * it iterates over both lists, comparing corresponding pairs of elements.
517 * If any comparison returns {@code false}, this method returns
518 * {@code false}. If either iterator runs out of elements before the
519 * other it returns {@code false} (as the lists are of unequal length);
520 * otherwise it returns {@code true} when the iterations complete.
521 *
522 * @param o the object to be compared for equality with this list
523 * @return {@code true} if the specified object is equal to this list
524 */
525 public boolean equals(Object o) {
526 if (o == this)
527 return true;
528 if (!(o instanceof List))
529 return false;
530
531 ListIterator<E> e1 = listIterator();
532 ListIterator<?> e2 = ((List<?>) o).listIterator();
533 while (e1.hasNext() && e2.hasNext()) {
534 E o1 = e1.next();
535 Object o2 = e2.next();
536 if (!(o1==null ? o2==null : o1.equals(o2)))
537 return false;
538 }
539 return !(e1.hasNext() || e2.hasNext());
540 }
541
542 /**
543 * Returns the hash code value for this list.
544 *
545 * @implSpec
546 * This implementation uses exactly the code that is used to define the
547 * list hash function in the documentation for the {@link List#hashCode}
548 * method.
549 *
550 * @return the hash code value for this list
551 */
552 public int hashCode() {
553 int hashCode = 1;
554 for (E e : this)
555 hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
556 return hashCode;
557 }
558
559 /**
560 * Removes from this list all of the elements whose index is between
561 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
562 * Shifts any succeeding elements to the left (reduces their index).
563 * This call shortens the list by {@code (toIndex - fromIndex)} elements.
564 * (If {@code toIndex==fromIndex}, this operation has no effect.)
565 *
566 * <p>This method is called by the {@code clear} operation on this list
567 * and its subLists. Overriding this method to take advantage of
568 * the internals of the list implementation can <i>substantially</i>
569 * improve the performance of the {@code clear} operation on this list
570 * and its subLists.
571 *
572 * @implSpec
573 * This implementation gets a list iterator positioned before
574 * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
575 * followed by {@code ListIterator.remove} until the entire range has
576 * been removed. <b>Note: if {@code ListIterator.remove} requires linear
577 * time, this implementation requires quadratic time.</b>
578 *
579 * @param fromIndex index of first element to be removed
580 * @param toIndex index after last element to be removed
581 */
582 protected void removeRange(int fromIndex, int toIndex) {
583 ListIterator<E> it = listIterator(fromIndex);
584 for (int i=0, n=toIndex-fromIndex; i<n; i++) {
585 it.next();
586 it.remove();
587 }
588 }
589
590 /**
591 * The number of times this list has been <i>structurally modified</i>.
592 * Structural modifications are those that change the size of the
593 * list, or otherwise perturb it in such a fashion that iterations in
|