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