jdk/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java
author chegar
Wed, 11 Nov 2015 09:19:12 +0000
changeset 33674 566777f73c32
parent 32991 b27c76b82713
child 39725 9548f8d846e9
permissions -rw-r--r--
8140606: Update library code to use internal Unsafe Reviewed-by: alanb, mchung, psandoz, weijun
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
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    37
import java.util.AbstractList;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    38
import java.util.Arrays;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    39
import java.util.Collection;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    40
import java.util.Comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    41
import java.util.ConcurrentModificationException;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    42
import java.util.Iterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    43
import java.util.List;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    44
import java.util.ListIterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    45
import java.util.NoSuchElementException;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    46
import java.util.Objects;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    47
import java.util.RandomAccess;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    48
import java.util.Spliterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    49
import java.util.Spliterators;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
    50
import java.util.function.Consumer;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
    51
import java.util.function.Predicate;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
    52
import java.util.function.UnaryOperator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * 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
    56
 * operations ({@code add}, {@code set}, and so on) are implemented by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * making a fresh copy of the underlying array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    59
 * <p>This is ordinarily too costly, but may be <em>more</em> efficient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * than alternatives when traversal operations vastly outnumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * mutations, and is useful when you cannot or don't want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * synchronize traversals, yet need to preclude interference among
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * concurrent threads.  The "snapshot" style iterator method uses a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * reference to the state of the array at the point that the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * was created. This array never changes during the lifetime of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * iterator, so interference is impossible and the iterator is
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    67
 * guaranteed not to throw {@code ConcurrentModificationException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * The iterator will not reflect additions, removals, or changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * the list since the iterator was created.  Element-changing
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    70
 * operations on iterators themselves ({@code remove}, {@code set}, and
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    71
 * {@code add}) are not supported. These methods throw
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    72
 * {@code UnsupportedOperationException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    74
 * <p>All elements are permitted, including {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>Memory consistency effects: As with other concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * collections, actions in a thread prior to placing an object into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * {@code CopyOnWriteArrayList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * actions subsequent to the access or removal of that element from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * the {@code CopyOnWriteArrayList} in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @author Doug Lea
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
    89
 * @param <E> the type of elements held in this list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
public class CopyOnWriteArrayList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final long serialVersionUID = 8673264195747942595L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
    95
    /**
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
    96
     * The lock protecting all mutators.  (We have a mild preference
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
    97
     * for builtin monitors over ReentrantLock when either will do.)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
    98
     */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
    99
    final transient Object lock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /** The array, accessed only via getArray/setArray. */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   102
    private transient volatile Object[] array;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Gets the array.  Non-private so as to also be accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * from CopyOnWriteArraySet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    final Object[] getArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Sets the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    final void setArray(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        array = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Creates an empty list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public CopyOnWriteArrayList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        setArray(new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Creates a list containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param c the collection of initially held elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public CopyOnWriteArrayList(Collection<? extends E> c) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   135
        Object[] elements;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   136
        if (c.getClass() == CopyOnWriteArrayList.class)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   137
            elements = ((CopyOnWriteArrayList<?>)c).getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   138
        else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   139
            elements = c.toArray();
31540
6efd719b3330 6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents: 25859
diff changeset
   140
            // 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
   141
            // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   142
            if (elements.getClass() != Object[].class)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   143
                elements = Arrays.copyOf(elements, elements.length, Object[].class);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   144
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        setArray(elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Creates a list holding a copy of the given array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param toCopyIn the array (a copy of this array is used as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *        internal array)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public CopyOnWriteArrayList(E[] toCopyIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        setArray(Arrays.copyOf(toCopyIn, toCopyIn.length, Object[].class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Returns the number of elements in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return the number of elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return getArray().length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   169
     * Returns {@code true} if this list contains no elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   171
     * @return {@code true} if this list contains no elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return size() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * static version of indexOf, to allow repeated calls without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * needing to re-acquire array each time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param elements the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param index first index to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param fence one past last index to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @return index of element, or -1 if absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private static int indexOf(Object o, Object[] elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                               int index, int fence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            for (int i = index; i < fence; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if (elements[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            for (int i = index; i < fence; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                if (o.equals(elements[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * static version of lastIndexOf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param elements the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param index first index to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return index of element, or -1 if absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private static int lastIndexOf(Object o, Object[] elements, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            for (int i = index; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if (elements[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            for (int i = index; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (o.equals(elements[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   221
     * Returns {@code true} if this list contains the specified element.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   222
     * 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
   223
     * at least one element {@code e} such that {@code Objects.equals(o, e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @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
   226
     * @return {@code true} if this list contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return indexOf(o, elements, 0, elements.length) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return indexOf(o, elements, 0, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * 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
   243
     * this list, searching forwards from {@code index}, or returns -1 if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * the element is not found.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   245
     * 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
   246
     * {@code i >= index && Objects.equals(get(i), e)},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param e element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param index index to start searching from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @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
   252
     *         this list at position {@code index} or later in the list;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   253
     *         {@code -1} if the element is not found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public int indexOf(E e, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return indexOf(e, elements, index, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public int lastIndexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return lastIndexOf(o, elements, elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Returns 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
   271
     * this list, searching backwards from {@code index}, or returns -1 if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * the element is not found.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   273
     * 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
   274
     * {@code i <= index && Objects.equals(get(i), e)},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param e element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param index index to start searching backwards from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @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
   280
     *         less than or equal to {@code index} in this list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *         -1 if the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @throws IndexOutOfBoundsException if the specified index is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *         than or equal to the current size of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public int lastIndexOf(E e, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return lastIndexOf(e, elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns a shallow copy of this list.  (The elements themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * are not copied.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return a clone of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        try {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   298
            @SuppressWarnings("unchecked")
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   299
            CopyOnWriteArrayList<E> clone =
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   300
                (CopyOnWriteArrayList<E>) super.clone();
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   301
            clone.resetLock();
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   302
            return clone;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // this shouldn't happen, since we are Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Returns an array containing all of the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * in proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * maintained by this list.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return an array containing all the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return Arrays.copyOf(elements, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Returns an array containing all of the elements in this list in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * proper sequence (from first to last element); the runtime type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * the returned array is that of the specified array.  If the list fits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * in the specified array, it is returned therein.  Otherwise, a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * array is allocated with the runtime type of the specified array and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * the size of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <p>If this list fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * (i.e., the array has more elements than this list), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * 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
   338
     * {@code null}.  (This is useful in determining the length of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * list <i>only</i> if the caller knows that this list does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * any null elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   347
     * <p>Suppose {@code x} is a list known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * 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
   349
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   351
     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   353
     * 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
   354
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param a the array into which the elements of the list are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *          same runtime type is allocated for this purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @return an array containing all the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *         this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    @SuppressWarnings("unchecked")
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   366
    public <T> T[] toArray(T[] a) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (a.length < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return (T[]) Arrays.copyOf(elements, len, a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            System.arraycopy(elements, 0, a, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (a.length > len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                a[len] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    // Positional Access Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    private E get(Object[] a, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return (E) a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   386
    static String outOfBounds(int index, int size) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   387
        return "Index: " + index + ", Size: " + size;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   388
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   389
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return get(getArray(), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Replaces the element at the specified position in this list with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public E set(int index, E element) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   406
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            E oldValue = get(elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (oldValue != element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                Object[] newElements = Arrays.copyOf(elements, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                newElements[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                // Not quite a no-op; ensures volatile write semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                setArray(elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            return oldValue;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Appends the specified element to the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param e element to be appended to this list
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   427
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public boolean add(E e) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   430
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            Object[] newElements = Arrays.copyOf(elements, len + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            newElements[len] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return true;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Inserts the specified element at the specified position in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * list. Shifts the element currently at that position (if any) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * any subsequent elements to the right (adds one to their indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public void add(int index, E element) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   448
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            if (index > len || index < 0)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   452
                throw new IndexOutOfBoundsException(outOfBounds(index, len));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            Object[] newElements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            int numMoved = len - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (numMoved == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                newElements = Arrays.copyOf(elements, len + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                newElements = new Object[len + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                System.arraycopy(elements, 0, newElements, 0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                System.arraycopy(elements, index, newElements, index + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            newElements[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            setArray(newElements);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Removes the element at the specified position in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Shifts any subsequent elements to the left (subtracts one from their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * indices).  Returns the element that was removed from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public E remove(int index) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   476
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            E oldValue = get(elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            int numMoved = len - index - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (numMoved == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                setArray(Arrays.copyOf(elements, len - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                Object[] newElements = new Object[len - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                System.arraycopy(elements, 0, newElements, 0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                System.arraycopy(elements, index + 1, newElements, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
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();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   508
        int index = indexOf(o, snapshot, 0, snapshot.length);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   509
        return (index < 0) ? false : remove(o, snapshot, index);
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;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   533
                index = indexOf(o, current, index, len);
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) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            int len = elements.length;
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)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                setArray(Arrays.copyOf(elements, newlen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                Object[] newElements = new Object[newlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                System.arraycopy(elements, 0, newElements, 0, fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                System.arraycopy(elements, toIndex, newElements,
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();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   588
        return indexOf(e, snapshot, 0, snapshot.length) >= 0 ? false :
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   589
            addIfAbsent(e, snapshot);
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;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   607
                if (indexOf(e, current, common, len) >= 0)
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        for (Object e : c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            if (indexOf(e, elements, 0, len) < 0)
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
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   646
     * (<a href="{@docRoot}/../api/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
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   649
     * (<a href="{@docRoot}/../api/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) {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   654
        if (c == null) throw new NullPointerException();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   655
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                // temp array holds those elements we know we want to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                int newlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                Object[] temp = new Object[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                for (int i = 0; i < len; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    Object element = elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    if (!c.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        temp[newlen++] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                if (newlen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    setArray(Arrays.copyOf(temp, newlen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Retains only the elements in this list that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * specified collection.  In other words, removes from this list all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * its elements that are not contained in the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @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
   682
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @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
   684
     *         is incompatible with the specified collection
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   685
     * (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @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
   687
     *         specified collection does not permit null elements
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   688
     * (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    public boolean retainAll(Collection<?> c) {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   693
        if (c == null) throw new NullPointerException();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   694
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                // temp array holds those elements we know we want to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                int newlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                Object[] temp = new Object[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                for (int i = 0; i < len; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    Object element = elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    if (c.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        temp[newlen++] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                if (newlen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    setArray(Arrays.copyOf(temp, newlen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            return false;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Appends all of the elements in the specified collection that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * are not already contained in this list, to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * this list, in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * specified collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @param c collection containing elements to be added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @return the number of elements added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @see #addIfAbsent(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    public int addAllAbsent(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        Object[] cs = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            return 0;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   730
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            int added = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   734
            // uniquify and compact elements in cs
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   735
            for (int i = 0; i < cs.length; ++i) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                Object e = cs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                if (indexOf(e, elements, 0, len) < 0 &&
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   738
                    indexOf(e, cs, 0, added) < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   739
                    cs[added++] = e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            if (added > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                Object[] newElements = Arrays.copyOf(elements, len + added);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   743
                System.arraycopy(cs, 0, newElements, len, added);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            return added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Removes all of the elements from this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * The list will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    public void clear() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   755
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            setArray(new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * Appends all of the elements in the specified collection to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * of this list, in the order that they are returned by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @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
   766
     * @return {@code true} if this list changed as a result of the call
2
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(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    public boolean addAll(Collection<? extends E> c) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   771
        Object[] cs = (c.getClass() == CopyOnWriteArrayList.class) ?
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   772
            ((CopyOnWriteArrayList<?>)c).getArray() : c.toArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            return false;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   775
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            int len = elements.length;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   778
            if (len == 0 && cs.getClass() == Object[].class)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   779
                setArray(cs);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   780
            else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   781
                Object[] newElements = Arrays.copyOf(elements, len + cs.length);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   782
                System.arraycopy(cs, 0, newElements, len, cs.length);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   783
                setArray(newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   784
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * Inserts all of the elements in the specified collection into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * list, starting at the specified position.  Shifts the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * currently at that position (if any) and any subsequent elements to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * the right (increases their indices).  The new elements will appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * in this list in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * specified collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @param index index at which to insert the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *        from the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @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
   800
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @see #add(int,Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        Object[] cs = c.toArray();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   807
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            if (index > len || index < 0)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   811
                throw new IndexOutOfBoundsException(outOfBounds(index, len));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            int numMoved = len - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            Object[] newElements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            if (numMoved == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                newElements = Arrays.copyOf(elements, len + cs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                newElements = new Object[len + cs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                System.arraycopy(elements, 0, newElements, 0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                System.arraycopy(elements, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                                 newElements, index + cs.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            System.arraycopy(cs, 0, newElements, index, cs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   831
    public void forEach(Consumer<? super E> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   832
        if (action == null) throw new NullPointerException();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   833
        for (Object x : getArray()) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   834
            @SuppressWarnings("unchecked") E e = (E) x;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   835
            action.accept(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   836
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   837
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   838
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   839
    public boolean removeIf(Predicate<? super E> filter) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   840
        if (filter == null) throw new NullPointerException();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   841
        synchronized (lock) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   842
            final Object[] elements = getArray();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   843
            final int len = elements.length;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   844
            int i;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   845
            for (i = 0; i < len; i++) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   846
                @SuppressWarnings("unchecked") E e = (E) elements[i];
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   847
                if (filter.test(e)) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   848
                    int newlen = i;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   849
                    final Object[] newElements = new Object[len - 1];
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   850
                    System.arraycopy(elements, 0, newElements, 0, newlen);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   851
                    for (i++; i < len; i++) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   852
                        @SuppressWarnings("unchecked") E x = (E) elements[i];
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   853
                        if (!filter.test(x))
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   854
                            newElements[newlen++] = x;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   855
                    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   856
                    setArray((newlen == len - 1)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   857
                             ? newElements // one match => one copy
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   858
                             : Arrays.copyOf(newElements, newlen));
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   859
                    return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   860
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   861
            }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   862
            return false;       // zero matches => zero copies
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   863
        }
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
    public void replaceAll(UnaryOperator<E> operator) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   867
        if (operator == null) throw new NullPointerException();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   868
        synchronized (lock) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   869
            Object[] elements = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   870
            int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   871
            Object[] newElements = Arrays.copyOf(elements, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   872
            for (int i = 0; i < len; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   873
                @SuppressWarnings("unchecked") E e = (E) elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   874
                newElements[i] = operator.apply(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   875
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   876
            setArray(newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   877
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   878
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   879
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   880
    public void sort(Comparator<? super E> c) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   881
        synchronized (lock) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   882
            Object[] elements = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   883
            Object[] newElements = Arrays.copyOf(elements, elements.length);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   884
            @SuppressWarnings("unchecked") E[] es = (E[])newElements;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   885
            Arrays.sort(es, c);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   886
            setArray(newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   887
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   888
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   889
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   891
     * Saves this list to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   893
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   894
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @serialData The length of the array backing the list is emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     *               (int), followed by all of its elements (each an Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *               in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    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
   900
        throws java.io.IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        // Write out array length
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
   906
        s.writeInt(elements.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        // Write out all elements in the proper order.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
   909
        for (Object element : elements)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
   910
            s.writeObject(element);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   914
     * 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
   915
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   916
     * @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
   917
     *         could not be found
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   918
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        // bind to new lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        resetLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        // Read in array length and allocate array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        int len = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        Object[] elements = new Object[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        // Read in all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        for (int i = 0; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            elements[i] = s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        setArray(elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Returns a string representation of this list.  The string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * representation consists of the string representations of the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * 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
   942
     * square brackets ({@code "[]"}).  Adjacent elements are separated by
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   943
     * the characters {@code ", "} (comma and space).  Elements are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * converted to strings as by {@link String#valueOf(Object)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @return a string representation of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        return Arrays.toString(getArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * Compares the specified object with this list for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * Returns {@code true} if the specified object is the same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * as this object, or if it is also a {@link List} and the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * of elements returned by an {@linkplain List#iterator() iterator}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * over the specified list is the same as the sequence returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * an iterator over this list.  The two sequences are considered to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * be the same if they have the same length and corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * elements at the same position in the sequence are <em>equal</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Two elements {@code e1} and {@code e2} are considered
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   962
     * <em>equal</em> if {@code Objects.equals(e1, e2)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @param o the object to be compared for equality with this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @return {@code true} if the specified object is equal to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        if (!(o instanceof List))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   973
        List<?> list = (List<?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        Iterator<?> it = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        Object[] elements = getArray();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   976
        for (int i = 0, len = elements.length; i < len; i++)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   977
            if (!it.hasNext() || !Objects.equals(elements[i], it.next()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        if (it.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * Returns the hash code value for this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * <p>This implementation uses the definition in {@link List#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @return the hash code value for this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        int hashCode = 1;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   993
        for (Object x : getArray())
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   994
            hashCode = 31 * hashCode + (x == null ? 0 : x.hashCode());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * Returns an iterator over the elements in this list in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * 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
  1004
     * {@code remove} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @return an iterator over the elements in this list in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        return new COWIterator<E>(getArray(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * 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
  1018
     * {@code remove}, {@code set} or {@code add} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        return new COWIterator<E>(getArray(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * 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
  1030
     * {@code remove}, {@code set} or {@code add} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1034
    public ListIterator<E> listIterator(int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        int len = elements.length;
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1037
        if (index < 0 || index > len)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1038
            throw new IndexOutOfBoundsException(outOfBounds(index, len));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        return new COWIterator<E>(elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1043
    /**
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1044
     * 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
  1045
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1046
     * <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
  1047
     * {@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
  1048
     * {@link Spliterator#SUBSIZED}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1049
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1050
     * <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
  1051
     * 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
  1052
     * operating on the spliterator.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1053
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1054
     * @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
  1055
     * @since 1.8
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1056
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1057
    public Spliterator<E> spliterator() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1058
        return Spliterators.spliterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1059
            (getArray(), Spliterator.IMMUTABLE | Spliterator.ORDERED);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1060
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1061
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1062
    static final class COWIterator<E> implements ListIterator<E> {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1063
        /** Snapshot of the array */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        private final Object[] snapshot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        /** Index of element to be returned by subsequent call to next.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        private int cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1068
        COWIterator(Object[] elements, int initialCursor) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            cursor = initialCursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            snapshot = elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            return cursor < snapshot.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            return cursor > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            if (! hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            return (E) snapshot[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            if (! hasPrevious())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            return (E) snapshot[--cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            return 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
        public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            return cursor-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
         * Not supported. Always throws UnsupportedOperationException.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1105
         * @throws UnsupportedOperationException always; {@code remove}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
         * Not supported. Always throws UnsupportedOperationException.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1114
         * @throws UnsupportedOperationException always; {@code set}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
         * Not supported. Always throws UnsupportedOperationException.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1123
         * @throws UnsupportedOperationException always; {@code add}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1129
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1130
        @Override
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1131
        @SuppressWarnings("unchecked")
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1132
        public void forEachRemaining(Consumer<? super E> action) {
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1133
            Objects.requireNonNull(action);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1134
            final int size = snapshot.length;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1135
            for (int i = cursor; i < size; i++) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1136
                action.accept((E) snapshot[i]);
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1137
            }
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1138
            cursor = size;
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1139
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * Returns a view of the portion of this list between
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1144
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * The returned list is backed by this list, so changes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * returned list are reflected in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * <p>The semantics of the list returned by this method become
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * undefined if the backing list (i.e., this list) is modified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * any way other than via the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * @param fromIndex low endpoint (inclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * @param toIndex high endpoint (exclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * @return a view of the specified range within this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    public List<E> subList(int fromIndex, int toIndex) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1158
        synchronized (lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            if (fromIndex < 0 || toIndex > len || fromIndex > toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            return new COWSubList<E>(this, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * Sublist for CopyOnWriteArrayList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * This class extends AbstractList merely for convenience, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * avoid having to define addAll, etc. This doesn't hurt, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * is wasteful.  This class does not need or use modCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * mechanics in AbstractList, but does need to check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * concurrent modification using similar mechanics.  On each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * operation, the array that we expect the backing list to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * is checked and updated.  Since we do this for all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * base operations invoked by those defined in AbstractList,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * all is well.  While inefficient, this is not worth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * improving.  The kinds of list operations inherited from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * AbstractList are already so slow on COW sublists that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * adding a bit more space/time doesn't seem even noticeable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    private static class COWSubList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        implements RandomAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        private final CopyOnWriteArrayList<E> l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        private final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        private Object[] expectedArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        // only call this holding l's lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        COWSubList(CopyOnWriteArrayList<E> list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                   int fromIndex, int toIndex) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1194
            // assert Thread.holdsLock(list.lock);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            l = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            offset = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            size = toIndex - fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        // only call this holding l's lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        private void checkForComodification() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1203
            // assert Thread.holdsLock(l.lock);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            if (l.getArray() != expectedArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        // only call this holding l's lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        private void rangeCheck(int index) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1210
            // assert Thread.holdsLock(l.lock);
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1211
            if (index < 0 || index >= size)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1212
                throw new IndexOutOfBoundsException(outOfBounds(index, size));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        public E set(int index, E element) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1216
            synchronized (l.lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                E x = l.set(index+offset, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        public E get(int index) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1226
            synchronized (l.lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                return l.get(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        public int size() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1234
            synchronized (l.lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        public void add(int index, E element) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1241
            synchronized (l.lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                checkForComodification();
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1243
                if (index < 0 || index > size)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1244
                    throw new IndexOutOfBoundsException
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1245
                        (outOfBounds(index, size));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                l.add(index+offset, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        public void clear() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1253
            synchronized (l.lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                l.removeRange(offset, offset+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        public E remove(int index) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1262
            synchronized (l.lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                E result = l.remove(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            int index = indexOf(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            if (index == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        public Iterator<E> iterator() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1281
            synchronized (l.lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                return new COWSubListIterator<E>(l, 0, offset, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1287
        public ListIterator<E> listIterator(int index) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1288
            synchronized (l.lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                checkForComodification();
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1290
                if (index < 0 || index > size)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1291
                    throw new IndexOutOfBoundsException
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1292
                        (outOfBounds(index, size));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                return new COWSubListIterator<E>(l, index, offset, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        public List<E> subList(int fromIndex, int toIndex) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1298
            synchronized (l.lock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                checkForComodification();
22628
9186623f7045 8011645: CopyOnWriteArrayList.COWSubList.subList does not validate range properly
chegar
parents: 19428
diff changeset
  1300
                if (fromIndex < 0 || toIndex > size || fromIndex > toIndex)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                    throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                return new COWSubList<E>(l, fromIndex + offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                                         toIndex + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1307
        public void forEach(Consumer<? super E> action) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1308
            if (action == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1309
            int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1310
            int hi = offset + size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1311
            Object[] a = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1312
            if (l.getArray() != a)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1313
                throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1314
            if (lo < 0 || hi > a.length)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1315
                throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1316
            for (int i = lo; i < hi; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1317
                @SuppressWarnings("unchecked") E e = (E) a[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1318
                action.accept(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1319
            }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1320
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1321
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1322
        public void replaceAll(UnaryOperator<E> operator) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1323
            if (operator == null) throw new NullPointerException();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1324
            synchronized (l.lock) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1325
                int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1326
                int hi = offset + size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1327
                Object[] elements = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1328
                if (l.getArray() != elements)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1329
                    throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1330
                int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1331
                if (lo < 0 || hi > len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1332
                    throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1333
                Object[] newElements = Arrays.copyOf(elements, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1334
                for (int i = lo; i < hi; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1335
                    @SuppressWarnings("unchecked") E e = (E) elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1336
                    newElements[i] = operator.apply(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1337
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1338
                l.setArray(expectedArray = newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1339
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1340
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1341
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1342
        public void sort(Comparator<? super E> c) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1343
            synchronized (l.lock) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1344
                int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1345
                int hi = offset + size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1346
                Object[] elements = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1347
                if (l.getArray() != elements)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1348
                    throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1349
                int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1350
                if (lo < 0 || hi > len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1351
                    throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1352
                Object[] newElements = Arrays.copyOf(elements, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1353
                @SuppressWarnings("unchecked") E[] es = (E[])newElements;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1354
                Arrays.sort(es, lo, hi, c);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1355
                l.setArray(expectedArray = newElements);
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1356
            }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1357
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1358
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1359
        public boolean removeAll(Collection<?> c) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1360
            if (c == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1361
            boolean removed = false;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1362
            synchronized (l.lock) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1363
                int n = size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1364
                if (n > 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1365
                    int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1366
                    int hi = offset + n;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1367
                    Object[] elements = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1368
                    if (l.getArray() != elements)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1369
                        throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1370
                    int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1371
                    if (lo < 0 || hi > len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1372
                        throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1373
                    int newSize = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1374
                    Object[] temp = new Object[n];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1375
                    for (int i = lo; i < hi; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1376
                        Object element = elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1377
                        if (!c.contains(element))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1378
                            temp[newSize++] = element;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1379
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1380
                    if (newSize != n) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1381
                        Object[] newElements = new Object[len - n + newSize];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1382
                        System.arraycopy(elements, 0, newElements, 0, lo);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1383
                        System.arraycopy(temp, 0, newElements, lo, newSize);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1384
                        System.arraycopy(elements, hi, newElements,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1385
                                         lo + newSize, len - hi);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1386
                        size = newSize;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1387
                        removed = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1388
                        l.setArray(expectedArray = newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1389
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1390
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1391
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1392
            return removed;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1393
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1394
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1395
        public boolean retainAll(Collection<?> c) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1396
            if (c == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1397
            boolean removed = false;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1398
            synchronized (l.lock) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1399
                int n = size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1400
                if (n > 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1401
                    int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1402
                    int hi = offset + n;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1403
                    Object[] elements = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1404
                    if (l.getArray() != elements)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1405
                        throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1406
                    int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1407
                    if (lo < 0 || hi > len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1408
                        throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1409
                    int newSize = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1410
                    Object[] temp = new Object[n];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1411
                    for (int i = lo; i < hi; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1412
                        Object element = elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1413
                        if (c.contains(element))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1414
                            temp[newSize++] = element;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1415
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1416
                    if (newSize != n) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1417
                        Object[] newElements = new Object[len - n + newSize];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1418
                        System.arraycopy(elements, 0, newElements, 0, lo);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1419
                        System.arraycopy(temp, 0, newElements, lo, newSize);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1420
                        System.arraycopy(elements, hi, newElements,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1421
                                         lo + newSize, len - hi);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1422
                        size = newSize;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1423
                        removed = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1424
                        l.setArray(expectedArray = newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1425
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1426
                }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1427
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1428
            return removed;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1429
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1430
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1431
        public boolean removeIf(Predicate<? super E> filter) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1432
            if (filter == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1433
            boolean removed = false;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1434
            synchronized (l.lock) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1435
                int n = size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1436
                if (n > 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1437
                    int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1438
                    int hi = offset + n;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1439
                    Object[] elements = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1440
                    if (l.getArray() != elements)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1441
                        throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1442
                    int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1443
                    if (lo < 0 || hi > len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1444
                        throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1445
                    int newSize = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1446
                    Object[] temp = new Object[n];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1447
                    for (int i = lo; i < hi; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1448
                        @SuppressWarnings("unchecked") E e = (E) elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1449
                        if (!filter.test(e))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1450
                            temp[newSize++] = e;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1451
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1452
                    if (newSize != n) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1453
                        Object[] newElements = new Object[len - n + newSize];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1454
                        System.arraycopy(elements, 0, newElements, 0, lo);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1455
                        System.arraycopy(temp, 0, newElements, lo, newSize);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1456
                        System.arraycopy(elements, hi, newElements,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1457
                                         lo + newSize, len - hi);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1458
                        size = newSize;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1459
                        removed = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1460
                        l.setArray(expectedArray = newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1461
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1462
                }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1463
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1464
            return removed;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1465
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1466
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1467
        public Spliterator<E> spliterator() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1468
            int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1469
            int hi = offset + size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1470
            Object[] a = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1471
            if (l.getArray() != a)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1472
                throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1473
            if (lo < 0 || hi > a.length)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1474
                throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1475
            return Spliterators.spliterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1476
                (a, lo, hi, Spliterator.IMMUTABLE | Spliterator.ORDERED);
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1477
        }
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1478
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    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
  1482
        private final ListIterator<E> it;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        private final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        private final int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1486
        COWSubListIterator(List<E> l, int index, int offset, int size) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            this.size = size;
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1489
            it = l.listIterator(index+offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            return nextIndex() < size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            if (hasNext())
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1498
                return it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            return previousIndex() >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            if (hasPrevious())
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1509
                return it.previous();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        public int nextIndex() {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1515
            return it.nextIndex() - offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        public int previousIndex() {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1519
            return it.previousIndex() - offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        }
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1533
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1534
        @Override
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1535
        @SuppressWarnings("unchecked")
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1536
        public void forEachRemaining(Consumer<? super E> action) {
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1537
            Objects.requireNonNull(action);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1538
            while (nextIndex() < size) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1539
                action.accept(it.next());
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1540
            }
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1541
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    // Support for resetting lock while deserializing
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1545
    private void resetLock() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1546
        U.putObjectVolatile(this, LOCK, new Object());
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1547
    }
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32991
diff changeset
  1548
    private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1549
    private static final long LOCK;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        try {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1552
            LOCK = U.objectFieldOffset
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1553
                (CopyOnWriteArrayList.class.getDeclaredField("lock"));
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1554
        } catch (ReflectiveOperationException e) {
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1555
            throw new Error(e);
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1556
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
}