src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 50764 5637aca18f1d
child 54683 3ffdc15cd044
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Expert Group.  Adapted and released, under explicit permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * from JDK ArrayList.java which carries the following copyright:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Copyright 1997 by Sun Microsystems, Inc.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
package java.util.concurrent;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
    36
50764
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 49564
diff changeset
    37
import java.lang.invoke.VarHandle;
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
    38
import java.lang.reflect.Field;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    39
import java.util.Arrays;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    40
import java.util.Collection;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    41
import java.util.Comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    42
import java.util.ConcurrentModificationException;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    43
import java.util.Iterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    44
import java.util.List;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    45
import java.util.ListIterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    46
import java.util.NoSuchElementException;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    47
import java.util.Objects;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    48
import java.util.RandomAccess;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    49
import java.util.Spliterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    50
import java.util.Spliterators;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
    51
import java.util.function.Consumer;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
    52
import java.util.function.Predicate;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
    53
import java.util.function.UnaryOperator;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 50764
diff changeset
    54
import jdk.internal.access.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * A thread-safe variant of {@link java.util.ArrayList} in which all mutative
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    58
 * operations ({@code add}, {@code set}, and so on) are implemented by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * making a fresh copy of the underlying array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    61
 * <p>This is ordinarily too costly, but may be <em>more</em> efficient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * than alternatives when traversal operations vastly outnumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * mutations, and is useful when you cannot or don't want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * synchronize traversals, yet need to preclude interference among
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * concurrent threads.  The "snapshot" style iterator method uses a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * reference to the state of the array at the point that the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * was created. This array never changes during the lifetime of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * iterator, so interference is impossible and the iterator is
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    69
 * guaranteed not to throw {@code ConcurrentModificationException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * The iterator will not reflect additions, removals, or changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * the list since the iterator was created.  Element-changing
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    72
 * operations on iterators themselves ({@code remove}, {@code set}, and
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    73
 * {@code add}) are not supported. These methods throw
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    74
 * {@code UnsupportedOperationException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    76
 * <p>All elements are permitted, including {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>Memory consistency effects: As with other concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * collections, actions in a thread prior to placing an object into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * {@code CopyOnWriteArrayList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * actions subsequent to the access or removal of that element from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * the {@code CopyOnWriteArrayList} in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p>This class is a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48541
diff changeset
    86
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @author Doug Lea
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
    91
 * @param <E> the type of elements held in this list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
public class CopyOnWriteArrayList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final long serialVersionUID = 8673264195747942595L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
    97
    /**
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
    98
     * The lock protecting all mutators.  (We have a mild preference
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
    99
     * for builtin monitors over ReentrantLock when either will do.)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   100
     */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   101
    final transient Object lock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /** The array, accessed only via getArray/setArray. */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   104
    private transient volatile Object[] array;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Gets the array.  Non-private so as to also be accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * from CopyOnWriteArraySet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    final Object[] getArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Sets the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    final void setArray(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        array = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Creates an empty list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public CopyOnWriteArrayList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        setArray(new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Creates a list containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param c the collection of initially held elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public CopyOnWriteArrayList(Collection<? extends E> c) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   137
        Object[] es;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   138
        if (c.getClass() == CopyOnWriteArrayList.class)
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   139
            es = ((CopyOnWriteArrayList<?>)c).getArray();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   140
        else {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   141
            es = c.toArray();
31540
6efd719b3330 6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents: 25859
diff changeset
   142
            // defend against c.toArray (incorrectly) not returning Object[]
6efd719b3330 6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents: 25859
diff changeset
   143
            // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   144
            if (es.getClass() != Object[].class)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   145
                es = Arrays.copyOf(es, es.length, Object[].class);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   146
        }
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   147
        setArray(es);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Creates a list holding a copy of the given array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param toCopyIn the array (a copy of this array is used as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *        internal array)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public CopyOnWriteArrayList(E[] toCopyIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        setArray(Arrays.copyOf(toCopyIn, toCopyIn.length, Object[].class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Returns the number of elements in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return the number of elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return getArray().length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   171
     * Returns {@code true} if this list contains no elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   173
     * @return {@code true} if this list contains no elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return size() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * static version of indexOf, to allow repeated calls without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * needing to re-acquire array each time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param o element to search for
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   183
     * @param es the array
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   184
     * @param from first index to search
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   185
     * @param to one past last index to search
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return index of element, or -1 if absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   188
    private static int indexOfRange(Object o, Object[] es, int from, int to) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (o == null) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   190
            for (int i = from; i < to; i++)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   191
                if (es[i] == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   194
            for (int i = from; i < to; i++)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   195
                if (o.equals(es[i]))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * static version of lastIndexOf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param o element to search for
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   204
     * @param es the array
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   205
     * @param from index of first element of range, last element to search
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   206
     * @param to one past last element of range, first element to search
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return index of element, or -1 if absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   209
    private static int lastIndexOfRange(Object o, Object[] es, int from, int to) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (o == null) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   211
            for (int i = to - 1; i >= from; i--)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   212
                if (es[i] == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } else {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   215
            for (int i = to - 1; i >= from; i--)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   216
                if (o.equals(es[i]))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    return i;
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
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   223
     * Returns {@code true} if this list contains the specified element.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   224
     * More formally, returns {@code true} if and only if this list contains
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   225
     * at least one element {@code e} such that {@code Objects.equals(o, e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param o element whose presence in this list is to be tested
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   228
     * @return {@code true} if this list contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public boolean contains(Object o) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   231
        return indexOf(o) >= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public int indexOf(Object o) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   238
        Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   239
        return indexOfRange(o, es, 0, es.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns the index of the first occurrence of the specified element in
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   244
     * this list, searching forwards from {@code index}, or returns -1 if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * the element is not found.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   246
     * More formally, returns the lowest index {@code i} such that
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   247
     * {@code i >= index && Objects.equals(get(i), e)},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param e element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param index index to start searching from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @return the index of the first occurrence of the element in
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   253
     *         this list at position {@code index} or later in the list;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   254
     *         {@code -1} if the element is not found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public int indexOf(E e, int index) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   258
        Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   259
        return indexOfRange(e, es, index, es.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public int lastIndexOf(Object o) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   266
        Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   267
        return lastIndexOfRange(o, es, 0, es.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Returns the index of the last occurrence of the specified element in
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   272
     * this list, searching backwards from {@code index}, or returns -1 if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * the element is not found.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   274
     * More formally, returns the highest index {@code i} such that
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   275
     * {@code i <= index && Objects.equals(get(i), e)},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param e element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param index index to start searching backwards from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return the index of the last occurrence of the element at position
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   281
     *         less than or equal to {@code index} in this list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *         -1 if the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @throws IndexOutOfBoundsException if the specified index is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *         than or equal to the current size of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public int lastIndexOf(E e, int index) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   287
        Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   288
        return lastIndexOfRange(e, es, 0, index + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Returns a shallow copy of this list.  (The elements themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * are not copied.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return a clone of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        try {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   299
            @SuppressWarnings("unchecked")
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   300
            CopyOnWriteArrayList<E> clone =
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   301
                (CopyOnWriteArrayList<E>) super.clone();
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   302
            clone.resetLock();
50764
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 49564
diff changeset
   303
            // Unlike in readObject, here we cannot visibility-piggyback on the
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 49564
diff changeset
   304
            // volatile write in setArray().
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 49564
diff changeset
   305
            VarHandle.releaseFence();
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   306
            return clone;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            // this shouldn't happen, since we are Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            throw new InternalError();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Returns an array containing all of the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * in proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * maintained by this list.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @return an array containing all the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public Object[] toArray() {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   327
        return getArray().clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Returns an array containing all of the elements in this list in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * proper sequence (from first to last element); the runtime type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * the returned array is that of the specified array.  If the list fits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * in the specified array, it is returned therein.  Otherwise, a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * array is allocated with the runtime type of the specified array and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * the size of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>If this list fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * (i.e., the array has more elements than this list), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * the array immediately following the end of the list is set to
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   341
     * {@code null}.  (This is useful in determining the length of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * list <i>only</i> if the caller knows that this list does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * any null elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   350
     * <p>Suppose {@code x} is a list known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * The following code can be used to dump the list into a newly
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   352
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   354
     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   356
     * Note that {@code toArray(new Object[0])} is identical in function to
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   357
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param a the array into which the elements of the list are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *          same runtime type is allocated for this purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @return an array containing all the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *         this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    @SuppressWarnings("unchecked")
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   369
    public <T> T[] toArray(T[] a) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   370
        Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   371
        int len = es.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (a.length < len)
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   373
            return (T[]) Arrays.copyOf(es, len, a.getClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        else {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   375
            System.arraycopy(es, 0, a, 0, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (a.length > len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                a[len] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    // Positional Access Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    @SuppressWarnings("unchecked")
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   385
    static <E> E elementAt(Object[] a, int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return (E) a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   389
    static String outOfBounds(int index, int size) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   390
        return "Index: " + index + ", Size: " + size;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   391
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   392
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public E get(int index) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   399
        return elementAt(getArray(), index);
2
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
     * Replaces the element at the specified position in this list with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public E set(int index, E element) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   409
        synchronized (lock) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   410
            Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   411
            E oldValue = elementAt(es, index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (oldValue != element) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   414
                es = es.clone();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   415
                es[index] = element;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   416
                setArray(es);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
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
     * Appends the specified element to the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param e element to be appended to this list
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   426
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public boolean add(E e) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   429
        synchronized (lock) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   430
            Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   431
            int len = es.length;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   432
            es = Arrays.copyOf(es, len + 1);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   433
            es[len] = e;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   434
            setArray(es);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Inserts the specified element at the specified position in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * list. Shifts the element currently at that position (if any) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * any subsequent elements to the right (adds one to their indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public void add(int index, E element) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   447
        synchronized (lock) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   448
            Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   449
            int len = es.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (index > len || index < 0)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   451
                throw new IndexOutOfBoundsException(outOfBounds(index, len));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            Object[] newElements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            int numMoved = len - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            if (numMoved == 0)
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   455
                newElements = Arrays.copyOf(es, len + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                newElements = new Object[len + 1];
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   458
                System.arraycopy(es, 0, newElements, 0, index);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   459
                System.arraycopy(es, index, newElements, index + 1,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            newElements[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Removes the element at the specified position in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Shifts any subsequent elements to the left (subtracts one from their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * indices).  Returns the element that was removed from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public E remove(int index) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   475
        synchronized (lock) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   476
            Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   477
            int len = es.length;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   478
            E oldValue = elementAt(es, index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            int numMoved = len - index - 1;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   480
            Object[] newElements;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (numMoved == 0)
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   482
                newElements = Arrays.copyOf(es, len - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            else {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   484
                newElements = new Object[len - 1];
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   485
                System.arraycopy(es, 0, newElements, 0, index);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   486
                System.arraycopy(es, index + 1, newElements, index,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   489
            setArray(newElements);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * Removes the first occurrence of the specified element from this list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * if it is present.  If this list does not contain the element, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * unchanged.  More formally, removes the element with the lowest index
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   498
     * {@code i} such that {@code Objects.equals(o, get(i))}
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   499
     * (if such an element exists).  Returns {@code true} if this list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * contained the specified element (or equivalently, if this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @param o element to be removed from this list, if present
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   504
     * @return {@code true} if this list contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public boolean remove(Object o) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   507
        Object[] snapshot = getArray();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   508
        int index = indexOfRange(o, snapshot, 0, snapshot.length);
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47423
diff changeset
   509
        return index >= 0 && remove(o, snapshot, index);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   510
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   511
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   512
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   513
     * A version of remove(Object) using the strong hint that given
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   514
     * recent snapshot contains o at the given index.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   515
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   516
    private boolean remove(Object o, Object[] snapshot, int index) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   517
        synchronized (lock) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   518
            Object[] current = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   519
            int len = current.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   520
            if (snapshot != current) findIndex: {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   521
                int prefix = Math.min(index, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   522
                for (int i = 0; i < prefix; i++) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   523
                    if (current[i] != snapshot[i]
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   524
                        && Objects.equals(o, current[i])) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   525
                        index = i;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   526
                        break findIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   527
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   529
                if (index >= len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   530
                    return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   531
                if (current[index] == o)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   532
                    break findIndex;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   533
                index = indexOfRange(o, current, index, len);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   534
                if (index < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   535
                    return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   537
            Object[] newElements = new Object[len - 1];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   538
            System.arraycopy(current, 0, newElements, 0, index);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   539
            System.arraycopy(current, index + 1,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   540
                             newElements, index,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   541
                             len - index - 1);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   542
            setArray(newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   543
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Removes from this list all of the elements whose index is between
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   549
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Shifts any succeeding elements to the left (reduces their index).
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   551
     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   552
     * (If {@code toIndex==fromIndex}, this operation has no effect.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @param fromIndex index of first element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @param toIndex index after last element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @throws IndexOutOfBoundsException if fromIndex or toIndex out of range
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   557
     *         ({@code fromIndex < 0 || toIndex > size() || toIndex < fromIndex})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   559
    void removeRange(int fromIndex, int toIndex) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   560
        synchronized (lock) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   561
            Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   562
            int len = es.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            if (fromIndex < 0 || toIndex > len || toIndex < fromIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            int newlen = len - (toIndex - fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            int numMoved = len - toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if (numMoved == 0)
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   569
                setArray(Arrays.copyOf(es, newlen));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                Object[] newElements = new Object[newlen];
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   572
                System.arraycopy(es, 0, newElements, 0, fromIndex);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   573
                System.arraycopy(es, toIndex, newElements,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                 fromIndex, numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   581
     * Appends the element, if not present.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @param e element to be added to this list, if absent
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   584
     * @return {@code true} if the element was added
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public boolean addIfAbsent(E e) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   587
        Object[] snapshot = getArray();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   588
        return indexOfRange(e, snapshot, 0, snapshot.length) < 0
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47423
diff changeset
   589
            && addIfAbsent(e, snapshot);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   590
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   591
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   592
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   593
     * A version of addIfAbsent using the strong hint that given
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   594
     * recent snapshot does not contain e.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   595
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   596
    private boolean addIfAbsent(E e, Object[] snapshot) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   597
        synchronized (lock) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   598
            Object[] current = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   599
            int len = current.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   600
            if (snapshot != current) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   601
                // Optimize for lost race to another addXXX operation
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   602
                int common = Math.min(snapshot.length, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   603
                for (int i = 0; i < common; i++)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   604
                    if (current[i] != snapshot[i]
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   605
                        && Objects.equals(e, current[i]))
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   606
                        return false;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   607
                if (indexOfRange(e, current, common, len) >= 0)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   608
                        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   610
            Object[] newElements = Arrays.copyOf(current, len + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            newElements[len] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   618
     * Returns {@code true} if this list contains all of the elements of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @param c collection to be checked for containment in this list
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   622
     * @return {@code true} if this list contains all of the elements of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *         specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @see #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    public boolean containsAll(Collection<?> c) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   628
        Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   629
        int len = es.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        for (Object e : c) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   631
            if (indexOfRange(e, es, 0, len) < 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Removes from this list all of its elements that are contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * the specified collection. This is a particularly expensive operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * in this class because of the need for an internal temporary array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param c collection containing elements to be removed from this list
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   643
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @throws ClassCastException if the class of an element of this list
9504
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9035
diff changeset
   645
     *         is incompatible with the specified collection
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48541
diff changeset
   646
     * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @throws NullPointerException if this list contains a null element and the
9504
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9035
diff changeset
   648
     *         specified collection does not permit null elements
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48541
diff changeset
   649
     * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public boolean removeAll(Collection<?> c) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   654
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   655
        return bulkRemove(e -> c.contains(e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Retains only the elements in this list that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * specified collection.  In other words, removes from this list all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * its elements that are not contained in the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @param c collection containing elements to be retained in this list
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   664
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @throws ClassCastException if the class of an element of this list
9504
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9035
diff changeset
   666
     *         is incompatible with the specified collection
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48541
diff changeset
   667
     * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @throws NullPointerException if this list contains a null element and the
9504
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9035
diff changeset
   669
     *         specified collection does not permit null elements
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48541
diff changeset
   670
     * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public boolean retainAll(Collection<?> c) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   675
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   676
        return bulkRemove(e -> !c.contains(e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * Appends all of the elements in the specified collection that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * are not already contained in this list, to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * this list, in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * specified collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @param c collection containing elements to be added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @return the number of elements added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @see #addIfAbsent(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public int addAllAbsent(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        Object[] cs = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            return 0;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   694
        synchronized (lock) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   695
            Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   696
            int len = es.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            int added = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   698
            // uniquify and compact elements in cs
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   699
            for (int i = 0; i < cs.length; ++i) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                Object e = cs[i];
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   701
                if (indexOfRange(e, es, 0, len) < 0 &&
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   702
                    indexOfRange(e, cs, 0, added) < 0)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   703
                    cs[added++] = e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            if (added > 0) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   706
                Object[] newElements = Arrays.copyOf(es, len + added);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   707
                System.arraycopy(cs, 0, newElements, len, added);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Removes all of the elements from this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * The list will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    public void clear() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   719
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            setArray(new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * Appends all of the elements in the specified collection to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * of this list, in the order that they are returned by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @param c collection containing elements to be added to this list
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   730
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @see #add(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    public boolean addAll(Collection<? extends E> c) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   735
        Object[] cs = (c.getClass() == CopyOnWriteArrayList.class) ?
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   736
            ((CopyOnWriteArrayList<?>)c).getArray() : c.toArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            return false;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   739
        synchronized (lock) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   740
            Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   741
            int len = es.length;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   742
            Object[] newElements;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   743
            if (len == 0 && cs.getClass() == Object[].class)
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   744
                newElements = cs;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   745
            else {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   746
                newElements = Arrays.copyOf(es, len + cs.length);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   747
                System.arraycopy(cs, 0, newElements, len, cs.length);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   748
            }
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   749
            setArray(newElements);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * Inserts all of the elements in the specified collection into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * list, starting at the specified position.  Shifts the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * currently at that position (if any) and any subsequent elements to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * the right (increases their indices).  The new elements will appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * in this list in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * specified collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @param index index at which to insert the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *        from the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param c collection containing elements to be added to this list
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   765
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @see #add(int,Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        Object[] cs = c.toArray();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   772
        synchronized (lock) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   773
            Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   774
            int len = es.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            if (index > len || index < 0)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   776
                throw new IndexOutOfBoundsException(outOfBounds(index, len));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            int numMoved = len - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            Object[] newElements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            if (numMoved == 0)
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   782
                newElements = Arrays.copyOf(es, len + cs.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                newElements = new Object[len + cs.length];
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   785
                System.arraycopy(es, 0, newElements, 0, index);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   786
                System.arraycopy(es, index,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                                 newElements, index + cs.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            System.arraycopy(cs, 0, newElements, index, cs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   796
    /**
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   797
     * @throws NullPointerException {@inheritDoc}
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   798
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   799
    public void forEach(Consumer<? super E> action) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
   800
        Objects.requireNonNull(action);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   801
        for (Object x : getArray()) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   802
            @SuppressWarnings("unchecked") E e = (E) x;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   803
            action.accept(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   804
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   805
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   806
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   807
    /**
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   808
     * @throws NullPointerException {@inheritDoc}
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   809
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   810
    public boolean removeIf(Predicate<? super E> filter) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
   811
        Objects.requireNonNull(filter);
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   812
        return bulkRemove(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   813
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   814
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   815
    // A tiny bit set implementation
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   816
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   817
    private static long[] nBits(int n) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   818
        return new long[((n - 1) >> 6) + 1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   819
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   820
    private static void setBit(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   821
        bits[i >> 6] |= 1L << i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   822
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   823
    private static boolean isClear(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   824
        return (bits[i >> 6] & (1L << i)) == 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   825
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   826
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   827
    private boolean bulkRemove(Predicate<? super E> filter) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   828
        synchronized (lock) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   829
            return bulkRemove(filter, 0, getArray().length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   830
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   831
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   832
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   833
    boolean bulkRemove(Predicate<? super E> filter, int i, int end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   834
        // assert Thread.holdsLock(lock);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   835
        final Object[] es = getArray();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   836
        // Optimize for initial run of survivors
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   837
        for (; i < end && !filter.test(elementAt(es, i)); i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   838
            ;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   839
        if (i < end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   840
            final int beg = i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   841
            final long[] deathRow = nBits(end - beg);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   842
            int deleted = 1;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   843
            deathRow[0] = 1L;   // set bit 0
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   844
            for (i = beg + 1; i < end; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   845
                if (filter.test(elementAt(es, i))) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   846
                    setBit(deathRow, i - beg);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   847
                    deleted++;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   848
                }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   849
            // Did filter reentrantly modify the list?
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   850
            if (es != getArray())
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   851
                throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   852
            final Object[] newElts = Arrays.copyOf(es, es.length - deleted);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   853
            int w = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   854
            for (i = beg; i < end; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   855
                if (isClear(deathRow, i - beg))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   856
                    newElts[w++] = es[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   857
            System.arraycopy(es, i, newElts, w, es.length - i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   858
            setArray(newElts);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   859
            return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   860
        } else {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   861
            if (es != getArray())
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   862
                throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   863
            return false;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   864
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   865
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   866
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   867
    public void replaceAll(UnaryOperator<E> operator) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   868
        synchronized (lock) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   869
            replaceAllRange(operator, 0, getArray().length);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   870
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   871
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   872
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   873
    void replaceAllRange(UnaryOperator<E> operator, int i, int end) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   874
        // assert Thread.holdsLock(lock);
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   875
        Objects.requireNonNull(operator);
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   876
        final Object[] es = getArray().clone();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   877
        for (; i < end; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   878
            es[i] = operator.apply(elementAt(es, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   879
        setArray(es);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   880
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   881
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   882
    public void sort(Comparator<? super E> c) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   883
        synchronized (lock) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   884
            sortRange(c, 0, getArray().length);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   885
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   886
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   887
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   888
    @SuppressWarnings("unchecked")
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   889
    void sortRange(Comparator<? super E> c, int i, int end) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   890
        // assert Thread.holdsLock(lock);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   891
        final Object[] es = getArray().clone();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   892
        Arrays.sort(es, i, end, (Comparator<Object>)c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   893
        setArray(es);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   894
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
   895
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   897
     * Saves this list to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   899
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   900
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @serialData The length of the array backing the list is emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *               (int), followed by all of its elements (each an Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *               in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    private void writeObject(java.io.ObjectOutputStream s)
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   906
        throws java.io.IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   910
        Object[] es = getArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        // Write out array length
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   912
        s.writeInt(es.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        // Write out all elements in the proper order.
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   915
        for (Object element : es)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
   916
            s.writeObject(element);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   920
     * Reconstitutes this list from a stream (that is, deserializes it).
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   921
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   922
     * @throws ClassNotFoundException if the class of a serialized object
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   923
     *         could not be found
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   924
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        // bind to new lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        resetLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        // Read in array length and allocate array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        int len = s.readInt();
47423
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47216
diff changeset
   936
        SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, len);
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   937
        Object[] es = new Object[len];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        // Read in all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        for (int i = 0; i < len; i++)
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   941
            es[i] = s.readObject();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   942
        setArray(es);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * Returns a string representation of this list.  The string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * representation consists of the string representations of the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * elements in the order they are returned by its iterator, enclosed in
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   949
     * square brackets ({@code "[]"}).  Adjacent elements are separated by
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   950
     * the characters {@code ", "} (comma and space).  Elements are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * converted to strings as by {@link String#valueOf(Object)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @return a string representation of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        return Arrays.toString(getArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * Compares the specified object with this list for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Returns {@code true} if the specified object is the same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * as this object, or if it is also a {@link List} and the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * of elements returned by an {@linkplain List#iterator() iterator}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * over the specified list is the same as the sequence returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * an iterator over this list.  The two sequences are considered to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * be the same if they have the same length and corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * elements at the same position in the sequence are <em>equal</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * Two elements {@code e1} and {@code e2} are considered
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   969
     * <em>equal</em> if {@code Objects.equals(e1, e2)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @param o the object to be compared for equality with this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * @return {@code true} if the specified object is equal to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        if (!(o instanceof List))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   980
        List<?> list = (List<?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        Iterator<?> it = list.iterator();
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47423
diff changeset
   982
        for (Object element : getArray())
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47423
diff changeset
   983
            if (!it.hasNext() || !Objects.equals(element, it.next()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                return false;
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47423
diff changeset
   985
        return !it.hasNext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   988
    private static int hashCodeOfRange(Object[] es, int from, int to) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   989
        int hashCode = 1;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   990
        for (int i = from; i < to; i++) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   991
            Object x = es[i];
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   992
            hashCode = 31 * hashCode + (x == null ? 0 : x.hashCode());
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   993
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   994
        return hashCode;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   995
    }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
   996
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * Returns the hash code value for this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * <p>This implementation uses the definition in {@link List#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * @return the hash code value for this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    public int hashCode() {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1005
        Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1006
        return hashCodeOfRange(es, 0, es.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * Returns an iterator over the elements in this list in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * traversing the iterator. The iterator does <em>NOT</em> support the
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1015
     * {@code remove} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * @return an iterator over the elements in this list in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        return new COWIterator<E>(getArray(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * traversing the iterator. The iterator does <em>NOT</em> support the
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1029
     * {@code remove}, {@code set} or {@code add} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        return new COWIterator<E>(getArray(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * traversing the iterator. The iterator does <em>NOT</em> support the
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1041
     * {@code remove}, {@code set} or {@code add} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     */
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1045
    public ListIterator<E> listIterator(int index) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1046
        Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1047
        int len = es.length;
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1048
        if (index < 0 || index > len)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1049
            throw new IndexOutOfBoundsException(outOfBounds(index, len));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1051
        return new COWIterator<E>(es, index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1054
    /**
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1055
     * Returns a {@link Spliterator} over the elements in this list.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1056
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1057
     * <p>The {@code Spliterator} reports {@link Spliterator#IMMUTABLE},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1058
     * {@link Spliterator#ORDERED}, {@link Spliterator#SIZED}, and
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1059
     * {@link Spliterator#SUBSIZED}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1060
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1061
     * <p>The spliterator provides a snapshot of the state of the list
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1062
     * when the spliterator was constructed. No synchronization is needed while
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1063
     * operating on the spliterator.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1064
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1065
     * @return a {@code Spliterator} over the elements in this list
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1066
     * @since 1.8
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1067
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1068
    public Spliterator<E> spliterator() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1069
        return Spliterators.spliterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1070
            (getArray(), Spliterator.IMMUTABLE | Spliterator.ORDERED);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1071
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1072
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1073
    static final class COWIterator<E> implements ListIterator<E> {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1074
        /** Snapshot of the array */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        private final Object[] snapshot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        /** Index of element to be returned by subsequent call to next.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        private int cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1079
        COWIterator(Object[] es, int initialCursor) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            cursor = initialCursor;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1081
            snapshot = es;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            return cursor < snapshot.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            return cursor > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            if (! hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            return (E) snapshot[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            if (! hasPrevious())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            return (E) snapshot[--cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        public int previousIndex() {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1111
            return cursor - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
         * Not supported. Always throws UnsupportedOperationException.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1116
         * @throws UnsupportedOperationException always; {@code remove}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
         * Not supported. Always throws UnsupportedOperationException.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1125
         * @throws UnsupportedOperationException always; {@code set}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
         * Not supported. Always throws UnsupportedOperationException.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1134
         * @throws UnsupportedOperationException always; {@code add}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1140
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1141
        @Override
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1142
        public void forEachRemaining(Consumer<? super E> action) {
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1143
            Objects.requireNonNull(action);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1144
            final int size = snapshot.length;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1145
            int i = cursor;
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1146
            cursor = size;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1147
            for (; i < size; i++)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1148
                action.accept(elementAt(snapshot, i));
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1149
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * Returns a view of the portion of this list between
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1154
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * The returned list is backed by this list, so changes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * returned list are reflected in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * <p>The semantics of the list returned by this method become
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * undefined if the backing list (i.e., this list) is modified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * any way other than via the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @param fromIndex low endpoint (inclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * @param toIndex high endpoint (exclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @return a view of the specified range within this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    public List<E> subList(int fromIndex, int toIndex) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1168
        synchronized (lock) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1169
            Object[] es = getArray();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1170
            int len = es.length;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1171
            int size = toIndex - fromIndex;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1172
            if (fromIndex < 0 || toIndex > len || size < 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                throw new IndexOutOfBoundsException();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1174
            return new COWSubList(es, fromIndex, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * Sublist for CopyOnWriteArrayList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     */
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1181
    private class COWSubList implements List<E>, RandomAccess {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        private final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        private Object[] expectedArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1186
        COWSubList(Object[] es, int offset, int size) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1187
            // assert Thread.holdsLock(lock);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1188
            expectedArray = es;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1189
            this.offset = offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1190
            this.size = size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        private void checkForComodification() {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1194
            // assert Thread.holdsLock(lock);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1195
            if (getArray() != expectedArray)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1199
        private Object[] getArrayChecked() {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1200
            // assert Thread.holdsLock(lock);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1201
            Object[] a = getArray();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1202
            if (a != expectedArray)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1203
                throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1204
            return a;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1205
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1206
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        private void rangeCheck(int index) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1208
            // assert Thread.holdsLock(lock);
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1209
            if (index < 0 || index >= size)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1210
                throw new IndexOutOfBoundsException(outOfBounds(index, size));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1213
        private void rangeCheckForAdd(int index) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1214
            // assert Thread.holdsLock(lock);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1215
            if (index < 0 || index > size)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1216
                throw new IndexOutOfBoundsException(outOfBounds(index, size));
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1217
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1218
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1219
        public Object[] toArray() {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1220
            final Object[] es;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1221
            final int offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1222
            final int size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1223
            synchronized (lock) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1224
                es = getArrayChecked();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1225
                offset = this.offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1226
                size = this.size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1227
            }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1228
            return Arrays.copyOfRange(es, offset, offset + size);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1229
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1230
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1231
        @SuppressWarnings("unchecked")
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1232
        public <T> T[] toArray(T[] a) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1233
            final Object[] es;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1234
            final int offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1235
            final int size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1236
            synchronized (lock) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1237
                es = getArrayChecked();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1238
                offset = this.offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1239
                size = this.size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1240
            }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1241
            if (a.length < size)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1242
                return (T[]) Arrays.copyOfRange(
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1243
                        es, offset, offset + size, a.getClass());
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1244
            else {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1245
                System.arraycopy(es, offset, a, 0, size);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1246
                if (a.length > size)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1247
                    a[size] = null;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1248
                return a;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1249
            }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1250
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1251
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1252
        public int indexOf(Object o) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1253
            final Object[] es;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1254
            final int offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1255
            final int size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1256
            synchronized (lock) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1257
                es = getArrayChecked();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1258
                offset = this.offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1259
                size = this.size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1260
            }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1261
            int i = indexOfRange(o, es, offset, offset + size);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1262
            return (i == -1) ? -1 : i - offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1263
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1264
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1265
        public int lastIndexOf(Object o) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1266
            final Object[] es;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1267
            final int offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1268
            final int size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1269
            synchronized (lock) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1270
                es = getArrayChecked();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1271
                offset = this.offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1272
                size = this.size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1273
            }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1274
            int i = lastIndexOfRange(o, es, offset, offset + size);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1275
            return (i == -1) ? -1 : i - offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1276
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1277
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1278
        public boolean contains(Object o) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1279
            return indexOf(o) >= 0;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1280
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1281
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1282
        public boolean containsAll(Collection<?> c) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1283
            final Object[] es;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1284
            final int offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1285
            final int size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1286
            synchronized (lock) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1287
                es = getArrayChecked();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1288
                offset = this.offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1289
                size = this.size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1290
            }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1291
            for (Object o : c)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1292
                if (indexOfRange(o, es, offset, offset + size) < 0)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1293
                    return false;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1294
            return true;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1295
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1296
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1297
        public boolean isEmpty() {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1298
            return size() == 0;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1299
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1300
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1301
        public String toString() {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1302
            return Arrays.toString(toArray());
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1303
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1304
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1305
        public int hashCode() {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1306
            final Object[] es;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1307
            final int offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1308
            final int size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1309
            synchronized (lock) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1310
                es = getArrayChecked();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1311
                offset = this.offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1312
                size = this.size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1313
            }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1314
            return hashCodeOfRange(es, offset, offset + size);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1315
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1316
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1317
        public boolean equals(Object o) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1318
            if (o == this)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1319
                return true;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1320
            if (!(o instanceof List))
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1321
                return false;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1322
            Iterator<?> it = ((List<?>)o).iterator();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1323
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1324
            final Object[] es;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1325
            final int offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1326
            final int size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1327
            synchronized (lock) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1328
                es = getArrayChecked();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1329
                offset = this.offset;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1330
                size = this.size;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1331
            }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1332
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1333
            for (int i = offset, end = offset + size; i < end; i++)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1334
                if (!it.hasNext() || !Objects.equals(es[i], it.next()))
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1335
                    return false;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1336
            return !it.hasNext();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1337
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1338
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        public E set(int index, E element) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1340
            synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                checkForComodification();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1343
                E x = CopyOnWriteArrayList.this.set(offset + index, element);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1344
                expectedArray = getArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        public E get(int index) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1350
            synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                checkForComodification();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1353
                return CopyOnWriteArrayList.this.get(offset + index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        public int size() {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1358
            synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1364
        public boolean add(E element) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1365
            synchronized (lock) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1366
                checkForComodification();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1367
                CopyOnWriteArrayList.this.add(offset + size, element);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1368
                expectedArray = getArray();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1369
                size++;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1370
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1371
            return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1372
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1373
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        public void add(int index, E element) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1375
            synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                checkForComodification();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1377
                rangeCheckForAdd(index);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1378
                CopyOnWriteArrayList.this.add(offset + index, element);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1379
                expectedArray = getArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1384
        public boolean addAll(Collection<? extends E> c) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1385
            synchronized (lock) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1386
                final Object[] oldArray = getArrayChecked();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1387
                boolean modified =
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1388
                    CopyOnWriteArrayList.this.addAll(offset + size, c);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1389
                size += (expectedArray = getArray()).length - oldArray.length;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1390
                return modified;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1391
            }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1392
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1393
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1394
        public boolean addAll(int index, Collection<? extends E> c) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1395
            synchronized (lock) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1396
                rangeCheckForAdd(index);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1397
                final Object[] oldArray = getArrayChecked();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1398
                boolean modified =
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1399
                    CopyOnWriteArrayList.this.addAll(offset + index, c);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1400
                size += (expectedArray = getArray()).length - oldArray.length;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1401
                return modified;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1402
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1403
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1404
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        public void clear() {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1406
            synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                checkForComodification();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1408
                removeRange(offset, offset + size);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1409
                expectedArray = getArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        public E remove(int index) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1415
            synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                checkForComodification();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1418
                E result = CopyOnWriteArrayList.this.remove(offset + index);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1419
                expectedArray = getArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        public boolean remove(Object o) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1426
            synchronized (lock) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1427
                checkForComodification();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1428
                int index = indexOf(o);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1429
                if (index == -1)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1430
                    return false;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1431
                remove(index);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1432
                return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1433
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        public Iterator<E> iterator() {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1437
            return listIterator(0);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1438
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1439
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1440
        public ListIterator<E> listIterator() {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1441
            return listIterator(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1444
        public ListIterator<E> listIterator(int index) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1445
            synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                checkForComodification();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1447
                rangeCheckForAdd(index);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1448
                return new COWSubListIterator<E>(
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1449
                    CopyOnWriteArrayList.this, index, offset, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        public List<E> subList(int fromIndex, int toIndex) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1454
            synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                checkForComodification();
22628
9186623f7045 8011645: CopyOnWriteArrayList.COWSubList.subList does not validate range properly
chegar
parents: 19428
diff changeset
  1456
                if (fromIndex < 0 || toIndex > size || fromIndex > toIndex)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                    throw new IndexOutOfBoundsException();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1458
                return new COWSubList(expectedArray, fromIndex + offset, toIndex - fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1462
        public void forEach(Consumer<? super E> action) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1463
            Objects.requireNonNull(action);
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1464
            int i, end; final Object[] es;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1465
            synchronized (lock) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1466
                es = getArrayChecked();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1467
                i = offset;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1468
                end = i + size;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1469
            }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1470
            for (; i < end; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1471
                action.accept(elementAt(es, i));
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1472
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1473
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1474
        public void replaceAll(UnaryOperator<E> operator) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1475
            synchronized (lock) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1476
                checkForComodification();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1477
                replaceAllRange(operator, offset, offset + size);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1478
                expectedArray = getArray();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1479
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1480
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1481
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1482
        public void sort(Comparator<? super E> c) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1483
            synchronized (lock) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1484
                checkForComodification();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1485
                sortRange(c, offset, offset + size);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1486
                expectedArray = getArray();
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1487
            }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1488
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1489
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1490
        public boolean removeAll(Collection<?> c) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1491
            Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1492
            return bulkRemove(e -> c.contains(e));
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1493
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1494
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1495
        public boolean retainAll(Collection<?> c) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1496
            Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1497
            return bulkRemove(e -> !c.contains(e));
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1498
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1499
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1500
        public boolean removeIf(Predicate<? super E> filter) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1501
            Objects.requireNonNull(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1502
            return bulkRemove(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1503
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1504
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1505
        private boolean bulkRemove(Predicate<? super E> filter) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1506
            synchronized (lock) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1507
                final Object[] oldArray = getArrayChecked();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1508
                boolean modified = CopyOnWriteArrayList.this.bulkRemove(
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1509
                    filter, offset, offset + size);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1510
                size += (expectedArray = getArray()).length - oldArray.length;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1511
                return modified;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1512
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1513
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1514
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1515
        public Spliterator<E> spliterator() {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1516
            synchronized (lock) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1517
                return Spliterators.spliterator(
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1518
                        getArrayChecked(), offset, offset + size,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1519
                        Spliterator.IMMUTABLE | Spliterator.ORDERED);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 39725
diff changeset
  1520
            }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1521
        }
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1522
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    private static class COWSubListIterator<E> implements ListIterator<E> {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1526
        private final ListIterator<E> it;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        private final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        private final int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1530
        COWSubListIterator(List<E> l, int index, int offset, int size) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            this.size = size;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1533
            it = l.listIterator(index + offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            return nextIndex() < size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            if (hasNext())
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1542
                return it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            return previousIndex() >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            if (hasPrevious())
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1553
                return it.previous();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        public int nextIndex() {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1559
            return it.nextIndex() - offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        public int previousIndex() {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1563
            return it.previousIndex() - offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        }
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1577
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1578
        @Override
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1579
        @SuppressWarnings("unchecked")
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1580
        public void forEachRemaining(Consumer<? super E> action) {
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1581
            Objects.requireNonNull(action);
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 49433
diff changeset
  1582
            while (hasNext()) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1583
                action.accept(it.next());
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1584
            }
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1585
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1588
    /** Initializes the lock; for use when deserializing or cloning. */
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1589
    private void resetLock() {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1590
        Field lockField = java.security.AccessController.doPrivileged(
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1591
            (java.security.PrivilegedAction<Field>) () -> {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1592
                try {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1593
                    Field f = CopyOnWriteArrayList.class
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1594
                        .getDeclaredField("lock");
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1595
                    f.setAccessible(true);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1596
                    return f;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1597
                } catch (ReflectiveOperationException e) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1598
                    throw new Error(e);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1599
                }});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        try {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1601
            lockField.set(this, new Object());
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1602
        } catch (IllegalAccessException e) {
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1603
            throw new Error(e);
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1604
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
}