AbstractSequentialList.java revision dd828f42a5c83b4270d4fbf6fce2da1878f1e84a
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/**
22 * AbstractSequentialList is an abstract implementation of the List interface.
23 * This implementation does not support adding. A subclass must implement the
24 * abstract method listIterator().
25 *
26 * @since Android 1.0
27 */
28public abstract class AbstractSequentialList<E> extends AbstractList<E> {
29
30    /**
31     * Constructs a new instance of this AbstractSequentialList.
32     *
33     * @since Android 1.0
34     */
35    protected AbstractSequentialList() {
36        super();
37    }
38
39    @Override
40    public void add(int location, E object) {
41        listIterator(location).add(object);
42    }
43
44    @Override
45    public boolean addAll(int location, Collection<? extends E> collection) {
46        ListIterator<E> it = listIterator(location);
47        Iterator<? extends E> colIt = collection.iterator();
48        // BEGIN android-removed
49        // int next = it.nextIndex();
50        // while (colIt.hasNext()) {
51        //     it.add(colIt.next());
52        //     it.previous();
53        // }
54        // return next != it.nextIndex();
55        // END android-removed
56
57        // BEGIN android-added
58        // BUG: previous/next inconsistant.  We care for list
59        // modification NOT iterator modification.
60        int size = this.size();
61        while (colIt.hasNext()) {
62            it.add(colIt.next());
63        }
64        return size != this.size();
65        // END android-added
66    }
67
68    @Override
69    public E get(int location) {
70        try {
71            return listIterator(location).next();
72        } catch (NoSuchElementException e) {
73            throw new IndexOutOfBoundsException();
74        }
75    }
76
77    @Override
78    public Iterator<E> iterator() {
79        return listIterator(0);
80    }
81
82    @Override
83    public abstract ListIterator<E> listIterator(int location);
84
85    @Override
86    public E remove(int location) {
87        try {
88            ListIterator<E> it = listIterator(location);
89            E result = it.next();
90            it.remove();
91            return result;
92        } catch (NoSuchElementException e) {
93            throw new IndexOutOfBoundsException();
94        }
95    }
96
97    @Override
98    public E set(int location, E object) {
99        ListIterator<E> it = listIterator(location);
100        E result = it.next();
101        it.set(object);
102        return result;
103    }
104}
105