rt/emul/compact/src/main/java/java/util/AbstractSequentialList.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 597 emul/compact/src/main/java/java/util/AbstractSequentialList.java@ee8a922f4268
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@597
     1
/*
jaroslav@597
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@597
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@597
     4
 *
jaroslav@597
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@597
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@597
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@597
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@597
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@597
    10
 *
jaroslav@597
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@597
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@597
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@597
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@597
    15
 * accompanied this code).
jaroslav@597
    16
 *
jaroslav@597
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@597
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@597
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@597
    20
 *
jaroslav@597
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@597
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@597
    23
 * questions.
jaroslav@597
    24
 */
jaroslav@597
    25
jaroslav@597
    26
package java.util;
jaroslav@597
    27
jaroslav@597
    28
/**
jaroslav@597
    29
 * This class provides a skeletal implementation of the <tt>List</tt>
jaroslav@597
    30
 * interface to minimize the effort required to implement this interface
jaroslav@597
    31
 * backed by a "sequential access" data store (such as a linked list).  For
jaroslav@597
    32
 * random access data (such as an array), <tt>AbstractList</tt> should be used
jaroslav@597
    33
 * in preference to this class.<p>
jaroslav@597
    34
 *
jaroslav@597
    35
 * This class is the opposite of the <tt>AbstractList</tt> class in the sense
jaroslav@597
    36
 * that it implements the "random access" methods (<tt>get(int index)</tt>,
jaroslav@597
    37
 * <tt>set(int index, E element)</tt>, <tt>add(int index, E element)</tt> and
jaroslav@597
    38
 * <tt>remove(int index)</tt>) on top of the list's list iterator, instead of
jaroslav@597
    39
 * the other way around.<p>
jaroslav@597
    40
 *
jaroslav@597
    41
 * To implement a list the programmer needs only to extend this class and
jaroslav@597
    42
 * provide implementations for the <tt>listIterator</tt> and <tt>size</tt>
jaroslav@597
    43
 * methods.  For an unmodifiable list, the programmer need only implement the
jaroslav@597
    44
 * list iterator's <tt>hasNext</tt>, <tt>next</tt>, <tt>hasPrevious</tt>,
jaroslav@597
    45
 * <tt>previous</tt> and <tt>index</tt> methods.<p>
jaroslav@597
    46
 *
jaroslav@597
    47
 * For a modifiable list the programmer should additionally implement the list
jaroslav@597
    48
 * iterator's <tt>set</tt> method.  For a variable-size list the programmer
jaroslav@597
    49
 * should additionally implement the list iterator's <tt>remove</tt> and
jaroslav@597
    50
 * <tt>add</tt> methods.<p>
jaroslav@597
    51
 *
jaroslav@597
    52
 * The programmer should generally provide a void (no argument) and collection
jaroslav@597
    53
 * constructor, as per the recommendation in the <tt>Collection</tt> interface
jaroslav@597
    54
 * specification.<p>
jaroslav@597
    55
 *
jaroslav@597
    56
 * This class is a member of the
jaroslav@597
    57
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@597
    58
 * Java Collections Framework</a>.
jaroslav@597
    59
 *
jaroslav@597
    60
 * @author  Josh Bloch
jaroslav@597
    61
 * @author  Neal Gafter
jaroslav@597
    62
 * @see Collection
jaroslav@597
    63
 * @see List
jaroslav@597
    64
 * @see AbstractList
jaroslav@597
    65
 * @see AbstractCollection
jaroslav@597
    66
 * @since 1.2
jaroslav@597
    67
 */
jaroslav@597
    68
jaroslav@597
    69
