1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package java.util; 19 20/** 21 * AbstractSequentialList is an abstract implementation of the List interface. 22 * This implementation does not support adding. A subclass must implement the 23 * abstract method listIterator(). 24 * 25 * @since 1.2 26 */ 27public abstract class AbstractSequentialList<E> extends AbstractList<E> { 28 29 /** 30 * Constructs a new instance of this AbstractSequentialList. 31 */ 32 protected AbstractSequentialList() { 33 } 34 35 @Override 36 public void add(int location, E object) { 37 listIterator(location).add(object); 38 } 39 40 @Override 41 public boolean addAll(int location, Collection<? extends E> collection) { 42 ListIterator<E> it = listIterator(location); 43 Iterator<? extends E> colIt = collection.iterator(); 44 int next = it.nextIndex(); 45 while (colIt.hasNext()) { 46 it.add(colIt.next()); 47 } 48 return next != it.nextIndex(); 49 } 50 51 @Override 52 public E get(int location) { 53 try { 54 return listIterator(location).next(); 55 } catch (NoSuchElementException e) { 56 throw new IndexOutOfBoundsException(); 57 } 58 } 59 60 @Override 61 public Iterator<E> iterator() { 62 return listIterator(0); 63 } 64 65 @Override 66 public abstract ListIterator<E> listIterator(int location); 67 68 @Override 69 public E remove(int location) { 70 try { 71 ListIterator<E> it = listIterator(location); 72 E result = it.next(); 73 it.remove(); 74 return result; 75 } catch (NoSuchElementException e) { 76 throw new IndexOutOfBoundsException(); 77 } 78 } 79 80 @Override 81 public E set(int location, E object) { 82 ListIterator<E> it = listIterator(location); 83 if (!it.hasNext()) { 84 throw new IndexOutOfBoundsException(); 85 } 86 E result = it.next(); 87 it.set(object); 88 return result; 89 } 90} 91