01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package java.util;
17:
18: /**
19: * Skeletal implementation of the List interface. <a
20: * href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/AbstractSequentialList.html">[Sun
21: * docs]</a>
22: *
23: * @param <E> element type.
24: */
25: public abstract class AbstractSequentialList<E> extends AbstractList<E> {
26:
27: // Should not be instantiated directly.
28: protected AbstractSequentialList() {
29: }
30:
31: @Override
32: public void add(int index, E element) {
33: ListIterator<E> iter = listIterator(index);
34: iter.add(element);
35: }
36:
37: @Override
38: public boolean addAll(int index, Collection<? extends E> c) {
39: boolean modified = false;
40: ListIterator<E> iter = listIterator(index);
41: Iterator<? extends E> colIter = c.iterator();
42: while (colIter.hasNext()) {
43: iter.add(colIter.next());
44: modified = true;
45: }
46: return modified;
47: }
48:
49: @Override
50: public E get(int index) {
51: ListIterator<E> iter = listIterator(index);
52: try {
53: return iter.next();
54: } catch (NoSuchElementException e) {
55: throw new IndexOutOfBoundsException("Can't get element "
56: + index);
57: }
58: }
59:
60: @Override
61: public Iterator<E> iterator() {
62: return listIterator();
63: }
64:
65: @Override
66: public abstract ListIterator<E> listIterator(int index);
67:
68: @Override
69: public E remove(int index) {
70: ListIterator<E> iter = listIterator(index);
71: E old;
72: try {
73: old = iter.next();
74: } catch (NoSuchElementException e) {
75: throw new IndexOutOfBoundsException("Can't remove element "
76: + index);
77: }
78: iter.remove();
79: return old;
80: }
81:
82: @Override
83: public E set(int index, E element) {
84: ListIterator<E> iter = listIterator(index);
85: try {
86: E old = iter.next();
87: iter.set(element);
88: return old;
89: } catch (NoSuchElementException e) {
90: throw new IndexOutOfBoundsException("Can't set element "
91: + index);
92: }
93: }
94:
95: @Override
96: public abstract int size();
97:
98: }
|