jdk/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java
author chegar
Fri, 31 Jan 2014 18:32:07 +0000
changeset 22628 9186623f7045
parent 19428 83f87aff7b07
permissions -rw-r--r--
8011645: CopyOnWriteArrayList.COWSubList.subList does not validate range properly Reviewed-by: martin, psandoz
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;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    36
import java.util.AbstractList;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    37
import java.util.Arrays;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    38
import java.util.Collection;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    39
import java.util.Comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    40
import java.util.ConcurrentModificationException;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    41
import java.util.Iterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    42
import java.util.List;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    43
import java.util.ListIterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    44
import java.util.NoSuchElementException;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    45
import java.util.Objects;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    46
import java.util.RandomAccess;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    47
import java.util.Spliterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    48
import java.util.Spliterators;
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
    49
import java.util.concurrent.locks.ReentrantLock;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @param <E> the type of elements held in this collection
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /** The lock protecting all mutators */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    96
    final transient ReentrantLock lock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /** The array, accessed only via getArray/setArray. */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
    99
    private transient volatile Object[] array;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Gets the array.  Non-private so as to also be accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * from CopyOnWriteArraySet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    final Object[] getArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Sets the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    final void setArray(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        array = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Creates an empty list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public CopyOnWriteArrayList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        setArray(new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Creates a list containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param c the collection of initially held elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public CopyOnWriteArrayList(Collection<? extends E> c) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   132
        Object[] elements;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   133
        if (c.getClass() == CopyOnWriteArrayList.class)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   134
            elements = ((CopyOnWriteArrayList<?>)c).getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   135
        else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   136
            elements = c.toArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   137
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   138
            if (elements.getClass() != Object[].class)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   139
                elements = Arrays.copyOf(elements, elements.length, Object[].class);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   140
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        setArray(elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Creates a list holding a copy of the given array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param toCopyIn the array (a copy of this array is used as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *        internal array)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public CopyOnWriteArrayList(E[] toCopyIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        setArray(Arrays.copyOf(toCopyIn, toCopyIn.length, Object[].class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Returns the number of elements in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @return the number of elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return getArray().length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   165
     * Returns {@code true} if this list contains no elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   167
     * @return {@code true} if this list contains no elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return size() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   174
     * Tests for equality, coping with nulls.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static boolean eq(Object o1, Object o2) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   177
        return (o1 == null) ? o2 == null : o1.equals(o2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * static version of indexOf, to allow repeated calls without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * needing to re-acquire array each time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param elements the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param index first index to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param fence one past last index to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return index of element, or -1 if absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static int indexOf(Object o, Object[] elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                               int index, int fence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            for (int i = index; i < fence; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (elements[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            for (int i = index; i < fence; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (o.equals(elements[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * static version of lastIndexOf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param elements the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param index first index to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return index of element, or -1 if absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private static int lastIndexOf(Object o, Object[] elements, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            for (int i = index; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (elements[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            for (int i = index; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                if (o.equals(elements[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   224
     * Returns {@code true} if this list contains the specified element.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   225
     * More formally, returns {@code true} if and only if this list contains
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   226
     * at least one element {@code e} such that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @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
   230
     * @return {@code true} if this list contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return indexOf(o, elements, 0, elements.length) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return indexOf(o, elements, 0, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * 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
   247
     * this list, searching forwards from {@code index}, or returns -1 if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * the element is not found.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   249
     * More formally, returns the lowest index {@code i} such that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <tt>(i&nbsp;&gt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(e==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;e.equals(get(i))))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param e element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param index index to start searching from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @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
   256
     *         this list at position {@code index} or later in the list;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   257
     *         {@code -1} if the element is not found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public int indexOf(E e, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return indexOf(e, elements, index, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public int lastIndexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return lastIndexOf(o, elements, elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * 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
   275
     * this list, searching backwards from {@code index}, or returns -1 if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * the element is not found.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   277
     * More formally, returns the highest index {@code i} such that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <tt>(i&nbsp;&lt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(e==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;e.equals(get(i))))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param e element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param index index to start searching backwards from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @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
   284
     *         less than or equal to {@code index} in this list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *         -1 if the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @throws IndexOutOfBoundsException if the specified index is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *         than or equal to the current size of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public int lastIndexOf(E e, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return lastIndexOf(e, elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Returns a shallow copy of this list.  (The elements themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * are not copied.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @return a clone of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        try {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   302
            @SuppressWarnings("unchecked")
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   303
            CopyOnWriteArrayList<E> clone =
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   304
                (CopyOnWriteArrayList<E>) super.clone();
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   305
            clone.resetLock();
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
   306
            return clone;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            // this shouldn't happen, since we are Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Returns an array containing all of the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * in proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * maintained by this list.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @return an array containing all the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return Arrays.copyOf(elements, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Returns an array containing all of the elements in this list in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * proper sequence (from first to last element); the runtime type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * the returned array is that of the specified array.  If the list fits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * in the specified array, it is returned therein.  Otherwise, a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * array is allocated with the runtime type of the specified array and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * the size of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <p>If this list fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * (i.e., the array has more elements than this list), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * 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
   342
     * {@code null}.  (This is useful in determining the length of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * list <i>only</i> if the caller knows that this list does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * any null elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   351
     * <p>Suppose {@code x} is a list known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * 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
   353
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   355
     *  <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   357
     * 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
   358
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param a the array into which the elements of the list are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *          same runtime type is allocated for this purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @return an array containing all the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *         this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public <T> T[] toArray(T a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (a.length < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return (T[]) Arrays.copyOf(elements, len, a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            System.arraycopy(elements, 0, a, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (a.length > len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                a[len] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    // Positional Access Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    private E get(Object[] a, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return (E) a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * {@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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            E oldValue = get(elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (oldValue != element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                Object[] newElements = Arrays.copyOf(elements, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                newElements[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                // Not quite a no-op; ensures volatile write semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                setArray(elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Appends the specified element to the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param e element to be appended to this list
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   431
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            Object[] newElements = Arrays.copyOf(elements, len + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            newElements[len] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * Inserts the specified element at the specified position in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * list. Shifts the element currently at that position (if any) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * any subsequent elements to the right (adds one to their indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (index > len || index < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                                    ", Size: "+len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            Object[] newElements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            int numMoved = len - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            if (numMoved == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                newElements = Arrays.copyOf(elements, len + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                newElements = new Object[len + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                System.arraycopy(elements, 0, newElements, 0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                System.arraycopy(elements, index, newElements, index + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            newElements[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Removes the element at the specified position in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Shifts any subsequent elements to the left (subtracts one from their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * indices).  Returns the element that was removed from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            E oldValue = get(elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            int numMoved = len - index - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            if (numMoved == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                setArray(Arrays.copyOf(elements, len - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                Object[] newElements = new Object[len - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                System.arraycopy(elements, 0, newElements, 0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                System.arraycopy(elements, index + 1, newElements, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * Removes the first occurrence of the specified element from this list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * if it is present.  If this list does not contain the element, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * unchanged.  More formally, removes the element with the lowest index
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   515
     * {@code i} such that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   517
     * (if such an element exists).  Returns {@code true} if this list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * contained the specified element (or equivalently, if this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @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
   522
     * @return {@code true} if this list contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    public boolean remove(Object o) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   525
        Object[] snapshot = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   526
        int index = indexOf(o, snapshot, 0, snapshot.length);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   527
        return (index < 0) ? false : remove(o, snapshot, index);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   528
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   529
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   530
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   531
     * A version of remove(Object) using the strong hint that given
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   532
     * recent snapshot contains o at the given index.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   533
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   534
    private boolean remove(Object o, Object[] snapshot, int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   538
            Object[] current = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   539
            int len = current.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   540
            if (snapshot != current) findIndex: {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   541
                int prefix = Math.min(index, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   542
                for (int i = 0; i < prefix; i++) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   543
                    if (current[i] != snapshot[i] && eq(o, current[i])) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   544
                        index = i;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   545
                        break findIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   546
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   548
                if (index >= len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   549
                    return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   550
                if (current[index] == o)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   551
                    break findIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   552
                index = indexOf(o, current, index, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   553
                if (index < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   554
                    return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   556
            Object[] newElements = new Object[len - 1];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   557
            System.arraycopy(current, 0, newElements, 0, index);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   558
            System.arraycopy(current, index + 1,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   559
                             newElements, index,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   560
                             len - index - 1);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   561
            setArray(newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   562
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * 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
   570
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Shifts any succeeding elements to the left (reduces their index).
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   572
     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   573
     * (If {@code toIndex==fromIndex}, this operation has no effect.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @param fromIndex index of first element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @param toIndex index after last element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @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
   578
     *         ({@code fromIndex < 0 || toIndex > size() || toIndex < fromIndex})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   580
    void removeRange(int fromIndex, int toIndex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            if (fromIndex < 0 || toIndex > len || toIndex < fromIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            int newlen = len - (toIndex - fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            int numMoved = len - toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            if (numMoved == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                setArray(Arrays.copyOf(elements, newlen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                Object[] newElements = new Object[newlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                System.arraycopy(elements, 0, newElements, 0, fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                System.arraycopy(elements, toIndex, newElements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                                 fromIndex, numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   606
     * Appends the element, if not present.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @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
   609
     * @return {@code true} if the element was added
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    public boolean addIfAbsent(E e) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   612
        Object[] snapshot = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   613
        return indexOf(e, snapshot, 0, snapshot.length) >= 0 ? false :
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   614
            addIfAbsent(e, snapshot);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   615
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   616
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   617
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   618
     * A version of addIfAbsent using the strong hint that given
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   619
     * recent snapshot does not contain e.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   620
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   621
    private boolean addIfAbsent(E e, Object[] snapshot) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   625
            Object[] current = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   626
            int len = current.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   627
            if (snapshot != current) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   628
                // Optimize for lost race to another addXXX operation
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   629
                int common = Math.min(snapshot.length, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   630
                for (int i = 0; i < common; i++)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   631
                    if (current[i] != snapshot[i] && eq(e, current[i]))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   632
                        return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   633
                if (indexOf(e, current, common, len) >= 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   634
                        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   636
            Object[] newElements = Arrays.copyOf(current, len + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            newElements[len] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   646
     * Returns {@code true} if this list contains all of the elements of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @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
   650
     * @return {@code true} if this list contains all of the elements of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *         specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @see #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    public boolean containsAll(Collection<?> c) {
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
        for (Object e : c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            if (indexOf(e, elements, 0, len) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * Removes from this list all of its elements that are contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * the specified collection. This is a particularly expensive operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * in this class because of the need for an internal temporary array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @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
   671
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @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
   673
     *         is incompatible with the specified collection
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9035
diff changeset
   674
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @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
   676
     *         specified collection does not permit null elements
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9035
diff changeset
   677
     *         (<a href="../Collection.html#optional-restrictions">optional</a>),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    public boolean removeAll(Collection<?> c) {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   682
        if (c == null) throw new NullPointerException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                // temp array holds those elements we know we want to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                int newlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                Object[] temp = new Object[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                for (int i = 0; i < len; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    Object element = elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                    if (!c.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                        temp[newlen++] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                if (newlen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    setArray(Arrays.copyOf(temp, newlen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * Retains only the elements in this list that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * specified collection.  In other words, removes from this list all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * its elements that are not contained in the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @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
   714
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @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
   716
     *         is incompatible with the specified collection
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9035
diff changeset
   717
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @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
   719
     *         specified collection does not permit null elements
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9035
diff changeset
   720
     *         (<a href="../Collection.html#optional-restrictions">optional</a>),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    public boolean retainAll(Collection<?> c) {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   725
        if (c == null) throw new NullPointerException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                // temp array holds those elements we know we want to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                int newlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                Object[] temp = new Object[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                for (int i = 0; i < len; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    Object element = elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    if (c.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                        temp[newlen++] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                if (newlen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    setArray(Arrays.copyOf(temp, newlen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            lock.unlock();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Appends all of the elements in the specified collection that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * are not already contained in this list, to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * this list, in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * specified collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @param c collection containing elements to be added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @return the number of elements added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * @see #addIfAbsent(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    public int addAllAbsent(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        Object[] cs = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            int added = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   772
            // uniquify and compact elements in cs
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   773
            for (int i = 0; i < cs.length; ++i) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                Object e = cs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                if (indexOf(e, elements, 0, len) < 0 &&
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   776
                    indexOf(e, cs, 0, added) < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   777
                    cs[added++] = e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            if (added > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                Object[] newElements = Arrays.copyOf(elements, len + added);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   781
                System.arraycopy(cs, 0, newElements, len, added);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            return added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            lock.unlock();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * Removes all of the elements from this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * The list will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            setArray(new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * Appends all of the elements in the specified collection to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * of this list, in the order that they are returned by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @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
   810
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @see #add(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    public boolean addAll(Collection<? extends E> c) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   815
        Object[] cs = (c.getClass() == CopyOnWriteArrayList.class) ?
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   816
            ((CopyOnWriteArrayList<?>)c).getArray() : c.toArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            int len = elements.length;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   824
            if (len == 0 && cs.getClass() == Object[].class)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   825
                setArray(cs);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   826
            else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   827
                Object[] newElements = Arrays.copyOf(elements, len + cs.length);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   828
                System.arraycopy(cs, 0, newElements, len, cs.length);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   829
                setArray(newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   830
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * Inserts all of the elements in the specified collection into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * list, starting at the specified position.  Shifts the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * currently at that position (if any) and any subsequent elements to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * the right (increases their indices).  The new elements will appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * in this list in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * specified collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @param index index at which to insert the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *        from the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * @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
   848
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @see #add(int,Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        Object[] cs = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            if (index > len || index < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                                                    ", Size: "+len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            int numMoved = len - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            Object[] newElements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            if (numMoved == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                newElements = Arrays.copyOf(elements, len + cs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                newElements = new Object[len + cs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                System.arraycopy(elements, 0, newElements, 0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                System.arraycopy(elements, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                                 newElements, index + cs.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            System.arraycopy(cs, 0, newElements, index, cs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   884
    public void forEach(Consumer<? super E> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   885
        if (action == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   886
        Object[] elements = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   887
        int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   888
        for (int i = 0; i < len; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   889
            @SuppressWarnings("unchecked") E e = (E) elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   890
            action.accept(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   891
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   892
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   893
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   894
    public boolean removeIf(Predicate<? super E> filter) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   895
        if (filter == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   896
        final ReentrantLock lock = this.lock;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   897
        lock.lock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   898
        try {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   899
            Object[] elements = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   900
            int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   901
            if (len != 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   902
                int newlen = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   903
                Object[] temp = new Object[len];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   904
                for (int i = 0; i < len; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   905
                    @SuppressWarnings("unchecked") E e = (E) elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   906
                    if (!filter.test(e))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   907
                        temp[newlen++] = e;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   908
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   909
                if (newlen != len) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   910
                    setArray(Arrays.copyOf(temp, newlen));
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   911
                    return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   912
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   913
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   914
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   915
        } finally {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   916
            lock.unlock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   917
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   918
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   919
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   920
    public void replaceAll(UnaryOperator<E> operator) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   921
        if (operator == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   922
        final ReentrantLock lock = this.lock;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   923
        lock.lock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   924
        try {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   925
            Object[] elements = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   926
            int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   927
            Object[] newElements = Arrays.copyOf(elements, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   928
            for (int i = 0; i < len; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   929
                @SuppressWarnings("unchecked") E e = (E) elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   930
                newElements[i] = operator.apply(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   931
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   932
            setArray(newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   933
        } finally {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   934
            lock.unlock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   935
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   936
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   937
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   938
    public void sort(Comparator<? super E> c) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   939
        final ReentrantLock lock = this.lock;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   940
        lock.lock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   941
        try {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   942
            Object[] elements = getArray();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   943
            Object[] newElements = Arrays.copyOf(elements, elements.length);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   944
            @SuppressWarnings("unchecked") E[] es = (E[])newElements;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   945
            Arrays.sort(es, c);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   946
            setArray(newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   947
        } finally {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   948
            lock.unlock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   949
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   950
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
   951
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   953
     * Saves this list to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   955
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   956
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @serialData The length of the array backing the list is emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *               (int), followed by all of its elements (each an Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *               in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    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
   962
        throws java.io.IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        // Write out array length
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
   968
        s.writeInt(elements.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        // 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
   971
        for (Object element : elements)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
   972
            s.writeObject(element);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   976
     * 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
   977
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   978
     * @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
   979
     *         could not be found
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   980
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        // bind to new lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        resetLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        // Read in array length and allocate array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        int len = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        Object[] elements = new Object[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        // Read in all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        for (int i = 0; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            elements[i] = s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        setArray(elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * Returns a string representation of this list.  The string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * representation consists of the string representations of the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * 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
  1004
     * square brackets ({@code "[]"}).  Adjacent elements are separated by
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1005
     * the characters {@code ", "} (comma and space).  Elements are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * converted to strings as by {@link String#valueOf(Object)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @return a string representation of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        return Arrays.toString(getArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * Compares the specified object with this list for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * Returns {@code true} if the specified object is the same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * as this object, or if it is also a {@link List} and the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * of elements returned by an {@linkplain List#iterator() iterator}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * over the specified list is the same as the sequence returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * an iterator over this list.  The two sequences are considered to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * be the same if they have the same length and corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * elements at the same position in the sequence are <em>equal</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * Two elements {@code e1} and {@code e2} are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * <em>equal</em> if {@code (e1==null ? e2==null : e1.equals(e2))}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * @param o the object to be compared for equality with this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * @return {@code true} if the specified object is equal to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        if (!(o instanceof List))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        List<?> list = (List<?>)(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        Iterator<?> it = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        for (int i = 0; i < len; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            if (!it.hasNext() || !eq(elements[i], it.next()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        if (it.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * Returns the hash code value for this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * <p>This implementation uses the definition in {@link List#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * @return the hash code value for this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        int hashCode = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        for (int i = 0; i < len; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            Object obj = elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * Returns an iterator over the elements in this list in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * 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
  1071
     * {@code remove} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * @return an iterator over the elements in this list in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        return new COWIterator<E>(getArray(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * 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
  1085
     * {@code remove}, {@code set} or {@code add} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        return new COWIterator<E>(getArray(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * 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
  1097
     * {@code remove}, {@code set} or {@code add} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     */
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1101
    public ListIterator<E> listIterator(int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        int len = elements.length;
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1104
        if (index < 0 || index > len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            throw new IndexOutOfBoundsException("Index: "+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        return new COWIterator<E>(elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1110
    /**
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1111
     * 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
  1112
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1113
     * <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
  1114
     * {@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
  1115
     * {@link Spliterator#SUBSIZED}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1116
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1117
     * <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
  1118
     * 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
  1119
     * operating on the spliterator.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1120
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1121
     * @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
  1122
     * @since 1.8
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1123
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1124
    public Spliterator<E> spliterator() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1125
        return Spliterators.spliterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1126
            (getArray(), Spliterator.IMMUTABLE | Spliterator.ORDERED);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1127
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1128
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1129
    static final class COWIterator<E> implements ListIterator<E> {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1130
        /** Snapshot of the array */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        private final Object[] snapshot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        /** Index of element to be returned by subsequent call to next.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        private int cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        private COWIterator(Object[] elements, int initialCursor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            cursor = initialCursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            snapshot = elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            return cursor < snapshot.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            return cursor > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            if (! hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            return (E) snapshot[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            if (! hasPrevious())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            return (E) snapshot[--cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            return cursor-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
         * Not supported. Always throws UnsupportedOperationException.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1172
         * @throws UnsupportedOperationException always; {@code remove}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
         * Not supported. Always throws UnsupportedOperationException.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1181
         * @throws UnsupportedOperationException always; {@code set}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
         * Not supported. Always throws UnsupportedOperationException.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1190
         * @throws UnsupportedOperationException always; {@code add}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        }
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1196
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1197
        @Override
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1198
        public void forEachRemaining(Consumer<? super E> action) {
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1199
            Objects.requireNonNull(action);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1200
            Object[] elements = snapshot;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1201
            final int size = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1202
            for (int i = cursor; i < size; i++) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1203
                @SuppressWarnings("unchecked") E e = (E) elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1204
                action.accept(e);
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1205
            }
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1206
            cursor = size;
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1207
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * Returns a view of the portion of this list between
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1212
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * The returned list is backed by this list, so changes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * returned list are reflected in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * <p>The semantics of the list returned by this method become
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * undefined if the backing list (i.e., this list) is modified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * any way other than via the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @param fromIndex low endpoint (inclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * @param toIndex high endpoint (exclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * @return a view of the specified range within this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            if (fromIndex < 0 || toIndex > len || fromIndex > toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            return new COWSubList<E>(this, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        }
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
     * Sublist for CopyOnWriteArrayList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * This class extends AbstractList merely for convenience, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * avoid having to define addAll, etc. This doesn't hurt, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * is wasteful.  This class does not need or use modCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * mechanics in AbstractList, but does need to check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * concurrent modification using similar mechanics.  On each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * operation, the array that we expect the backing list to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * is checked and updated.  Since we do this for all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * base operations invoked by those defined in AbstractList,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * all is well.  While inefficient, this is not worth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * improving.  The kinds of list operations inherited from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * AbstractList are already so slow on COW sublists that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * adding a bit more space/time doesn't seem even noticeable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    private static class COWSubList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        implements RandomAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        private final CopyOnWriteArrayList<E> l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        private final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        private Object[] expectedArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        // only call this holding l's lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        COWSubList(CopyOnWriteArrayList<E> list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                   int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            l = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            offset = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            size = toIndex - fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        // only call this holding l's lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        private void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            if (l.getArray() != expectedArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        // only call this holding l's lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        private void rangeCheck(int index) {
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1280
            if (index < 0 || index >= size)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                                                    ",Size: "+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                E x = l.set(index+offset, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                return l.get(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                checkForComodification();
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1327
                if (index < 0 || index > size)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                    throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                l.add(index+offset, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                l.removeRange(offset, offset+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                E result = l.remove(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            int index = indexOf(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            if (index == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                return new COWSubListIterator<E>(l, 0, offset, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1384
        public ListIterator<E> listIterator(int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                checkForComodification();
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1389
                if (index < 0 || index > size)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                    throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                                                        ", Size: "+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                return new COWSubListIterator<E>(l, index, offset, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                checkForComodification();
22628
9186623f7045 8011645: CopyOnWriteArrayList.COWSubList.subList does not validate range properly
chegar
parents: 19428
diff changeset
  1403
                if (fromIndex < 0 || toIndex > size || fromIndex > toIndex)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                    throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                return new COWSubList<E>(l, fromIndex + offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                                         toIndex + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1412
        public void forEach(Consumer<? super E> action) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1413
            if (action == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1414
            int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1415
            int hi = offset + size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1416
            Object[] a = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1417
            if (l.getArray() != a)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1418
                throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1419
            if (lo < 0 || hi > a.length)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1420
                throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1421
            for (int i = lo; i < hi; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1422
                @SuppressWarnings("unchecked") E e = (E) a[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1423
                action.accept(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1424
            }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1425
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1426
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1427
        public void replaceAll(UnaryOperator<E> operator) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1428
            if (operator == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1429
            final ReentrantLock lock = l.lock;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1430
            lock.lock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1431
            try {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1432
                int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1433
                int hi = offset + size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1434
                Object[] elements = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1435
                if (l.getArray() != elements)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1436
                    throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1437
                int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1438
                if (lo < 0 || hi > len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1439
                    throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1440
                Object[] newElements = Arrays.copyOf(elements, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1441
                for (int i = lo; i < hi; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1442
                    @SuppressWarnings("unchecked") E e = (E) elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1443
                    newElements[i] = operator.apply(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1444
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1445
                l.setArray(expectedArray = newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1446
            } finally {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1447
                lock.unlock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1448
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1449
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1450
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1451
        public void sort(Comparator<? super E> c) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1452
            final ReentrantLock lock = l.lock;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1453
            lock.lock();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1454
            try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1455
                int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1456
                int hi = offset + size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1457
                Object[] elements = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1458
                if (l.getArray() != elements)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1459
                    throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1460
                int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1461
                if (lo < 0 || hi > len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1462
                    throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1463
                Object[] newElements = Arrays.copyOf(elements, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1464
                @SuppressWarnings("unchecked") E[] es = (E[])newElements;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1465
                Arrays.sort(es, lo, hi, c);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1466
                l.setArray(expectedArray = newElements);
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1467
            } finally {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1468
                lock.unlock();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1469
            }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1470
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1471
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1472
        public boolean removeAll(Collection<?> c) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1473
            if (c == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1474
            boolean removed = false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1475
            final ReentrantLock lock = l.lock;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1476
            lock.lock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1477
            try {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1478
                int n = size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1479
                if (n > 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1480
                    int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1481
                    int hi = offset + n;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1482
                    Object[] elements = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1483
                    if (l.getArray() != elements)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1484
                        throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1485
                    int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1486
                    if (lo < 0 || hi > len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1487
                        throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1488
                    int newSize = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1489
                    Object[] temp = new Object[n];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1490
                    for (int i = lo; i < hi; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1491
                        Object element = elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1492
                        if (!c.contains(element))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1493
                            temp[newSize++] = element;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1494
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1495
                    if (newSize != n) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1496
                        Object[] newElements = new Object[len - n + newSize];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1497
                        System.arraycopy(elements, 0, newElements, 0, lo);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1498
                        System.arraycopy(temp, 0, newElements, lo, newSize);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1499
                        System.arraycopy(elements, hi, newElements,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1500
                                         lo + newSize, len - hi);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1501
                        size = newSize;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1502
                        removed = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1503
                        l.setArray(expectedArray = newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1504
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1505
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1506
            } finally {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1507
                lock.unlock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1508
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1509
            return removed;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1510
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1511
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1512
        public boolean retainAll(Collection<?> c) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1513
            if (c == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1514
            boolean removed = false;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1515
            final ReentrantLock lock = l.lock;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1516
            lock.lock();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1517
            try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1518
                int n = size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1519
                if (n > 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1520
                    int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1521
                    int hi = offset + n;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1522
                    Object[] elements = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1523
                    if (l.getArray() != elements)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1524
                        throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1525
                    int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1526
                    if (lo < 0 || hi > len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1527
                        throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1528
                    int newSize = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1529
                    Object[] temp = new Object[n];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1530
                    for (int i = lo; i < hi; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1531
                        Object element = elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1532
                        if (c.contains(element))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1533
                            temp[newSize++] = element;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1534
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1535
                    if (newSize != n) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1536
                        Object[] newElements = new Object[len - n + newSize];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1537
                        System.arraycopy(elements, 0, newElements, 0, lo);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1538
                        System.arraycopy(temp, 0, newElements, lo, newSize);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1539
                        System.arraycopy(elements, hi, newElements,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1540
                                         lo + newSize, len - hi);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1541
                        size = newSize;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1542
                        removed = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1543
                        l.setArray(expectedArray = newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1544
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1545
                }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1546
            } finally {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1547
                lock.unlock();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1548
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1549
            return removed;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1550
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1551
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1552
        public boolean removeIf(Predicate<? super E> filter) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1553
            if (filter == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1554
            boolean removed = false;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1555
            final ReentrantLock lock = l.lock;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1556
            lock.lock();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1557
            try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1558
                int n = size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1559
                if (n > 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1560
                    int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1561
                    int hi = offset + n;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1562
                    Object[] elements = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1563
                    if (l.getArray() != elements)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1564
                        throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1565
                    int len = elements.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1566
                    if (lo < 0 || hi > len)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1567
                        throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1568
                    int newSize = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1569
                    Object[] temp = new Object[n];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1570
                    for (int i = lo; i < hi; ++i) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1571
                        @SuppressWarnings("unchecked") E e = (E) elements[i];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1572
                        if (!filter.test(e))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1573
                            temp[newSize++] = e;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1574
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1575
                    if (newSize != n) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1576
                        Object[] newElements = new Object[len - n + newSize];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1577
                        System.arraycopy(elements, 0, newElements, 0, lo);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1578
                        System.arraycopy(temp, 0, newElements, lo, newSize);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1579
                        System.arraycopy(elements, hi, newElements,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1580
                                         lo + newSize, len - hi);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1581
                        size = newSize;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1582
                        removed = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1583
                        l.setArray(expectedArray = newElements);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1584
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1585
                }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1586
            } finally {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1587
                lock.unlock();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1588
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1589
            return removed;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1590
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1591
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1592
        public Spliterator<E> spliterator() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1593
            int lo = offset;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1594
            int hi = offset + size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1595
            Object[] a = expectedArray;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1596
            if (l.getArray() != a)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1597
                throw new ConcurrentModificationException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1598
            if (lo < 0 || hi > a.length)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1599
                throw new IndexOutOfBoundsException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1600
            return Spliterators.spliterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1601
                (a, lo, hi, Spliterator.IMMUTABLE | Spliterator.ORDERED);
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 14342
diff changeset
  1602
        }
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1603
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    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
  1607
        private final ListIterator<E> it;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        private final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        private final int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1611
        COWSubListIterator(List<E> l, int index, int offset, int size) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            this.size = size;
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1614
            it = l.listIterator(index+offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            return nextIndex() < size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            if (hasNext())
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1623
                return it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            return previousIndex() >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            if (hasPrevious())
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1634
                return it.previous();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        public int nextIndex() {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1640
            return it.nextIndex() - offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        public int previousIndex() {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1644
            return it.previousIndex() - offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        }
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1658
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1659
        @Override
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1660
        public void forEachRemaining(Consumer<? super E> action) {
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1661
            Objects.requireNonNull(action);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1662
            int s = size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1663
            ListIterator<E> i = it;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1664
            while (nextIndex() < s) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 17180
diff changeset
  1665
                action.accept(i.next());
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1666
            }
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17166
diff changeset
  1667
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
    // 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
  1671
    private void resetLock() {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1672
        UNSAFE.putObjectVolatile(this, lockOffset, new ReentrantLock());
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1673
    }
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1674
    private static final sun.misc.Unsafe UNSAFE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    private static final long lockOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        try {
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1678
            UNSAFE = sun.misc.Unsafe.getUnsafe();
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9504
diff changeset
  1679
            Class<?> k = CopyOnWriteArrayList.class;
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1680
            lockOffset = UNSAFE.objectFieldOffset
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1681
                (k.getDeclaredField("lock"));
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1682
        } catch (Exception e) {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1683
            throw new Error(e);
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1684
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
}