public abstract class AbstractSequentialList<E> extends AbstractList<E> {
jaroslav@597
    70
    /**
jaroslav@597
    71
     * Sole constructor.  (For invocation by subclass constructors, typically
jaroslav@597
    72
     * implicit.)
jaroslav@597
    73
     */
jaroslav@597
    74
    protected AbstractSequentialList() {
jaroslav@597
    75
    }
jaroslav@597
    76
jaroslav@597
    77
    /**
jaroslav@597
    78
     * Returns the element at the specified position in this list.
jaroslav@597
    79
     *
jaroslav@597
    80
     * <p>This implementation first gets a list iterator pointing to the
jaroslav@597
    81
     * indexed element (with <tt>listIterator(index)</tt>).  Then, it gets
jaroslav@597
    82
     * the element using <tt>ListIterator.next</tt> and returns it.
jaroslav@597
    83
     *
jaroslav@597
    84
     * @throws IndexOutOfBoundsException {@inheritDoc}
jaroslav@597
    85
     */
jaroslav@597
    86
    public E get(int index) {
jaroslav@597
    87
        try {
jaroslav@597
    88
            return listIterator(index).next();
jaroslav@597
    89
        } catch (NoSuchElementException exc) {
jaroslav@597
    90
            throw new IndexOutOfBoundsException("Index: "+index);
jaroslav@597
    91
        }
jaroslav@597
    92
    }
jaroslav@597
    93
jaroslav@597
    94
    /**
jaroslav@597
    95
     * Replaces the element at the specified position in this list with the
jaroslav@597
    96
     * specified element (optional operation).
jaroslav@597
    97
     *
jaroslav@597
    98
     * <p>This implementation first gets a list iterator pointing to the
jaroslav@597
    99
     * indexed element (with <tt>listIterator(index)</tt>).  Then, it gets
jaroslav@597
   100
     * the current element using <tt>ListIterator.next</tt> and replaces it
jaroslav@597
   101
     * with <tt>ListIterator.set</tt>.
jaroslav@597
   102
     *
jaroslav@597
   103
     * <p>Note that this implementation will throw an
jaroslav@597
   104
     * <tt>UnsupportedOperationException</tt> if the list iterator does not
jaroslav@597
   105
     * implement the <tt>set</tt> operation.
jaroslav@597
   106
     *
jaroslav@597
   107
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@597
   108
     * @throws ClassCastException            {@inheritDoc}
jaroslav@597
   109
     * @throws NullPointerException          {@inheritDoc}
jaroslav@597
   110
     * @throws IllegalArgumentException      {@inheritDoc}
jaroslav@597
   111
     * @throws IndexOutOfBoundsException     {@inheritDoc}
jaroslav@597
   112
     */
jaroslav@597
   113
    public E set(int index, E element) {
jaroslav@597
   114
        try {
jaroslav@597
   115
            ListIterator<E> e = listIterator(index);
jaroslav@597
   116
            E oldVal = e.next();
jaroslav@597
   117
            e.set(element);
jaroslav@597
   118
            return oldVal;
jaroslav@597
   119
        } catch (NoSuchElementException exc) {
jaroslav@597
   120
            throw new IndexOutOfBoundsException("Index: "+index);
jaroslav@597
   121
        }
jaroslav@597
   122
    }
jaroslav@597
   123
jaroslav@597
   124
    /**
jaroslav@597
   125
     * Inserts the specified element at the specified position in this list
jaroslav@597
   126
     * (optional operation).  Shifts the element currently at that position
jaroslav@597
   127
     * (if any) and any subsequent elements to the right (adds one to their
jaroslav@597
   128
     * indices).
jaroslav@597
   129
     *
jaroslav@597
   130
     * <p>This implementation first gets a list iterator pointing to the
jaroslav@597
   131
     * indexed element (with <tt>listIterator(index)</tt>).  Then, it
jaroslav@597
   132
     * inserts the specified element with <tt>ListIterator.add</tt>.
jaroslav@597
   133
     *
jaroslav@597
   134
     * <p>Note that this implementation will throw an
jaroslav@597
   135
     * <tt>UnsupportedOperationException</tt> if the list iterator does not
jaroslav@597
   136
     * implement the <tt>add</tt> operation.
jaroslav@597
   137
     *
jaroslav@597
   138
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@597
   139
     * @throws ClassCastException            {@inheritDoc}
jaroslav@597
   140
     * @throws NullPointerException          {@inheritDoc}
jaroslav@597
   141
     * @throws IllegalArgumentException      {@inheritDoc}
jaroslav@597
   142
     * @throws IndexOutOfBoundsException     {@inheritDoc}
jaroslav@597
   143
     */
jaroslav@597
   144
    public void add(int index, E element) {
jaroslav@597
   145
        try {
jaroslav@597
   146
            listIterator(index).add(element);
jaroslav@597
   147
        } catch (NoSuchElementException exc) {
jaroslav@597
   148
            throw new IndexOutOfBoundsException("Index: "+index);
jaroslav@597
   149
        }
jaroslav@597
   150
    }
jaroslav@597
   151
jaroslav@597
   152
    /**
jaroslav@597
   153
     * Removes the element at the specified position in this list (optional
jaroslav@597
   154
     * operation).  Shifts any subsequent elements to the left (subtracts one
jaroslav@597
   155
     * from their indices).  Returns the element that was removed from the
jaroslav@597
   156
     * list.
jaroslav@597
   157
     *
jaroslav@597
   158
     * <p>This implementation first gets a list iterator pointing to the
jaroslav@597
   159
     * indexed element (with <tt>listIterator(index)</tt>).  Then, it removes
jaroslav@597
   160
     * the element with <tt>ListIterator.remove</tt>.
jaroslav@597
   161
     *
jaroslav@597
   162
     * <p>Note that this implementation will throw an
jaroslav@597
   163
     * <tt>UnsupportedOperationException</tt> if the list iterator does not
jaroslav@597
   164
     * implement the <tt>remove</tt> operation.
jaroslav@597
   165
     *
jaroslav@597
   166
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@597
   167
     * @throws IndexOutOfBoundsException     {@inheritDoc}
jaroslav@597
   168
     */
jaroslav@597
   169
    public E remove(int index) {
jaroslav@597
   170
        try {
jaroslav@597
   171
            ListIterator<E> e = listIterator(index);
jaroslav@597
   172
            E outCast = e.next();
jaroslav@597
   173
            e.remove();
jaroslav@597
   174
            return outCast;
jaroslav@597
   175
        } catch (NoSuchElementException exc) {
jaroslav@597
   176
            throw new IndexOutOfBoundsException("Index: "+index);
jaroslav@597
   177
        }
jaroslav@597
   178
    }
jaroslav@597
   179
jaroslav@597
   180
jaroslav@597
   181
    // Bulk Operations
jaroslav@597
   182
jaroslav@597
   183
    /**
jaroslav@597
   184
     * Inserts all of the elements in the specified collection into this
jaroslav@597
   185
     * list at the specified position (optional operation).  Shifts the
jaroslav@597
   186
     * element currently at that position (if any) and any subsequent
jaroslav@597
   187
     * elements to the right (increases their indices).  The new elements
jaroslav@597
   188
     * will appear in this list in the order that they are returned by the
jaroslav@597
   189
     * specified collection's iterator.  The behavior of this operation is
jaroslav@597
   190
     * undefined if the specified collection is modified while the
jaroslav@597
   191
     * operation is in progress.  (Note that this will occur if the specified
jaroslav@597
   192
     * collection is this list, and it's nonempty.)
jaroslav@597
   193
     *
jaroslav@597
   194
     * <p>This implementation gets an iterator over the specified collection and
jaroslav@597
   195
     * a list iterator over this list pointing to the indexed element (with
jaroslav@597
   196
     * <tt>listIterator(index)</tt>).  Then, it iterates over the specified
jaroslav@597
   197
     * collection, inserting the elements obtained from the iterator into this
jaroslav@597
   198
     * list, one at a time, using <tt>ListIterator.add</tt> followed by
jaroslav@597
   199
     * <tt>ListIterator.next</tt> (to skip over the added element).
jaroslav@597
   200
     *
jaroslav@597
   201
     * <p>Note that this implementation will throw an
jaroslav@597
   202
     * <tt>UnsupportedOperationException</tt> if the list iterator returned by
jaroslav@597
   203
     * the <tt>listIterator</tt> method does not implement the <tt>add</tt>
jaroslav@597
   204
     * operation.
jaroslav@597
   205
     *
jaroslav@597
   206
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@597
   207
     * @throws ClassCastException            {@inheritDoc}
jaroslav@597
   208
     * @throws NullPointerException          {@inheritDoc}
jaroslav@597
   209
     * @throws IllegalArgumentException      {@inheritDoc}
jaroslav@597
   210
     * @throws IndexOutOfBoundsException     {@inheritDoc}
jaroslav@597
   211
     */
jaroslav@597
   212
    public boolean addAll(int index, Collection<? extends E> c) {
jaroslav@597
   213
        try {
jaroslav@597
   214
            boolean modified = false;
jaroslav@597
   215
            ListIterator<E> e1 = listIterator(index);
jaroslav@597
   216
            Iterator<? extends E> e2 = c.iterator();
jaroslav@597
   217
            while (e2.hasNext()) {
jaroslav@597
   218
                e1.add(e2.next());
jaroslav@597
   219
                modified = true;
jaroslav@597
   220
            }
jaroslav@597
   221
            return modified;
jaroslav@597
   222
        } catch (NoSuchElementException exc) {
jaroslav@597
   223
            throw new IndexOutOfBoundsException("Index: "+index);
jaroslav@597
   224
        }
jaroslav@597
   225
    }
jaroslav@597
   226
jaroslav@597
   227
jaroslav@597
   228
    // Iterators
jaroslav@597
   229
jaroslav@597
   230
    /**
jaroslav@597
   231
     * Returns an iterator over the elements in this list (in proper
jaroslav@597
   232
     * sequence).<p>
jaroslav@597
   233
     *
jaroslav@597
   234
     * This implementation merely returns a list iterator over the list.
jaroslav@597
   235
     *
jaroslav@597
   236
     * @return an iterator over the elements in this list (in proper sequence)
jaroslav@597
   237
     */
jaroslav@597
   238
    public Iterator<E> iterator() {
jaroslav@597
   239
        return listIterator();
jaroslav@597
   240
    }
jaroslav@597
   241
jaroslav@597
   242
    /**
jaroslav@597
   243
     * Returns a list iterator over the elements in this list (in proper
jaroslav@597
   244
     * sequence).
jaroslav@597
   245
     *
jaroslav@597
   246
     * @param  index index of first element to be returned from the list
jaroslav@597
   247
     *         iterator (by a call to the <code>next</code> method)
jaroslav@597
   248
     * @return a list iterator over the elements in this list (in proper
jaroslav@597
   249
     *         sequence)
jaroslav@597
   250
     * @throws IndexOutOfBoundsException {@inheritDoc}
jaroslav@597
   251
     */
jaroslav@597
   252
    public abstract ListIterator<E> listIterator(int index);
jaroslav@597
   253
}