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 super(); 34 } 35 36 @Override 37 public void add(int location, E object) { 38 listIterator(location).add(object); 39 } 40 41 @Override 42 public boolean addAll(int location, Collection<? extends E> collection) { 43 ListIterator<E> it = listIterator(location); 44 Iterator<? extends E> colIt = collection.iterator(); 45 int next = it.nextIndex(); 46 while (colIt.hasNext()) { 47 it.add(colIt.next()); 48 } 49 return next != it.nextIndex(); 50 } 51 52 @Override 53 public E get(int location) { 54 try { 55 return listIterator(location).next(); 56 } catch (NoSuchElementException e) { 57 throw new IndexOutOfBoundsException(); 58 } 59 } 60 61 @Override 62 public Iterator<E> iterator() { 63 return listIterator(0); 64 } 65 66 @Override 67 public abstract ListIterator<E> listIterator(int location); 68 69 @Override 70 public E remove(int location) { 71 try { 72 ListIterator<E> it = listIterator(location); 73 E result = it.next(); 74 it.remove(); 75 return result; 76 } catch (NoSuchElementException e) { 77 throw new IndexOutOfBoundsException(); 78 } 79 } 80 81 @Override 82 public E set(int location, E object) { 83 ListIterator<E> it = listIterator(location); 84 if (!it.hasNext()) { 85 throw new IndexOutOfBoundsException(); 86 } 87 E result = it.next(); 88 it.set(object); 89 return result; 90 } 91} 92