jdk/src/share/classes/java/util/concurrent/ConcurrentHashMap.java
author dl
Thu, 07 Apr 2011 15:06:32 +0100
changeset 9242 ef138d47df58
parent 7518 0282db800fe1
child 9279 5f5d493d30a0
permissions -rw-r--r--
7034657: Update Creative Commons license URL in legal notices Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
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.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * A hash table supporting full concurrency of retrievals and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * adjustable expected concurrency for updates. This class obeys the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * same functional specification as {@link java.util.Hashtable}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * includes versions of methods corresponding to each method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <tt>Hashtable</tt>. However, even though all operations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * thread-safe, retrieval operations do <em>not</em> entail locking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * and there is <em>not</em> any support for locking the entire table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * in a way that prevents all access.  This class is fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * interoperable with <tt>Hashtable</tt> in programs that rely on its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * thread safety but not on its synchronization details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p> Retrieval operations (including <tt>get</tt>) generally do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * block, so may overlap with update operations (including
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <tt>put</tt> and <tt>remove</tt>). Retrievals reflect the results
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * of the most recently <em>completed</em> update operations holding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * upon their onset.  For aggregate operations such as <tt>putAll</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * and <tt>clear</tt>, concurrent retrievals may reflect insertion or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * removal of only some entries.  Similarly, Iterators and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Enumerations return elements reflecting the state of the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * at some point at or since the creation of the iterator/enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * They do <em>not</em> throw {@link ConcurrentModificationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * However, iterators are designed to be used by only one thread at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p> The allowed concurrency among update operations is guided by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * the optional <tt>concurrencyLevel</tt> constructor argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * (default <tt>16</tt>), which is used as a hint for internal sizing.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * table is internally partitioned to try to permit the indicated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * number of concurrent updates without contention. Because placement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * in hash tables is essentially random, the actual concurrency will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * vary.  Ideally, you should choose a value to accommodate as many
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * threads as will ever concurrently modify the table. Using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * significantly higher value than you need can waste space and time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * and a significantly lower value can lead to thread contention. But
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * overestimates and underestimates within an order of magnitude do
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * not usually have much noticeable impact. A value of one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * appropriate when it is known that only one thread will modify and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * all others will only read. Also, resizing this or any other kind of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * hash table is a relatively slow operation, so, when possible, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * a good idea to provide estimates of expected table sizes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>This class and its views and iterators implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <em>optional</em> methods of the {@link Map} and {@link Iterator}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * does <em>not</em> allow <tt>null</tt> to be used as a key or value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        implements ConcurrentMap<K, V>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final long serialVersionUID = 7249069246763182397L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * The basic strategy is to subdivide the table among Segments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * each of which itself is a concurrently readable hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /* ---------------- Constants -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * The default initial capacity for this table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * used when not otherwise specified in a constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static final int DEFAULT_INITIAL_CAPACITY = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * The default load factor for this table, used when not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * otherwise specified in a constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * The default concurrency level for this table, used when not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * otherwise specified in a constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static final int DEFAULT_CONCURRENCY_LEVEL = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * The maximum capacity, used if a higher value is implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * specified by either of the constructors with arguments.  MUST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * be a power of two <= 1<<30 to ensure that entries are indexable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * using ints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    static final int MAXIMUM_CAPACITY = 1 << 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * The maximum number of segments to allow; used to bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * constructor arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static final int MAX_SEGMENTS = 1 << 16; // slightly conservative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Number of unsynchronized retries in size and containsValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * methods before resorting to locking. This is used to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * unbounded retries if tables undergo continuous modification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * which would make it impossible to obtain an accurate result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static final int RETRIES_BEFORE_LOCK = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /* ---------------- Fields -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Mask value for indexing into segments. The upper bits of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * key's hash code are used to choose the segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    final int segmentMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Shift value for indexing within segments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    final int segmentShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * The segments, each of which is a specialized hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    final Segment<K,V>[] segments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    transient Set<K> keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    transient Set<Map.Entry<K,V>> entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    transient Collection<V> values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /* ---------------- Small Utilities -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Applies a supplemental hash function to a given hashCode, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * defends against poor quality hash functions.  This is critical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * because ConcurrentHashMap uses power-of-two length hash tables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * that otherwise encounter collisions for hashCodes that do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * differ in lower or upper bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private static int hash(int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // Spread bits to regularize both segment and index locations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // using variant of single-word Wang/Jenkins hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        h += (h <<  15) ^ 0xffffcd7d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        h ^= (h >>> 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        h += (h <<   3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        h ^= (h >>>  6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        h += (h <<   2) + (h << 14);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return h ^ (h >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns the segment that should be used for key with given hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param hash the hash code for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @return the segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    final Segment<K,V> segmentFor(int hash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return segments[(hash >>> segmentShift) & segmentMask];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /* ---------------- Inner Classes -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * ConcurrentHashMap list entry. Note that this is never exported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * out as a user-visible Map.Entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Because the value field is volatile, not final, it is legal wrt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * the Java Memory Model for an unsynchronized reader to see null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * instead of initial value when read via a data race.  Although a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * reordering leading to this is not likely to ever actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * occur, the Segment.readValueUnderLock method is used as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * backup in case a null (pre-initialized) value is ever seen in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * an unsynchronized access method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    static final class HashEntry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        final K key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        final int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        volatile V value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        final HashEntry<K,V> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        HashEntry(K key, int hash, HashEntry<K,V> next, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            this.hash = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        static final <K,V> HashEntry<K,V>[] newArray(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return new HashEntry[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Segments are specialized versions of hash tables.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * subclasses from ReentrantLock opportunistically, just to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * simplify some locking and avoid separate construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    static final class Segment<K,V> extends ReentrantLock implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * Segments maintain a table of entry lists that are ALWAYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * kept in a consistent state, so can be read without locking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * Next fields of nodes are immutable (final).  All list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * additions are performed at the front of each bin. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         * makes it easy to check changes, and also fast to traverse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * When nodes would otherwise be changed, new nodes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * created to replace them. This works well for hash tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         * since the bin lists tend to be short. (The average length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         * is less than two for the default load factor threshold.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         * Read operations can thus proceed without locking, but rely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         * on selected uses of volatiles to ensure that completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         * write operations performed by other threads are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         * noticed. For most purposes, the "count" field, tracking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         * number of elements, serves as that volatile variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * ensuring visibility.  This is convenient because this field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * needs to be read in many read operations anyway:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         *   - All (unsynchronized) read operations must first read the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         *     "count" field, and should not look at table entries if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         *     it is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         *   - All (synchronized) write operations should write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         *     the "count" field after structurally changing any bin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         *     The operations must not take any action that could even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         *     momentarily cause a concurrent read operation to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         *     inconsistent data. This is made easier by the nature of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         *     the read operations in Map. For example, no operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         *     can reveal that the table has grown but the threshold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         *     has not yet been updated, so there are no atomicity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         *     requirements for this with respect to reads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         * As a guide, all critical volatile reads and writes to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * count field are marked in code comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        private static final long serialVersionUID = 2249069246763182397L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         * The number of elements in this segment's region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        transient volatile int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * Number of updates that alter the size of the table. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         * used during bulk-read methods to make sure they see a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         * consistent snapshot: If modCounts change during a traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         * of segments computing size or checking containsValue, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * we might have an inconsistent view of state so (usually)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * must retry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        transient int modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
         * The table is rehashed when its size exceeds this threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         * (The value of this field is always <tt>(int)(capacity *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * loadFactor)</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        transient int threshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         * The per-segment table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        transient volatile HashEntry<K,V>[] table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * The load factor for the hash table.  Even though this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * is same for all segments, it is replicated to avoid needing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * links to outer object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        final float loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        Segment(int initialCapacity, float lf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            loadFactor = lf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            setTable(HashEntry.<K,V>newArray(initialCapacity));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        static final <K,V> Segment<K,V>[] newArray(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return new Segment[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * Sets table to new HashEntry array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * Call only while holding lock or in constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        void setTable(HashEntry<K,V>[] newTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            threshold = (int)(newTable.length * loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            table = newTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * Returns properly casted first entry of bin for given hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        HashEntry<K,V> getFirst(int hash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            HashEntry<K,V>[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return tab[hash & (tab.length - 1)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * Reads value field of an entry under lock. Called if value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         * field ever appears to be null. This is possible only if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
         * compiler happens to reorder a HashEntry initialization with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
         * its table assignment, which is legal under memory model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
         * but is not known to ever occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        V readValueUnderLock(HashEntry<K,V> e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                return e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        /* Specialized implementations of map methods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        V get(Object key, int hash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (count != 0) { // read-volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                HashEntry<K,V> e = getFirst(hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                while (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    if (e.hash == hash && key.equals(e.key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        V v = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        if (v != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        return readValueUnderLock(e); // recheck
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    e = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        boolean containsKey(Object key, int hash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (count != 0) { // read-volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                HashEntry<K,V> e = getFirst(hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                while (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    if (e.hash == hash && key.equals(e.key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    e = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (count != 0) { // read-volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                HashEntry<K,V>[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                for (int i = 0 ; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    for (HashEntry<K,V> e = tab[i]; e != null; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        V v = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        if (v == null) // recheck
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                            v = readValueUnderLock(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        if (value.equals(v))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        boolean replace(K key, int hash, V oldValue, V newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                HashEntry<K,V> e = getFirst(hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                while (e != null && (e.hash != hash || !key.equals(e.key)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    e = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                boolean replaced = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                if (e != null && oldValue.equals(e.value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    replaced = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    e.value = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                return replaced;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        V replace(K key, int hash, V newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                HashEntry<K,V> e = getFirst(hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                while (e != null && (e.hash != hash || !key.equals(e.key)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    e = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                V oldValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    oldValue = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    e.value = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        V put(K key, int hash, V value, boolean onlyIfAbsent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                int c = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                if (c++ > threshold) // ensure capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    rehash();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                HashEntry<K,V>[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                int index = hash & (tab.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                HashEntry<K,V> first = tab[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                HashEntry<K,V> e = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                while (e != null && (e.hash != hash || !key.equals(e.key)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    e = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                V oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    oldValue = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    if (!onlyIfAbsent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                        e.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    oldValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    ++modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    tab[index] = new HashEntry<K,V>(key, hash, first, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    count = c; // write-volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        void rehash() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            HashEntry<K,V>[] oldTable = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            int oldCapacity = oldTable.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (oldCapacity >= MAXIMUM_CAPACITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
             * Reclassify nodes in each list to new Map.  Because we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
             * using power-of-two expansion, the elements from each bin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
             * must either stay at same index, or move with a power of two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
             * offset. We eliminate unnecessary node creation by catching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
             * cases where old nodes can be reused because their next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
             * fields won't change. Statistically, at the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
             * threshold, only about one-sixth of them need cloning when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
             * a table doubles. The nodes they replace will be garbage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
             * collectable as soon as they are no longer referenced by any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
             * reader thread that may be in the midst of traversing table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
             * right now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            HashEntry<K,V>[] newTable = HashEntry.newArray(oldCapacity<<1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            threshold = (int)(newTable.length * loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            int sizeMask = newTable.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            for (int i = 0; i < oldCapacity ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                // We need to guarantee that any existing reads of old Map can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                //  proceed. So we cannot yet null out each bin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                HashEntry<K,V> e = oldTable[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    HashEntry<K,V> next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    int idx = e.hash & sizeMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    //  Single node on list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    if (next == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                        newTable[idx] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        // Reuse trailing consecutive sequence at same slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        HashEntry<K,V> lastRun = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        int lastIdx = idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        for (HashEntry<K,V> last = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                             last != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                             last = last.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                            int k = last.hash & sizeMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                            if (k != lastIdx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                lastIdx = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                                lastRun = last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        newTable[lastIdx] = lastRun;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        // Clone all remaining nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        for (HashEntry<K,V> p = e; p != lastRun; p = p.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                            int k = p.hash & sizeMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                            HashEntry<K,V> n = newTable[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                            newTable[k] = new HashEntry<K,V>(p.key, p.hash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                                             n, p.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            table = newTable;
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
         * Remove; match on key only if value null, else match both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        V remove(Object key, int hash, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                int c = count - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                HashEntry<K,V>[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                int index = hash & (tab.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                HashEntry<K,V> first = tab[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                HashEntry<K,V> e = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                while (e != null && (e.hash != hash || !key.equals(e.key)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    e = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                V oldValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    V v = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    if (value == null || value.equals(v)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        oldValue = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                        // All entries following removed node can stay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        // in list, but all preceding ones need to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        // cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        ++modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        HashEntry<K,V> newFirst = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        for (HashEntry<K,V> p = first; p != e; p = p.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                            newFirst = new HashEntry<K,V>(p.key, p.hash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                                                          newFirst, p.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                        tab[index] = newFirst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                        count = c; // write-volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if (count != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    HashEntry<K,V>[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    for (int i = 0; i < tab.length ; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    ++modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    count = 0; // write-volatile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    /* ---------------- Public operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Creates a new, empty map with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * capacity, load factor and concurrency level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @param initialCapacity the initial capacity. The implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * performs internal sizing to accommodate this many elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @param loadFactor  the load factor threshold, used to control resizing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * Resizing may be performed when the average number of elements per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * bin exceeds this threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @param concurrencyLevel the estimated number of concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * updating threads. The implementation performs internal sizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * to try to accommodate this many threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @throws IllegalArgumentException if the initial capacity is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * negative or the load factor or concurrencyLevel are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * nonpositive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    public ConcurrentHashMap(int initialCapacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                             float loadFactor, int concurrencyLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (concurrencyLevel > MAX_SEGMENTS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            concurrencyLevel = MAX_SEGMENTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        // Find power-of-two sizes best matching arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        int sshift = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        int ssize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        while (ssize < concurrencyLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            ++sshift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            ssize <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        segmentShift = 32 - sshift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        segmentMask = ssize - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        this.segments = Segment.newArray(ssize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (initialCapacity > MAXIMUM_CAPACITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            initialCapacity = MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        int c = initialCapacity / ssize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        if (c * ssize < initialCapacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            ++c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        int cap = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        while (cap < c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            cap <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        for (int i = 0; i < this.segments.length; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            this.segments[i] = new Segment<K,V>(cap, loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Creates a new, empty map with the specified initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * and load factor and with the default concurrencyLevel (16).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @param initialCapacity The implementation performs internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * sizing to accommodate this many elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @param loadFactor  the load factor threshold, used to control resizing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Resizing may be performed when the average number of elements per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * bin exceeds this threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @throws IllegalArgumentException if the initial capacity of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * elements is negative or the load factor is nonpositive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public ConcurrentHashMap(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Creates a new, empty map with the specified initial capacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * and with default load factor (0.75) and concurrencyLevel (16).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @param initialCapacity the initial capacity. The implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * performs internal sizing to accommodate this many elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @throws IllegalArgumentException if the initial capacity of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * elements is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public ConcurrentHashMap(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * Creates a new, empty map with a default initial capacity (16),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * load factor (0.75) and concurrencyLevel (16).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    public ConcurrentHashMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Creates a new map with the same mappings as the given map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * The map is created with a capacity of 1.5 times the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * of mappings in the given map or 16 (whichever is greater),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * and a default load factor (0.75) and concurrencyLevel (16).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @param m the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                      DEFAULT_INITIAL_CAPACITY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
             DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * Returns <tt>true</tt> if this map contains no key-value mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @return <tt>true</tt> if this map contains no key-value mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        final Segment<K,V>[] segments = this.segments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         * We keep track of per-segment modCounts to avoid ABA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         * problems in which an element in one segment was added and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
         * in another removed during traversal, in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
         * table was never actually empty at any point. Note the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
         * similar use of modCounts in the size() and containsValue()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
         * methods, which are the only other methods also susceptible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
         * to ABA problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        int[] mc = new int[segments.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        int mcsum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        for (int i = 0; i < segments.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            if (segments[i].count != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                mcsum += mc[i] = segments[i].modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        // If mcsum happens to be zero, then we know we got a snapshot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        // before any modifications at all were made.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        // probably common enough to bother tracking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (mcsum != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            for (int i = 0; i < segments.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                if (segments[i].count != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    mc[i] != segments[i].modCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * Returns the number of key-value mappings in this map.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * <tt>Integer.MAX_VALUE</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        final Segment<K,V>[] segments = this.segments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        long sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        long check = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        int[] mc = new int[segments.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        // Try a few times to get accurate count. On failure due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        // continuous async changes in table, resort to locking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            check = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            int mcsum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            for (int i = 0; i < segments.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                sum += segments[i].count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                mcsum += mc[i] = segments[i].modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            if (mcsum != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                for (int i = 0; i < segments.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    check += segments[i].count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    if (mc[i] != segments[i].modCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                        check = -1; // force retry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            if (check == sum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (check != sum) { // Resort to locking all segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            for (int i = 0; i < segments.length; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                segments[i].lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            for (int i = 0; i < segments.length; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                sum += segments[i].count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            for (int i = 0; i < segments.length; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                segments[i].unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (sum > Integer.MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            return (int)sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * {@code k} to a value {@code v} such that {@code key.equals(k)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * then this method returns {@code v}; otherwise it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        int hash = hash(key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        return segmentFor(hash).get(key, hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * Tests if the specified object is a key in this table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @param  key   possible key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @return <tt>true</tt> if and only if the specified object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *         is a key in this table, as determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *         <tt>equals</tt> method; <tt>false</tt> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        int hash = hash(key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        return segmentFor(hash).containsKey(key, hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * Returns <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * specified value. Note: This method requires a full internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * traversal of the hash table, and so is much slower than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * method <tt>containsKey</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @param value value whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @return <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *         specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @throws NullPointerException if the specified value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        // See explanation of modCount use above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        final Segment<K,V>[] segments = this.segments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        int[] mc = new int[segments.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        // Try a few times without locking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            int mcsum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            for (int i = 0; i < segments.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                int c = segments[i].count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                mcsum += mc[i] = segments[i].modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                if (segments[i].containsValue(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            boolean cleanSweep = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            if (mcsum != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                for (int i = 0; i < segments.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    int c = segments[i].count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    if (mc[i] != segments[i].modCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                        cleanSweep = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            if (cleanSweep)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        // Resort to locking all segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        for (int i = 0; i < segments.length; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            segments[i].lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            for (int i = 0; i < segments.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                if (segments[i].containsValue(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            for (int i = 0; i < segments.length; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                segments[i].unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        return found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * Legacy method testing if some key maps into the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * in this table.  This method is identical in functionality to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * {@link #containsValue}, and exists solely to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * full compatibility with class {@link java.util.Hashtable},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * which supported this method prior to introduction of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * Java Collections framework.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * @param  value a value to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * @return <tt>true</tt> if and only if some key maps to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *         <tt>value</tt> argument in this table as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *         determined by the <tt>equals</tt> method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *         <tt>false</tt> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @throws NullPointerException if the specified value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    public boolean contains(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        return containsValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Maps the specified key to the specified value in this table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * Neither the key nor the value can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * <p> The value can be retrieved by calling the <tt>get</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * with a key that is equal to the original key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @param key key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * @param value value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * @throws NullPointerException if the specified key or value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        int hash = hash(key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        return segmentFor(hash).put(key, hash, value, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @return the previous value associated with the specified key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *         or <tt>null</tt> if there was no mapping for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @throws NullPointerException if the specified key or value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    public V putIfAbsent(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        int hash = hash(key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        return segmentFor(hash).put(key, hash, value, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * Copies all of the mappings from the specified map to this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * These mappings replace any mappings that this map had for any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @param m mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            put(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * Removes the key (and its corresponding value) from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * This method does nothing if the key is not in the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * @param  key the key that needs to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        int hash = hash(key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        return segmentFor(hash).remove(key, hash, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    public boolean remove(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        int hash = hash(key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        return segmentFor(hash).remove(key, hash, value) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * @throws NullPointerException if any of the arguments are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    public boolean replace(K key, V oldValue, V newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        if (oldValue == null || newValue == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        int hash = hash(key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        return segmentFor(hash).replace(key, hash, oldValue, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * @return the previous value associated with the specified key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *         or <tt>null</tt> if there was no mapping for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @throws NullPointerException if the specified key or value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    public V replace(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        int hash = hash(key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        return segmentFor(hash).replace(key, hash, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        for (int i = 0; i < segments.length; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            segments[i].clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * reflected in the set, and vice-versa.  The set supports element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * removal, which removes the corresponding mapping from this map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * operations.  It does not support the <tt>add</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * that will never throw {@link ConcurrentModificationException},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * and guarantees to traverse elements as they existed upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * construction of the iterator, and may (but is not guaranteed to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * reflect any modifications subsequent to construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        Set<K> ks = keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        return (ks != null) ? ks : (keySet = new KeySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * reflected in the collection, and vice-versa.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * mapping from this map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * <tt>retainAll</tt>, and <tt>clear</tt> operations.  It does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * that will never throw {@link ConcurrentModificationException},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * and guarantees to traverse elements as they existed upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * construction of the iterator, and may (but is not guaranteed to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * reflect any modifications subsequent to construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        Collection<V> vs = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        return (vs != null) ? vs : (values = new Values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * reflected in the set, and vice-versa.  The set supports element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * removal, which removes the corresponding mapping from the map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * operations.  It does not support the <tt>add</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * that will never throw {@link ConcurrentModificationException},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * and guarantees to traverse elements as they existed upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * construction of the iterator, and may (but is not guaranteed to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * reflect any modifications subsequent to construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        Set<Map.Entry<K,V>> es = entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        return (es != null) ? es : (entrySet = new EntrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * Returns an enumeration of the keys in this table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @return an enumeration of the keys in this table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @see #keySet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    public Enumeration<K> keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        return new KeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * Returns an enumeration of the values in this table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @return an enumeration of the values in this table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @see #values()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    public Enumeration<V> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        return new ValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    /* ---------------- Iterator Support -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    abstract class HashIterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        int nextSegmentIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        int nextTableIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        HashEntry<K,V>[] currentTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        HashEntry<K, V> nextEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        HashEntry<K, V> lastReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        HashIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            nextSegmentIndex = segments.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            nextTableIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            advance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        public boolean hasMoreElements() { return hasNext(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        final void advance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            if (nextEntry != null && (nextEntry = nextEntry.next) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            while (nextTableIndex >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                if ( (nextEntry = currentTable[nextTableIndex--]) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            while (nextSegmentIndex >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                Segment<K,V> seg = segments[nextSegmentIndex--];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                if (seg.count != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                    currentTable = seg.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                    for (int j = currentTable.length - 1; j >= 0; --j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                        if ( (nextEntry = currentTable[j]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                            nextTableIndex = j - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        public boolean hasNext() { return nextEntry != null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        HashEntry<K,V> nextEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            if (nextEntry == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            lastReturned = nextEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            advance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            return lastReturned;
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 void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            if (lastReturned == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            ConcurrentHashMap.this.remove(lastReturned.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    final class KeyIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        extends HashIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        implements Iterator<K>, Enumeration<K>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        public K next()        { return super.nextEntry().key; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        public K nextElement() { return super.nextEntry().key; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    final class ValueIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        extends HashIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        implements Iterator<V>, Enumeration<V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        public V next()        { return super.nextEntry().value; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        public V nextElement() { return super.nextEntry().value; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * Custom Entry class used by EntryIterator.next(), that relays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * setValue changes to the underlying map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    final class WriteThroughEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        extends AbstractMap.SimpleEntry<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        WriteThroughEntry(K k, V v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            super(k,v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
         * Set our entry's value and write through to the map. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
         * value to return is somewhat arbitrary here. Since a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
         * WriteThroughEntry does not necessarily track asynchronous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
         * changes, the most recent "previous" value could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
         * different from what we return (or could even have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
         * removed in which case the put will re-establish). We do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
         * and cannot guarantee more.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            if (value == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            V v = super.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            ConcurrentHashMap.this.put(getKey(), value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            return v;
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
    final class EntryIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        extends HashIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        implements Iterator<Entry<K,V>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            HashEntry<K,V> e = super.nextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            return new WriteThroughEntry(e.key, e.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    final class KeySet extends AbstractSet<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            return new KeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            return ConcurrentHashMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            return ConcurrentHashMap.this.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            return ConcurrentHashMap.this.containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            return ConcurrentHashMap.this.remove(o) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            ConcurrentHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    final class Values extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            return new ValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            return ConcurrentHashMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            return ConcurrentHashMap.this.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            return ConcurrentHashMap.this.containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            ConcurrentHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            return new EntryIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            V v = ConcurrentHashMap.this.get(e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            return v != null && v.equals(e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            return ConcurrentHashMap.this.remove(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            return ConcurrentHashMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            return ConcurrentHashMap.this.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            ConcurrentHashMap.this.clear();
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
    /* ---------------- Serialization Support -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * Save the state of the <tt>ConcurrentHashMap</tt> instance to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * stream (i.e., serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * @param s the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * @serialData
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * the key (Object) and value (Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * for each key-value mapping, followed by a null pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * The key-value mappings are emitted in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1273
    private void writeObject(java.io.ObjectOutputStream s) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        for (int k = 0; k < segments.length; ++k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            Segment<K,V> seg = segments[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            seg.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                HashEntry<K,V>[] tab = seg.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                for (int i = 0; i < tab.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                    for (HashEntry<K,V> e = tab[i]; e != null; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                        s.writeObject(e.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                        s.writeObject(e.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                seg.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * Reconstitute the <tt>ConcurrentHashMap</tt> instance from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * stream (i.e., deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * @param s the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    private void readObject(java.io.ObjectInputStream s)
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
  1301
        throws IOException, ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        // Initialize each segment to be minimally sized, and let grow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        for (int i = 0; i < segments.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            segments[i].setTable(new HashEntry[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        // Read the keys and values, and put the mappings in the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            K key = (K) s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            V value = (V) s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
}