jdk/src/share/classes/java/util/AbstractList.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7518 0282db800fe1
child 7816 55a18147b4bf
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7518
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class provides a skeletal implementation of the {@link List}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * interface to minimize the effort required to implement this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * backed by a "random access" data store (such as an array).  For sequential
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * access data (such as a linked list), {@link AbstractSequentialList} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * be used in preference to this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>To implement an unmodifiable list, the programmer needs only to extend
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * this class and provide implementations for the {@link #get(int)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * {@link List#size() size()} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>To implement a modifiable list, the programmer must additionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * override the {@link #set(int, Object) set(int, E)} method (which otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * throws an {@code UnsupportedOperationException}).  If the list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * variable-size the programmer must additionally override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>The programmer should generally provide a void (no argument) and collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * constructor, as per the recommendation in the {@link Collection} interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>Unlike the other abstract collection implementations, the programmer does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <i>not</i> have to provide an iterator implementation; the iterator and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * list iterator are implemented by this class, on top of the "random access"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link #get(int)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * {@link #set(int, Object) set(int, E)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * {@link #add(int, Object) add(int, E)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@link #remove(int)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>The documentation for each non-abstract method in this class describes its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * implementation in detail.  Each of these methods may be overridden if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * collection being implemented admits a more efficient implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Sole constructor.  (For invocation by subclass constructors, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected AbstractList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Appends the specified element to the end of this list (optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * operation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <p>Lists that support this operation may place limitations on what
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * elements may be added to this list.  In particular, some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * lists will refuse to add null elements, and others will impose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * restrictions on the type of elements that may be added.  List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * classes should clearly specify in their documentation any restrictions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * on what elements may be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <p>This implementation calls {@code add(size(), e)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <p>Note that this implementation throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * {@code UnsupportedOperationException} unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * {@link #add(int, Object) add(int, E)} is overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param e element to be appended to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @return {@code true} (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @throws UnsupportedOperationException if the {@code add} operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *         is not supported by this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @throws ClassCastException if the class of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *         prevents it from being added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @throws NullPointerException if the specified element is null and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *         list does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @throws IllegalArgumentException if some property of this element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *         prevents it from being added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        add(size(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    abstract public E get(int index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <p>This implementation always throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * {@code UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws IndexOutOfBoundsException     {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <p>This implementation always throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * {@code UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @throws IndexOutOfBoundsException     {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * <p>This implementation always throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * {@code UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @throws IndexOutOfBoundsException     {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // Search Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <p>This implementation first gets a list iterator (with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * {@code listIterator()}).  Then, it iterates over the list until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * specified element is found or the end of the list is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @throws ClassCastException   {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public int indexOf(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   178
        ListIterator<E> it = listIterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (o==null) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   180
            while (it.hasNext())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   181
                if (it.next()==null)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   182
                    return it.previousIndex();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   184
            while (it.hasNext())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   185
                if (o.equals(it.next()))
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   186
                    return it.previousIndex();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <p>This implementation first gets a list iterator that points to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * of the list (with {@code listIterator(size())}).  Then, it iterates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * backwards over the list until the specified element is found, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * beginning of the list is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @throws ClassCastException   {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public int lastIndexOf(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   203
        ListIterator<E> it = listIterator(size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (o==null) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   205
            while (it.hasPrevious())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   206
                if (it.previous()==null)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   207
                    return it.nextIndex();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } else {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   209
            while (it.hasPrevious())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   210
                if (o.equals(it.previous()))
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   211
                    return it.nextIndex();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Removes all of the elements from this list (optional operation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * The list will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <p>This implementation calls {@code removeRange(0, size())}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p>Note that this implementation throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * {@code UnsupportedOperationException} unless {@code remove(int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * index)} or {@code removeRange(int fromIndex, int toIndex)} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @throws UnsupportedOperationException if the {@code clear} operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *         is not supported by this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        removeRange(0, size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <p>This implementation gets an iterator over the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * and iterates over it, inserting the elements obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * iterator into this list at the appropriate position, one at a time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * using {@code add(int, E)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Many implementations will override this method for efficiency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <p>Note that this implementation throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * {@code UnsupportedOperationException} unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * {@link #add(int, Object) add(int, E)} is overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @throws IndexOutOfBoundsException     {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        boolean modified = false;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   259
        for (E e : c) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   260
            add(index++, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    // Iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Returns an iterator over the elements in this list in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <p>This implementation returns a straightforward implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * iterator interface, relying on the backing list's {@code size()},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * {@code get(int)}, and {@code remove(int)} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <p>Note that the iterator returned by this method will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * {@link UnsupportedOperationException} in response to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * {@code remove} method unless the list's {@code remove(int)} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <p>This implementation can be made to throw runtime exceptions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * face of concurrent modification, as described in the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * for the (protected) {@link #modCount} field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return an iterator over the elements in this list in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p>This implementation returns {@code listIterator(0)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @see #listIterator(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return listIterator(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <p>This implementation returns a straightforward implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * {@code ListIterator} interface that extends the implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * {@code Iterator} interface returned by the {@code iterator()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * The {@code ListIterator} implementation relies on the backing list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * and {@code remove(int)} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <p>Note that the list iterator returned by this implementation will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * throw an {@link UnsupportedOperationException} in response to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * {@code remove}, {@code set} and {@code add} methods unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * list's {@code remove(int)}, {@code set(int, E)}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * {@code add(int, E)} methods are overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <p>This implementation can be made to throw runtime exceptions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * face of concurrent modification, as described in the specification for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * the (protected) {@link #modCount} field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return new ListItr(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * Index of element to be returned by subsequent call to next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int cursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * Index of element returned by most recent call to next or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * previous.  Reset to -1 if this element is deleted by a call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         * to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        int lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * The modCount value that the iterator believes that the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * List should have.  If this expectation is violated, the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         * has detected concurrent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            return cursor != size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                E next = get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                lastRet = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                AbstractList.this.remove(lastRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                if (lastRet < cursor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    cursor--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        final void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    private class ListItr extends Itr implements ListIterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        ListItr(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            cursor = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return cursor != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                int i = cursor - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                E previous = get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                lastRet = cursor = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                return previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            return cursor-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                AbstractList.this.set(lastRet, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                AbstractList.this.add(i, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <p>This implementation returns a list that subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * {@code AbstractList}.  The subclass stores, in private fields, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * offset of the subList within the backing list, the size of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * (which can change over its lifetime), and the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * {@code modCount} value of the backing list.  There are two variants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * of the subclass, one of which implements {@code RandomAccess}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * If this list implements {@code RandomAccess} the returned list will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * be an instance of the subclass that implements {@code RandomAccess}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * <p>The subclass's {@code set(int, E)}, {@code get(int)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Collection)} and {@code removeRange(int, int)} methods all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * delegate to the corresponding methods on the backing abstract list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * after bounds-checking the index and adjusting for the offset.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * {@code addAll(Collection c)} method merely returns {@code addAll(size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * c)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * <p>The {@code listIterator(int)} method returns a "wrapper object"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * over a list iterator on the backing list, which is created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * corresponding method on the backing list.  The {@code iterator} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * merely returns {@code listIterator()}, and the {@code size} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * merely returns the subclass's {@code size} field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p>All methods first check to see if the actual {@code modCount} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * the backing list is equal to its expected value, and throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * {@code ConcurrentModificationException} if it is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @throws IndexOutOfBoundsException if an endpoint index value is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *         {@code (fromIndex < 0 || toIndex > size)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @throws IllegalArgumentException if the endpoint indices are out of order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *         {@code (fromIndex > toIndex)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return (this instanceof RandomAccess ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                new RandomAccessSubList<E>(this, fromIndex, toIndex) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                new SubList<E>(this, fromIndex, toIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Compares the specified object with this list for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * {@code true} if and only if the specified object is also a list, both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * lists have the same size, and all corresponding pairs of elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * the two lists are <i>equal</i>.  (Two elements {@code e1} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * e1.equals(e2))}.)  In other words, two lists are defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * equal if they contain the same elements in the same order.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * This implementation first checks if the specified object is this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * list. If so, it returns {@code true}; if not, it checks if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * specified object is a list. If not, it returns {@code false}; if so,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * it iterates over both lists, comparing corresponding pairs of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * If any comparison returns {@code false}, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * {@code false}.  If either iterator runs out of elements before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * other it returns {@code false} (as the lists are of unequal length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * otherwise it returns {@code true} when the iterations complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param o the object to be compared for equality with this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @return {@code true} if the specified object is equal to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (!(o instanceof List))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        ListIterator<E> e1 = listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        ListIterator e2 = ((List) o).listIterator();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   520
        while (e1.hasNext() && e2.hasNext()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            E o1 = e1.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            Object o2 = e2.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            if (!(o1==null ? o2==null : o1.equals(o2)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        return !(e1.hasNext() || e2.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Returns the hash code value for this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * <p>This implementation uses exactly the code that is used to define the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * list hash function in the documentation for the {@link List#hashCode}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @return the hash code value for this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        int hashCode = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        for (E e : this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Removes from this list all of the elements whose index is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Shifts any succeeding elements to the left (reduces their index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * (If {@code toIndex==fromIndex}, this operation has no effect.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <p>This method is called by the {@code clear} operation on this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * and its subLists.  Overriding this method to take advantage of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * the internals of the list implementation can <i>substantially</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * improve the performance of the {@code clear} operation on this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * and its subLists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * <p>This implementation gets a list iterator positioned before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * followed by {@code ListIterator.remove} until the entire range has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * been removed.  <b>Note: if {@code ListIterator.remove} requires linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * time, this implementation requires quadratic time.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param fromIndex index of first element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @param toIndex index after last element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    protected void removeRange(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        ListIterator<E> it = listIterator(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        for (int i=0, n=toIndex-fromIndex; i<n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * The number of times this list has been <i>structurally modified</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Structural modifications are those that change the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * list, or otherwise perturb it in such a fashion that iterations in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * progress may yield incorrect results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <p>This field is used by the iterator and list iterator implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * returned by the {@code iterator} and {@code listIterator} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * If the value of this field changes unexpectedly, the iterator (or list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * iterator) will throw a {@code ConcurrentModificationException} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * response to the {@code next}, {@code remove}, {@code previous},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * {@code set} or {@code add} operations.  This provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * <i>fail-fast</i> behavior, rather than non-deterministic behavior in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * the face of concurrent modification during iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * <p><b>Use of this field by subclasses is optional.</b> If a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * wishes to provide fail-fast iterators (and list iterators), then it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * merely has to increment this field in its {@code add(int, E)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * {@code remove(int)} methods (and any other methods that it overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * that result in structural modifications to the list).  A single call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * {@code add(int, E)} or {@code remove(int)} must add no more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * one to this field, or the iterators (and list iterators) will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * bogus {@code ConcurrentModificationExceptions}.  If an implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * does not wish to provide fail-fast iterators, this field may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    protected transient int modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    private void rangeCheckForAdd(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (index < 0 || index > size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    private String outOfBoundsMsg(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        return "Index: "+index+", Size: "+size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
class SubList<E> extends AbstractList<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    private final AbstractList<E> l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    private final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    SubList(AbstractList<E> list, int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (fromIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        if (toIndex > list.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            throw new IndexOutOfBoundsException("toIndex = " + toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (fromIndex > toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            throw new IllegalArgumentException("fromIndex(" + fromIndex +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                               ") > toIndex(" + toIndex + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        l = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        offset = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        size = toIndex - fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        return l.set(index+offset, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return l.get(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        l.add(index+offset, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        E result = l.remove(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    protected void removeRange(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        l.removeRange(fromIndex+offset, toIndex+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        size -= (toIndex-fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    public boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        return addAll(size, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        int cSize = c.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        if (cSize==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        l.addAll(offset+index, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        size += cSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        return listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return new ListIterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            private final ListIterator<E> i = l.listIterator(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                return nextIndex() < size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                if (hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    return i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                return previousIndex() >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                if (hasPrevious())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    return i.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                return i.nextIndex() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                return i.previousIndex() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                SubList.this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                i.set(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                i.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                SubList.this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        return new SubList<E>(this, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    private void rangeCheck(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if (index < 0 || index >= size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    private void rangeCheckForAdd(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (index < 0 || index > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    private String outOfBoundsMsg(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        return "Index: "+index+", Size: "+size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    private void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        if (this.modCount != l.modCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        super(list, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        return new RandomAccessSubList<E>(this, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
}