jdk/src/java.base/share/classes/java/util/AbstractList.java
author psandoz
Fri, 09 Sep 2016 14:54:29 -0700
changeset 40806 46132d430504
parent 39568 1987f3929c39
child 44743 f0bbd698c486
permissions -rw-r--r--
8164691: Stream specification clarifications for iterate and collect Reviewed-by: briangoetz, smarks, tvaleev
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
     2
 * Copyright (c) 1997, 2016, 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
39568
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
    28
import java.util.function.Consumer;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class provides a skeletal implementation of the {@link List}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * interface to minimize the effort required to implement this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * backed by a "random access" data store (such as an array).  For sequential
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * access data (such as a linked list), {@link AbstractSequentialList} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * be used in preference to this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>To implement an unmodifiable list, the programmer needs only to extend
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * this class and provide implementations for the {@link #get(int)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * {@link List#size() size()} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>To implement a modifiable list, the programmer must additionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * override the {@link #set(int, Object) set(int, E)} method (which otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * throws an {@code UnsupportedOperationException}).  If the list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * variable-size the programmer must additionally override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>The programmer should generally provide a void (no argument) and collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * constructor, as per the recommendation in the {@link Collection} interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>Unlike the other abstract collection implementations, the programmer does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <i>not</i> have to provide an iterator implementation; the iterator and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * list iterator are implemented by this class, on top of the "random access"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * {@link #get(int)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@link #set(int, Object) set(int, E)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * {@link #add(int, Object) add(int, E)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * {@link #remove(int)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>The documentation for each non-abstract method in this class describes its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * implementation in detail.  Each of these methods may be overridden if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * collection being implemented admits a more efficient implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Sole constructor.  (For invocation by subclass constructors, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected AbstractList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Appends the specified element to the end of this list (optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * operation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <p>Lists that support this operation may place limitations on what
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * elements may be added to this list.  In particular, some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * lists will refuse to add null elements, and others will impose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * restrictions on the type of elements that may be added.  List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * classes should clearly specify in their documentation any restrictions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * on what elements may be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
    92
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
    93
     * This implementation calls {@code add(size(), e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>Note that this implementation throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * {@code UnsupportedOperationException} unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * {@link #add(int, Object) add(int, E)} is overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param e element to be appended to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @return {@code true} (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @throws UnsupportedOperationException if the {@code add} operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *         is not supported by this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws ClassCastException if the class of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *         prevents it from being added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @throws NullPointerException if the specified element is null and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *         list does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @throws IllegalArgumentException if some property of this element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *         prevents it from being added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        add(size(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   120
    public abstract E get(int index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   125
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   126
     * This implementation always throws an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * {@code UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @throws IndexOutOfBoundsException     {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   142
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   143
     * This implementation always throws an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * {@code UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @throws IndexOutOfBoundsException     {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   159
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   160
     * This implementation always throws an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * {@code UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @throws IndexOutOfBoundsException     {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // Search Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   176
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   177
     * This implementation first gets a list iterator (with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * {@code listIterator()}).  Then, it iterates over the list until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * specified element is found or the end of the list is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @throws ClassCastException   {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public int indexOf(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   185
        ListIterator<E> it = listIterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (o==null) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   187
            while (it.hasNext())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   188
                if (it.next()==null)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   189
                    return it.previousIndex();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   191
            while (it.hasNext())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   192
                if (o.equals(it.next()))
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   193
                    return it.previousIndex();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   201
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   202
     * This implementation first gets a list iterator that points to the end
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * of the list (with {@code listIterator(size())}).  Then, it iterates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * backwards over the list until the specified element is found, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * beginning of the list is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @throws ClassCastException   {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public int lastIndexOf(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   211
        ListIterator<E> it = listIterator(size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (o==null) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   213
            while (it.hasPrevious())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   214
                if (it.previous()==null)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   215
                    return it.nextIndex();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        } else {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   217
            while (it.hasPrevious())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   218
                if (o.equals(it.previous()))
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   219
                    return it.nextIndex();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Removes all of the elements from this list (optional operation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * The list will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   231
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   232
     * This implementation calls {@code removeRange(0, size())}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <p>Note that this implementation throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * {@code UnsupportedOperationException} unless {@code remove(int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * index)} or {@code removeRange(int fromIndex, int toIndex)} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @throws UnsupportedOperationException if the {@code clear} operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *         is not supported by this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        removeRange(0, size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   249
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   250
     * This implementation gets an iterator over the specified collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * and iterates over it, inserting the elements obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * iterator into this list at the appropriate position, one at a time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * using {@code add(int, E)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Many implementations will override this method for efficiency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <p>Note that this implementation throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * {@code UnsupportedOperationException} unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * {@link #add(int, Object) add(int, E)} is overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @throws IndexOutOfBoundsException     {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        boolean modified = false;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   269
        for (E e : c) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   270
            add(index++, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // Iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns an iterator over the elements in this list in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   282
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   283
     * This implementation returns a straightforward implementation of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * iterator interface, relying on the backing list's {@code size()},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * {@code get(int)}, and {@code remove(int)} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <p>Note that the iterator returned by this method will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * {@link UnsupportedOperationException} in response to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * {@code remove} method unless the list's {@code remove(int)} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <p>This implementation can be made to throw runtime exceptions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * face of concurrent modification, as described in the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * for the (protected) {@link #modCount} field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return an iterator over the elements in this list in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return new Itr();
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
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   305
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   306
     * This implementation returns {@code listIterator(0)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @see #listIterator(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return listIterator(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   317
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   318
     * This implementation returns a straightforward implementation of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * {@code ListIterator} interface that extends the implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * {@code Iterator} interface returned by the {@code iterator()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * The {@code ListIterator} implementation relies on the backing list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * and {@code remove(int)} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <p>Note that the list iterator returned by this implementation will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * throw an {@link UnsupportedOperationException} in response to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * {@code remove}, {@code set} and {@code add} methods unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * list's {@code remove(int)}, {@code set(int, E)}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * {@code add(int, E)} methods are overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <p>This implementation can be made to throw runtime exceptions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * face of concurrent modification, as described in the specification for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * the (protected) {@link #modCount} field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return new ListItr(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * Index of element to be returned by subsequent call to next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        int cursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         * Index of element returned by most recent call to next or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * previous.  Reset to -1 if this element is deleted by a call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         * to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         * The modCount value that the iterator believes that the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         * List should have.  If this expectation is violated, the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * has detected concurrent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return cursor != size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                E next = get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                lastRet = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                AbstractList.this.remove(lastRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                if (lastRet < cursor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    cursor--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        final void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private class ListItr extends Itr implements ListIterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        ListItr(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            cursor = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            return cursor != 0;
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 E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                int i = cursor - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                E previous = get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                lastRet = cursor = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                return previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return cursor-1;
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 set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                AbstractList.this.set(lastRet, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                AbstractList.this.add(i, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   464
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   465
     * This implementation returns a list that subclasses
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * {@code AbstractList}.  The subclass stores, in private fields, the
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   467
     * size of the subList (which can change over its lifetime), and the
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   468
     * expected {@code modCount} value of the backing list.  There are two
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   469
     * variants of the subclass, one of which implements {@code RandomAccess}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * If this list implements {@code RandomAccess} the returned list will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * be an instance of the subclass that implements {@code RandomAccess}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * <p>The subclass's {@code set(int, E)}, {@code get(int)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Collection)} and {@code removeRange(int, int)} methods all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * delegate to the corresponding methods on the backing abstract list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * after bounds-checking the index and adjusting for the offset.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * {@code addAll(Collection c)} method merely returns {@code addAll(size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * c)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * <p>The {@code listIterator(int)} method returns a "wrapper object"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * over a list iterator on the backing list, which is created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * corresponding method on the backing list.  The {@code iterator} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * merely returns {@code listIterator()}, and the {@code size} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * merely returns the subclass's {@code size} field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <p>All methods first check to see if the actual {@code modCount} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * the backing list is equal to its expected value, and throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * {@code ConcurrentModificationException} if it is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @throws IndexOutOfBoundsException if an endpoint index value is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *         {@code (fromIndex < 0 || toIndex > size)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @throws IllegalArgumentException if the endpoint indices are out of order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *         {@code (fromIndex > toIndex)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public List<E> subList(int fromIndex, int toIndex) {
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   497
        subListRangeCheck(fromIndex, toIndex, size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return (this instanceof RandomAccess ?
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   499
                new RandomAccessSubList<>(this, fromIndex, toIndex) :
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   500
                new SubList<>(this, fromIndex, toIndex));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   503
    static void subListRangeCheck(int fromIndex, int toIndex, int size) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   504
        if (fromIndex < 0)
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   505
            throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   506
        if (toIndex > size)
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   507
            throw new IndexOutOfBoundsException("toIndex = " + toIndex);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   508
        if (fromIndex > toIndex)
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   509
            throw new IllegalArgumentException("fromIndex(" + fromIndex +
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   510
                                               ") > toIndex(" + toIndex + ")");
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   511
    }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   512
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Compares the specified object with this list for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * {@code true} if and only if the specified object is also a list, both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * lists have the same size, and all corresponding pairs of elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * the two lists are <i>equal</i>.  (Two elements {@code e1} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * e1.equals(e2))}.)  In other words, two lists are defined to be
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   522
     * equal if they contain the same elements in the same order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   524
     * @implSpec
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * This implementation first checks if the specified object is this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * list. If so, it returns {@code true}; if not, it checks if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * specified object is a list. If not, it returns {@code false}; if so,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * it iterates over both lists, comparing corresponding pairs of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * If any comparison returns {@code false}, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * {@code false}.  If either iterator runs out of elements before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * other it returns {@code false} (as the lists are of unequal length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * otherwise it returns {@code true} when the iterations complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @param o the object to be compared for equality with this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @return {@code true} if the specified object is equal to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if (!(o instanceof List))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        ListIterator<E> e1 = listIterator();
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 7816
diff changeset
   544
        ListIterator<?> e2 = ((List<?>) o).listIterator();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   545
        while (e1.hasNext() && e2.hasNext()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            E o1 = e1.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            Object o2 = e2.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (!(o1==null ? o2==null : o1.equals(o2)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return !(e1.hasNext() || e2.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Returns the hash code value for this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   557
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   558
     * This implementation uses exactly the code that is used to define the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * list hash function in the documentation for the {@link List#hashCode}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @return the hash code value for this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        int hashCode = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        for (E e : this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Removes from this list all of the elements whose index is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Shifts any succeeding elements to the left (reduces their index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * (If {@code toIndex==fromIndex}, this operation has no effect.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <p>This method is called by the {@code clear} operation on this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * and its subLists.  Overriding this method to take advantage of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * the internals of the list implementation can <i>substantially</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * improve the performance of the {@code clear} operation on this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * and its subLists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   584
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 14342
diff changeset
   585
     * This implementation gets a list iterator positioned before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * followed by {@code ListIterator.remove} until the entire range has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * been removed.  <b>Note: if {@code ListIterator.remove} requires linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * time, this implementation requires quadratic time.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @param fromIndex index of first element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param toIndex index after last element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    protected void removeRange(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        ListIterator<E> it = listIterator(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        for (int i=0, n=toIndex-fromIndex; i<n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * The number of times this list has been <i>structurally modified</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Structural modifications are those that change the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * list, or otherwise perturb it in such a fashion that iterations in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * progress may yield incorrect results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <p>This field is used by the iterator and list iterator implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * returned by the {@code iterator} and {@code listIterator} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * If the value of this field changes unexpectedly, the iterator (or list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * iterator) will throw a {@code ConcurrentModificationException} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * response to the {@code next}, {@code remove}, {@code previous},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * {@code set} or {@code add} operations.  This provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * <i>fail-fast</i> behavior, rather than non-deterministic behavior in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * the face of concurrent modification during iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <p><b>Use of this field by subclasses is optional.</b> If a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * wishes to provide fail-fast iterators (and list iterators), then it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * merely has to increment this field in its {@code add(int, E)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * {@code remove(int)} methods (and any other methods that it overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * that result in structural modifications to the list).  A single call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * {@code add(int, E)} or {@code remove(int)} must add no more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * one to this field, or the iterators (and list iterators) will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * bogus {@code ConcurrentModificationExceptions}.  If an implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * does not wish to provide fail-fast iterators, this field may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    protected transient int modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    private void rangeCheckForAdd(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (index < 0 || index > size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    private String outOfBoundsMsg(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return "Index: "+index+", Size: "+size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   638
39568
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   639
    /**
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   640
     * An index-based split-by-two, lazily initialized Spliterator covering
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   641
     * a List that access elements via {@link List#get}.
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   642
     *
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   643
     * If access results in an IndexOutOfBoundsException then a
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   644
     * ConcurrentModificationException is thrown instead (since the list has
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   645
     * been structurally modified while traversing).
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   646
     *
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   647
     * If the List is an instance of AbstractList then concurrent modification
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   648
     * checking is performed using the AbstractList's modCount field.
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   649
     */
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   650
    static final class RandomAccessSpliterator<E> implements Spliterator<E> {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   651
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   652
        private final List<E> list;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   653
        private int index; // current index, modified on advance/split
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   654
        private int fence; // -1 until used; then one past last index
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   655
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   656
        // The following fields are valid if covering an AbstractList
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   657
        private final AbstractList<E> alist;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   658
        private int expectedModCount; // initialized when fence set
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   659
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   660
        RandomAccessSpliterator(List<E> list) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   661
            assert list instanceof RandomAccess;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   662
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   663
            this.list = list;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   664
            this.index = 0;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   665
            this.fence = -1;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   666
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   667
            this.alist = list instanceof AbstractList ? (AbstractList<E>) list : null;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   668
            this.expectedModCount = alist != null ? alist.modCount : 0;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   669
        }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   670
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   671
        /** Create new spliterator covering the given  range */
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   672
        private RandomAccessSpliterator(RandomAccessSpliterator<E> parent,
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   673
                                int origin, int fence) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   674
            this.list = parent.list;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   675
            this.index = origin;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   676
            this.fence = fence;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   677
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   678
            this.alist = parent.alist;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   679
            this.expectedModCount = parent.expectedModCount;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   680
        }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   681
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   682
        private int getFence() { // initialize fence to size on first use
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   683
            int hi;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   684
            List<E> lst = list;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   685
            if ((hi = fence) < 0) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   686
                if (alist != null) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   687
                    expectedModCount = alist.modCount;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   688
                }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   689
                hi = fence = lst.size();
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   690
            }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   691
            return hi;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   692
        }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   693
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   694
        public Spliterator<E> trySplit() {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   695
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   696
            return (lo >= mid) ? null : // divide range in half unless too small
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   697
                    new RandomAccessSpliterator<>(this, lo, index = mid);
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   698
        }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   699
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   700
        public boolean tryAdvance(Consumer<? super E> action) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   701
            if (action == null)
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   702
                throw new NullPointerException();
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   703
            int hi = getFence(), i = index;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   704
            if (i < hi) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   705
                index = i + 1;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   706
                action.accept(get(list, i));
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   707
                checkAbstractListModCount(alist, expectedModCount);
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   708
                return true;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   709
            }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   710
            return false;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   711
        }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   712
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   713
        public void forEachRemaining(Consumer<? super E> action) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   714
            Objects.requireNonNull(action);
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   715
            List<E> lst = list;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   716
            int hi = getFence();
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   717
            int i = index;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   718
            index = hi;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   719
            for (; i < hi; i++) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   720
                action.accept(get(lst, i));
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   721
            }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   722
            checkAbstractListModCount(alist, expectedModCount);
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   723
        }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   724
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   725
        public long estimateSize() {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   726
            return (long) (getFence() - index);
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   727
        }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   728
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   729
        public int characteristics() {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   730
            return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   731
        }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   732
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   733
        private static <E> E get(List<E> list, int i) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   734
            try {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   735
                return list.get(i);
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   736
            } catch (IndexOutOfBoundsException ex) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   737
                throw new ConcurrentModificationException();
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   738
            }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   739
        }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   740
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   741
        static void checkAbstractListModCount(AbstractList<?> alist, int expectedModCount) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   742
            if (alist != null && alist.modCount != expectedModCount) {
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   743
                throw new ConcurrentModificationException();
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   744
            }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   745
        }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   746
    }
1987f3929c39 8158365: List.spliterator should optimize for RandomAccess lists
psandoz
parents: 36747
diff changeset
   747
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   748
    private static class SubList<E> extends AbstractList<E> {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   749
        private final AbstractList<E> root;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   750
        private final SubList<E> parent;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   751
        private final int offset;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   752
        protected int size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   754
        /**
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   755
         * Constructs a sublist of an arbitrary AbstractList, which is
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   756
         * not a SubList itself.
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   757
         */
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   758
        public SubList(AbstractList<E> root, int fromIndex, int toIndex) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   759
            this.root = root;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   760
            this.parent = null;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   761
            this.offset = fromIndex;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   762
            this.size = toIndex - fromIndex;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   763
            this.modCount = root.modCount;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   764
        }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   765
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   766
        /**
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   767
         * Constructs a sublist of another SubList.
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   768
         */
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   769
        protected SubList(SubList<E> parent, int fromIndex, int toIndex) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   770
            this.root = parent.root;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   771
            this.parent = parent;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   772
            this.offset = parent.offset + fromIndex;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   773
            this.size = toIndex - fromIndex;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   774
            this.modCount = root.modCount;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   775
        }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   776
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   777
        public E set(int index, E element) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   778
            Objects.checkIndex(index, size);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   779
            checkForComodification();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   780
            return root.set(offset + index, element);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   781
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   783
        public E get(int index) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   784
            Objects.checkIndex(index, size);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   785
            checkForComodification();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   786
            return root.get(offset + index);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   787
        }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   788
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   789
        public int size() {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   790
            checkForComodification();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   791
            return size;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   792
        }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   793
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   794
        public void add(int index, E element) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   795
            rangeCheckForAdd(index);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   796
            checkForComodification();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   797
            root.add(offset + index, element);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   798
            updateSizeAndModCount(1);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   799
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   801
        public E remove(int index) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   802
            Objects.checkIndex(index, size);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   803
            checkForComodification();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   804
            E result = root.remove(offset + index);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   805
            updateSizeAndModCount(-1);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   806
            return result;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   807
        }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   808
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   809
        protected void removeRange(int fromIndex, int toIndex) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   810
            checkForComodification();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   811
            root.removeRange(offset + fromIndex, offset + toIndex);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   812
            updateSizeAndModCount(fromIndex - toIndex);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   813
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   815
        public boolean addAll(Collection<? extends E> c) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   816
            return addAll(size, c);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   817
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   819
        public boolean addAll(int index, Collection<? extends E> c) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   820
            rangeCheckForAdd(index);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   821
            int cSize = c.size();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   822
            if (cSize==0)
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   823
                return false;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   824
            checkForComodification();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   825
            root.addAll(offset + index, c);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   826
            updateSizeAndModCount(cSize);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   827
            return true;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   828
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   830
        public Iterator<E> iterator() {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   831
            return listIterator();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   832
        }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   833
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   834
        public ListIterator<E> listIterator(int index) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   835
            checkForComodification();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   836
            rangeCheckForAdd(index);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   837
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   838
            return new ListIterator<E>() {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   839
                private final ListIterator<E> i =
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   840
                        root.listIterator(offset + index);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   841
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   842
                public boolean hasNext() {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   843
                    return nextIndex() < size;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   844
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   846
                public E next() {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   847
                    if (hasNext())
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   848
                        return i.next();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   849
                    else
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   850
                        throw new NoSuchElementException();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   851
                }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   852
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   853
                public boolean hasPrevious() {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   854
                    return previousIndex() >= 0;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   855
                }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   856
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   857
                public E previous() {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   858
                    if (hasPrevious())
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   859
                        return i.previous();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   860
                    else
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   861
                        throw new NoSuchElementException();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   862
                }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   863
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   864
                public int nextIndex() {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   865
                    return i.nextIndex() - offset;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   866
                }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   867
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   868
                public int previousIndex() {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   869
                    return i.previousIndex() - offset;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   870
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   872
                public void remove() {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   873
                    i.remove();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   874
                    updateSizeAndModCount(-1);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   875
                }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   876
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   877
                public void set(E e) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   878
                    i.set(e);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   879
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   881
                public void add(E e) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   882
                    i.add(e);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   883
                    updateSizeAndModCount(1);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   884
                }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   885
            };
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   886
        }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   887
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   888
        public List<E> subList(int fromIndex, int toIndex) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   889
            subListRangeCheck(fromIndex, toIndex, size);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   890
            return new SubList<>(this, fromIndex, toIndex);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   891
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   893
        private void rangeCheckForAdd(int index) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   894
            if (index < 0 || index > size)
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   895
                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   896
        }
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   897
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   898
        private String outOfBoundsMsg(int index) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   899
            return "Index: "+index+", Size: "+size;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   900
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   902
        private void checkForComodification() {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   903
            if (root.modCount != this.modCount)
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   904
                throw new ConcurrentModificationException();
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   905
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   907
        private void updateSizeAndModCount(int sizeChange) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   908
            SubList<E> slist = this;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   909
            do {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   910
                slist.size += sizeChange;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   911
                slist.modCount = root.modCount;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   912
                slist = slist.parent;
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   913
            } while (slist != null);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   914
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   917
    private static class RandomAccessSubList<E>
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   918
            extends SubList<E> implements RandomAccess {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   920
        /**
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   921
         * Constructs a sublist of an arbitrary AbstractList, which is
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   922
         * not a RandomAccessSubList itself.
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   923
         */
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   924
        RandomAccessSubList(AbstractList<E> root,
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   925
                int fromIndex, int toIndex) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   926
            super(root, fromIndex, toIndex);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   927
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   929
        /**
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   930
         * Constructs a sublist of another RandomAccessSubList.
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   931
         */
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   932
        RandomAccessSubList(RandomAccessSubList<E> parent,
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   933
                int fromIndex, int toIndex) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   934
            super(parent, fromIndex, toIndex);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   935
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
36747
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   937
        public List<E> subList(int fromIndex, int toIndex) {
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   938
            subListRangeCheck(fromIndex, toIndex, size);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   939
            return new RandomAccessSubList<>(this, fromIndex, toIndex);
b984c3a69c74 8079136: Accessing a nested sublist leads to StackOverflowError
igerasim
parents: 32649
diff changeset
   940
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
}