001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.util;
019:
020: /**
021: * AbstractSequentialList is an abstract implementation of the List interface.
022: * This implementation does not support adding. A subclass must implement the
023: * abstract method listIterator().
024: *
025: * @since 1.2
026: */
027: public abstract class AbstractSequentialList<E> extends AbstractList<E> {
028:
029: /**
030: * Constructs a new instance of this AbstractSequentialList.
031: */
032: protected AbstractSequentialList() {
033: super ();
034: }
035:
036: /**
037: * Inserts the specified object into this List at the specified location.
038: * The object is inserted before any previous element at the specified
039: * location. If the location is equal to the size of this List, the object
040: * is added at the end.
041: *
042: * @param location
043: * the index at which to insert
044: * @param object
045: * the object to add
046: *
047: * @exception UnsupportedOperationException
048: * when adding to this List is not supported
049: * @exception ClassCastException
050: * when the class of the object is inappropriate for this
051: * List
052: * @exception IllegalArgumentException
053: * when the object cannot be added to this List
054: * @exception IndexOutOfBoundsException
055: * when <code>location < 0 || >= size()</code>
056: * @exception NullPointerException
057: * when the object is null and this List does not support
058: * null elements
059: */
060: @Override
061: public void add(int location, E object) {
062: listIterator(location).add(object);
063: }
064:
065: /**
066: * Inserts the objects in the specified Collection at the specified location
067: * in this List. The objects are added in the order they are returned from
068: * the Collection iterator.
069: *
070: * @param location
071: * the index at which to insert
072: * @param collection
073: * the Collection of objects
074: * @return true if this List is modified, false otherwise
075: *
076: * @exception UnsupportedOperationException
077: * when adding to this List is not supported
078: * @exception ClassCastException
079: * when the class of an object is inappropriate for this List
080: * @exception IllegalArgumentException
081: * when an object cannot be added to this List
082: * @exception IndexOutOfBoundsException
083: * when <code>location < 0 || >= size()</code>
084: */
085: @Override
086: public boolean addAll(int location,
087: Collection<? extends E> collection) {
088: ListIterator<E> it = listIterator(location);
089: Iterator<? extends E> colIt = collection.iterator();
090: int next = it.nextIndex();
091: while (colIt.hasNext()) {
092: it.add(colIt.next());
093: }
094: return next != it.nextIndex();
095: }
096:
097: /**
098: * Answers the element at the specified location in this List.
099: *
100: * @param location
101: * the index of the element to return
102: * @return the element at the specified location
103: *
104: * @exception IndexOutOfBoundsException
105: * when <code>location < 0 || >= size()</code>
106: */
107: @Override
108: public E get(int location) {
109: try {
110: return listIterator(location).next();
111: } catch (NoSuchElementException e) {
112: throw new IndexOutOfBoundsException();
113: }
114: }
115:
116: /**
117: * Answers an Iterator on the elements of this List. The elements are
118: * iterated in the same order that they occur in the List.
119: *
120: * @return an Iterator on the elements of this List
121: *
122: * @see Iterator
123: */
124: @Override
125: public Iterator<E> iterator() {
126: return listIterator(0);
127: }
128:
129: /**
130: * Answers a ListIterator on the elements of this List. The elements are
131: * iterated in the same order that they occur in the List. The iteration
132: * starts at the specified location.
133: *
134: * @param location
135: * the index at which to start the iteration
136: * @return a ListIterator on the elements of this List
137: *
138: * @exception IndexOutOfBoundsException
139: * when <code>location < 0 || >= size()</code>
140: *
141: * @see ListIterator
142: */
143: @Override
144: public abstract ListIterator<E> listIterator(int location);
145:
146: /**
147: * Removes the object at the specified location from this List.
148: *
149: * @param location
150: * the index of the object to remove
151: * @return the removed object
152: *
153: * @exception UnsupportedOperationException
154: * when removing from this List is not supported
155: * @exception IndexOutOfBoundsException
156: * when <code>location < 0 || >= size()</code>
157: */
158: @Override
159: public E remove(int location) {
160: try {
161: ListIterator<E> it = listIterator(location);
162: E result = it.next();
163: it.remove();
164: return result;
165: } catch (NoSuchElementException e) {
166: throw new IndexOutOfBoundsException();
167: }
168: }
169:
170: /**
171: * Replaces the element at the specified location in this List with the
172: * specified object.
173: *
174: * @param location
175: * the index at which to put the specified object
176: * @param object
177: * the object to add
178: * @return the previous element at the index
179: *
180: * @exception UnsupportedOperationException
181: * when replacing elements in this List is not supported
182: * @exception ClassCastException
183: * when the class of an object is inappropriate for this List
184: * @exception IllegalArgumentException
185: * when an object cannot be added to this List
186: * @exception IndexOutOfBoundsException
187: * when <code>location < 0 || >= size()</code>
188: */
189: @Override
190: public E set(int location, E object) {
191: ListIterator<E> it = listIterator(location);
192: if (!it.hasNext()) {
193: throw new IndexOutOfBoundsException();
194: }
195: E result = it.next();
196: it.set(object);
197: return result;
198: }
199: }
|