jdk/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8544 225896f7b33c
child 9504 a61398ab96e0
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8544
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * Expert Group.  Adapted and released, under explicit permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * from JDK ArrayList.java which carries the following copyright:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Copyright 1997 by Sun Microsystems, Inc.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A thread-safe variant of {@link java.util.ArrayList} in which all mutative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * operations (<tt>add</tt>, <tt>set</tt>, and so on) are implemented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * making a fresh copy of the underlying array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p> This is ordinarily too costly, but may be <em>more</em> efficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * than alternatives when traversal operations vastly outnumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * mutations, and is useful when you cannot or don't want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * synchronize traversals, yet need to preclude interference among
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * concurrent threads.  The "snapshot" style iterator method uses a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * reference to the state of the array at the point that the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * was created. This array never changes during the lifetime of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * iterator, so interference is impossible and the iterator is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * guaranteed not to throw <tt>ConcurrentModificationException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The iterator will not reflect additions, removals, or changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * the list since the iterator was created.  Element-changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * operations on iterators themselves (<tt>remove</tt>, <tt>set</tt>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <tt>add</tt>) are not supported. These methods throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <tt>UnsupportedOperationException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>All elements are permitted, including <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>Memory consistency effects: As with other concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * collections, actions in a thread prior to placing an object into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * {@code CopyOnWriteArrayList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * actions subsequent to the access or removal of that element from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * the {@code CopyOnWriteArrayList} in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public class CopyOnWriteArrayList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final long serialVersionUID = 8673264195747942595L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /** The lock protecting all mutators */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    transient final ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** The array, accessed only via getArray/setArray. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private volatile transient Object[] array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Gets the array.  Non-private so as to also be accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * from CopyOnWriteArraySet class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    final Object[] getArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Sets the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    final void setArray(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        array = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Creates an empty list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public CopyOnWriteArrayList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        setArray(new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Creates a list containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param c the collection of initially held elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public CopyOnWriteArrayList(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Object[] elements = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (elements.getClass() != Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            elements = Arrays.copyOf(elements, elements.length, Object[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        setArray(elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Creates a list holding a copy of the given array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param toCopyIn the array (a copy of this array is used as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *        internal array)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public CopyOnWriteArrayList(E[] toCopyIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        setArray(Arrays.copyOf(toCopyIn, toCopyIn.length, Object[].class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Returns the number of elements in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return the number of elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return getArray().length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Returns <tt>true</tt> if this list contains no elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return <tt>true</tt> if this list contains no elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return size() == 0;
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
     * Test for equality, coping with nulls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static boolean eq(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return (o1 == null ? o2 == null : o1.equals(o2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * static version of indexOf, to allow repeated calls without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * needing to re-acquire array each time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param elements the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param index first index to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param fence one past last index to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return index of element, or -1 if absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private static int indexOf(Object o, Object[] elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                               int index, int fence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            for (int i = index; i < fence; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (elements[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            for (int i = index; i < fence; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if (o.equals(elements[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * static version of lastIndexOf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param elements the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param index first index to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return index of element, or -1 if absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private static int lastIndexOf(Object o, Object[] elements, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            for (int i = index; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (elements[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            for (int i = index; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (o.equals(elements[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Returns <tt>true</tt> if this list contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * More formally, returns <tt>true</tt> if and only if this list contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * at least one element <tt>e</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param o element whose presence in this list is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @return <tt>true</tt> if this list contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return indexOf(o, elements, 0, elements.length) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return indexOf(o, elements, 0, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Returns the index of the first occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * this list, searching forwards from <tt>index</tt>, or returns -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * More formally, returns the lowest index <tt>i</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <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
   233
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param e element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param index index to start searching from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @return the index of the first occurrence of the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *         this list at position <tt>index</tt> or later in the list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *         <tt>-1</tt> if the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public int indexOf(E e, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return indexOf(e, elements, index, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public int lastIndexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return lastIndexOf(o, elements, elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Returns the index of the last occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * this list, searching backwards from <tt>index</tt>, or returns -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * More formally, returns the highest index <tt>i</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <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
   261
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param e element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param index index to start searching backwards from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @return the index of the last occurrence of the element at position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *         less than or equal to <tt>index</tt> in this list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *         -1 if the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @throws IndexOutOfBoundsException if the specified index is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *         than or equal to the current size of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public int lastIndexOf(E e, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return lastIndexOf(e, elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Returns a shallow copy of this list.  (The elements themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * are not copied.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return a clone of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            CopyOnWriteArrayList c = (CopyOnWriteArrayList)(super.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            c.resetLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // this shouldn't happen, since we are Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Returns an array containing all of the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * in proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * maintained by this list.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return an array containing all the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return Arrays.copyOf(elements, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Returns an array containing all of the elements in this list in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * proper sequence (from first to last element); the runtime type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * the returned array is that of the specified array.  If the list fits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * in the specified array, it is returned therein.  Otherwise, a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * array is allocated with the runtime type of the specified array and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * the size of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p>If this list fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * (i.e., the array has more elements than this list), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * the array immediately following the end of the list is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <tt>null</tt>.  (This is useful in determining the length of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * list <i>only</i> if the caller knows that this list does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * any null elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <p>Suppose <tt>x</tt> is a list known to contain only strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * The following code can be used to dump the list into a newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * allocated array of <tt>String</tt>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *     String[] y = x.toArray(new String[0]);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <tt>toArray()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param a the array into which the elements of the list are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *          same runtime type is allocated for this purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @return an array containing all the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *         this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public <T> T[] toArray(T a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (a.length < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return (T[]) Arrays.copyOf(elements, len, a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            System.arraycopy(elements, 0, a, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (a.length > len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                a[len] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    // Positional Access Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private E get(Object[] a, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return (E) a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return get(getArray(), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Replaces the element at the specified position in this list with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            E oldValue = get(elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (oldValue != element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                Object[] newElements = Arrays.copyOf(elements, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                newElements[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                // Not quite a no-op; ensures volatile write semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                setArray(elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Appends the specified element to the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @param e element to be appended to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @return <tt>true</tt> (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            Object[] newElements = Arrays.copyOf(elements, len + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            newElements[len] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Inserts the specified element at the specified position in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * list. Shifts the element currently at that position (if any) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * any subsequent elements to the right (adds one to their indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            if (index > len || index < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                                                    ", Size: "+len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            Object[] newElements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            int numMoved = len - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (numMoved == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                newElements = Arrays.copyOf(elements, len + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                newElements = new Object[len + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                System.arraycopy(elements, 0, newElements, 0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                System.arraycopy(elements, index, newElements, index + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            newElements[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Removes the element at the specified position in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Shifts any subsequent elements to the left (subtracts one from their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * indices).  Returns the element that was removed from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            E oldValue = get(elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            int numMoved = len - index - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            if (numMoved == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                setArray(Arrays.copyOf(elements, len - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                Object[] newElements = new Object[len - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                System.arraycopy(elements, 0, newElements, 0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                System.arraycopy(elements, index + 1, newElements, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Removes the first occurrence of the specified element from this list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * if it is present.  If this list does not contain the element, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * unchanged.  More formally, removes the element with the lowest index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * <tt>i</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * (if such an element exists).  Returns <tt>true</tt> if this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * contained the specified element (or equivalently, if this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @param o element to be removed from this list, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @return <tt>true</tt> if this list contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                // Copy while searching for element to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                // This wins in the normal case of element being present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                int newlen = len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                Object[] newElements = new Object[newlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                for (int i = 0; i < newlen; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    if (eq(o, elements[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        // found one;  copy remaining and exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                        for (int k = i + 1; k < len; ++k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                            newElements[k-1] = elements[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        newElements[i] = elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                // special handling for last cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                if (eq(o, elements[newlen])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Removes from this list all of the elements whose index is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Shifts any succeeding elements to the left (reduces their index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * (If <tt>toIndex==fromIndex</tt>, this operation has no effect.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @param fromIndex index of first element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @param toIndex index after last element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @throws IndexOutOfBoundsException if fromIndex or toIndex out of range
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   550
     *         ({@code{fromIndex < 0 || toIndex > size() || toIndex < fromIndex})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    private void removeRange(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (fromIndex < 0 || toIndex > len || toIndex < fromIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            int newlen = len - (toIndex - fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            int numMoved = len - toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            if (numMoved == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                setArray(Arrays.copyOf(elements, newlen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                Object[] newElements = new Object[newlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                System.arraycopy(elements, 0, newElements, 0, fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                System.arraycopy(elements, toIndex, newElements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                                 fromIndex, numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Append the element if not present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @param e element to be added to this list, if absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @return <tt>true</tt> if the element was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public boolean addIfAbsent(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            // Copy while checking if already present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            // This wins in the most common case where it is not present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            Object[] newElements = new Object[len + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            for (int i = 0; i < len; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                if (eq(e, elements[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    return false; // exit, throwing away copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    newElements[i] = elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            newElements[len] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * Returns <tt>true</tt> if this list contains all of the elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @param c collection to be checked for containment in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @return <tt>true</tt> if this list contains all of the elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *         specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @see #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public boolean containsAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        for (Object e : c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if (indexOf(e, elements, 0, len) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Removes from this list all of its elements that are contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * the specified collection. This is a particularly expensive operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * in this class because of the need for an internal temporary array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @param c collection containing elements to be removed from this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @return <tt>true</tt> if this list changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @throws ClassCastException if the class of an element of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *         is incompatible with the specified collection (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @throws NullPointerException if this list contains a null element and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *         specified collection does not permit null elements (optional),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    public boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                // temp array holds those elements we know we want to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                int newlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                Object[] temp = new Object[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                for (int i = 0; i < len; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    Object element = elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    if (!c.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        temp[newlen++] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                if (newlen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    setArray(Arrays.copyOf(temp, newlen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            lock.unlock();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * Retains only the elements in this list that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * specified collection.  In other words, removes from this list all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * its elements that are not contained in the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @param c collection containing elements to be retained in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @return <tt>true</tt> if this list changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @throws ClassCastException if the class of an element of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *         is incompatible with the specified collection (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @throws NullPointerException if this list contains a null element and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *         specified collection does not permit null elements (optional),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    public boolean retainAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                // temp array holds those elements we know we want to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                int newlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                Object[] temp = new Object[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                for (int i = 0; i < len; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    Object element = elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    if (c.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                        temp[newlen++] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                if (newlen != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    setArray(Arrays.copyOf(temp, newlen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * Appends all of the elements in the specified collection that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * are not already contained in this list, to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * this list, in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * specified collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @param c collection containing elements to be added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @return the number of elements added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @see #addIfAbsent(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    public int addAllAbsent(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        Object[] cs = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        Object[] uniq = new Object[cs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            int added = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            for (int i = 0; i < cs.length; ++i) { // scan for duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                Object e = cs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                if (indexOf(e, elements, 0, len) < 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    indexOf(e, uniq, 0, added) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    uniq[added++] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            if (added > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                Object[] newElements = Arrays.copyOf(elements, len + added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                System.arraycopy(uniq, 0, newElements, len, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            return added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * Removes all of the elements from this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * The list will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            setArray(new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * Appends all of the elements in the specified collection to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * of this list, in the order that they are returned by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param c collection containing elements to be added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @return <tt>true</tt> if this list changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @see #add(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    public boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        Object[] cs = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            Object[] newElements = Arrays.copyOf(elements, len + cs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            System.arraycopy(cs, 0, newElements, len, cs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * Inserts all of the elements in the specified collection into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * list, starting at the specified position.  Shifts the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * currently at that position (if any) and any subsequent elements to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * the right (increases their indices).  The new elements will appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * in this list in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * specified collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @param index index at which to insert the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *        from the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @param c collection containing elements to be added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @return <tt>true</tt> if this list changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @see #add(int,Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        Object[] cs = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            if (index > len || index < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                                                    ", Size: "+len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            if (cs.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            int numMoved = len - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            Object[] newElements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            if (numMoved == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                newElements = Arrays.copyOf(elements, len + cs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                newElements = new Object[len + cs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                System.arraycopy(elements, 0, newElements, 0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                System.arraycopy(elements, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                                 newElements, index + cs.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                                 numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            System.arraycopy(cs, 0, newElements, index, cs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            setArray(newElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
   835
     * Saves the state of the list to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @serialData The length of the array backing the list is emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *               (int), followed by all of its elements (each an Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *               in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @param s the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        throws java.io.IOException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        // Write out array length
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
   849
        s.writeInt(elements.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        // 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
   852
        for (Object element : elements)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
   853
            s.writeObject(element);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
   857
     * Reconstitutes the list from a stream (that is, deserializes it).
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7668
diff changeset
   858
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @param s the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        // bind to new lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        resetLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        // Read in array length and allocate array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        int len = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        Object[] elements = new Object[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        // Read in all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        for (int i = 0; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            elements[i] = s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        setArray(elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Returns a string representation of this list.  The string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * representation consists of the string representations of the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * elements in the order they are returned by its iterator, enclosed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * square brackets (<tt>"[]"</tt>).  Adjacent elements are separated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * the characters <tt>", "</tt> (comma and space).  Elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * converted to strings as by {@link String#valueOf(Object)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @return a string representation of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        return Arrays.toString(getArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * Compares the specified object with this list for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Returns {@code true} if the specified object is the same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * as this object, or if it is also a {@link List} and the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * of elements returned by an {@linkplain List#iterator() iterator}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * over the specified list is the same as the sequence returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * an iterator over this list.  The two sequences are considered to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * be the same if they have the same length and corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * elements at the same position in the sequence are <em>equal</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * Two elements {@code e1} and {@code e2} are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * <em>equal</em> if {@code (e1==null ? e2==null : e1.equals(e2))}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * @param o the object to be compared for equality with this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @return {@code true} if the specified object is equal to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        if (!(o instanceof List))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        List<?> list = (List<?>)(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        Iterator<?> it = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        for (int i = 0; i < len; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            if (!it.hasNext() || !eq(elements[i], it.next()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        if (it.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * Returns the hash code value for this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * <p>This implementation uses the definition in {@link List#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @return the hash code value for this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        int hashCode = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        for (int i = 0; i < len; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            Object obj = elements[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * Returns an iterator over the elements in this list in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * traversing the iterator. The iterator does <em>NOT</em> support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * <tt>remove</tt> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @return an iterator over the elements in this list in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        return new COWIterator<E>(getArray(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * traversing the iterator. The iterator does <em>NOT</em> support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * <tt>remove</tt>, <tt>set</tt> or <tt>add</tt> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        return new COWIterator<E>(getArray(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * <p>The returned iterator provides a snapshot of the state of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * when the iterator was constructed. No synchronization is needed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * traversing the iterator. The iterator does <em>NOT</em> support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * <tt>remove</tt>, <tt>set</tt> or <tt>add</tt> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        if (index<0 || index>len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            throw new IndexOutOfBoundsException("Index: "+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        return new COWIterator<E>(elements, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    private static class COWIterator<E> implements ListIterator<E> {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   990
        /** Snapshot of the array */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        private final Object[] snapshot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        /** Index of element to be returned by subsequent call to next.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        private int cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        private COWIterator(Object[] elements, int initialCursor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            cursor = initialCursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            snapshot = 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
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            return cursor < snapshot.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            return cursor > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            if (! hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            return (E) snapshot[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            if (! hasPrevious())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            return (E) snapshot[--cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            return cursor-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
         * Not supported. Always throws UnsupportedOperationException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
         * @throws UnsupportedOperationException always; <tt>remove</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
         * Not supported. Always throws UnsupportedOperationException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
         * @throws UnsupportedOperationException always; <tt>set</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
         * Not supported. Always throws UnsupportedOperationException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
         * @throws UnsupportedOperationException always; <tt>add</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
         *         is not supported by this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * Returns a view of the portion of this list between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * The returned list is backed by this list, so changes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * returned list are reflected in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * <p>The semantics of the list returned by this method become
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * undefined if the backing list (i.e., this list) is modified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * any way other than via the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @param fromIndex low endpoint (inclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * @param toIndex high endpoint (exclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @return a view of the specified range within this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            Object[] elements = getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            int len = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            if (fromIndex < 0 || toIndex > len || fromIndex > toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            return new COWSubList<E>(this, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * Sublist for CopyOnWriteArrayList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * This class extends AbstractList merely for convenience, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * avoid having to define addAll, etc. This doesn't hurt, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * is wasteful.  This class does not need or use modCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * mechanics in AbstractList, but does need to check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * concurrent modification using similar mechanics.  On each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * operation, the array that we expect the backing list to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * is checked and updated.  Since we do this for all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * base operations invoked by those defined in AbstractList,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * all is well.  While inefficient, this is not worth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * improving.  The kinds of list operations inherited from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * AbstractList are already so slow on COW sublists that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * adding a bit more space/time doesn't seem even noticeable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    private static class COWSubList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        implements RandomAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        private final CopyOnWriteArrayList<E> l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        private final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        private Object[] expectedArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        // only call this holding l's lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        COWSubList(CopyOnWriteArrayList<E> list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                   int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            l = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            offset = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            size = toIndex - fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        // only call this holding l's lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        private void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            if (l.getArray() != expectedArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        // only call this holding l's lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        private void rangeCheck(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            if (index<0 || index>=size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                                                    ",Size: "+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                E x = l.set(index+offset, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                return l.get(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                if (index<0 || index>size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                    throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                l.add(index+offset, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                l.removeRange(offset, offset+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                E result = l.remove(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                expectedArray = l.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            int index = indexOf(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            if (index == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                return new COWSubListIterator<E>(l, 0, offset, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                if (index<0 || index>size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                    throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                                                        ", Size: "+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                return new COWSubListIterator<E>(l, index, offset, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            final ReentrantLock lock = l.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                if (fromIndex<0 || toIndex>size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                    throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                return new COWSubList<E>(l, fromIndex + offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                                         toIndex + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    private static class COWSubListIterator<E> implements ListIterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        private final ListIterator<E> i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        private final int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        private final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        private final int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        COWSubListIterator(List<E> l, int index, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                           int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            i = l.listIterator(index+offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            return nextIndex() < size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            if (hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                return i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            return previousIndex() >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            if (hasPrevious())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                return i.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                throw new NoSuchElementException();
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 int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            return i.nextIndex() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            return i.previousIndex() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            throw new UnsupportedOperationException();
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 void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    // 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
  1321
    private void resetLock() {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1322
        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
  1323
    }
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1324
    private static final sun.misc.Unsafe UNSAFE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    private static final long lockOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        try {
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1328
            UNSAFE = sun.misc.Unsafe.getUnsafe();
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1329
            Class k = CopyOnWriteArrayList.class;
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1330
            lockOffset = UNSAFE.objectFieldOffset
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1331
                (k.getDeclaredField("lock"));
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1332
        } catch (Exception e) {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1333
            throw new Error(e);
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1334
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
}