jdk/src/share/classes/java/util/AbstractList.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 4110 ac033ba6ede4
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ListIterator<E> e = listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (o==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            while (e.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (e.next()==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    return e.previousIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            while (e.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                if (o.equals(e.next()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    return e.previousIndex();
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        ListIterator<E> e = listIterator(size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (o==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            while (e.hasPrevious())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                if (e.previous()==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    return e.nextIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            while (e.hasPrevious())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if (o.equals(e.previous()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    return e.nextIndex();
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        Iterator<? extends E> e = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        while (e.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            add(index++, e.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    // Iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Returns an iterator over the elements in this list in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <p>This implementation returns a straightforward implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * iterator interface, relying on the backing list's {@code size()},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * {@code get(int)}, and {@code remove(int)} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * <p>Note that the iterator returned by this method will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * {@link UnsupportedOperationException} in response to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * {@code remove} method unless the list's {@code remove(int)} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <p>This implementation can be made to throw runtime exceptions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * face of concurrent modification, as described in the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * for the (protected) {@link #modCount} field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @return an iterator over the elements in this list in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <p>This implementation returns {@code listIterator(0)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @see #listIterator(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return listIterator(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <p>This implementation returns a straightforward implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * {@code ListIterator} interface that extends the implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * {@code Iterator} interface returned by the {@code iterator()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * The {@code ListIterator} implementation relies on the backing list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * and {@code remove(int)} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <p>Note that the list iterator returned by this implementation will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * throw an {@link UnsupportedOperationException} in response to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * {@code remove}, {@code set} and {@code add} methods unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * list's {@code remove(int)}, {@code set(int, E)}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * {@code add(int, E)} methods are overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p>This implementation can be made to throw runtime exceptions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * face of concurrent modification, as described in the specification for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * the (protected) {@link #modCount} field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return new ListItr(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * Index of element to be returned by subsequent call to next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        int cursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * Index of element returned by most recent call to next or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         * previous.  Reset to -1 if this element is deleted by a call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         * to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        int lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * The modCount value that the iterator believes that the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         * List should have.  If this expectation is violated, the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
         * has detected concurrent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return cursor != size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                E next = get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                lastRet = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                AbstractList.this.remove(lastRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                if (lastRet < cursor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    cursor--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        final void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    private class ListItr extends Itr implements ListIterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        ListItr(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            cursor = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            return cursor != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                int i = cursor - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                E previous = get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                lastRet = cursor = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                return previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return cursor-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                AbstractList.this.set(lastRet, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                AbstractList.this.add(i, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                throw new ConcurrentModificationException();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * <p>This implementation returns a list that subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * {@code AbstractList}.  The subclass stores, in private fields, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * offset of the subList within the backing list, the size of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * (which can change over its lifetime), and the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * {@code modCount} value of the backing list.  There are two variants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * of the subclass, one of which implements {@code RandomAccess}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * If this list implements {@code RandomAccess} the returned list will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * be an instance of the subclass that implements {@code RandomAccess}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * <p>The subclass's {@code set(int, E)}, {@code get(int)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Collection)} and {@code removeRange(int, int)} methods all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * delegate to the corresponding methods on the backing abstract list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * after bounds-checking the index and adjusting for the offset.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * {@code addAll(Collection c)} method merely returns {@code addAll(size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * c)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * <p>The {@code listIterator(int)} method returns a "wrapper object"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * over a list iterator on the backing list, which is created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * corresponding method on the backing list.  The {@code iterator} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * merely returns {@code listIterator()}, and the {@code size} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * merely returns the subclass's {@code size} field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * <p>All methods first check to see if the actual {@code modCount} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * the backing list is equal to its expected value, and throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * {@code ConcurrentModificationException} if it is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @throws IndexOutOfBoundsException if an endpoint index value is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *         {@code (fromIndex < 0 || toIndex > size)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @throws IllegalArgumentException if the endpoint indices are out of order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *         {@code (fromIndex > toIndex)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return (this instanceof RandomAccess ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                new RandomAccessSubList<E>(this, fromIndex, toIndex) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                new SubList<E>(this, fromIndex, toIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Compares the specified object with this list for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * {@code true} if and only if the specified object is also a list, both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * lists have the same size, and all corresponding pairs of elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * the two lists are <i>equal</i>.  (Two elements {@code e1} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * e1.equals(e2))}.)  In other words, two lists are defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * equal if they contain the same elements in the same order.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * This implementation first checks if the specified object is this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * list. If so, it returns {@code true}; if not, it checks if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * specified object is a list. If not, it returns {@code false}; if so,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * it iterates over both lists, comparing corresponding pairs of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * If any comparison returns {@code false}, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * {@code false}.  If either iterator runs out of elements before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * other it returns {@code false} (as the lists are of unequal length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * otherwise it returns {@code true} when the iterations complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param o the object to be compared for equality with this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @return {@code true} if the specified object is equal to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (!(o instanceof List))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        ListIterator<E> e1 = listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        ListIterator e2 = ((List) o).listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        while(e1.hasNext() && e2.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            E o1 = e1.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            Object o2 = e2.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            if (!(o1==null ? o2==null : o1.equals(o2)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return !(e1.hasNext() || e2.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Returns the hash code value for this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * <p>This implementation uses exactly the code that is used to define the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * list hash function in the documentation for the {@link List#hashCode}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @return the hash code value for this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        int hashCode = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        for (E e : this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Removes from this list all of the elements whose index is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Shifts any succeeding elements to the left (reduces their index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * (If {@code toIndex==fromIndex}, this operation has no effect.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * <p>This method is called by the {@code clear} operation on this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * and its subLists.  Overriding this method to take advantage of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * the internals of the list implementation can <i>substantially</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * improve the performance of the {@code clear} operation on this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * and its subLists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * <p>This implementation gets a list iterator positioned before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * followed by {@code ListIterator.remove} until the entire range has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * been removed.  <b>Note: if {@code ListIterator.remove} requires linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * time, this implementation requires quadratic time.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @param fromIndex index of first element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @param toIndex index after last element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    protected void removeRange(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        ListIterator<E> it = listIterator(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        for (int i=0, n=toIndex-fromIndex; i<n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            it.remove();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * The number of times this list has been <i>structurally modified</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Structural modifications are those that change the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * list, or otherwise perturb it in such a fashion that iterations in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * progress may yield incorrect results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <p>This field is used by the iterator and list iterator implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * returned by the {@code iterator} and {@code listIterator} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * If the value of this field changes unexpectedly, the iterator (or list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * iterator) will throw a {@code ConcurrentModificationException} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * response to the {@code next}, {@code remove}, {@code previous},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * {@code set} or {@code add} operations.  This provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * <i>fail-fast</i> behavior, rather than non-deterministic behavior in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * the face of concurrent modification during iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * <p><b>Use of this field by subclasses is optional.</b> If a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * wishes to provide fail-fast iterators (and list iterators), then it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * merely has to increment this field in its {@code add(int, E)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * {@code remove(int)} methods (and any other methods that it overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * that result in structural modifications to the list).  A single call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * {@code add(int, E)} or {@code remove(int)} must add no more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * one to this field, or the iterators (and list iterators) will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * bogus {@code ConcurrentModificationExceptions}.  If an implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * does not wish to provide fail-fast iterators, this field may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    protected transient int modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    private void rangeCheckForAdd(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (index < 0 || index > size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private String outOfBoundsMsg(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return "Index: "+index+", Size: "+size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
class SubList<E> extends AbstractList<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    private final AbstractList<E> l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    private final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    SubList(AbstractList<E> list, int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (fromIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        if (toIndex > list.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            throw new IndexOutOfBoundsException("toIndex = " + toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (fromIndex > toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            throw new IllegalArgumentException("fromIndex(" + fromIndex +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                                               ") > toIndex(" + toIndex + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        l = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        offset = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        size = toIndex - fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return l.set(index+offset, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        return l.get(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        l.add(index+offset, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        E result = l.remove(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    protected void removeRange(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        l.removeRange(fromIndex+offset, toIndex+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        size -= (toIndex-fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        return addAll(size, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        int cSize = c.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (cSize==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        l.addAll(offset+index, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        size += cSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        return listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        return new ListIterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            private final ListIterator<E> i = l.listIterator(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                return nextIndex() < size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                if (hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    return i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                return previousIndex() >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                if (hasPrevious())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    return i.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                return i.nextIndex() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                return i.previousIndex() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                SubList.this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                i.set(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                i.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                SubList.this.modCount = l.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                size++;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        return new SubList<E>(this, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    private void rangeCheck(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if (index < 0 || index >= size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    private void rangeCheckForAdd(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if (index < 0 || index > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    private String outOfBoundsMsg(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        return "Index: "+index+", Size: "+size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    private void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (this.modCount != l.modCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        super(list, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        return new RandomAccessSubList<E>(this, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
}