jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java
author psandoz
Tue, 12 May 2015 10:50:40 +0200
changeset 30441 415a5242e046
parent 25859 3317bb8137f4
child 30655 d83f50188ca9
permissions -rw-r--r--
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false Reviewed-by: martin, dholmes Contributed-by: Doug Lea <dl@cs.oswego.edu>, Paul Sandoz <paul.sandoz@oracle.com>
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;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
    37
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
    38
import java.io.ObjectStreamField;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.Serializable;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    40
import java.lang.reflect.ParameterizedType;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    41
import java.lang.reflect.Type;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    42
import java.util.AbstractMap;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    43
import java.util.Arrays;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    44
import java.util.Collection;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    45
import java.util.Comparator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    46
import java.util.Enumeration;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    47
import java.util.HashMap;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    48
import java.util.Hashtable;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    49
import java.util.Iterator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    50
import java.util.Map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    51
import java.util.NoSuchElementException;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    52
import java.util.Set;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    53
import java.util.Spliterator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    54
import java.util.concurrent.ConcurrentMap;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    55
import java.util.concurrent.ForkJoinPool;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    56
import java.util.concurrent.atomic.AtomicReference;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
    57
import java.util.concurrent.locks.LockSupport;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    58
import java.util.concurrent.locks.ReentrantLock;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    59
import java.util.function.BiConsumer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    60
import java.util.function.BiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    61
import java.util.function.BinaryOperator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    62
import java.util.function.Consumer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    63
import java.util.function.DoubleBinaryOperator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    64
import java.util.function.Function;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    65
import java.util.function.IntBinaryOperator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    66
import java.util.function.LongBinaryOperator;
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
    67
import java.util.function.Predicate;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    68
import java.util.function.ToDoubleBiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    69
import java.util.function.ToDoubleFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    70
import java.util.function.ToIntBiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    71
import java.util.function.ToIntFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    72
import java.util.function.ToLongBiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    73
import java.util.function.ToLongFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    74
import java.util.stream.Stream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * A hash table supporting full concurrency of retrievals and
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    78
 * high expected concurrency for updates. This class obeys the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * same functional specification as {@link java.util.Hashtable}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * includes versions of methods corresponding to each method of
17717
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
    81
 * {@code Hashtable}. However, even though all operations are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * thread-safe, retrieval operations do <em>not</em> entail locking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * and there is <em>not</em> any support for locking the entire table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * in a way that prevents all access.  This class is fully
17717
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
    85
 * interoperable with {@code Hashtable} in programs that rely on its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * thread safety but not on its synchronization details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    88
 * <p>Retrieval operations (including {@code get}) generally do not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    89
 * block, so may overlap with update operations (including {@code put}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    90
 * and {@code remove}). Retrievals reflect the results of the most
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    91
 * recently <em>completed</em> update operations holding upon their
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    92
 * onset. (More formally, an update operation for a given key bears a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    93
 * <em>happens-before</em> relation with any (non-null) retrieval for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    94
 * that key reporting the updated value.)  For aggregate operations
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    95
 * such as {@code putAll} and {@code clear}, concurrent retrievals may
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    96
 * reflect insertion or removal of only some entries.  Similarly,
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
    97
 * Iterators, Spliterators and Enumerations return elements reflecting the
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
    98
 * state of the hash table at some point at or since the creation of the
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    99
 * iterator/enumeration.  They do <em>not</em> throw {@link
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
   100
 * java.util.ConcurrentModificationException ConcurrentModificationException}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
   101
 * However, iterators are designed to be used by only one thread at a time.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
   102
 * Bear in mind that the results of aggregate status methods including
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
   103
 * {@code size}, {@code isEmpty}, and {@code containsValue} are typically
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
   104
 * useful only when a map is not undergoing concurrent updates in other threads.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   105
 * Otherwise the results of these methods reflect transient states
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   106
 * that may be adequate for monitoring or estimation purposes, but not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   107
 * for program control.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   109
 * <p>The table is dynamically expanded when there are too many
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   110
 * collisions (i.e., keys that have distinct hash codes but fall into
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   111
 * the same slot modulo the table size), with the expected average
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   112
 * effect of maintaining roughly two bins per mapping (corresponding
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   113
 * to a 0.75 load factor threshold for resizing). There may be much
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   114
 * variance around this average as mappings are added and removed, but
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   115
 * overall, this maintains a commonly accepted time/space tradeoff for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   116
 * hash tables.  However, resizing this or any other kind of hash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   117
 * table may be a relatively slow operation. When possible, it is a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   118
 * good idea to provide a size estimate as an optional {@code
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   119
 * initialCapacity} constructor argument. An additional optional
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   120
 * {@code loadFactor} constructor argument provides a further means of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   121
 * customizing initial table capacity by specifying the table density
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   122
 * to be used in calculating the amount of space to allocate for the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   123
 * given number of elements.  Also, for compatibility with previous
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   124
 * versions of this class, constructors may optionally specify an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   125
 * expected {@code concurrencyLevel} as an additional hint for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   126
 * internal sizing.  Note that using many keys with exactly the same
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   127
 * {@code hashCode()} is a sure way to slow down performance of any
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   128
 * hash table. To ameliorate impact, when keys are {@link Comparable},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   129
 * this class may use comparison order among keys to help break ties.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   130
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   131
 * <p>A {@link Set} projection of a ConcurrentHashMap may be created
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   132
 * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   133
 * (using {@link #keySet(Object)} when only keys are of interest, and the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   134
 * mapped values are (perhaps transiently) not used or all take the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   135
 * same mapping value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   136
 *
24972
d3304c4e66b5 8044730: small errors in ConcurrentHashMap and LongAdder docs
smarks
parents: 21981
diff changeset
   137
 * <p>A ConcurrentHashMap can be used as a scalable frequency map (a
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   138
 * form of histogram or multiset) by using {@link
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   139
 * java.util.concurrent.atomic.LongAdder} values and initializing via
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   140
 * {@link #computeIfAbsent computeIfAbsent}. For example, to add a count
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   141
 * to a {@code ConcurrentHashMap<String,LongAdder> freqs}, you can use
24972
d3304c4e66b5 8044730: small errors in ConcurrentHashMap and LongAdder docs
smarks
parents: 21981
diff changeset
   142
 * {@code freqs.computeIfAbsent(key, k -> new LongAdder()).increment();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * <p>This class and its views and iterators implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <em>optional</em> methods of the {@link Map} and {@link Iterator}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *
17717
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
   148
 * <p>Like {@link Hashtable} but unlike {@link HashMap}, this class
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
   149
 * does <em>not</em> allow {@code null} to be used as a key or value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   151
 * <p>ConcurrentHashMaps support a set of sequential and parallel bulk
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   152
 * operations that, unlike most {@link Stream} methods, are designed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   153
 * to be safely, and often sensibly, applied even with maps that are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   154
 * being concurrently updated by other threads; for example, when
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   155
 * computing a snapshot summary of the values in a shared registry.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   156
 * There are three kinds of operation, each with four forms, accepting
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   157
 * functions with Keys, Values, Entries, and (Key, Value) arguments
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   158
 * and/or return values. Because the elements of a ConcurrentHashMap
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   159
 * are not ordered in any particular way, and may be processed in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   160
 * different orders in different parallel executions, the correctness
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   161
 * of supplied functions should not depend on any ordering, or on any
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   162
 * other objects or values that may transiently change while
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   163
 * computation is in progress; and except for forEach actions, should
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   164
 * ideally be side-effect-free. Bulk operations on {@link java.util.Map.Entry}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   165
 * objects do not support method {@code setValue}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   166
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   167
 * <ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   168
 * <li> forEach: Perform a given action on each element.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   169
 * A variant form applies a given transformation on each element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   170
 * before performing the action.</li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   171
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   172
 * <li> search: Return the first available non-null result of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   173
 * applying a given function on each element; skipping further
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   174
 * search when a result is found.</li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   175
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   176
 * <li> reduce: Accumulate each element.  The supplied reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   177
 * function cannot rely on ordering (more formally, it should be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   178
 * both associative and commutative).  There are five variants:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   179
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   180
 * <ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   181
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   182
 * <li> Plain reductions. (There is not a form of this method for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   183
 * (key, value) function arguments since there is no corresponding
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   184
 * return type.)</li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   185
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   186
 * <li> Mapped reductions that accumulate the results of a given
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   187
 * function applied to each element.</li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   188
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   189
 * <li> Reductions to scalar doubles, longs, and ints, using a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   190
 * given basis value.</li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   191
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   192
 * </ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   193
 * </li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   194
 * </ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   195
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   196
 * <p>These bulk operations accept a {@code parallelismThreshold}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   197
 * argument. Methods proceed sequentially if the current map size is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   198
 * estimated to be less than the given threshold. Using a value of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   199
 * {@code Long.MAX_VALUE} suppresses all parallelism.  Using a value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   200
 * of {@code 1} results in maximal parallelism by partitioning into
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   201
 * enough subtasks to fully utilize the {@link
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   202
 * ForkJoinPool#commonPool()} that is used for all parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   203
 * computations. Normally, you would initially choose one of these
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   204
 * extreme values, and then measure performance of using in-between
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   205
 * values that trade off overhead versus throughput.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   206
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   207
 * <p>The concurrency properties of bulk operations follow
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   208
 * from those of ConcurrentHashMap: Any non-null result returned
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   209
 * from {@code get(key)} and related access methods bears a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   210
 * happens-before relation with the associated insertion or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   211
 * update.  The result of any bulk operation reflects the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   212
 * composition of these per-element relations (but is not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   213
 * necessarily atomic with respect to the map as a whole unless it
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   214
 * is somehow known to be quiescent).  Conversely, because keys
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   215
 * and values in the map are never null, null serves as a reliable
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   216
 * atomic indicator of the current lack of any result.  To
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   217
 * maintain this property, null serves as an implicit basis for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   218
 * all non-scalar reduction operations. For the double, long, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   219
 * int versions, the basis should be one that, when combined with
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   220
 * any other value, returns that other value (more formally, it
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   221
 * should be the identity element for the reduction). Most common
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   222
 * reductions have these properties; for example, computing a sum
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   223
 * with basis 0 or a minimum with basis MAX_VALUE.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   224
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   225
 * <p>Search and transformation functions provided as arguments
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   226
 * should similarly return null to indicate the lack of any result
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   227
 * (in which case it is not used). In the case of mapped
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   228
 * reductions, this also enables transformations to serve as
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   229
 * filters, returning null (or, in the case of primitive
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   230
 * specializations, the identity basis) if the element should not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   231
 * be combined. You can create compound transformations and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   232
 * filterings by composing them yourself under this "null means
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   233
 * there is nothing there now" rule before using them in search or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   234
 * reduce operations.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   235
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   236
 * <p>Methods accepting and/or returning Entry arguments maintain
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   237
 * key-value associations. They may be useful for example when
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   238
 * finding the key for the greatest value. Note that "plain" Entry
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   239
 * arguments can be supplied using {@code new
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   240
 * AbstractMap.SimpleEntry(k,v)}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   241
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   242
 * <p>Bulk operations may complete abruptly, throwing an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   243
 * exception encountered in the application of a supplied
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   244
 * function. Bear in mind when handling such exceptions that other
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   245
 * concurrently executing functions could also have thrown
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   246
 * exceptions, or would have done so if the first exception had
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   247
 * not occurred.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   248
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   249
 * <p>Speedups for parallel compared to sequential forms are common
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   250
 * but not guaranteed.  Parallel operations involving brief functions
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   251
 * on small maps may execute more slowly than sequential forms if the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   252
 * underlying work to parallelize the computation is more expensive
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   253
 * than the computation itself.  Similarly, parallelization may not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   254
 * lead to much actual parallelism if all processors are busy
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   255
 * performing unrelated tasks.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   256
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   257
 * <p>All arguments to all task methods must be non-null.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   258
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 */
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   268
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   269
    implements ConcurrentMap<K,V>, Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static final long serialVersionUID = 7249069246763182397L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /*
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   273
     * Overview:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   274
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   275
     * The primary design goal of this hash table is to maintain
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   276
     * concurrent readability (typically method get(), but also
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   277
     * iterators and related methods) while minimizing update
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   278
     * contention. Secondary goals are to keep space consumption about
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   279
     * the same or better than java.util.HashMap, and to support high
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   280
     * initial insertion rates on an empty table by many threads.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   281
     *
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   282
     * This map usually acts as a binned (bucketed) hash table.  Each
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   283
     * key-value mapping is held in a Node.  Most nodes are instances
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   284
     * of the basic Node class with hash, key, value, and next
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   285
     * fields. However, various subclasses exist: TreeNodes are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   286
     * arranged in balanced trees, not lists.  TreeBins hold the roots
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   287
     * of sets of TreeNodes. ForwardingNodes are placed at the heads
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   288
     * of bins during resizing. ReservationNodes are used as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   289
     * placeholders while establishing values in computeIfAbsent and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   290
     * related methods.  The types TreeBin, ForwardingNode, and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   291
     * ReservationNode do not hold normal user keys, values, or
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   292
     * hashes, and are readily distinguishable during search etc
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   293
     * because they have negative hash fields and null key and value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   294
     * fields. (These special nodes are either uncommon or transient,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   295
     * so the impact of carrying around some unused fields is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   296
     * insignificant.)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   297
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   298
     * The table is lazily initialized to a power-of-two size upon the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   299
     * first insertion.  Each bin in the table normally contains a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   300
     * list of Nodes (most often, the list has only zero or one Node).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   301
     * Table accesses require volatile/atomic reads, writes, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   302
     * CASes.  Because there is no other way to arrange this without
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   303
     * adding further indirections, we use intrinsics
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   304
     * (sun.misc.Unsafe) operations.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   305
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   306
     * We use the top (sign) bit of Node hash fields for control
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   307
     * purposes -- it is available anyway because of addressing
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   308
     * constraints.  Nodes with negative hash fields are specially
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   309
     * handled or ignored in map methods.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   310
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   311
     * Insertion (via put or its variants) of the first node in an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   312
     * empty bin is performed by just CASing it to the bin.  This is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   313
     * by far the most common case for put operations under most
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   314
     * key/hash distributions.  Other update operations (insert,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   315
     * delete, and replace) require locks.  We do not want to waste
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   316
     * the space required to associate a distinct lock object with
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   317
     * each bin, so instead use the first node of a bin list itself as
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   318
     * a lock. Locking support for these locks relies on builtin
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   319
     * "synchronized" monitors.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   320
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   321
     * Using the first node of a list as a lock does not by itself
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   322
     * suffice though: When a node is locked, any update must first
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   323
     * validate that it is still the first node after locking it, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   324
     * retry if not. Because new nodes are always appended to lists,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   325
     * once a node is first in a bin, it remains first until deleted
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   326
     * or the bin becomes invalidated (upon resizing).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   327
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   328
     * The main disadvantage of per-bin locks is that other update
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   329
     * operations on other nodes in a bin list protected by the same
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   330
     * lock can stall, for example when user equals() or mapping
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   331
     * functions take a long time.  However, statistically, under
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   332
     * random hash codes, this is not a common problem.  Ideally, the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   333
     * frequency of nodes in bins follows a Poisson distribution
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   334
     * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   335
     * parameter of about 0.5 on average, given the resizing threshold
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   336
     * of 0.75, although with a large variance because of resizing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   337
     * granularity. Ignoring variance, the expected occurrences of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   338
     * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   339
     * first values are:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   340
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   341
     * 0:    0.60653066
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   342
     * 1:    0.30326533
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   343
     * 2:    0.07581633
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   344
     * 3:    0.01263606
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   345
     * 4:    0.00157952
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   346
     * 5:    0.00015795
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   347
     * 6:    0.00001316
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   348
     * 7:    0.00000094
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   349
     * 8:    0.00000006
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   350
     * more: less than 1 in ten million
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   351
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   352
     * Lock contention probability for two threads accessing distinct
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   353
     * elements is roughly 1 / (8 * #elements) under random hashes.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   354
     *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   355
     * Actual hash code distributions encountered in practice
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   356
     * sometimes deviate significantly from uniform randomness.  This
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   357
     * includes the case when N > (1<<30), so some keys MUST collide.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   358
     * Similarly for dumb or hostile usages in which multiple keys are
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   359
     * designed to have identical hash codes or ones that differs only
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   360
     * in masked-out high bits. So we use a secondary strategy that
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   361
     * applies when the number of nodes in a bin exceeds a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   362
     * threshold. These TreeBins use a balanced tree to hold nodes (a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   363
     * specialized form of red-black trees), bounding search time to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   364
     * O(log N).  Each search step in a TreeBin is at least twice as
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   365
     * slow as in a regular list, but given that N cannot exceed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   366
     * (1<<64) (before running out of addresses) this bounds search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   367
     * steps, lock hold times, etc, to reasonable constants (roughly
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   368
     * 100 nodes inspected per operation worst case) so long as keys
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   369
     * are Comparable (which is very common -- String, Long, etc).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   370
     * TreeBin nodes (TreeNodes) also maintain the same "next"
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   371
     * traversal pointers as regular nodes, so can be traversed in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   372
     * iterators in the same way.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   373
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   374
     * The table is resized when occupancy exceeds a percentage
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   375
     * threshold (nominally, 0.75, but see below).  Any thread
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   376
     * noticing an overfull bin may assist in resizing after the
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   377
     * initiating thread allocates and sets up the replacement array.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   378
     * However, rather than stalling, these other threads may proceed
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   379
     * with insertions etc.  The use of TreeBins shields us from the
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   380
     * worst case effects of overfilling while resizes are in
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   381
     * progress.  Resizing proceeds by transferring bins, one by one,
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   382
     * from the table to the next table. However, threads claim small
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   383
     * blocks of indices to transfer (via field transferIndex) before
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   384
     * doing so, reducing contention.  A generation stamp in field
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   385
     * sizeCtl ensures that resizings do not overlap. Because we are
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   386
     * using power-of-two expansion, the elements from each bin must
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   387
     * either stay at same index, or move with a power of two
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   388
     * offset. We eliminate unnecessary node creation by catching
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   389
     * cases where old nodes can be reused because their next fields
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   390
     * won't change.  On average, only about one-sixth of them need
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   391
     * cloning when a table doubles. The nodes they replace will be
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   392
     * garbage collectable as soon as they are no longer referenced by
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   393
     * any reader thread that may be in the midst of concurrently
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   394
     * traversing table.  Upon transfer, the old table bin contains
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   395
     * only a special forwarding node (with hash field "MOVED") that
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   396
     * contains the next table as its key. On encountering a
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   397
     * forwarding node, access and update operations restart, using
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   398
     * the new table.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   399
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   400
     * Each bin transfer requires its bin lock, which can stall
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   401
     * waiting for locks while resizing. However, because other
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   402
     * threads can join in and help resize rather than contend for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   403
     * locks, average aggregate waits become shorter as resizing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   404
     * progresses.  The transfer operation must also ensure that all
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   405
     * accessible bins in both the old and new table are usable by any
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   406
     * traversal.  This is arranged in part by proceeding from the
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   407
     * last bin (table.length - 1) up towards the first.  Upon seeing
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   408
     * a forwarding node, traversals (see class Traverser) arrange to
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   409
     * move to the new table without revisiting nodes.  To ensure that
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   410
     * no intervening nodes are skipped even when moved out of order,
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   411
     * a stack (see class TableStack) is created on first encounter of
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   412
     * a forwarding node during a traversal, to maintain its place if
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   413
     * later processing the current table. The need for these
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   414
     * save/restore mechanics is relatively rare, but when one
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   415
     * forwarding node is encountered, typically many more will be.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   416
     * So Traversers use a simple caching scheme to avoid creating so
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   417
     * many new TableStack nodes. (Thanks to Peter Levart for
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   418
     * suggesting use of a stack here.)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   419
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   420
     * The traversal scheme also applies to partial traversals of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   421
     * ranges of bins (via an alternate Traverser constructor)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   422
     * to support partitioned aggregate operations.  Also, read-only
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   423
     * operations give up if ever forwarded to a null table, which
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   424
     * provides support for shutdown-style clearing, which is also not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   425
     * currently implemented.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   426
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   427
     * Lazy table initialization minimizes footprint until first use,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   428
     * and also avoids resizings when the first operation is from a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   429
     * putAll, constructor with map argument, or deserialization.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   430
     * These cases attempt to override the initial capacity settings,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   431
     * but harmlessly fail to take effect in cases of races.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   432
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   433
     * The element count is maintained using a specialization of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   434
     * LongAdder. We need to incorporate a specialization rather than
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   435
     * just use a LongAdder in order to access implicit
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   436
     * contention-sensing that leads to creation of multiple
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   437
     * CounterCells.  The counter mechanics avoid contention on
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   438
     * updates but can encounter cache thrashing if read too
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   439
     * frequently during concurrent access. To avoid reading so often,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   440
     * resizing under contention is attempted only upon adding to a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   441
     * bin already holding two or more nodes. Under uniform hash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   442
     * distributions, the probability of this occurring at threshold
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   443
     * is around 13%, meaning that only about 1 in 8 puts check
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   444
     * threshold (and after resizing, many fewer do so).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   445
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   446
     * TreeBins use a special form of comparison for search and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   447
     * related operations (which is the main reason we cannot use
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   448
     * existing collections such as TreeMaps). TreeBins contain
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   449
     * Comparable elements, but may contain others, as well as
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   450
     * elements that are Comparable but not necessarily Comparable for
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   451
     * the same T, so we cannot invoke compareTo among them. To handle
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   452
     * this, the tree is ordered primarily by hash value, then by
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   453
     * Comparable.compareTo order if applicable.  On lookup at a node,
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   454
     * if elements are not comparable or compare as 0 then both left
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   455
     * and right children may need to be searched in the case of tied
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   456
     * hash values. (This corresponds to the full list search that
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   457
     * would be necessary if all elements were non-Comparable and had
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   458
     * tied hashes.) On insertion, to keep a total ordering (or as
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   459
     * close as is required here) across rebalancings, we compare
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   460
     * classes and identityHashCodes as tie-breakers. The red-black
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   461
     * balancing code is updated from pre-jdk-collections
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   462
     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   463
     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   464
     * Algorithms" (CLR).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   465
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   466
     * TreeBins also require an additional locking mechanism.  While
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   467
     * list traversal is always possible by readers even during
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   468
     * updates, tree traversal is not, mainly because of tree-rotations
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   469
     * that may change the root node and/or its linkages.  TreeBins
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   470
     * include a simple read-write lock mechanism parasitic on the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   471
     * main bin-synchronization strategy: Structural adjustments
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   472
     * associated with an insertion or removal are already bin-locked
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   473
     * (and so cannot conflict with other writers) but must wait for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   474
     * ongoing readers to finish. Since there can be only one such
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   475
     * waiter, we use a simple scheme using a single "waiter" field to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   476
     * block writers.  However, readers need never block.  If the root
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   477
     * lock is held, they proceed along the slow traversal path (via
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   478
     * next-pointers) until the lock becomes available or the list is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   479
     * exhausted, whichever comes first. These cases are not fast, but
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   480
     * maximize aggregate expected throughput.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   481
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   482
     * Maintaining API and serialization compatibility with previous
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   483
     * versions of this class introduces several oddities. Mainly: We
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   484
     * leave untouched but unused constructor arguments refering to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   485
     * concurrencyLevel. We accept a loadFactor constructor argument,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   486
     * but apply it only to initial table capacity (which is the only
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   487
     * time that we can guarantee to honor it.) We also declare an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   488
     * unused "Segment" class that is instantiated in minimal form
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   489
     * only when serializing.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   490
     *
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   491
     * Also, solely for compatibility with previous versions of this
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   492
     * class, it extends AbstractMap, even though all of its methods
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   493
     * are overridden, so it is just useless baggage.
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   494
     *
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   495
     * This file is organized to make things a little easier to follow
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   496
     * while reading than they might otherwise: First the main static
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   497
     * declarations and utilities, then fields, then main public
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   498
     * methods (with a few factorings of multiple public methods into
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   499
     * internal ones), then sizing methods, trees, traversers, and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   500
     * bulk operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /* ---------------- Constants -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   506
     * The largest possible table capacity.  This value must be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   507
     * exactly 1<<30 to stay within Java array allocation and indexing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   508
     * bounds for power of two table sizes, and is further required
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   509
     * because the top two bits of 32bit hash fields are used for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   510
     * control purposes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   512
    private static final int MAXIMUM_CAPACITY = 1 << 30;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   513
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   514
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   515
     * The default initial table capacity.  Must be a power of 2
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   516
     * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   517
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   518
    private static final int DEFAULT_CAPACITY = 16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   521
     * The largest possible (non-power of two) array size.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   522
     * Needed by toArray and related methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   524
    static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   527
     * The default concurrency level for this table. Unused but
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   528
     * defined for compatibility with previous versions of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   530
    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   531
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   532
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   533
     * The load factor for this table. Overrides of this value in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   534
     * constructors affect only the initial table capacity.  The
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   535
     * actual floating point value isn't normally used -- it is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   536
     * simpler to use expressions such as {@code n - (n >>> 2)} for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   537
     * the associated resizing threshold.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   538
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   539
    private static final float LOAD_FACTOR = 0.75f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   542
     * The bin count threshold for using a tree rather than list for a
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   543
     * bin.  Bins are converted to trees when adding an element to a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   544
     * bin with at least this many nodes. The value must be greater
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   545
     * than 2, and should be at least 8 to mesh with assumptions in
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   546
     * tree removal about conversion back to plain bins upon
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   547
     * shrinkage.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   549
    static final int TREEIFY_THRESHOLD = 8;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   550
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   551
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   552
     * The bin count threshold for untreeifying a (split) bin during a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   553
     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   554
     * most 6 to mesh with shrinkage detection under removal.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   555
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   556
    static final int UNTREEIFY_THRESHOLD = 6;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   557
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   558
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   559
     * The smallest table capacity for which bins may be treeified.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   560
     * (Otherwise the table is resized if too many nodes in a bin.)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   561
     * The value should be at least 4 * TREEIFY_THRESHOLD to avoid
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   562
     * conflicts between resizing and treeification thresholds.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   563
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   564
    static final int MIN_TREEIFY_CAPACITY = 64;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   567
     * Minimum number of rebinnings per transfer step. Ranges are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   568
     * subdivided to allow multiple resizer threads.  This value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   569
     * serves as a lower bound to avoid resizers encountering
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   570
     * excessive memory contention.  The value should be at least
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   571
     * DEFAULT_CAPACITY.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   572
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   573
    private static final int MIN_TRANSFER_STRIDE = 16;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   574
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   575
    /**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   576
     * The number of bits used for generation stamp in sizeCtl.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   577
     * Must be at least 6 for 32bit arrays.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   578
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   579
    private static int RESIZE_STAMP_BITS = 16;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   580
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   581
    /**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   582
     * The maximum number of threads that can help resize.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   583
     * Must fit in 32 - RESIZE_STAMP_BITS bits.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   584
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   585
    private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   586
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   587
    /**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   588
     * The bit shift for recording size stamp in sizeCtl.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   589
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   590
    private static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   591
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   592
    /*
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   593
     * Encodings for Node hash fields. See above for explanation.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   594
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   595
    static final int MOVED     = -1; // hash for forwarding nodes
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   596
    static final int TREEBIN   = -2; // hash for roots of trees
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   597
    static final int RESERVED  = -3; // hash for transient reservations
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   598
    static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   599
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   600
    /** Number of CPUS, to place bounds on some sizings */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   601
    static final int NCPU = Runtime.getRuntime().availableProcessors();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   602
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   603
    /** For serialization compatibility. */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   604
    private static final ObjectStreamField[] serialPersistentFields = {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   605
        new ObjectStreamField("segments", Segment[].class),
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   606
        new ObjectStreamField("segmentMask", Integer.TYPE),
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   607
        new ObjectStreamField("segmentShift", Integer.TYPE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   608
    };
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   609
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   610
    /* ---------------- Nodes -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   611
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   612
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   613
     * Key-value entry.  This class is never exported out as a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   614
     * user-mutable Map.Entry (i.e., one supporting setValue; see
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   615
     * MapEntry below), but can be used for read-only traversals used
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   616
     * in bulk tasks.  Subclasses of Node with a negative hash field
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   617
     * are special, and contain null keys and values (but are never
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   618
     * exported).  Otherwise, keys and vals are never null.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   619
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   620
    static class Node<K,V> implements Map.Entry<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   621
        final int hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   622
        final K key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   623
        volatile V val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   624
        volatile Node<K,V> next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   625
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   626
        Node(int hash, K key, V val, Node<K,V> next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   627
            this.hash = hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   628
            this.key = key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   629
            this.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   630
            this.next = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   631
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   632
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   633
        public final K getKey()       { return key; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   634
        public final V getValue()     { return val; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   635
        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   636
        public final String toString(){ return key + "=" + val; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   637
        public final V setValue(V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   638
            throw new UnsupportedOperationException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   639
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   640
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   641
        public final boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   642
            Object k, v, u; Map.Entry<?,?> e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   643
            return ((o instanceof Map.Entry) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   644
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   645
                    (v = e.getValue()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   646
                    (k == key || k.equals(key)) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   647
                    (v == (u = val) || v.equals(u)));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   648
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   649
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   650
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   651
         * Virtualized support for map.get(); overridden in subclasses.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   652
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   653
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   654
            Node<K,V> e = this;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   655
            if (k != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   656
                do {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   657
                    K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   658
                    if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   659
                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   660
                        return e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   661
                } while ((e = e.next) != null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   662
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   663
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   664
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   665
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   666
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   667
    /* ---------------- Static utilities -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   668
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   669
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   670
     * Spreads (XORs) higher bits of hash to lower and also forces top
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   671
     * bit to 0. Because the table uses power-of-two masking, sets of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   672
     * hashes that vary only in bits above the current mask will
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   673
     * always collide. (Among known examples are sets of Float keys
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   674
     * holding consecutive whole numbers in small tables.)  So we
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   675
     * apply a transform that spreads the impact of higher bits
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   676
     * downward. There is a tradeoff between speed, utility, and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   677
     * quality of bit-spreading. Because many common sets of hashes
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   678
     * are already reasonably distributed (so don't benefit from
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   679
     * spreading), and because we use trees to handle large sets of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   680
     * collisions in bins, we just XOR some shifted bits in the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   681
     * cheapest possible way to reduce systematic lossage, as well as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   682
     * to incorporate impact of the highest bits that would otherwise
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   683
     * never be used in index calculations because of table bounds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   685
    static final int spread(int h) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   686
        return (h ^ (h >>> 16)) & HASH_BITS;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   687
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   688
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   689
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   690
     * Returns a power of two table size for the given desired capacity.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   691
     * See Hackers Delight, sec 3.2
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   692
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   693
    private static final int tableSizeFor(int c) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   694
        int n = c - 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   695
        n |= n >>> 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   696
        n |= n >>> 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   697
        n |= n >>> 4;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   698
        n |= n >>> 8;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   699
        n |= n >>> 16;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   700
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   701
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   702
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   703
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   704
     * Returns x's Class if it is of the form "class C implements
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   705
     * Comparable<C>", else null.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   706
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   707
    static Class<?> comparableClassFor(Object x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   708
        if (x instanceof Comparable) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   709
            Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   710
            if ((c = x.getClass()) == String.class) // bypass checks
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   711
                return c;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   712
            if ((ts = c.getGenericInterfaces()) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   713
                for (int i = 0; i < ts.length; ++i) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   714
                    if (((t = ts[i]) instanceof ParameterizedType) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   715
                        ((p = (ParameterizedType)t).getRawType() ==
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   716
                         Comparable.class) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   717
                        (as = p.getActualTypeArguments()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   718
                        as.length == 1 && as[0] == c) // type arg is c
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   719
                        return c;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   720
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   721
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   722
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   723
        return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   724
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   725
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   726
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   727
     * Returns k.compareTo(x) if x matches kc (k's screened comparable
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   728
     * class), else 0.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   729
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   730
    @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   731
    static int compareComparables(Class<?> kc, Object k, Object x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   732
        return (x == null || x.getClass() != kc ? 0 :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   733
                ((Comparable)k).compareTo(x));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   734
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   735
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   736
    /* ---------------- Table element access -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   737
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   738
    /*
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   739
     * Volatile access methods are used for table elements as well as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   740
     * elements of in-progress next table while resizing.  All uses of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   741
     * the tab arguments must be null checked by callers.  All callers
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   742
     * also paranoically precheck that tab's length is not zero (or an
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   743
     * equivalent check), thus ensuring that any index argument taking
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   744
     * the form of a hash value anded with (length - 1) is a valid
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   745
     * index.  Note that, to be correct wrt arbitrary concurrency
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   746
     * errors by users, these checks must operate on local variables,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   747
     * which accounts for some odd-looking inline assignments below.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   748
     * Note that calls to setTabAt always occur within locked regions,
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   749
     * and so in principle require only release ordering, not
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   750
     * full volatile semantics, but are currently coded as volatile
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   751
     * writes to be conservative.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   752
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   753
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   754
    @SuppressWarnings("unchecked")
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   755
    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   756
        return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   757
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   758
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   759
    static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   760
                                        Node<K,V> c, Node<K,V> v) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   761
        return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   762
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   763
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   764
    static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   765
        U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   766
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    /* ---------------- Fields -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   771
     * The array of bins. Lazily initialized upon first insertion.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   772
     * Size is always a power of two. Accessed directly by iterators.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   773
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   774
    transient volatile Node<K,V>[] table;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   775
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   776
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   777
     * The next table to use; non-null only while resizing.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   778
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   779
    private transient volatile Node<K,V>[] nextTable;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   780
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   781
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   782
     * Base counter value, used mainly when there is no contention,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   783
     * but also as a fallback during table initialization
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   784
     * races. Updated via CAS.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   785
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   786
    private transient volatile long baseCount;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   787
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   788
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   789
     * Table initialization and resizing control.  When negative, the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   790
     * table is being initialized or resized: -1 for initialization,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   791
     * else -(1 + the number of active resizing threads).  Otherwise,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   792
     * when table is null, holds the initial table size to use upon
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   793
     * creation, or 0 for default. After initialization, holds the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   794
     * next element count value upon which to resize the table.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   795
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   796
    private transient volatile int sizeCtl;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   797
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   798
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   799
     * The next table index (plus one) to split while resizing.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   800
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   801
    private transient volatile int transferIndex;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   802
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   803
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   804
     * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
   805
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   806
    private transient volatile int cellsBusy;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   807
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   808
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   809
     * Table of counter cells. When non-null, size is a power of 2.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   810
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   811
    private transient volatile CounterCell[] counterCells;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   812
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   813
    // views
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   814
    private transient KeySetView<K,V> keySet;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   815
    private transient ValuesView<K,V> values;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   816
    private transient EntrySetView<K,V> entrySet;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   817
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   818
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   819
    /* ---------------- Public operations -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   820
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   821
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   822
     * Creates a new, empty map with the default initial table size (16).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   823
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   824
    public ConcurrentHashMap() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   825
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   826
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   827
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   828
     * Creates a new, empty map with an initial table size
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   829
     * accommodating the specified number of elements without the need
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   830
     * to dynamically resize.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   831
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   832
     * @param initialCapacity The implementation performs internal
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   833
     * sizing to accommodate this many elements.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   834
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   835
     * elements is negative
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   836
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   837
    public ConcurrentHashMap(int initialCapacity) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   838
        if (initialCapacity < 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   839
            throw new IllegalArgumentException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   840
        int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   841
                   MAXIMUM_CAPACITY :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   842
                   tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   843
        this.sizeCtl = cap;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   844
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   845
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   846
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   847
     * Creates a new map with the same mappings as the given map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   848
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   849
     * @param m the map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   850
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   851
    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   852
        this.sizeCtl = DEFAULT_CAPACITY;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   853
        putAll(m);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   854
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   855
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   856
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   857
     * Creates a new, empty map with an initial table size based on
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   858
     * the given number of elements ({@code initialCapacity}) and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   859
     * initial table density ({@code loadFactor}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   860
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   861
     * @param initialCapacity the initial capacity. The implementation
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   862
     * performs internal sizing to accommodate this many elements,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   863
     * given the specified load factor.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   864
     * @param loadFactor the load factor (table density) for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   865
     * establishing the initial table size
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   866
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   867
     * elements is negative or the load factor is nonpositive
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   868
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   869
     * @since 1.6
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   870
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   871
    public ConcurrentHashMap(int initialCapacity, float loadFactor) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   872
        this(initialCapacity, loadFactor, 1);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   873
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   874
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
   875
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   876
     * Creates a new, empty map with an initial table size based on
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   877
     * the given number of elements ({@code initialCapacity}), table
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   878
     * density ({@code loadFactor}), and number of concurrently
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   879
     * updating threads ({@code concurrencyLevel}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   880
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   881
     * @param initialCapacity the initial capacity. The implementation
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   882
     * performs internal sizing to accommodate this many elements,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   883
     * given the specified load factor.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   884
     * @param loadFactor the load factor (table density) for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   885
     * establishing the initial table size
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   886
     * @param concurrencyLevel the estimated number of concurrently
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   887
     * updating threads. The implementation may use this value as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   888
     * a sizing hint.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   889
     * @throws IllegalArgumentException if the initial capacity is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   890
     * negative or the load factor or concurrencyLevel are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   891
     * nonpositive
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   892
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   893
    public ConcurrentHashMap(int initialCapacity,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   894
                             float loadFactor, int concurrencyLevel) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   895
        if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   896
            throw new IllegalArgumentException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   897
        if (initialCapacity < concurrencyLevel)   // Use at least as many bins
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   898
            initialCapacity = concurrencyLevel;   // as estimated threads
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   899
        long size = (long)(1.0 + (long)initialCapacity / loadFactor);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   900
        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   901
            MAXIMUM_CAPACITY : tableSizeFor((int)size);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   902
        this.sizeCtl = cap;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   903
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   904
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   905
    // Original (since JDK1.2) Map methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   906
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   907
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   908
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   909
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   910
    public int size() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   911
        long n = sumCount();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   912
        return ((n < 0L) ? 0 :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   913
                (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   914
                (int)n);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   915
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   916
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   917
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   918
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   920
    public boolean isEmpty() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   921
        return sumCount() <= 0L; // ignore transient negative values
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   922
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   923
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   924
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   925
     * Returns the value to which the specified key is mapped,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   926
     * or {@code null} if this map contains no mapping for the key.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   927
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   928
     * <p>More formally, if this map contains a mapping from a key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   929
     * {@code k} to a value {@code v} such that {@code key.equals(k)},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   930
     * then this method returns {@code v}; otherwise it returns
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   931
     * {@code null}.  (There can be at most one such mapping.)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   932
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   933
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   934
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   935
    public V get(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   936
        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   937
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   938
        if ((tab = table) != null && (n = tab.length) > 0 &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   939
            (e = tabAt(tab, (n - 1) & h)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   940
            if ((eh = e.hash) == h) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   941
                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   942
                    return e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   943
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   944
            else if (eh < 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   945
                return (p = e.find(h, key)) != null ? p.val : null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   946
            while ((e = e.next) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   947
                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   948
                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   949
                    return e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   950
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   951
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   952
        return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   953
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   954
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   955
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   956
     * Tests if the specified object is a key in this table.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   957
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   958
     * @param  key possible key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   959
     * @return {@code true} if and only if the specified object
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   960
     *         is a key in this table, as determined by the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   961
     *         {@code equals} method; {@code false} otherwise
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   962
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   963
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   964
    public boolean containsKey(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   965
        return get(key) != null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   966
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   967
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   968
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   969
     * Returns {@code true} if this map maps one or more keys to the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   970
     * specified value. Note: This method may require a full traversal
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   971
     * of the map, and is much slower than method {@code containsKey}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   972
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   973
     * @param value value whose presence in this map is to be tested
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   974
     * @return {@code true} if this map maps one or more keys to the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   975
     *         specified value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   976
     * @throws NullPointerException if the specified value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   977
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   978
    public boolean containsValue(Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   979
        if (value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   980
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   981
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   982
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   983
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   984
            for (Node<K,V> p; (p = it.advance()) != null; ) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   985
                V v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   986
                if ((v = p.val) == value || (v != null && value.equals(v)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   987
                    return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   988
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   989
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   990
        return false;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   991
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   994
     * Maps the specified key to the specified value in this table.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   995
     * Neither the key nor the value can be null.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   996
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   997
     * <p>The value can be retrieved by calling the {@code get} method
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   998
     * with a key that is equal to the original key.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   999
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1000
     * @param key key with which the specified value is to be associated
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1001
     * @param value value to be associated with the specified key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1002
     * @return the previous value associated with {@code key}, or
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1003
     *         {@code null} if there was no mapping for {@code key}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1004
     * @throws NullPointerException if the specified key or value is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1006
    public V put(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1007
        return putVal(key, value, false);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1008
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1009
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1010
    /** Implementation for put and putIfAbsent */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1011
    final V putVal(K key, V value, boolean onlyIfAbsent) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1012
        if (key == null || value == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1013
        int hash = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1014
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1015
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1016
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1017
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1018
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1019
            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1020
                if (casTabAt(tab, i, null,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1021
                             new Node<K,V>(hash, key, value, null)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1022
                    break;                   // no lock when adding to empty bin
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1023
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1024
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1025
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1026
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1027
                V oldVal = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1028
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1029
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1030
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1031
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1032
                            for (Node<K,V> e = f;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1033
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1034
                                if (e.hash == hash &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1035
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1036
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1037
                                    oldVal = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1038
                                    if (!onlyIfAbsent)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1039
                                        e.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1040
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1041
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1042
                                Node<K,V> pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1043
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1044
                                    pred.next = new Node<K,V>(hash, key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1045
                                                              value, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1046
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1047
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1048
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1049
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1050
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1051
                            Node<K,V> p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1052
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1053
                            if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1054
                                                           value)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1055
                                oldVal = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1056
                                if (!onlyIfAbsent)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1057
                                    p.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1058
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1059
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1060
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1061
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1062
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1063
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1064
                        treeifyBin(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1065
                    if (oldVal != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1066
                        return oldVal;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1067
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1068
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1069
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1070
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1071
        addCount(1L, binCount);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1072
        return null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1073
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1074
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1075
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1076
     * Copies all of the mappings from the specified map to this one.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1077
     * These mappings replace any mappings that this map had for any of the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1078
     * keys currently in the specified map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1079
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1080
     * @param m mappings to be stored in this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1081
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1082
    public void putAll(Map<? extends K, ? extends V> m) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1083
        tryPresize(m.size());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1084
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1085
            putVal(e.getKey(), e.getValue(), false);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1086
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1087
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1088
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1089
     * Removes the key (and its corresponding value) from this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1090
     * This method does nothing if the key is not in the map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1091
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1092
     * @param  key the key that needs to be removed
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1093
     * @return the previous value associated with {@code key}, or
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1094
     *         {@code null} if there was no mapping for {@code key}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1095
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1096
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1097
    public V remove(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1098
        return replaceNode(key, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1099
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1100
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1101
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1102
     * Implementation for the four public remove/replace methods:
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1103
     * Replaces node value with v, conditional upon match of cv if
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1104
     * non-null.  If resulting value is null, delete.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1105
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1106
    final V replaceNode(Object key, V value, Object cv) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1107
        int hash = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1108
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1109
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1110
            if (tab == null || (n = tab.length) == 0 ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1111
                (f = tabAt(tab, i = (n - 1) & hash)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1112
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1113
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1114
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1115
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1116
                V oldVal = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1117
                boolean validated = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1118
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1119
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1120
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1121
                            validated = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1122
                            for (Node<K,V> e = f, pred = null;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1123
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1124
                                if (e.hash == hash &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1125
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1126
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1127
                                    V ev = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1128
                                    if (cv == null || cv == ev ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1129
                                        (ev != null && cv.equals(ev))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1130
                                        oldVal = ev;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1131
                                        if (value != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1132
                                            e.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1133
                                        else if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1134
                                            pred.next = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1135
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1136
                                            setTabAt(tab, i, e.next);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1137
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1138
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1139
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1140
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1141
                                if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1142
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1143
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1144
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1145
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1146
                            validated = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1147
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1148
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1149
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1150
                                (p = r.findTreeNode(hash, key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1151
                                V pv = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1152
                                if (cv == null || cv == pv ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1153
                                    (pv != null && cv.equals(pv))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1154
                                    oldVal = pv;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1155
                                    if (value != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1156
                                        p.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1157
                                    else if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1158
                                        setTabAt(tab, i, untreeify(t.first));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1159
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1160
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1161
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1162
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1163
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1164
                if (validated) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1165
                    if (oldVal != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1166
                        if (value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1167
                            addCount(-1L, -1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1168
                        return oldVal;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1169
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1170
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1171
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1172
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1173
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1174
        return null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1175
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1178
     * Removes all of the mappings from this map.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  1179
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1180
    public void clear() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1181
        long delta = 0L; // negative number of deletions
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1182
        int i = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1183
        Node<K,V>[] tab = table;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1184
        while (tab != null && i < tab.length) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1185
            int fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1186
            Node<K,V> f = tabAt(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1187
            if (f == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1188
                ++i;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1189
            else if ((fh = f.hash) == MOVED) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1190
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1191
                i = 0; // restart
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1192
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1193
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1194
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1195
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1196
                        Node<K,V> p = (fh >= 0 ? f :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1197
                                       (f instanceof TreeBin) ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1198
                                       ((TreeBin<K,V>)f).first : null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1199
                        while (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1200
                            --delta;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1201
                            p = p.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1202
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1203
                        setTabAt(tab, i++, null);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1204
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1205
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1206
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1207
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1208
        if (delta != 0L)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1209
            addCount(delta, -1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1210
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1211
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1212
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1213
     * Returns a {@link Set} view of the keys contained in this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1214
     * The set is backed by the map, so changes to the map are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1215
     * reflected in the set, and vice-versa. The set supports element
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1216
     * removal, which removes the corresponding mapping from this map,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1217
     * via the {@code Iterator.remove}, {@code Set.remove},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1218
     * {@code removeAll}, {@code retainAll}, and {@code clear}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1219
     * operations.  It does not support the {@code add} or
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1220
     * {@code addAll} operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1221
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1222
     * <p>The view's iterators and spliterators are
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1223
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1224
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1225
     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1226
     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1227
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1228
     * @return the set view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1229
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1230
    public KeySetView<K,V> keySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1231
        KeySetView<K,V> ks;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1232
        return (ks = keySet) != null ? ks : (keySet = new KeySetView<K,V>(this, null));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1233
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1234
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1235
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1236
     * Returns a {@link Collection} view of the values contained in this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1237
     * The collection is backed by the map, so changes to the map are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1238
     * reflected in the collection, and vice-versa.  The collection
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1239
     * supports element removal, which removes the corresponding
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1240
     * mapping from this map, via the {@code Iterator.remove},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1241
     * {@code Collection.remove}, {@code removeAll},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1242
     * {@code retainAll}, and {@code clear} operations.  It does not
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1243
     * support the {@code add} or {@code addAll} operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1244
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1245
     * <p>The view's iterators and spliterators are
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1246
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1247
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1248
     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT}
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1249
     * and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1250
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1251
     * @return the collection view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1252
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1253
    public Collection<V> values() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1254
        ValuesView<K,V> vs;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1255
        return (vs = values) != null ? vs : (values = new ValuesView<K,V>(this));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1256
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1257
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1258
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1259
     * Returns a {@link Set} view of the mappings contained in this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1260
     * The set is backed by the map, so changes to the map are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1261
     * reflected in the set, and vice-versa.  The set supports element
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1262
     * removal, which removes the corresponding mapping from the map,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1263
     * via the {@code Iterator.remove}, {@code Set.remove},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1264
     * {@code removeAll}, {@code retainAll}, and {@code clear}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1265
     * operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1266
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1267
     * <p>The view's iterators and spliterators are
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1268
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1269
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1270
     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1271
     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1272
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1273
     * @return the set view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1274
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1275
    public Set<Map.Entry<K,V>> entrySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1276
        EntrySetView<K,V> es;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1277
        return (es = entrySet) != null ? es : (entrySet = new EntrySetView<K,V>(this));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1278
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1279
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1280
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1281
     * Returns the hash code value for this {@link Map}, i.e.,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1282
     * the sum of, for each key-value pair in the map,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1283
     * {@code key.hashCode() ^ value.hashCode()}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1284
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1285
     * @return the hash code value for this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1286
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1287
    public int hashCode() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1288
        int h = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1289
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1290
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1291
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1292
            for (Node<K,V> p; (p = it.advance()) != null; )
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1293
                h += p.key.hashCode() ^ p.val.hashCode();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1294
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1295
        return h;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1296
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1297
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1298
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1299
     * Returns a string representation of this map.  The string
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1300
     * representation consists of a list of key-value mappings (in no
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1301
     * particular order) enclosed in braces ("{@code {}}").  Adjacent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1302
     * mappings are separated by the characters {@code ", "} (comma
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1303
     * and space).  Each key-value mapping is rendered as the key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1304
     * followed by an equals sign ("{@code =}") followed by the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1305
     * associated value.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1306
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1307
     * @return a string representation of this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1308
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1309
    public String toString() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1310
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1311
        int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1312
        Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1313
        StringBuilder sb = new StringBuilder();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1314
        sb.append('{');
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1315
        Node<K,V> p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1316
        if ((p = it.advance()) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1317
            for (;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1318
                K k = p.key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1319
                V v = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1320
                sb.append(k == this ? "(this Map)" : k);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1321
                sb.append('=');
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1322
                sb.append(v == this ? "(this Map)" : v);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1323
                if ((p = it.advance()) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1324
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1325
                sb.append(',').append(' ');
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1326
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1327
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1328
        return sb.append('}').toString();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1329
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1330
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1331
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1332
     * Compares the specified object with this map for equality.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1333
     * Returns {@code true} if the given object is a map with the same
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1334
     * mappings as this map.  This operation may return misleading
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1335
     * results if either map is concurrently modified during execution
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1336
     * of this method.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1337
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1338
     * @param o object to be compared for equality with this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1339
     * @return {@code true} if the specified object is equal to this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1340
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1341
    public boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1342
        if (o != this) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1343
            if (!(o instanceof Map))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1344
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1345
            Map<?,?> m = (Map<?,?>) o;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1346
            Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1347
            int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1348
            Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1349
            for (Node<K,V> p; (p = it.advance()) != null; ) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1350
                V val = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1351
                Object v = m.get(p.key);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1352
                if (v == null || (v != val && !v.equals(val)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1353
                    return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1354
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1355
            for (Map.Entry<?,?> e : m.entrySet()) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1356
                Object mk, mv, v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1357
                if ((mk = e.getKey()) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1358
                    (mv = e.getValue()) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1359
                    (v = get(mk)) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1360
                    (mv != v && !mv.equals(v)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1361
                    return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1362
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1363
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1364
        return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1365
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1366
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1367
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1368
     * Stripped-down version of helper class used in previous version,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1369
     * declared for the sake of serialization compatibility
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1370
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1371
    static class Segment<K,V> extends ReentrantLock implements Serializable {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1372
        private static final long serialVersionUID = 2249069246763182397L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1373
        final float loadFactor;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1374
        Segment(float lf) { this.loadFactor = lf; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1375
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1376
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1377
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1378
     * Saves the state of the {@code ConcurrentHashMap} instance to a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1379
     * stream (i.e., serializes it).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1380
     * @param s the stream
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1381
     * @throws java.io.IOException if an I/O error occurs
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1382
     * @serialData
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1383
     * the key (Object) and value (Object)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1384
     * for each key-value mapping, followed by a null pair.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1385
     * The key-value mappings are emitted in no particular order.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1386
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1387
    private void writeObject(java.io.ObjectOutputStream s)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1388
        throws java.io.IOException {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1389
        // For serialization compatibility
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1390
        // Emulate segment calculation from previous version of this class
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1391
        int sshift = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1392
        int ssize = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1393
        while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1394
            ++sshift;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1395
            ssize <<= 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1396
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1397
        int segmentShift = 32 - sshift;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1398
        int segmentMask = ssize - 1;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1399
        @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1400
        Segment<K,V>[] segments = (Segment<K,V>[])
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1401
            new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1402
        for (int i = 0; i < segments.length; ++i)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1403
            segments[i] = new Segment<K,V>(LOAD_FACTOR);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1404
        s.putFields().put("segments", segments);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1405
        s.putFields().put("segmentShift", segmentShift);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1406
        s.putFields().put("segmentMask", segmentMask);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1407
        s.writeFields();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1408
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1409
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1410
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1411
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1412
            for (Node<K,V> p; (p = it.advance()) != null; ) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1413
                s.writeObject(p.key);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1414
                s.writeObject(p.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1415
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1416
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1417
        s.writeObject(null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1418
        s.writeObject(null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1419
        segments = null; // throw away
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1420
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1421
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1422
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1423
     * Reconstitutes the instance from a stream (that is, deserializes it).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1424
     * @param s the stream
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1425
     * @throws ClassNotFoundException if the class of a serialized object
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1426
     *         could not be found
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1427
     * @throws java.io.IOException if an I/O error occurs
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1428
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1429
    private void readObject(java.io.ObjectInputStream s)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1430
        throws java.io.IOException, ClassNotFoundException {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1431
        /*
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1432
         * To improve performance in typical cases, we create nodes
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1433
         * while reading, then place in table once size is known.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1434
         * However, we must also validate uniqueness and deal with
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1435
         * overpopulated bins while doing so, which requires
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1436
         * specialized versions of putVal mechanics.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1437
         */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1438
        sizeCtl = -1; // force exclusion for table construction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1439
        s.defaultReadObject();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1440
        long size = 0L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1441
        Node<K,V> p = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1442
        for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1443
            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1444
            K k = (K) s.readObject();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1445
            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1446
            V v = (V) s.readObject();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1447
            if (k != null && v != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1448
                p = new Node<K,V>(spread(k.hashCode()), k, v, p);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1449
                ++size;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1450
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1451
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1452
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1453
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1454
        if (size == 0L)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1455
            sizeCtl = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1456
        else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1457
            int n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1458
            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1459
                n = MAXIMUM_CAPACITY;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1460
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1461
                int sz = (int)size;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1462
                n = tableSizeFor(sz + (sz >>> 1) + 1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1463
            }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1464
            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1465
            Node<K,V>[] tab = (Node<K,V>[])new Node<?,?>[n];
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1466
            int mask = n - 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1467
            long added = 0L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1468
            while (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1469
                boolean insertAtFront;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1470
                Node<K,V> next = p.next, first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1471
                int h = p.hash, j = h & mask;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1472
                if ((first = tabAt(tab, j)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1473
                    insertAtFront = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1474
                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1475
                    K k = p.key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1476
                    if (first.hash < 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1477
                        TreeBin<K,V> t = (TreeBin<K,V>)first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1478
                        if (t.putTreeVal(h, k, p.val) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1479
                            ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1480
                        insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1481
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1482
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1483
                        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1484
                        insertAtFront = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1485
                        Node<K,V> q; K qk;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1486
                        for (q = first; q != null; q = q.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1487
                            if (q.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1488
                                ((qk = q.key) == k ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1489
                                 (qk != null && k.equals(qk)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1490
                                insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1491
                                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1492
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1493
                            ++binCount;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1494
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1495
                        if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1496
                            insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1497
                            ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1498
                            p.next = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1499
                            TreeNode<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1500
                            for (q = p; q != null; q = q.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1501
                                TreeNode<K,V> t = new TreeNode<K,V>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1502
                                    (q.hash, q.key, q.val, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1503
                                if ((t.prev = tl) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1504
                                    hd = t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1505
                                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1506
                                    tl.next = t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1507
                                tl = t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1508
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1509
                            setTabAt(tab, j, new TreeBin<K,V>(hd));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1510
                        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1511
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1512
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1513
                if (insertAtFront) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1514
                    ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1515
                    p.next = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1516
                    setTabAt(tab, j, p);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1517
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1518
                p = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1519
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1520
            table = tab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1521
            sizeCtl = n - (n >>> 2);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1522
            baseCount = added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1523
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1524
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1525
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1526
    // ConcurrentMap methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1527
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1528
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1529
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1530
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1531
     * @return the previous value associated with the specified key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1532
     *         or {@code null} if there was no mapping for the key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1533
     * @throws NullPointerException if the specified key or value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1534
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1535
    public V putIfAbsent(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1536
        return putVal(key, value, true);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1537
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1538
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1539
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1540
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1541
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1542
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1543
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1544
    public boolean remove(Object key, Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1545
        if (key == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1546
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1547
        return value != null && replaceNode(key, null, value) != null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1548
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1549
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1550
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1551
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1552
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1553
     * @throws NullPointerException if any of the arguments are null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1554
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1555
    public boolean replace(K key, V oldValue, V newValue) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1556
        if (key == null || oldValue == null || newValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1557
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1558
        return replaceNode(key, newValue, oldValue) != null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1559
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1560
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1561
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1562
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1563
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1564
     * @return the previous value associated with the specified key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1565
     *         or {@code null} if there was no mapping for the key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1566
     * @throws NullPointerException if the specified key or value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1567
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1568
    public V replace(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1569
        if (key == null || value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1570
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1571
        return replaceNode(key, value, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1572
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1573
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1574
    // Overrides of JDK8+ Map extension method defaults
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1575
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1576
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1577
     * Returns the value to which the specified key is mapped, or the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1578
     * given default value if this map contains no mapping for the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1579
     * key.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1580
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1581
     * @param key the key whose associated value is to be returned
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1582
     * @param defaultValue the value to return if this map contains
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1583
     * no mapping for the given key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1584
     * @return the mapping for the key, if present; else the default value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1585
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1586
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1587
    public V getOrDefault(Object key, V defaultValue) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1588
        V v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1589
        return (v = get(key)) == null ? defaultValue : v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1590
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1591
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1592
    public void forEach(BiConsumer<? super K, ? super V> action) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1593
        if (action == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1594
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1595
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1596
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1597
            for (Node<K,V> p; (p = it.advance()) != null; ) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1598
                action.accept(p.key, p.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1599
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1600
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1601
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1602
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1603
    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1604
        if (function == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1605
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1606
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1607
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1608
            for (Node<K,V> p; (p = it.advance()) != null; ) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1609
                V oldValue = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1610
                for (K key = p.key;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1611
                    V newValue = function.apply(key, oldValue);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1612
                    if (newValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1613
                        throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1614
                    if (replaceNode(key, newValue, oldValue) != null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1615
                        (oldValue = get(key)) == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1616
                        break;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1617
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1618
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1619
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1620
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1621
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1622
    /**
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1623
     * Helper method for EntrySet.removeIf
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1624
     */
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1625
    boolean removeEntryIf(Predicate<? super Entry<K, V>> function) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1626
        if (function == null) throw new NullPointerException();
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1627
        Node<K,V>[] t;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1628
        boolean removed = false;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1629
        if ((t = table) != null) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1630
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1631
            for (Node<K,V> p; (p = it.advance()) != null; ) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1632
                K k = p.key;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1633
                V v = p.val;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1634
                Map.Entry<K,V> e = new AbstractMap.SimpleImmutableEntry<>(k, v);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1635
                if (function.test(e) && replaceNode(k, null, v) != null)
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1636
                    removed = true;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1637
            }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1638
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1639
        return removed;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1640
    }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1641
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1642
    /**
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1643
     * Helper method for Values.removeIf
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1644
     */
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1645
    boolean removeValueIf(Predicate<? super  V> function) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1646
        if (function == null) throw new NullPointerException();
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1647
        Node<K,V>[] t;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1648
        boolean removed = false;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1649
        if ((t = table) != null) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1650
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1651
            for (Node<K,V> p; (p = it.advance()) != null; ) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1652
                K k = p.key;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1653
                V v = p.val;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1654
                if (function.test(v) && replaceNode(k, null, v) != null)
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1655
                    removed = true;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1656
            }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1657
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1658
        return removed;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1659
    }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1660
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1661
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1662
     * If the specified key is not already associated with a value,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1663
     * attempts to compute its value using the given mapping function
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1664
     * and enters it into this map unless {@code null}.  The entire
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1665
     * method invocation is performed atomically, so the function is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1666
     * applied at most once per key.  Some attempted update operations
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1667
     * on this map by other threads may be blocked while computation
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1668
     * is in progress, so the computation should be short and simple,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1669
     * and must not attempt to update any other mappings of this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1670
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1671
     * @param key key with which the specified value is to be associated
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1672
     * @param mappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1673
     * @return the current (existing or computed) value associated with
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1674
     *         the specified key, or null if the computed value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1675
     * @throws NullPointerException if the specified key or mappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1676
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1677
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1678
     *         attempts a recursive update to this map that would
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1679
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1680
     * @throws RuntimeException or Error if the mappingFunction does so,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1681
     *         in which case the mapping is left unestablished
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1682
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1683
    public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1684
        if (key == null || mappingFunction == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1685
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1686
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1687
        V val = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1688
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1689
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1690
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1691
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1692
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1693
            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1694
                Node<K,V> r = new ReservationNode<K,V>();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1695
                synchronized (r) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1696
                    if (casTabAt(tab, i, null, r)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1697
                        binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1698
                        Node<K,V> node = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1699
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1700
                            if ((val = mappingFunction.apply(key)) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1701
                                node = new Node<K,V>(h, key, val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1702
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1703
                            setTabAt(tab, i, node);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1704
                        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1705
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1706
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1707
                if (binCount != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1708
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1709
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1710
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1711
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1712
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1713
                boolean added = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1714
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1715
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1716
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1717
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1718
                            for (Node<K,V> e = f;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1719
                                K ek; V ev;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1720
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1721
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1722
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1723
                                    val = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1724
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1725
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1726
                                Node<K,V> pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1727
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1728
                                    if ((val = mappingFunction.apply(key)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1729
                                        added = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1730
                                        pred.next = new Node<K,V>(h, key, val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1731
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1732
                                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1733
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1734
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1735
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1736
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1737
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1738
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1739
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1740
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1741
                                (p = r.findTreeNode(h, key, null)) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1742
                                val = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1743
                            else if ((val = mappingFunction.apply(key)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1744
                                added = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1745
                                t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1746
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1747
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1748
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1749
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1750
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1751
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1752
                        treeifyBin(tab, i);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1753
                    if (!added)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1754
                        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1755
                    break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1756
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1757
            }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  1758
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1759
        if (val != null)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1760
            addCount(1L, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1761
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1762
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1763
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1764
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1765
     * If the value for the specified key is present, attempts to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1766
     * compute a new mapping given the key and its current mapped
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1767
     * value.  The entire method invocation is performed atomically.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1768
     * Some attempted update operations on this map by other threads
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1769
     * may be blocked while computation is in progress, so the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1770
     * computation should be short and simple, and must not attempt to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1771
     * update any other mappings of this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1772
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1773
     * @param key key with which a value may be associated
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1774
     * @param remappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1775
     * @return the new value associated with the specified key, or null if none
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1776
     * @throws NullPointerException if the specified key or remappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1777
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1778
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1779
     *         attempts a recursive update to this map that would
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1780
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1781
     * @throws RuntimeException or Error if the remappingFunction does so,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1782
     *         in which case the mapping is unchanged
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1783
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1784
    public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1785
        if (key == null || remappingFunction == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1786
            throw new NullPointerException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1787
        int h = spread(key.hashCode());
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1788
        V val = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1789
        int delta = 0;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1790
        int binCount = 0;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1791
        for (Node<K,V>[] tab = table;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1792
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1793
            if (tab == null || (n = tab.length) == 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1794
                tab = initTable();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1795
            else if ((f = tabAt(tab, i = (n - 1) & h)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1796
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1797
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1798
                tab = helpTransfer(tab, f);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1799
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1800
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1801
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1802
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1803
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1804
                            for (Node<K,V> e = f, pred = null;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1805
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1806
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1807
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1808
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1809
                                    val = remappingFunction.apply(key, e.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1810
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1811
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1812
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1813
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1814
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1815
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1816
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1817
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1818
                                            setTabAt(tab, i, en);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1819
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1820
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1821
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1822
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1823
                                if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1824
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1825
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1826
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1827
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1828
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1829
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1830
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1831
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1832
                                (p = r.findTreeNode(h, key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1833
                                val = remappingFunction.apply(key, p.val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1834
                                if (val != null)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1835
                                    p.val = val;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1836
                                else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1837
                                    delta = -1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1838
                                    if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1839
                                        setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1840
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1841
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1842
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1843
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1844
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1845
                if (binCount != 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1846
                    break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1847
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1848
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1849
        if (delta != 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1850
            addCount((long)delta, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1851
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1852
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1853
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1854
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1855
     * Attempts to compute a mapping for the specified key and its
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1856
     * current mapped value (or {@code null} if there is no current
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1857
     * mapping). The entire method invocation is performed atomically.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1858
     * Some attempted update operations on this map by other threads
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1859
     * may be blocked while computation is in progress, so the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1860
     * computation should be short and simple, and must not attempt to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1861
     * update any other mappings of this Map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1862
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1863
     * @param key key with which the specified value is to be associated
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1864
     * @param remappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1865
     * @return the new value associated with the specified key, or null if none
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1866
     * @throws NullPointerException if the specified key or remappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1867
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1868
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1869
     *         attempts a recursive update to this map that would
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1870
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1871
     * @throws RuntimeException or Error if the remappingFunction does so,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1872
     *         in which case the mapping is unchanged
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1873
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1874
    public V compute(K key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1875
                     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1876
        if (key == null || remappingFunction == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1877
            throw new NullPointerException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1878
        int h = spread(key.hashCode());
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1879
        V val = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1880
        int delta = 0;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1881
        int binCount = 0;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1882
        for (Node<K,V>[] tab = table;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1883
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1884
            if (tab == null || (n = tab.length) == 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1885
                tab = initTable();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1886
            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1887
                Node<K,V> r = new ReservationNode<K,V>();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1888
                synchronized (r) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1889
                    if (casTabAt(tab, i, null, r)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1890
                        binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1891
                        Node<K,V> node = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1892
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1893
                            if ((val = remappingFunction.apply(key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1894
                                delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1895
                                node = new Node<K,V>(h, key, val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1896
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1897
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1898
                            setTabAt(tab, i, node);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1899
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1900
                    }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1901
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1902
                if (binCount != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1903
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1904
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1905
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1906
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1907
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1908
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1909
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1910
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1911
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1912
                            for (Node<K,V> e = f, pred = null;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1913
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1914
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1915
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1916
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1917
                                    val = remappingFunction.apply(key, e.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1918
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1919
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1920
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1921
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1922
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1923
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1924
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1925
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1926
                                            setTabAt(tab, i, en);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1927
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1928
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1929
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1930
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1931
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1932
                                    val = remappingFunction.apply(key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1933
                                    if (val != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1934
                                        delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1935
                                        pred.next =
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1936
                                            new Node<K,V>(h, key, val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1937
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1938
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1939
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1940
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1941
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1942
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1943
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1944
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1945
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1946
                            if ((r = t.root) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1947
                                p = r.findTreeNode(h, key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1948
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1949
                                p = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1950
                            V pv = (p == null) ? null : p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1951
                            val = remappingFunction.apply(key, pv);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1952
                            if (val != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1953
                                if (p != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1954
                                    p.val = val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1955
                                else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1956
                                    delta = 1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1957
                                    t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1958
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1959
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1960
                            else if (p != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1961
                                delta = -1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1962
                                if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1963
                                    setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1964
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1965
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1966
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1967
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1968
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1969
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1970
                        treeifyBin(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1971
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1972
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1973
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1974
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1975
        if (delta != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1976
            addCount((long)delta, binCount);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1977
        return val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1978
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1979
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1980
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1981
     * If the specified key is not already associated with a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1982
     * (non-null) value, associates it with the given value.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1983
     * Otherwise, replaces the value with the results of the given
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1984
     * remapping function, or removes if {@code null}. The entire
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1985
     * method invocation is performed atomically.  Some attempted
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1986
     * update operations on this map by other threads may be blocked
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1987
     * while computation is in progress, so the computation should be
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1988
     * short and simple, and must not attempt to update any other
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1989
     * mappings of this Map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1990
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1991
     * @param key key with which the specified value is to be associated
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1992
     * @param value the value to use if absent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1993
     * @param remappingFunction the function to recompute a value if present
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1994
     * @return the new value associated with the specified key, or null if none
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1995
     * @throws NullPointerException if the specified key or the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1996
     *         remappingFunction is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1997
     * @throws RuntimeException or Error if the remappingFunction does so,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1998
     *         in which case the mapping is unchanged
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1999
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2000
    public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2001
        if (key == null || value == null || remappingFunction == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2002
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2003
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2004
        V val = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2005
        int delta = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2006
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2007
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2008
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2009
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2010
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2011
            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2012
                if (casTabAt(tab, i, null, new Node<K,V>(h, key, value, null))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2013
                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2014
                    val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2015
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2016
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2017
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2018
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2019
                tab = helpTransfer(tab, f);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2020
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2021
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2022
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2023
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2024
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2025
                            for (Node<K,V> e = f, pred = null;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2026
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2027
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2028
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2029
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2030
                                    val = remappingFunction.apply(e.val, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2031
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2032
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2033
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2034
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2035
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2036
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2037
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2038
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2039
                                            setTabAt(tab, i, en);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2040
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2041
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2042
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2043
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2044
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2045
                                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2046
                                    val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2047
                                    pred.next =
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2048
                                        new Node<K,V>(h, key, val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2049
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2050
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2051
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2052
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2053
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2054
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2055
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2056
                            TreeNode<K,V> r = t.root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2057
                            TreeNode<K,V> p = (r == null) ? null :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2058
                                r.findTreeNode(h, key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2059
                            val = (p == null) ? value :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2060
                                remappingFunction.apply(p.val, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2061
                            if (val != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2062
                                if (p != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2063
                                    p.val = val;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2064
                                else {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2065
                                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2066
                                    t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2067
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2068
                            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2069
                            else if (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2070
                                delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2071
                                if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2072
                                    setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2073
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2074
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2075
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2076
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2077
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2078
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2079
                        treeifyBin(tab, i);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2080
                    break;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2081
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2082
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2083
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2084
        if (delta != 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2085
            addCount((long)delta, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2086
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2087
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2088
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2089
    // Hashtable legacy methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2090
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2091
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2092
     * Legacy method testing if some key maps into the specified value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2093
     * in this table.  This method is identical in functionality to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2094
     * {@link #containsValue(Object)}, and exists solely to ensure
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2095
     * full compatibility with class {@link java.util.Hashtable},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2096
     * which supported this method prior to introduction of the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2097
     * Java Collections framework.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2098
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2099
     * @param  value a value to search for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2100
     * @return {@code true} if and only if some key maps to the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2101
     *         {@code value} argument in this table as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2102
     *         determined by the {@code equals} method;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2103
     *         {@code false} otherwise
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2104
     * @throws NullPointerException if the specified value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2105
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2106
    public boolean contains(Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2107
        return containsValue(value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2108
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2109
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2110
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2111
     * Returns an enumeration of the keys in this table.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2112
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2113
     * @return an enumeration of the keys in this table
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2114
     * @see #keySet()
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2115
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2116
    public Enumeration<K> keys() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2117
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2118
        int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2119
        return new KeyIterator<K,V>(t, f, 0, f, this);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2120
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2121
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2122
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2123
     * Returns an enumeration of the values in this table.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2124
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2125
     * @return an enumeration of the values in this table
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2126
     * @see #values()
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2127
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2128
    public Enumeration<V> elements() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2129
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2130
        int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2131
        return new ValueIterator<K,V>(t, f, 0, f, this);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2132
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2133
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2134
    // ConcurrentHashMap-only methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2135
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2136
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2137
     * Returns the number of mappings. This method should be used
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2138
     * instead of {@link #size} because a ConcurrentHashMap may
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2139
     * contain more mappings than can be represented as an int. The
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2140
     * value returned is an estimate; the actual count may differ if
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2141
     * there are concurrent insertions or removals.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2142
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2143
     * @return the number of mappings
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2144
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2145
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2146
    public long mappingCount() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2147
        long n = sumCount();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2148
        return (n < 0L) ? 0L : n; // ignore transient negative values
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2149
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2150
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2151
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2152
     * Creates a new {@link Set} backed by a ConcurrentHashMap
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2153
     * from the given type to {@code Boolean.TRUE}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2154
     *
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2155
     * @param <K> the element type of the returned set
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2156
     * @return the new set
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2157
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2158
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2159
    public static <K> KeySetView<K,Boolean> newKeySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2160
        return new KeySetView<K,Boolean>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2161
            (new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2162
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2163
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2164
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2165
     * Creates a new {@link Set} backed by a ConcurrentHashMap
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2166
     * from the given type to {@code Boolean.TRUE}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2167
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2168
     * @param initialCapacity The implementation performs internal
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2169
     * sizing to accommodate this many elements.
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2170
     * @param <K> the element type of the returned set
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2171
     * @return the new set
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2172
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2173
     * elements is negative
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2174
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2175
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2176
    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2177
        return new KeySetView<K,Boolean>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2178
            (new ConcurrentHashMap<K,Boolean>(initialCapacity), Boolean.TRUE);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2179
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2180
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2181
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2182
     * Returns a {@link Set} view of the keys in this map, using the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2183
     * given common mapped value for any additions (i.e., {@link
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2184
     * Collection#add} and {@link Collection#addAll(Collection)}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2185
     * This is of course only appropriate if it is acceptable to use
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2186
     * the same value for all additions from this view.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2187
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2188
     * @param mappedValue the mapped value to use for any additions
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2189
     * @return the set view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2190
     * @throws NullPointerException if the mappedValue is null
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2191
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2192
    public KeySetView<K,V> keySet(V mappedValue) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2193
        if (mappedValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2194
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2195
        return new KeySetView<K,V>(this, mappedValue);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2196
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2197
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2198
    /* ---------------- Special Nodes -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2199
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2200
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2201
     * A node inserted at head of bins during transfer operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2202
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2203
    static final class ForwardingNode<K,V> extends Node<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2204
        final Node<K,V>[] nextTable;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2205
        ForwardingNode(Node<K,V>[] tab) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2206
            super(MOVED, null, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2207
            this.nextTable = tab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2208
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2209
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2210
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2211
            // loop to avoid arbitrarily deep recursion on forwarding nodes
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2212
            outer: for (Node<K,V>[] tab = nextTable;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2213
                Node<K,V> e; int n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2214
                if (k == null || tab == null || (n = tab.length) == 0 ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2215
                    (e = tabAt(tab, (n - 1) & h)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2216
                    return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2217
                for (;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2218
                    int eh; K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2219
                    if ((eh = e.hash) == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2220
                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2221
                        return e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2222
                    if (eh < 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2223
                        if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2224
                            tab = ((ForwardingNode<K,V>)e).nextTable;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2225
                            continue outer;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2226
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2227
                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2228
                            return e.find(h, k);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2229
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2230
                    if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2231
                        return null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2232
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2233
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2234
        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2235
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2236
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2237
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2238
     * A place-holder node used in computeIfAbsent and compute
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2239
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2240
    static final class ReservationNode<K,V> extends Node<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2241
        ReservationNode() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2242
            super(RESERVED, null, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2243
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2244
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2245
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2246
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2247
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2248
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2249
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2250
    /* ---------------- Table Initialization and Resizing -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2251
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2252
    /**
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2253
     * Returns the stamp bits for resizing a table of size n.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2254
     * Must be negative when shifted left by RESIZE_STAMP_SHIFT.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2255
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2256
    static final int resizeStamp(int n) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2257
        return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1));
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2258
    }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2259
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2260
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2261
     * Initializes table, using the size recorded in sizeCtl.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2262
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2263
    private final Node<K,V>[] initTable() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2264
        Node<K,V>[] tab; int sc;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2265
        while ((tab = table) == null || tab.length == 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2266
            if ((sc = sizeCtl) < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2267
                Thread.yield(); // lost initialization race; just spin
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2268
            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2269
                try {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2270
                    if ((tab = table) == null || tab.length == 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2271
                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2272
                        @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2273
                        Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2274
                        table = tab = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2275
                        sc = n - (n >>> 2);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2276
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2277
                } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2278
                    sizeCtl = sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2279
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2280
                break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2281
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2282
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2283
        return tab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2284
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2285
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2286
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2287
     * Adds to count, and if table is too small and not already
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2288
     * resizing, initiates transfer. If already resizing, helps
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2289
     * perform transfer if work is available.  Rechecks occupancy
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2290
     * after a transfer to see if another resize is already needed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2291
     * because resizings are lagging additions.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2292
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2293
     * @param x the count to add
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2294
     * @param check if <0, don't check resize, if <= 1 only check if uncontended
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2295
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2296
    private final void addCount(long x, int check) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2297
        CounterCell[] as; long b, s;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2298
        if ((as = counterCells) != null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2299
            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2300
            CounterCell a; long v; int m;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2301
            boolean uncontended = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2302
            if (as == null || (m = as.length - 1) < 0 ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2303
                (a = as[ThreadLocalRandom.getProbe() & m]) == null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2304
                !(uncontended =
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2305
                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2306
                fullAddCount(x, uncontended);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2307
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2308
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2309
            if (check <= 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2310
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2311
            s = sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2312
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2313
        if (check >= 0) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2314
            Node<K,V>[] tab, nt; int n, sc;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2315
            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2316
                   (n = tab.length) < MAXIMUM_CAPACITY) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2317
                int rs = resizeStamp(n);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2318
                if (sc < 0) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2319
                    if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2320
                        sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2321
                        transferIndex <= 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2322
                        break;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2323
                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2324
                        transfer(tab, nt);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2325
                }
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2326
                else if (U.compareAndSwapInt(this, SIZECTL, sc,
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2327
                                             (rs << RESIZE_STAMP_SHIFT) + 2))
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2328
                    transfer(tab, null);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2329
                s = sumCount();
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2330
            }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2331
        }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2332
    }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2333
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2334
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2335
     * Helps transfer if a resize is in progress.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2336
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2337
    final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2338
        Node<K,V>[] nextTab; int sc;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2339
        if (tab != null && (f instanceof ForwardingNode) &&
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2340
            (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2341
            int rs = resizeStamp(tab.length);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2342
            while (nextTab == nextTable && table == tab &&
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2343
                   (sc = sizeCtl) < 0) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2344
                if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2345
                    sc == rs + MAX_RESIZERS || transferIndex <= 0)
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2346
                    break;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2347
                if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2348
                    transfer(tab, nextTab);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2349
                    break;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2350
                }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2351
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2352
            return nextTab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2353
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2354
        return table;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2355
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2356
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2357
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2358
     * Tries to presize table to accommodate the given number of elements.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2359
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2360
     * @param size number of elements (doesn't need to be perfectly accurate)
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2361
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2362
    private final void tryPresize(int size) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2363
        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2364
            tableSizeFor(size + (size >>> 1) + 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2365
        int sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2366
        while ((sc = sizeCtl) >= 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2367
            Node<K,V>[] tab = table; int n;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2368
            if (tab == null || (n = tab.length) == 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2369
                n = (sc > c) ? sc : c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2370
                if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2371
                    try {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2372
                        if (table == tab) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2373
                            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2374
                            Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2375
                            table = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2376
                            sc = n - (n >>> 2);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2377
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2378
                    } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2379
                        sizeCtl = sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2380
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2381
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2382
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2383
            else if (c <= sc || n >= MAXIMUM_CAPACITY)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2384
                break;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2385
            else if (tab == table) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2386
                int rs = resizeStamp(n);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2387
                if (sc < 0) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2388
                    Node<K,V>[] nt;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2389
                    if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2390
                        sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2391
                        transferIndex <= 0)
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2392
                        break;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2393
                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2394
                        transfer(tab, nt);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2395
                }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2396
                else if (U.compareAndSwapInt(this, SIZECTL, sc,
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2397
                                             (rs << RESIZE_STAMP_SHIFT) + 2))
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2398
                    transfer(tab, null);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2399
            }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
  2400
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2404
     * Moves and/or copies the nodes in each bin to new table. See
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2405
     * above for explanation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2407
    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2408
        int n = tab.length, stride;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2409
        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2410
            stride = MIN_TRANSFER_STRIDE; // subdivide range
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2411
        if (nextTab == null) {            // initiating
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2412
            try {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2413
                @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2414
                Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2415
                nextTab = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2416
            } catch (Throwable ex) {      // try to cope with OOME
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2417
                sizeCtl = Integer.MAX_VALUE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2418
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2419
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2420
            nextTable = nextTab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2421
            transferIndex = n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2423
        int nextn = nextTab.length;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2424
        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2425
        boolean advance = true;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2426
        boolean finishing = false; // to ensure sweep before committing nextTab
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2427
        for (int i = 0, bound = 0;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2428
            Node<K,V> f; int fh;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2429
            while (advance) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2430
                int nextIndex, nextBound;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2431
                if (--i >= bound || finishing)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2432
                    advance = false;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2433
                else if ((nextIndex = transferIndex) <= 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2434
                    i = -1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2435
                    advance = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2436
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2437
                else if (U.compareAndSwapInt
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2438
                         (this, TRANSFERINDEX, nextIndex,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2439
                          nextBound = (nextIndex > stride ?
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2440
                                       nextIndex - stride : 0))) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2441
                    bound = nextBound;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2442
                    i = nextIndex - 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2443
                    advance = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2444
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2445
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2446
            if (i < 0 || i >= n || i + n >= nextn) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2447
                int sc;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2448
                if (finishing) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2449
                    nextTable = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2450
                    table = nextTab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2451
                    sizeCtl = (n << 1) - (n >>> 1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2452
                    return;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2453
                }
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2454
                if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2455
                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2456
                        return;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2457
                    finishing = advance = true;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2458
                    i = n; // recheck before commit
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2459
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2460
            }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2461
            else if ((f = tabAt(tab, i)) == null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2462
                advance = casTabAt(tab, i, null, fwd);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2463
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2464
                advance = true; // already processed
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2465
            else {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2466
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2467
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2468
                        Node<K,V> ln, hn;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2469
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2470
                            int runBit = fh & n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2471
                            Node<K,V> lastRun = f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2472
                            for (Node<K,V> p = f.next; p != null; p = p.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2473
                                int b = p.hash & n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2474
                                if (b != runBit) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2475
                                    runBit = b;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2476
                                    lastRun = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2477
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2478
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2479
                            if (runBit == 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2480
                                ln = lastRun;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2481
                                hn = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2483
                            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2484
                                hn = lastRun;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2485
                                ln = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2486
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2487
                            for (Node<K,V> p = f; p != lastRun; p = p.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2488
                                int ph = p.hash; K pk = p.key; V pv = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2489
                                if ((ph & n) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2490
                                    ln = new Node<K,V>(ph, pk, pv, ln);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2491
                                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2492
                                    hn = new Node<K,V>(ph, pk, pv, hn);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2493
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2494
                            setTabAt(nextTab, i, ln);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2495
                            setTabAt(nextTab, i + n, hn);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2496
                            setTabAt(tab, i, fwd);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2497
                            advance = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2499
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2500
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2501
                            TreeNode<K,V> lo = null, loTail = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2502
                            TreeNode<K,V> hi = null, hiTail = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2503
                            int lc = 0, hc = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2504
                            for (Node<K,V> e = t.first; e != null; e = e.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2505
                                int h = e.hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2506
                                TreeNode<K,V> p = new TreeNode<K,V>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2507
                                    (h, e.key, e.val, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2508
                                if ((h & n) == 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2509
                                    if ((p.prev = loTail) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2510
                                        lo = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2511
                                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2512
                                        loTail.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2513
                                    loTail = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2514
                                    ++lc;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2515
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2516
                                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2517
                                    if ((p.prev = hiTail) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2518
                                        hi = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2519
                                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2520
                                        hiTail.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2521
                                    hiTail = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2522
                                    ++hc;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2523
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2524
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2525
                            ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2526
                                (hc != 0) ? new TreeBin<K,V>(lo) : t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2527
                            hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2528
                                (lc != 0) ? new TreeBin<K,V>(hi) : t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2529
                            setTabAt(nextTab, i, ln);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2530
                            setTabAt(nextTab, i + n, hn);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2531
                            setTabAt(tab, i, fwd);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2532
                            advance = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
            }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2537
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2538
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2539
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2540
    /* ---------------- Counter support -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2541
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2542
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2543
     * A padded cell for distributing counts.  Adapted from LongAdder
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2544
     * and Striped64.  See their internal docs for explanation.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2545
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2546
    @sun.misc.Contended static final class CounterCell {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2547
        volatile long value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2548
        CounterCell(long x) { value = x; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2549
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2550
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2551
    final long sumCount() {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2552
        CounterCell[] as = counterCells; CounterCell a;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2553
        long sum = baseCount;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2554
        if (as != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2555
            for (int i = 0; i < as.length; ++i) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2556
                if ((a = as[i]) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2557
                    sum += a.value;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2558
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2559
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2560
        return sum;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2561
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2562
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2563
    // See LongAdder version for explanation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2564
    private final void fullAddCount(long x, boolean wasUncontended) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2565
        int h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2566
        if ((h = ThreadLocalRandom.getProbe()) == 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2567
            ThreadLocalRandom.localInit();      // force initialization
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2568
            h = ThreadLocalRandom.getProbe();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2569
            wasUncontended = true;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2570
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2571
        boolean collide = false;                // True if last slot nonempty
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2572
        for (;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2573
            CounterCell[] as; CounterCell a; int n; long v;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2574
            if ((as = counterCells) != null && (n = as.length) > 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2575
                if ((a = as[(n - 1) & h]) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2576
                    if (cellsBusy == 0) {            // Try to attach new Cell
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2577
                        CounterCell r = new CounterCell(x); // Optimistic create
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2578
                        if (cellsBusy == 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2579
                            U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2580
                            boolean created = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2581
                            try {               // Recheck under lock
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2582
                                CounterCell[] rs; int m, j;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2583
                                if ((rs = counterCells) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2584
                                    (m = rs.length) > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2585
                                    rs[j = (m - 1) & h] == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2586
                                    rs[j] = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2587
                                    created = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2588
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2589
                            } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2590
                                cellsBusy = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2591
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2592
                            if (created)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2593
                                break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2594
                            continue;           // Slot is now non-empty
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2595
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2596
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2597
                    collide = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2598
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2599
                else if (!wasUncontended)       // CAS already known to fail
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2600
                    wasUncontended = true;      // Continue after rehash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2601
                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2602
                    break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2603
                else if (counterCells != as || n >= NCPU)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2604
                    collide = false;            // At max size or stale
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2605
                else if (!collide)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2606
                    collide = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2607
                else if (cellsBusy == 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2608
                         U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2609
                    try {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2610
                        if (counterCells == as) {// Expand table unless stale
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2611
                            CounterCell[] rs = new CounterCell[n << 1];
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2612
                            for (int i = 0; i < n; ++i)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2613
                                rs[i] = as[i];
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2614
                            counterCells = rs;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2615
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2616
                    } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2617
                        cellsBusy = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2618
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2619
                    collide = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2620
                    continue;                   // Retry with expanded table
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2621
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2622
                h = ThreadLocalRandom.advanceProbe(h);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2623
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2624
            else if (cellsBusy == 0 && counterCells == as &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2625
                     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2626
                boolean init = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2627
                try {                           // Initialize table
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2628
                    if (counterCells == as) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2629
                        CounterCell[] rs = new CounterCell[2];
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2630
                        rs[h & 1] = new CounterCell(x);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2631
                        counterCells = rs;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2632
                        init = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2633
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2634
                } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2635
                    cellsBusy = 0;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2636
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2637
                if (init)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2638
                    break;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2639
            }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2640
            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2641
                break;                          // Fall back on using base
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2642
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2643
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2644
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2645
    /* ---------------- Conversion from/to TreeBins -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2646
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2647
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2648
     * Replaces all linked nodes in bin at given index unless table is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2649
     * too small, in which case resizes instead.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2650
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2651
    private final void treeifyBin(Node<K,V>[] tab, int index) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2652
        Node<K,V> b; int n, sc;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2653
        if (tab != null) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2654
            if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2655
                tryPresize(n << 1);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2656
            else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2657
                synchronized (b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2658
                    if (tabAt(tab, index) == b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2659
                        TreeNode<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2660
                        for (Node<K,V> e = b; e != null; e = e.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2661
                            TreeNode<K,V> p =
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2662
                                new TreeNode<K,V>(e.hash, e.key, e.val,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2663
                                                  null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2664
                            if ((p.prev = tl) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2665
                                hd = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2666
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2667
                                tl.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2668
                            tl = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2669
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2670
                        setTabAt(tab, index, new TreeBin<K,V>(hd));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2671
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2672
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2673
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2674
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2675
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2676
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2677
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2678
     * Returns a list on non-TreeNodes replacing those in given list.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2679
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2680
    static <K,V> Node<K,V> untreeify(Node<K,V> b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2681
        Node<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2682
        for (Node<K,V> q = b; q != null; q = q.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2683
            Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2684
            if (tl == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2685
                hd = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2686
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2687
                tl.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2688
            tl = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2689
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2690
        return hd;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2691
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2692
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2693
    /* ---------------- TreeNodes -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2694
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2695
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2696
     * Nodes for use in TreeBins
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2697
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2698
    static final class TreeNode<K,V> extends Node<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2699
        TreeNode<K,V> parent;  // red-black tree links
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2700
        TreeNode<K,V> left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2701
        TreeNode<K,V> right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2702
        TreeNode<K,V> prev;    // needed to unlink next upon deletion
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2703
        boolean red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2704
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2705
        TreeNode(int hash, K key, V val, Node<K,V> next,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2706
                 TreeNode<K,V> parent) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2707
            super(hash, key, val, next);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2708
            this.parent = parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2709
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2710
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2711
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2712
            return findTreeNode(h, k, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2713
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2714
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2715
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2716
         * Returns the TreeNode (or null if not found) for the given key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2717
         * starting at given root.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2718
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2719
        final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2720
            if (k != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2721
                TreeNode<K,V> p = this;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2722
                do  {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2723
                    int ph, dir; K pk; TreeNode<K,V> q;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2724
                    TreeNode<K,V> pl = p.left, pr = p.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2725
                    if ((ph = p.hash) > h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2726
                        p = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2727
                    else if (ph < h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2728
                        p = pr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2729
                    else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2730
                        return p;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2731
                    else if (pl == null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2732
                        p = pr;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2733
                    else if (pr == null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2734
                        p = pl;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2735
                    else if ((kc != null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2736
                              (kc = comparableClassFor(k)) != null) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2737
                             (dir = compareComparables(kc, k, pk)) != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2738
                        p = (dir < 0) ? pl : pr;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2739
                    else if ((q = pr.findTreeNode(h, k, kc)) != null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2740
                        return q;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2741
                    else
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2742
                        p = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2743
                } while (p != null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2744
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2745
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2746
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2747
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2748
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2749
    /* ---------------- TreeBins -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2750
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2751
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2752
     * TreeNodes used at the heads of bins. TreeBins do not hold user
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2753
     * keys or values, but instead point to list of TreeNodes and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2754
     * their root. They also maintain a parasitic read-write lock
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2755
     * forcing writers (who hold bin lock) to wait for readers (who do
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2756
     * not) to complete before tree restructuring operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2757
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2758
    static final class TreeBin<K,V> extends Node<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2759
        TreeNode<K,V> root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2760
        volatile TreeNode<K,V> first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2761
        volatile Thread waiter;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2762
        volatile int lockState;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2763
        // values for lockState
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2764
        static final int WRITER = 1; // set while holding write lock
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2765
        static final int WAITER = 2; // set when waiting for write lock
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2766
        static final int READER = 4; // increment value for setting read lock
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2767
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2768
        /**
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2769
         * Tie-breaking utility for ordering insertions when equal
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2770
         * hashCodes and non-comparable. We don't require a total
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2771
         * order, just a consistent insertion rule to maintain
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2772
         * equivalence across rebalancings. Tie-breaking further than
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2773
         * necessary simplifies testing a bit.
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2774
         */
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2775
        static int tieBreakOrder(Object a, Object b) {
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2776
            int d;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2777
            if (a == null || b == null ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2778
                (d = a.getClass().getName().
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2779
                 compareTo(b.getClass().getName())) == 0)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2780
                d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2781
                     -1 : 1);
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2782
            return d;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2783
        }
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2784
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2785
        /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2786
         * Creates bin with initial set of nodes headed by b.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2787
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2788
        TreeBin(TreeNode<K,V> b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2789
            super(TREEBIN, null, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2790
            this.first = b;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2791
            TreeNode<K,V> r = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2792
            for (TreeNode<K,V> x = b, next; x != null; x = next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2793
                next = (TreeNode<K,V>)x.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2794
                x.left = x.right = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2795
                if (r == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2796
                    x.parent = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2797
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2798
                    r = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2799
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2800
                else {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2801
                    K k = x.key;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2802
                    int h = x.hash;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2803
                    Class<?> kc = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2804
                    for (TreeNode<K,V> p = r;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2805
                        int dir, ph;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2806
                        K pk = p.key;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2807
                        if ((ph = p.hash) > h)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2808
                            dir = -1;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2809
                        else if (ph < h)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2810
                            dir = 1;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2811
                        else if ((kc == null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2812
                                  (kc = comparableClassFor(k)) == null) ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2813
                                 (dir = compareComparables(kc, k, pk)) == 0)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2814
                            dir = tieBreakOrder(k, pk);
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2815
                            TreeNode<K,V> xp = p;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2816
                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2817
                            x.parent = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2818
                            if (dir <= 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2819
                                xp.left = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2820
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2821
                                xp.right = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2822
                            r = balanceInsertion(r, x);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2823
                            break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2824
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2825
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2826
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2827
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2828
            this.root = r;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2829
            assert checkInvariants(root);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2830
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2831
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2832
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2833
         * Acquires write lock for tree restructuring.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2834
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2835
        private final void lockRoot() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2836
            if (!U.compareAndSwapInt(this, LOCKSTATE, 0, WRITER))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2837
                contendedLock(); // offload to separate method
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2838
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2839
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2840
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2841
         * Releases write lock for tree restructuring.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2842
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2843
        private final void unlockRoot() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2844
            lockState = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2845
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2846
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2847
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2848
         * Possibly blocks awaiting root lock.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2849
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2850
        private final void contendedLock() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2851
            boolean waiting = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2852
            for (int s;;) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2853
                if (((s = lockState) & ~WAITER) == 0) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2854
                    if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2855
                        if (waiting)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2856
                            waiter = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2857
                        return;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2858
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2859
                }
19413
56082c988815 8023104: ConcurrentHashMap typo
dl
parents: 19380
diff changeset
  2860
                else if ((s & WAITER) == 0) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2861
                    if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2862
                        waiting = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2863
                        waiter = Thread.currentThread();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2864
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2865
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2866
                else if (waiting)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2867
                    LockSupport.park(this);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2868
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2869
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2870
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2871
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2872
         * Returns matching node or null if none. Tries to search
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2873
         * using tree comparisons from root, but continues linear
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2874
         * search when lock not available.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2875
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2876
        final Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2877
            if (k != null) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2878
                for (Node<K,V> e = first; e != null; ) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2879
                    int s; K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2880
                    if (((s = lockState) & (WAITER|WRITER)) != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2881
                        if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2882
                            ((ek = e.key) == k || (ek != null && k.equals(ek))))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2883
                            return e;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2884
                        e = e.next;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2885
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2886
                    else if (U.compareAndSwapInt(this, LOCKSTATE, s,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2887
                                                 s + READER)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2888
                        TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2889
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2890
                            p = ((r = root) == null ? null :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2891
                                 r.findTreeNode(h, k, null));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2892
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2893
                            Thread w;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2894
                            if (U.getAndAddInt(this, LOCKSTATE, -READER) ==
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2895
                                (READER|WAITER) && (w = waiter) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2896
                                LockSupport.unpark(w);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2897
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2898
                        return p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2899
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2900
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2901
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2902
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2903
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2904
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2905
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2906
         * Finds or adds a node.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2907
         * @return null if added
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2908
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2909
        final TreeNode<K,V> putTreeVal(int h, K k, V v) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2910
            Class<?> kc = null;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2911
            boolean searched = false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2912
            for (TreeNode<K,V> p = root;;) {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2913
                int dir, ph; K pk;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2914
                if (p == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2915
                    first = root = new TreeNode<K,V>(h, k, v, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2916
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2917
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2918
                else if ((ph = p.hash) > h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2919
                    dir = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2920
                else if (ph < h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2921
                    dir = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2922
                else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2923
                    return p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2924
                else if ((kc == null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2925
                          (kc = comparableClassFor(k)) == null) ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2926
                         (dir = compareComparables(kc, k, pk)) == 0) {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2927
                    if (!searched) {
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2928
                        TreeNode<K,V> q, ch;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2929
                        searched = true;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2930
                        if (((ch = p.left) != null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2931
                             (q = ch.findTreeNode(h, k, kc)) != null) ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2932
                            ((ch = p.right) != null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2933
                             (q = ch.findTreeNode(h, k, kc)) != null))
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2934
                            return q;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2935
                    }
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2936
                    dir = tieBreakOrder(k, pk);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2937
                }
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2938
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2939
                TreeNode<K,V> xp = p;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2940
                if ((p = (dir <= 0) ? p.left : p.right) == null) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2941
                    TreeNode<K,V> x, f = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2942
                    first = x = new TreeNode<K,V>(h, k, v, f, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2943
                    if (f != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2944
                        f.prev = x;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2945
                    if (dir <= 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2946
                        xp.left = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2947
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2948
                        xp.right = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2949
                    if (!xp.red)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2950
                        x.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2951
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2952
                        lockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2953
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2954
                            root = balanceInsertion(root, x);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2955
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2956
                            unlockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2957
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2958
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2959
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2960
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2961
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2962
            assert checkInvariants(root);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2963
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2964
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2965
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2966
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2967
         * Removes the given node, that must be present before this
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2968
         * call.  This is messier than typical red-black deletion code
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2969
         * because we cannot swap the contents of an interior node
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2970
         * with a leaf successor that is pinned by "next" pointers
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2971
         * that are accessible independently of lock. So instead we
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2972
         * swap the tree linkages.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2973
         *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2974
         * @return true if now too small, so should be untreeified
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2975
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2976
        final boolean removeTreeNode(TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2977
            TreeNode<K,V> next = (TreeNode<K,V>)p.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2978
            TreeNode<K,V> pred = p.prev;  // unlink traversal pointers
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2979
            TreeNode<K,V> r, rl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2980
            if (pred == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2981
                first = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2982
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2983
                pred.next = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2984
            if (next != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2985
                next.prev = pred;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2986
            if (first == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2987
                root = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2988
                return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2989
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2990
            if ((r = root) == null || r.right == null || // too small
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2991
                (rl = r.left) == null || rl.left == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2992
                return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2993
            lockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2994
            try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2995
                TreeNode<K,V> replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2996
                TreeNode<K,V> pl = p.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2997
                TreeNode<K,V> pr = p.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2998
                if (pl != null && pr != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2999
                    TreeNode<K,V> s = pr, sl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3000
                    while ((sl = s.left) != null) // find successor
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3001
                        s = sl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3002
                    boolean c = s.red; s.red = p.red; p.red = c; // swap colors
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3003
                    TreeNode<K,V> sr = s.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3004
                    TreeNode<K,V> pp = p.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3005
                    if (s == pr) { // p was s's direct parent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3006
                        p.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3007
                        s.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3008
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3009
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3010
                        TreeNode<K,V> sp = s.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3011
                        if ((p.parent = sp) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3012
                            if (s == sp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3013
                                sp.left = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3014
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3015
                                sp.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3016
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3017
                        if ((s.right = pr) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3018
                            pr.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3019
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3020
                    p.left = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3021
                    if ((p.right = sr) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3022
                        sr.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3023
                    if ((s.left = pl) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3024
                        pl.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3025
                    if ((s.parent = pp) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3026
                        r = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3027
                    else if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3028
                        pp.left = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3029
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3030
                        pp.right = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3031
                    if (sr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3032
                        replacement = sr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3033
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3034
                        replacement = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3035
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3036
                else if (pl != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3037
                    replacement = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3038
                else if (pr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3039
                    replacement = pr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3040
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3041
                    replacement = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3042
                if (replacement != p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3043
                    TreeNode<K,V> pp = replacement.parent = p.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3044
                    if (pp == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3045
                        r = replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3046
                    else if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3047
                        pp.left = replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3048
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3049
                        pp.right = replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3050
                    p.left = p.right = p.parent = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3051
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3052
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3053
                root = (p.red) ? r : balanceDeletion(r, replacement);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3054
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3055
                if (p == replacement) {  // detach pointers
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3056
                    TreeNode<K,V> pp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3057
                    if ((pp = p.parent) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3058
                        if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3059
                            pp.left = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3060
                        else if (p == pp.right)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3061
                            pp.right = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3062
                        p.parent = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3063
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3064
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3065
            } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3066
                unlockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3067
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3068
            assert checkInvariants(root);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3069
            return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3070
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3071
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3072
        /* ------------------------------------------------------------ */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3073
        // Red-black tree methods, all adapted from CLR
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3074
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3075
        static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3076
                                              TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3077
            TreeNode<K,V> r, pp, rl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3078
            if (p != null && (r = p.right) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3079
                if ((rl = p.right = r.left) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3080
                    rl.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3081
                if ((pp = r.parent = p.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3082
                    (root = r).red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3083
                else if (pp.left == p)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3084
                    pp.left = r;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3085
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3086
                    pp.right = r;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3087
                r.left = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3088
                p.parent = r;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3089
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3090
            return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3091
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3092
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3093
        static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3094
                                               TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3095
            TreeNode<K,V> l, pp, lr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3096
            if (p != null && (l = p.left) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3097
                if ((lr = p.left = l.right) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3098
                    lr.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3099
                if ((pp = l.parent = p.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3100
                    (root = l).red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3101
                else if (pp.right == p)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3102
                    pp.right = l;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3103
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3104
                    pp.left = l;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3105
                l.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3106
                p.parent = l;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3107
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3108
            return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3109
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3110
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3111
        static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3112
                                                    TreeNode<K,V> x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3113
            x.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3114
            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3115
                if ((xp = x.parent) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3116
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3117
                    return x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3118
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3119
                else if (!xp.red || (xpp = xp.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3120
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3121
                if (xp == (xppl = xpp.left)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3122
                    if ((xppr = xpp.right) != null && xppr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3123
                        xppr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3124
                        xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3125
                        xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3126
                        x = xpp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3127
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3128
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3129
                        if (x == xp.right) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3130
                            root = rotateLeft(root, x = xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3131
                            xpp = (xp = x.parent) == null ? null : xp.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3132
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3133
                        if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3134
                            xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3135
                            if (xpp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3136
                                xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3137
                                root = rotateRight(root, xpp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3138
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3139
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3140
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3141
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3142
                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3143
                    if (xppl != null && xppl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3144
                        xppl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3145
                        xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3146
                        xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3147
                        x = xpp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3148
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3149
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3150
                        if (x == xp.left) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3151
                            root = rotateRight(root, x = xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3152
                            xpp = (xp = x.parent) == null ? null : xp.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3153
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3154
                        if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3155
                            xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3156
                            if (xpp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3157
                                xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3158
                                root = rotateLeft(root, xpp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3159
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3160
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3161
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3162
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3163
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3164
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3165
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3166
        static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3167
                                                   TreeNode<K,V> x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3168
            for (TreeNode<K,V> xp, xpl, xpr;;)  {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3169
                if (x == null || x == root)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3170
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3171
                else if ((xp = x.parent) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3172
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3173
                    return x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3174
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3175
                else if (x.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3176
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3177
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3178
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3179
                else if ((xpl = xp.left) == x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3180
                    if ((xpr = xp.right) != null && xpr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3181
                        xpr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3182
                        xp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3183
                        root = rotateLeft(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3184
                        xpr = (xp = x.parent) == null ? null : xp.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3185
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3186
                    if (xpr == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3187
                        x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3188
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3189
                        TreeNode<K,V> sl = xpr.left, sr = xpr.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3190
                        if ((sr == null || !sr.red) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3191
                            (sl == null || !sl.red)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3192
                            xpr.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3193
                            x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3194
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3195
                        else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3196
                            if (sr == null || !sr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3197
                                if (sl != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3198
                                    sl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3199
                                xpr.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3200
                                root = rotateRight(root, xpr);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3201
                                xpr = (xp = x.parent) == null ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3202
                                    null : xp.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3203
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3204
                            if (xpr != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3205
                                xpr.red = (xp == null) ? false : xp.red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3206
                                if ((sr = xpr.right) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3207
                                    sr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3208
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3209
                            if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3210
                                xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3211
                                root = rotateLeft(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3212
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3213
                            x = root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3214
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3215
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3216
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3217
                else { // symmetric
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3218
                    if (xpl != null && xpl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3219
                        xpl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3220
                        xp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3221
                        root = rotateRight(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3222
                        xpl = (xp = x.parent) == null ? null : xp.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3223
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3224
                    if (xpl == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3225
                        x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3226
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3227
                        TreeNode<K,V> sl = xpl.left, sr = xpl.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3228
                        if ((sl == null || !sl.red) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3229
                            (sr == null || !sr.red)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3230
                            xpl.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3231
                            x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3232
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3233
                        else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3234
                            if (sl == null || !sl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3235
                                if (sr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3236
                                    sr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3237
                                xpl.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3238
                                root = rotateLeft(root, xpl);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3239
                                xpl = (xp = x.parent) == null ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3240
                                    null : xp.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3241
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3242
                            if (xpl != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3243
                                xpl.red = (xp == null) ? false : xp.red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3244
                                if ((sl = xpl.left) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3245
                                    sl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3246
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3247
                            if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3248
                                xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3249
                                root = rotateRight(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3250
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3251
                            x = root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3252
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3253
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3254
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3255
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3256
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3257
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3258
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3259
         * Recursive invariant check
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3260
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3261
        static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3262
            TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3263
                tb = t.prev, tn = (TreeNode<K,V>)t.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3264
            if (tb != null && tb.next != t)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3265
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3266
            if (tn != null && tn.prev != t)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3267
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3268
            if (tp != null && t != tp.left && t != tp.right)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3269
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3270
            if (tl != null && (tl.parent != t || tl.hash > t.hash))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3271
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3272
            if (tr != null && (tr.parent != t || tr.hash < t.hash))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3273
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3274
            if (t.red && tl != null && tl.red && tr != null && tr.red)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3275
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3276
            if (tl != null && !checkInvariants(tl))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3277
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3278
            if (tr != null && !checkInvariants(tr))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3279
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3280
            return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3281
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3282
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3283
        private static final sun.misc.Unsafe U;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3284
        private static final long LOCKSTATE;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3285
        static {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3286
            try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3287
                U = sun.misc.Unsafe.getUnsafe();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3288
                Class<?> k = TreeBin.class;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3289
                LOCKSTATE = U.objectFieldOffset
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3290
                    (k.getDeclaredField("lockState"));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3291
            } catch (Exception e) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3292
                throw new Error(e);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3293
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3294
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3295
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3296
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3297
    /* ----------------Table Traversal -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3298
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3299
    /**
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3300
     * Records the table, its length, and current traversal index for a
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3301
     * traverser that must process a region of a forwarded table before
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3302
     * proceeding with current table.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3303
     */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3304
    static final class TableStack<K,V> {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3305
        int length;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3306
        int index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3307
        Node<K,V>[] tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3308
        TableStack<K,V> next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3309
    }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3310
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3311
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3312
     * Encapsulates traversal for methods such as containsValue; also
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3313
     * serves as a base class for other iterators and spliterators.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3314
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3315
     * Method advance visits once each still-valid node that was
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3316
     * reachable upon iterator construction. It might miss some that
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3317
     * were added to a bin after the bin was visited, which is OK wrt
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3318
     * consistency guarantees. Maintaining this property in the face
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3319
     * of possible ongoing resizes requires a fair amount of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3320
     * bookkeeping state that is difficult to optimize away amidst
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3321
     * volatile accesses.  Even so, traversal maintains reasonable
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3322
     * throughput.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3323
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3324
     * Normally, iteration proceeds bin-by-bin traversing lists.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3325
     * However, if the table has been resized, then all future steps
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3326
     * must traverse both the bin at the current index as well as at
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3327
     * (index + baseSize); and so on for further resizings. To
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3328
     * paranoically cope with potential sharing by users of iterators
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3329
     * across threads, iteration terminates if a bounds checks fails
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3330
     * for a table read.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3331
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3332
    static class Traverser<K,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3333
        Node<K,V>[] tab;        // current table; updated if resized
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3334
        Node<K,V> next;         // the next entry to use
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3335
        TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3336
        int index;              // index of bin to use next
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3337
        int baseIndex;          // current index of initial table
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3338
        int baseLimit;          // index bound for initial table
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3339
        final int baseSize;     // initial table size
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3340
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3341
        Traverser(Node<K,V>[] tab, int size, int index, int limit) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3342
            this.tab = tab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3343
            this.baseSize = size;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3344
            this.baseIndex = this.index = index;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3345
            this.baseLimit = limit;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3346
            this.next = null;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3347
        }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3348
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3349
        /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3350
         * Advances if possible, returning next valid node, or null if none.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
         */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3352
        final Node<K,V> advance() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3353
            Node<K,V> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3354
            if ((e = next) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3355
                e = e.next;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3356
            for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3357
                Node<K,V>[] t; int i, n;  // must use locals in checks
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3358
                if (e != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3359
                    return next = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3360
                if (baseIndex >= baseLimit || (t = tab) == null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3361
                    (n = t.length) <= (i = index) || i < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3362
                    return next = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3363
                if ((e = tabAt(t, i)) != null && e.hash < 0) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3364
                    if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3365
                        tab = ((ForwardingNode<K,V>)e).nextTable;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3366
                        e = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3367
                        pushState(t, i, n);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3368
                        continue;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3369
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3370
                    else if (e instanceof TreeBin)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3371
                        e = ((TreeBin<K,V>)e).first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3372
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3373
                        e = null;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3374
                }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3375
                if (stack != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3376
                    recoverState(n);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3377
                else if ((index = i + baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3378
                    index = ++baseIndex; // visit upper slots if present
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
        }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3381
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3382
        /**
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3383
         * Saves traversal state upon encountering a forwarding node.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3384
         */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3385
        private void pushState(Node<K,V>[] t, int i, int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3386
            TableStack<K,V> s = spare;  // reuse if possible
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3387
            if (s != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3388
                spare = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3389
            else
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3390
                s = new TableStack<K,V>();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3391
            s.tab = t;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3392
            s.length = n;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3393
            s.index = i;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3394
            s.next = stack;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3395
            stack = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3396
        }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3397
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3398
        /**
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3399
         * Possibly pops traversal state.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3400
         *
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3401
         * @param n length of current table
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3402
         */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3403
        private void recoverState(int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3404
            TableStack<K,V> s; int len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3405
            while ((s = stack) != null && (index += (len = s.length)) >= n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3406
                n = len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3407
                index = s.index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3408
                tab = s.tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3409
                s.tab = null;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3410
                TableStack<K,V> next = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3411
                s.next = spare; // save for reuse
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3412
                stack = next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3413
                spare = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3414
            }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3415
            if (s == null && (index += baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3416
                index = ++baseIndex;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3417
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3418
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3420
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3421
     * Base of key, value, and entry Iterators. Adds fields to
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3422
     * Traverser to support iterator.remove.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3423
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3424
    static class BaseIterator<K,V> extends Traverser<K,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3425
        final ConcurrentHashMap<K,V> map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3426
        Node<K,V> lastReturned;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3427
        BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3428
                    ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3429
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3430
            this.map = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3431
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3432
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3433
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3434
        public final boolean hasNext() { return next != null; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3435
        public final boolean hasMoreElements() { return next != null; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3436
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3437
        public final void remove() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3438
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3439
            if ((p = lastReturned) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3440
                throw new IllegalStateException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3441
            lastReturned = null;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3442
            map.replaceNode(p.key, null, null);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3443
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3444
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3445
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3446
    static final class KeyIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3447
        implements Iterator<K>, Enumeration<K> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3448
        KeyIterator(Node<K,V>[] tab, int index, int size, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3449
                    ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3450
            super(tab, index, size, limit, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3451
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3452
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3453
        public final K next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3454
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3455
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3456
                throw new NoSuchElementException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3457
            K k = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3458
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3459
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3460
            return k;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3462
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3463
        public final K nextElement() { return next(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3464
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3465
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3466
    static final class ValueIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3467
        implements Iterator<V>, Enumeration<V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3468
        ValueIterator(Node<K,V>[] tab, int index, int size, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3469
                      ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3470
            super(tab, index, size, limit, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3471
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3472
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3473
        public final V next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3474
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3475
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3476
                throw new NoSuchElementException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3477
            V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3478
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3479
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3480
            return v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3481
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3482
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3483
        public final V nextElement() { return next(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3484
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3485
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3486
    static final class EntryIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3487
        implements Iterator<Map.Entry<K,V>> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3488
        EntryIterator(Node<K,V>[] tab, int index, int size, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3489
                      ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3490
            super(tab, index, size, limit, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3491
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3492
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3493
        public final Map.Entry<K,V> next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3494
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3495
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3496
                throw new NoSuchElementException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3497
            K k = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3498
            V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3499
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3500
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3501
            return new MapEntry<K,V>(k, v, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3502
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3505
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3506
     * Exported Entry for EntryIterator
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3507
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3508
    static final class MapEntry<K,V> implements Map.Entry<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3509
        final K key; // non-null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3510
        V val;       // non-null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3511
        final ConcurrentHashMap<K,V> map;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3512
        MapEntry(K key, V val, ConcurrentHashMap<K,V> map) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3513
            this.key = key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3514
            this.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3515
            this.map = map;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3516
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3517
        public K getKey()        { return key; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3518
        public V getValue()      { return val; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3519
        public int hashCode()    { return key.hashCode() ^ val.hashCode(); }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3520
        public String toString() { return key + "=" + val; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3521
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3522
        public boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3523
            Object k, v; Map.Entry<?,?> e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3524
            return ((o instanceof Map.Entry) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3525
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3526
                    (v = e.getValue()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3527
                    (k == key || k.equals(key)) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3528
                    (v == val || v.equals(val)));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3529
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3530
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3531
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3532
         * Sets our entry's value and writes through to the map. The
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3533
         * value to return is somewhat arbitrary here. Since we do not
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3534
         * necessarily track asynchronous changes, the most recent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3535
         * "previous" value could be different from what we return (or
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3536
         * could even have been removed, in which case the put will
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3537
         * re-establish). We do not and cannot guarantee more.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3538
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3539
        public V setValue(V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3540
            if (value == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3541
            V v = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3542
            val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3543
            map.put(key, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3544
            return v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3545
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3546
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3547
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3548
    static final class KeySpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3549
        implements Spliterator<K> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3550
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3551
        KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3552
                       long est) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3553
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3554
            this.est = est;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3555
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3556
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3557
        public Spliterator<K> trySplit() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3558
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3559
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3560
                new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3561
                                        f, est >>>= 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3562
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3563
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3564
        public void forEachRemaining(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3565
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3566
            for (Node<K,V> p; (p = advance()) != null;)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3567
                action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3568
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3569
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3570
        public boolean tryAdvance(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3571
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3572
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3573
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3574
                return false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3575
            action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3576
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3577
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3578
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3579
        public long estimateSize() { return est; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3580
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3581
        public int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3582
            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3583
                Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3584
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3585
    }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3586
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3587
    static final class ValueSpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3588
        implements Spliterator<V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3589
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3590
        ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3591
                         long est) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3592
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3593
            this.est = est;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3594
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3595
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3596
        public Spliterator<V> trySplit() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3597
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3598
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3599
                new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3600
                                          f, est >>>= 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3601
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3602
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3603
        public void forEachRemaining(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3604
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3605
            for (Node<K,V> p; (p = advance()) != null;)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3606
                action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3607
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3608
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3609
        public boolean tryAdvance(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3610
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3611
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3612
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3613
                return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3614
            action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3615
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3616
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3617
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3618
        public long estimateSize() { return est; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3619
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3620
        public int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3621
            return Spliterator.CONCURRENT | Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3622
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3623
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3625
    static final class EntrySpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3626
        implements Spliterator<Map.Entry<K,V>> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3627
        final ConcurrentHashMap<K,V> map; // To export MapEntry
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3628
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3629
        EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3630
                         long est, ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3631
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3632
            this.map = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3633
            this.est = est;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3634
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3635
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3636
        public Spliterator<Map.Entry<K,V>> trySplit() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3637
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3638
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3639
                new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3640
                                          f, est >>>= 1, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3641
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3642
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3643
        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3644
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3645
            for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3646
                action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3647
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3648
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3649
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3650
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3651
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3652
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3653
                return false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3654
            action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3655
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3656
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3657
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3658
        public long estimateSize() { return est; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3659
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3660
        public int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3661
            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3662
                Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3663
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3664
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3665
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3666
    // Parallel bulk operations
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3667
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3668
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3669
     * Computes initial batch value for bulk tasks. The returned value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3670
     * is approximately exp2 of the number of times (minus one) to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3671
     * split task by two before executing leaf action. This value is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3672
     * faster to compute and more convenient to use as a guide to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3673
     * splitting than is the depth, since it is used while dividing by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3674
     * two anyway.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3675
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3676
    final int batchFor(long b) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3677
        long n;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3678
        if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3679
            return 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3680
        int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3681
        return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3682
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3683
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3684
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3685
     * Performs the given action for each (key, value).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3686
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3687
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3688
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3689
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3690
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3691
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3692
    public void forEach(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3693
                        BiConsumer<? super K,? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3694
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3695
        new ForEachMappingTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3696
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3697
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3698
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3699
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3700
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3701
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3702
     * of each (key, value).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3703
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3704
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3705
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3706
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3707
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3708
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3709
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3710
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3711
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3712
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3713
    public <U> void forEach(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3714
                            BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3715
                            Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3716
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3717
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3718
        new ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3719
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3720
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3721
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3722
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3723
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3724
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3725
     * function on each (key, value), or null if none.  Upon
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3726
     * success, further element processing is suppressed and the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3727
     * results of any other parallel invocations of the search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3728
     * function are ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3729
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3730
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3731
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3732
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3733
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3734
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3735
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3736
     * function on each (key, value), or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3737
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3738
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3739
    public <U> U search(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3740
                        BiFunction<? super K, ? super V, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3741
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3742
        return new SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3743
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3744
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3745
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3746
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3747
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3748
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3749
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3750
     * combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3751
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3752
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3753
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3754
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3755
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3756
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3757
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3758
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3759
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3760
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3761
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3762
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3763
    public <U> U reduce(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3764
                        BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3765
                        BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3766
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3767
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3768
        return new MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3769
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3770
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3771
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3772
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3773
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3774
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3775
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3776
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3777
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3778
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3779
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3780
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3781
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3782
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3783
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3784
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3785
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3786
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3787
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3788
    public double reduceToDouble(long parallelismThreshold,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3789
                                 ToDoubleBiFunction<? super K, ? super V> transformer,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3790
                                 double basis,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3791
                                 DoubleBinaryOperator reducer) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3792
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3793
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3794
        return new MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3795
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3796
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3797
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3798
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3799
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3800
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3801
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3802
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3803
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3804
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3805
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3806
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3807
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3808
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3809
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3810
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3811
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3812
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3813
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3814
    public long reduceToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3815
                             ToLongBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3816
                             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3817
                             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3818
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3819
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3820
        return new MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3821
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3822
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3823
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3824
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3825
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3826
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3827
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3828
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3829
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3830
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3831
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3832
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3833
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3834
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3835
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3836
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3837
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3838
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3839
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3840
    public int reduceToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3841
                           ToIntBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3842
                           int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3843
                           IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3844
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3845
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3846
        return new MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3847
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3848
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3849
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3850
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3851
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3852
     * Performs the given action for each key.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3853
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3854
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3855
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3856
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3857
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3858
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3859
    public void forEachKey(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3860
                           Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3861
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3862
        new ForEachKeyTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3863
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3864
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3865
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3866
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3867
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3868
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3869
     * of each key.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3870
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3871
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3872
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3873
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3874
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3875
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3876
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3877
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3878
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3879
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3880
    public <U> void forEachKey(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3881
                               Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3882
                               Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3883
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3884
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3885
        new ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3886
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3887
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3888
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3889
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3890
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3891
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3892
     * function on each key, or null if none. Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3893
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3894
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3895
     * ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3896
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3897
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3898
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3899
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3900
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3901
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3902
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3903
     * function on each key, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3904
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3905
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3906
    public <U> U searchKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3907
                            Function<? super K, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3908
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3909
        return new SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3910
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3911
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3912
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3913
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3914
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3915
     * Returns the result of accumulating all keys using the given
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3916
     * reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3917
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3918
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3919
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3920
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3921
     * @return the result of accumulating all keys using the given
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3922
     * reducer to combine values, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3923
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3924
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3925
    public K reduceKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3926
                        BiFunction<? super K, ? super K, ? extends K> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3927
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3928
        return new ReduceKeysTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3929
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3930
             null, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3931
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3932
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3933
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3934
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3935
     * of all keys using the given reducer to combine values, or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3936
     * null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3937
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3938
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3939
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3940
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3941
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3942
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3943
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3944
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3945
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3946
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3947
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3948
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3949
    public <U> U reduceKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3950
                            Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3951
         BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3952
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3953
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3954
        return new MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3955
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3956
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3957
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3958
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3959
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3960
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3961
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3962
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3963
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3964
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3965
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3966
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3967
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3968
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3969
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3970
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3971
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3972
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3973
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3974
    public double reduceKeysToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3975
                                     ToDoubleFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3976
                                     double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3977
                                     DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3978
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3979
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3980
        return new MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3981
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3982
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3983
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3984
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3985
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3986
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3987
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3988
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3989
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3990
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3991
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3992
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3993
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3994
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3995
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3996
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3997
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3998
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3999
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4000
    public long reduceKeysToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4001
                                 ToLongFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4002
                                 long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4003
                                 LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4004
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4005
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4006
        return new MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4007
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4008
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4009
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4010
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4011
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4012
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4013
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4014
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4015
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4016
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4017
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4018
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4019
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4020
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4021
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4022
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4023
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4024
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4025
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4026
    public int reduceKeysToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4027
                               ToIntFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4028
                               int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4029
                               IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4030
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4031
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4032
        return new MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4033
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4034
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4035
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4036
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4037
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4038
     * Performs the given action for each value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4039
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4040
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4041
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4042
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4043
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4044
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4045
    public void forEachValue(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4046
                             Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4047
        if (action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4048
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4049
        new ForEachValueTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4050
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4051
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4052
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4053
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4054
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4055
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4056
     * of each value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4057
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4058
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4059
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4060
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4061
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4062
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4063
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4064
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4065
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4066
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4067
    public <U> void forEachValue(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4068
                                 Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4069
                                 Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4070
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4071
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4072
        new ForEachTransformedValueTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4073
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4074
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4075
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4076
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4077
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4078
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4079
     * function on each value, or null if none.  Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4080
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4081
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4082
     * ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4083
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4084
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4085
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4086
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4087
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4088
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4089
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4090
     * function on each value, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4091
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4092
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4093
    public <U> U searchValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4094
                              Function<? super V, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4095
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4096
        return new SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4097
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4098
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4099
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4100
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4101
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4102
     * Returns the result of accumulating all values using the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4103
     * given reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4104
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4105
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4106
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4107
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4108
     * @return the result of accumulating all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4109
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4110
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4111
    public V reduceValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4112
                          BiFunction<? super V, ? super V, ? extends V> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4113
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4114
        return new ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4115
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4116
             null, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4117
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4118
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4119
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4120
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4121
     * of all values using the given reducer to combine values, or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4122
     * null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4123
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4124
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4125
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4126
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4127
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4128
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4129
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4130
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4131
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4132
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4133
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4134
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4135
    public <U> U reduceValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4136
                              Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4137
                              BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4138
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4139
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4140
        return new MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4141
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4142
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4143
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4144
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4145
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4146
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4147
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4148
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4149
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4150
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4151
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4152
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4153
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4154
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4155
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4156
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4157
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4158
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4159
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4160
    public double reduceValuesToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4161
                                       ToDoubleFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4162
                                       double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4163
                                       DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4164
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4165
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4166
        return new MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4167
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4168
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4169
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4170
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4171
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4172
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4173
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4174
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4175
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4176
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4177
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4178
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4179
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4180
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4181
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4182
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4183
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4184
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4185
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4186
    public long reduceValuesToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4187
                                   ToLongFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4188
                                   long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4189
                                   LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4190
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4191
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4192
        return new MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4193
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4194
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4195
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4196
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4197
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4198
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4199
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4200
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4201
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4202
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4203
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4204
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4205
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4206
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4207
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4208
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4209
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4210
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4211
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4212
    public int reduceValuesToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4213
                                 ToIntFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4214
                                 int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4215
                                 IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4216
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4217
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4218
        return new MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4219
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4220
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4221
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4222
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4223
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4224
     * Performs the given action for each entry.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4225
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4226
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4227
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4228
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4229
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4230
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4231
    public void forEachEntry(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4232
                             Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4233
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4234
        new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4235
                                  action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4236
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4237
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4238
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4239
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4240
     * of each entry.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4241
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4242
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4243
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4244
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4245
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4246
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4247
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4248
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4249
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4250
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4251
    public <U> void forEachEntry(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4252
                                 Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4253
                                 Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4254
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4255
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4256
        new ForEachTransformedEntryTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4257
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4258
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4259
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4260
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4261
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4262
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4263
     * function on each entry, or null if none.  Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4264
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4265
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4266
     * ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4267
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4268
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4269
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4270
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4271
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4272
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4273
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4274
     * function on each entry, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4275
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4276
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4277
    public <U> U searchEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4278
                               Function<Map.Entry<K,V>, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4279
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4280
        return new SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4281
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4282
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4283
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4284
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4285
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4286
     * Returns the result of accumulating all entries using the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4287
     * given reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4288
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4289
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4290
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4291
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4292
     * @return the result of accumulating all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4293
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4294
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4295
    public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4296
                                        BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4297
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4298
        return new ReduceEntriesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4299
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4300
             null, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4301
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4302
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4303
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4304
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4305
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4306
     * or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4307
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4308
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4309
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4310
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4311
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4312
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4313
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4314
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4315
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4316
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4317
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4318
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4319
    public <U> U reduceEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4320
                               Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4321
                               BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4322
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4323
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4324
        return new MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4325
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4326
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4327
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4328
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4329
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4330
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4331
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4332
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4333
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4334
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4335
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4336
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4337
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4338
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4339
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4340
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4341
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4342
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4343
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4344
    public double reduceEntriesToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4345
                                        ToDoubleFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4346
                                        double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4347
                                        DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4348
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4349
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4350
        return new MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4351
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4352
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4353
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4354
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4355
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4356
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4357
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4358
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4359
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4360
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4361
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4362
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4363
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4364
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4365
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4366
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4367
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4368
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4369
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4370
    public long reduceEntriesToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4371
                                    ToLongFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4372
                                    long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4373
                                    LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4374
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4375
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4376
        return new MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4377
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4378
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4379
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4380
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4381
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4382
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4383
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4384
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4385
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4386
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4387
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4388
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4389
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4390
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4391
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4392
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4393
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4394
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4395
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4396
    public int reduceEntriesToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4397
                                  ToIntFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4398
                                  int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4399
                                  IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4400
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4401
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4402
        return new MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4403
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4404
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4405
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4406
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4407
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4408
    /* ----------------Views -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4409
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4410
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4411
     * Base class for views.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4412
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4413
    abstract static class CollectionView<K,V,E>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4414
        implements Collection<E>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4415
        private static final long serialVersionUID = 7249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4416
        final ConcurrentHashMap<K,V> map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4417
        CollectionView(ConcurrentHashMap<K,V> map)  { this.map = map; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4418
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4419
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4420
         * Returns the map backing this view.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4421
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4422
         * @return the map backing this view
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4423
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4424
        public ConcurrentHashMap<K,V> getMap() { return map; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4425
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4426
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4427
         * Removes all of the elements from this view, by removing all
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4428
         * the mappings from the map backing this view.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4429
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4430
        public final void clear()      { map.clear(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4431
        public final int size()        { return map.size(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4432
        public final boolean isEmpty() { return map.isEmpty(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4433
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4434
        // implementations below rely on concrete classes supplying these
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4435
        // abstract methods
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4436
        /**
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4437
         * Returns an iterator over the elements in this collection.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4438
         *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4439
         * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4440
         * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4441
         *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4442
         * @return an iterator over the elements in this collection
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4443
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4444
        public abstract Iterator<E> iterator();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4445
        public abstract boolean contains(Object o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4446
        public abstract boolean remove(Object o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4447
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4448
        private static final String oomeMsg = "Required array size too large";
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4449
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4450
        public final Object[] toArray() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4451
            long sz = map.mappingCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4452
            if (sz > MAX_ARRAY_SIZE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4453
                throw new OutOfMemoryError(oomeMsg);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4454
            int n = (int)sz;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4455
            Object[] r = new Object[n];
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4456
            int i = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4457
            for (E e : this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4458
                if (i == n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4459
                    if (n >= MAX_ARRAY_SIZE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4460
                        throw new OutOfMemoryError(oomeMsg);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4461
                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4462
                        n = MAX_ARRAY_SIZE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4463
                    else
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4464
                        n += (n >>> 1) + 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4465
                    r = Arrays.copyOf(r, n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4466
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4467
                r[i++] = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4468
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4469
            return (i == n) ? r : Arrays.copyOf(r, i);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4470
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4471
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4472
        @SuppressWarnings("unchecked")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4473
        public final <T> T[] toArray(T[] a) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4474
            long sz = map.mappingCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4475
            if (sz > MAX_ARRAY_SIZE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4476
                throw new OutOfMemoryError(oomeMsg);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4477
            int m = (int)sz;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4478
            T[] r = (a.length >= m) ? a :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4479
                (T[])java.lang.reflect.Array
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4480
                .newInstance(a.getClass().getComponentType(), m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4481
            int n = r.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4482
            int i = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4483
            for (E e : this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4484
                if (i == n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4485
                    if (n >= MAX_ARRAY_SIZE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4486
                        throw new OutOfMemoryError(oomeMsg);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4487
                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4488
                        n = MAX_ARRAY_SIZE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4489
                    else
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4490
                        n += (n >>> 1) + 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4491
                    r = Arrays.copyOf(r, n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4492
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4493
                r[i++] = (T)e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4494
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4495
            if (a == r && i < n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4496
                r[i] = null; // null-terminate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4497
                return r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4498
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4499
            return (i == n) ? r : Arrays.copyOf(r, i);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4500
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4501
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4502
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4503
         * Returns a string representation of this collection.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4504
         * The string representation consists of the string representations
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4505
         * of the collection's elements in the order they are returned by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4506
         * its iterator, enclosed in square brackets ({@code "[]"}).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4507
         * Adjacent elements are separated by the characters {@code ", "}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4508
         * (comma and space).  Elements are converted to strings as by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4509
         * {@link String#valueOf(Object)}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4510
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4511
         * @return a string representation of this collection
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4512
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4513
        public final String toString() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4514
            StringBuilder sb = new StringBuilder();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4515
            sb.append('[');
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4516
            Iterator<E> it = iterator();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4517
            if (it.hasNext()) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4518
                for (;;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4519
                    Object e = it.next();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4520
                    sb.append(e == this ? "(this Collection)" : e);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4521
                    if (!it.hasNext())
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4522
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4523
                    sb.append(',').append(' ');
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4524
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4525
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4526
            return sb.append(']').toString();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4527
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4528
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4529
        public final boolean containsAll(Collection<?> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4530
            if (c != this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4531
                for (Object e : c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4532
                    if (e == null || !contains(e))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4533
                        return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4534
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4535
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4536
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4537
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4538
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4539
        public final boolean removeAll(Collection<?> c) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  4540
            if (c == null) throw new NullPointerException();
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4541
            boolean modified = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4542
            for (Iterator<E> it = iterator(); it.hasNext();) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4543
                if (c.contains(it.next())) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4544
                    it.remove();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4545
                    modified = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4546
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4547
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4548
            return modified;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4549
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4550
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4551
        public final boolean retainAll(Collection<?> c) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  4552
            if (c == null) throw new NullPointerException();
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4553
            boolean modified = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4554
            for (Iterator<E> it = iterator(); it.hasNext();) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4555
                if (!c.contains(it.next())) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4556
                    it.remove();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4557
                    modified = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4558
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4559
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4560
            return modified;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4561
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4562
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4563
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4564
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4565
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4566
     * A view of a ConcurrentHashMap as a {@link Set} of keys, in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4567
     * which additions may optionally be enabled by mapping to a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4568
     * common value.  This class cannot be directly instantiated.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4569
     * See {@link #keySet() keySet()},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4570
     * {@link #keySet(Object) keySet(V)},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4571
     * {@link #newKeySet() newKeySet()},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4572
     * {@link #newKeySet(int) newKeySet(int)}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4573
     *
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4574
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4575
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4576
    public static class KeySetView<K,V> extends CollectionView<K,V,K>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4577
        implements Set<K>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4578
        private static final long serialVersionUID = 7249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4579
        private final V value;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4580
        KeySetView(ConcurrentHashMap<K,V> map, V value) {  // non-public
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4581
            super(map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4582
            this.value = value;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4583
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4584
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4585
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4586
         * Returns the default mapped value for additions,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4587
         * or {@code null} if additions are not supported.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4588
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4589
         * @return the default mapped value for additions, or {@code null}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4590
         * if not supported
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4591
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4592
        public V getMappedValue() { return value; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4593
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4594
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4595
         * {@inheritDoc}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4596
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4597
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4598
        public boolean contains(Object o) { return map.containsKey(o); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4599
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4600
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4601
         * Removes the key from this map view, by removing the key (and its
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4602
         * corresponding value) from the backing map.  This method does
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4603
         * nothing if the key is not in the map.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4604
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4605
         * @param  o the key to be removed from the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4606
         * @return {@code true} if the backing map contained the specified key
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4607
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4608
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4609
        public boolean remove(Object o) { return map.remove(o) != null; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4610
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4611
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4612
         * @return an iterator over the keys of the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4613
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4614
        public Iterator<K> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4615
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4616
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4617
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4618
            return new KeyIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4619
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4620
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4621
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4622
         * Adds the specified key to this set view by mapping the key to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4623
         * the default mapped value in the backing map, if defined.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4624
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4625
         * @param e key to be added
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4626
         * @return {@code true} if this set changed as a result of the call
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4627
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4628
         * @throws UnsupportedOperationException if no default mapped value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4629
         * for additions was provided
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4630
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4631
        public boolean add(K e) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4632
            V v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4633
            if ((v = value) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4634
                throw new UnsupportedOperationException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4635
            return map.putVal(e, v, true) == null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4636
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4637
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4638
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4639
         * Adds all of the elements in the specified collection to this set,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4640
         * as if by calling {@link #add} on each one.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4641
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4642
         * @param c the elements to be inserted into this set
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4643
         * @return {@code true} if this set changed as a result of the call
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4644
         * @throws NullPointerException if the collection or any of its
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4645
         * elements are {@code null}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4646
         * @throws UnsupportedOperationException if no default mapped value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4647
         * for additions was provided
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4648
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4649
        public boolean addAll(Collection<? extends K> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4650
            boolean added = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4651
            V v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4652
            if ((v = value) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4653
                throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4654
            for (K e : c) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4655
                if (map.putVal(e, v, true) == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4656
                    added = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4657
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4658
            return added;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4659
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4660
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4661
        public int hashCode() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4662
            int h = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4663
            for (K e : this)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4664
                h += e.hashCode();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4665
            return h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4666
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4667
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4668
        public boolean equals(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4669
            Set<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4670
            return ((o instanceof Set) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4671
                    ((c = (Set<?>)o) == this ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4672
                     (containsAll(c) && c.containsAll(this))));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4673
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4674
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4675
        public Spliterator<K> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4676
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4677
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4678
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4679
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4680
            return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4681
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4682
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4683
        public void forEach(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4684
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4685
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4686
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4687
                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4688
                for (Node<K,V> p; (p = it.advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4689
                    action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4690
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4691
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4692
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4693
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4694
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4695
     * A view of a ConcurrentHashMap as a {@link Collection} of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4696
     * values, in which additions are disabled. This class cannot be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4697
     * directly instantiated. See {@link #values()}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4698
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4699
    static final class ValuesView<K,V> extends CollectionView<K,V,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4700
        implements Collection<V>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4701
        private static final long serialVersionUID = 2249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4702
        ValuesView(ConcurrentHashMap<K,V> map) { super(map); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4703
        public final boolean contains(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4704
            return map.containsValue(o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4705
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4706
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4707
        public final boolean remove(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4708
            if (o != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4709
                for (Iterator<V> it = iterator(); it.hasNext();) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4710
                    if (o.equals(it.next())) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4711
                        it.remove();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4712
                        return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4713
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4714
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4715
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4716
            return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4717
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4718
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4719
        public final Iterator<V> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4720
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4721
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4722
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4723
            return new ValueIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4724
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4725
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4726
        public final boolean add(V e) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4727
            throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4728
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4729
        public final boolean addAll(Collection<? extends V> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4730
            throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4731
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4732
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4733
        public boolean removeIf(Predicate<? super V> filter) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4734
            return map.removeValueIf(filter);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4735
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4736
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4737
        public Spliterator<V> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4738
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4739
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4740
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4741
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4742
            return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4743
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4744
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4745
        public void forEach(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4746
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4747
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4748
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4749
                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4750
                for (Node<K,V> p; (p = it.advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4751
                    action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4752
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4753
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4754
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4755
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4756
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4757
     * A view of a ConcurrentHashMap as a {@link Set} of (key, value)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4758
     * entries.  This class cannot be directly instantiated. See
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4759
     * {@link #entrySet()}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4760
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4761
    static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4762
        implements Set<Map.Entry<K,V>>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4763
        private static final long serialVersionUID = 2249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4764
        EntrySetView(ConcurrentHashMap<K,V> map) { super(map); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4765
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4766
        public boolean contains(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4767
            Object k, v, r; Map.Entry<?,?> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4768
            return ((o instanceof Map.Entry) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4769
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4770
                    (r = map.get(k)) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4771
                    (v = e.getValue()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4772
                    (v == r || v.equals(r)));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4773
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4774
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4775
        public boolean remove(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4776
            Object k, v; Map.Entry<?,?> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4777
            return ((o instanceof Map.Entry) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4778
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4779
                    (v = e.getValue()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4780
                    map.remove(k, v));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4781
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4782
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4783
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4784
         * @return an iterator over the entries of the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4785
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4786
        public Iterator<Map.Entry<K,V>> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4787
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4788
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4789
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4790
            return new EntryIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4791
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4792
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4793
        public boolean add(Entry<K,V> e) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4794
            return map.putVal(e.getKey(), e.getValue(), false) == null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4795
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4796
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4797
        public boolean addAll(Collection<? extends Entry<K,V>> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4798
            boolean added = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4799
            for (Entry<K,V> e : c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4800
                if (add(e))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4801
                    added = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4802
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4803
            return added;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4804
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4805
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4806
        public boolean removeIf(Predicate<? super Entry<K, V>> filter) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4807
            return map.removeEntryIf(filter);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4808
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4809
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4810
        public final int hashCode() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4811
            int h = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4812
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4813
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4814
                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4815
                for (Node<K,V> p; (p = it.advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4816
                    h += p.hashCode();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4817
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4818
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4819
            return h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4820
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4821
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4822
        public final boolean equals(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4823
            Set<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4824
            return ((o instanceof Set) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4825
                    ((c = (Set<?>)o) == this ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4826
                     (containsAll(c) && c.containsAll(this))));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4827
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4828
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4829
        public Spliterator<Map.Entry<K,V>> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4830
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4831
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4832
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4833
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4834
            return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4835
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4836
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4837
        public void forEach(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4838
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4839
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4840
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4841
                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4842
                for (Node<K,V> p; (p = it.advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4843
                    action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4844
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4845
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4846
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4847
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4848
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4849
    // -------------------------------------------------------
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4850
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4851
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4852
     * Base class for bulk tasks. Repeats some fields and code from
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4853
     * class Traverser, because we need to subclass CountedCompleter.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4854
     */
19380
22eefc44fad6 8022724: lint warnings in j.u.concurrent packages
dl
parents: 19036
diff changeset
  4855
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4856
    abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4857
        Node<K,V>[] tab;        // same as Traverser
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4858
        Node<K,V> next;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4859
        TableStack<K,V> stack, spare;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4860
        int index;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4861
        int baseIndex;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4862
        int baseLimit;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4863
        final int baseSize;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4864
        int batch;              // split control
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4865
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4866
        BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4867
            super(par);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4868
            this.batch = b;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4869
            this.index = this.baseIndex = i;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4870
            if ((this.tab = t) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4871
                this.baseSize = this.baseLimit = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4872
            else if (par == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4873
                this.baseSize = this.baseLimit = t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4874
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4875
                this.baseLimit = f;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4876
                this.baseSize = par.baseSize;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4877
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4878
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4879
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4880
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4881
         * Same as Traverser version
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4882
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4883
        final Node<K,V> advance() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4884
            Node<K,V> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4885
            if ((e = next) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4886
                e = e.next;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4887
            for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4888
                Node<K,V>[] t; int i, n;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4889
                if (e != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4890
                    return next = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4891
                if (baseIndex >= baseLimit || (t = tab) == null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4892
                    (n = t.length) <= (i = index) || i < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4893
                    return next = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4894
                if ((e = tabAt(t, i)) != null && e.hash < 0) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4895
                    if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4896
                        tab = ((ForwardingNode<K,V>)e).nextTable;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4897
                        e = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4898
                        pushState(t, i, n);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4899
                        continue;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4900
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4901
                    else if (e instanceof TreeBin)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4902
                        e = ((TreeBin<K,V>)e).first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4903
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4904
                        e = null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4905
                }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4906
                if (stack != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4907
                    recoverState(n);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4908
                else if ((index = i + baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4909
                    index = ++baseIndex;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  4910
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4911
        }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4912
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4913
        private void pushState(Node<K,V>[] t, int i, int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4914
            TableStack<K,V> s = spare;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4915
            if (s != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4916
                spare = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4917
            else
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4918
                s = new TableStack<K,V>();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4919
            s.tab = t;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4920
            s.length = n;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4921
            s.index = i;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4922
            s.next = stack;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4923
            stack = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4924
        }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4925
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4926
        private void recoverState(int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4927
            TableStack<K,V> s; int len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4928
            while ((s = stack) != null && (index += (len = s.length)) >= n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4929
                n = len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4930
                index = s.index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4931
                tab = s.tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4932
                s.tab = null;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4933
                TableStack<K,V> next = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4934
                s.next = spare; // save for reuse
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4935
                stack = next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4936
                spare = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4937
            }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4938
            if (s == null && (index += baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4939
                index = ++baseIndex;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4940
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4941
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4942
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4943
    /*
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4944
     * Task classes. Coded in a regular but ugly format/style to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4945
     * simplify checks that each variant differs in the right way from
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4946
     * others. The null screenings exist because compilers cannot tell
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4947
     * that we've already null-checked task arguments, so we force
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4948
     * simplest hoisted bypass to help avoid convoluted traps.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4949
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4950
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4951
    static final class ForEachKeyTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4952
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4953
        final Consumer<? super K> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4954
        ForEachKeyTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4955
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4956
             Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4957
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4958
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4959
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4960
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4961
            final Consumer<? super K> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4962
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4963
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4964
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4965
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4966
                    new ForEachKeyTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4967
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4968
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4969
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4970
                for (Node<K,V> p; (p = advance()) != null;)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4971
                    action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4972
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4973
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4974
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4975
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4976
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4977
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4978
    static final class ForEachValueTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4979
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4980
        final Consumer<? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4981
        ForEachValueTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4982
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4983
             Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4984
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4985
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4986
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4987
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4988
            final Consumer<? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4989
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4990
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4991
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4992
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4993
                    new ForEachValueTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4994
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4995
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4996
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4997
                for (Node<K,V> p; (p = advance()) != null;)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4998
                    action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4999
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5000
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5001
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5002
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5003
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5004
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5005
    static final class ForEachEntryTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5006
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5007
        final Consumer<? super Entry<K,V>> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5008
        ForEachEntryTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5009
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5010
             Consumer<? super Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5011
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5012
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5013
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5014
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5015
            final Consumer<? super Entry<K,V>> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5016
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5017
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5018
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5019
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5020
                    new ForEachEntryTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5021
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5022
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5023
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5024
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5025
                    action.accept(p);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5026
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5027
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5028
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5029
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5030
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5031
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5032
    static final class ForEachMappingTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5033
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5034
        final BiConsumer<? super K, ? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5035
        ForEachMappingTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5036
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5037
             BiConsumer<? super K,? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5038
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5039
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5040
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5041
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5042
            final BiConsumer<? super K, ? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5043
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5044
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5045
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5046
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5047
                    new ForEachMappingTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5048
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5049
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5050
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5051
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5052
                    action.accept(p.key, p.val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5053
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5054
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5055
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5056
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5057
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5058
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5059
    static final class ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5060
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5061
        final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5062
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5063
        ForEachTransformedKeyTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5064
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5065
             Function<? super K, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5066
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5067
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5068
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5069
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5070
            final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5071
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5072
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5073
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5074
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5075
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5076
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5077
                    new ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5078
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5079
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5080
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5081
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5082
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5083
                    if ((u = transformer.apply(p.key)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5084
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5085
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5086
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5087
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5088
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5089
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5090
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5091
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5092
    static final class ForEachTransformedValueTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5093
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5094
        final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5095
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5096
        ForEachTransformedValueTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5097
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5098
             Function<? super V, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5099
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5100
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5101
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5102
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5103
            final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5104
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5105
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5106
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5107
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5108
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5109
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5110
                    new ForEachTransformedValueTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5111
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5112
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5113
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5114
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5115
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5116
                    if ((u = transformer.apply(p.val)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5117
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5118
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5119
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5120
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5121
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5122
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5123
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5124
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5125
    static final class ForEachTransformedEntryTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5126
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5127
        final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5128
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5129
        ForEachTransformedEntryTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5130
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5131
             Function<Map.Entry<K,V>, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5132
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5133
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5134
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5135
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5136
            final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5137
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5138
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5139
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5140
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5141
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5142
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5143
                    new ForEachTransformedEntryTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5144
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5145
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5146
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5147
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5148
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5149
                    if ((u = transformer.apply(p)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5150
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5151
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5152
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5153
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5154
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5155
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5156
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5157
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5158
    static final class ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5159
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5160
        final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5161
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5162
        ForEachTransformedMappingTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5163
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5164
             BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5165
             Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5166
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5167
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5168
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5169
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5170
            final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5171
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5172
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5173
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5174
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5175
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5176
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5177
                    new ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5178
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5179
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5180
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5181
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5182
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5183
                    if ((u = transformer.apply(p.key, p.val)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5184
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5185
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5186
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5187
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5188
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5189
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5190
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5191
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5192
    static final class SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5193
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5194
        final Function<? super K, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5195
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5196
        SearchKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5197
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5198
             Function<? super K, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5199
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5200
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5201
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5202
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5203
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5204
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5205
            final Function<? super K, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5206
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5207
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5208
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5209
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5210
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5211
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5212
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5213
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5214
                    new SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5215
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5216
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5217
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5218
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5219
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5220
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5221
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5222
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5223
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5224
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5225
                    if ((u = searchFunction.apply(p.key)) != null) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5226
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5227
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5228
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5229
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5230
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5231
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5232
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5233
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5234
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5235
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5236
    static final class SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5237
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5238
        final Function<? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5239
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5240
        SearchValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5241
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5242
             Function<? super V, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5243
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5244
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5245
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5246
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5247
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5248
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5249
            final Function<? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5250
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5251
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5252
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5253
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5254
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5255
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5256
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5257
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5258
                    new SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5259
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5260
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5261
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5262
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5263
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5264
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5265
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5266
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5267
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5268
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5269
                    if ((u = searchFunction.apply(p.val)) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5270
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5271
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5272
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5273
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5274
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5275
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5276
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5277
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5278
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5279
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5280
    static final class SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5281
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5282
        final Function<Entry<K,V>, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5283
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5284
        SearchEntriesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5285
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5286
             Function<Entry<K,V>, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5287
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5288
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5289
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5290
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5291
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5292
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5293
            final Function<Entry<K,V>, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5294
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5295
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5296
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5297
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5298
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5299
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5300
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5301
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5302
                    new SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5303
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5304
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5305
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5306
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5307
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5308
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5309
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5310
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5311
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5312
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5313
                    if ((u = searchFunction.apply(p)) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5314
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5315
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5316
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5317
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5318
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5319
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5320
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5321
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5322
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5323
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5324
    static final class SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5325
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5326
        final BiFunction<? super K, ? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5327
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5328
        SearchMappingsTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5329
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5330
             BiFunction<? super K, ? super V, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5331
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5332
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5333
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5334
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5335
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5336
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5337
            final BiFunction<? super K, ? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5338
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5339
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5340
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5341
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5342
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5343
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5344
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5345
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5346
                    new SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5347
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5348
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5349
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5350
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5351
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5352
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5353
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5354
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5355
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5356
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5357
                    if ((u = searchFunction.apply(p.key, p.val)) != null) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5358
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5359
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5360
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5361
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5362
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5363
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5364
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5365
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5366
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5367
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5368
    static final class ReduceKeysTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5369
        extends BulkTask<K,V,K> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5370
        final BiFunction<? super K, ? super K, ? extends K> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5371
        K result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5372
        ReduceKeysTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5373
        ReduceKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5374
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5375
             ReduceKeysTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5376
             BiFunction<? super K, ? super K, ? extends K> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5377
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5378
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5379
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5380
        public final K getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5381
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5382
            final BiFunction<? super K, ? super K, ? extends K> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5383
            if ((reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5384
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5385
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5386
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5387
                    (rights = new ReduceKeysTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5388
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5389
                      rights, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5390
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5391
                K r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5392
                for (Node<K,V> p; (p = advance()) != null; ) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5393
                    K u = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5394
                    r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5395
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5396
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5397
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5398
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5399
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5400
                    ReduceKeysTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5401
                        t = (ReduceKeysTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5402
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5403
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5404
                        K tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5405
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5406
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5407
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5408
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5409
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5410
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5411
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5412
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5413
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5414
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5415
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5416
    static final class ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5417
        extends BulkTask<K,V,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5418
        final BiFunction<? super V, ? super V, ? extends V> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5419
        V result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5420
        ReduceValuesTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5421
        ReduceValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5422
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5423
             ReduceValuesTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5424
             BiFunction<? super V, ? super V, ? extends V> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5425
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5426
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5427
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5428
        public final V getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5429
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5430
            final BiFunction<? super V, ? super V, ? extends V> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5431
            if ((reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5432
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5433
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5434
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5435
                    (rights = new ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5436
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5437
                      rights, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5438
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5439
                V r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5440
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5441
                    V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5442
                    r = (r == null) ? v : reducer.apply(r, v);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5443
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5444
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5445
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5446
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5447
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5448
                    ReduceValuesTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5449
                        t = (ReduceValuesTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5450
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5451
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5452
                        V tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5453
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5454
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5455
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5456
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5457
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5458
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5459
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5460
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5461
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5462
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5463
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5464
    static final class ReduceEntriesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5465
        extends BulkTask<K,V,Map.Entry<K,V>> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5466
        final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5467
        Map.Entry<K,V> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5468
        ReduceEntriesTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5469
        ReduceEntriesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5470
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5471
             ReduceEntriesTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5472
             BiFunction<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5473
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5474
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5475
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5476
        public final Map.Entry<K,V> getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5477
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5478
            final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5479
            if ((reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5480
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5481
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5482
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5483
                    (rights = new ReduceEntriesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5484
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5485
                      rights, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5486
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5487
                Map.Entry<K,V> r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5488
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5489
                    r = (r == null) ? p : reducer.apply(r, p);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5490
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5491
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5492
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5493
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5494
                    ReduceEntriesTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5495
                        t = (ReduceEntriesTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5496
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5497
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5498
                        Map.Entry<K,V> tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5499
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5500
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5501
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5502
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5503
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5504
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5505
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5506
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5507
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5508
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5509
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5510
    static final class MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5511
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5512
        final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5513
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5514
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5515
        MapReduceKeysTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5516
        MapReduceKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5517
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5518
             MapReduceKeysTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5519
             Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5520
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5521
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5522
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5523
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5524
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5525
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5526
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5527
            final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5528
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5529
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5530
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5531
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5532
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5533
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5534
                    (rights = new MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5535
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5536
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5537
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5538
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5539
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5540
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5541
                    if ((u = transformer.apply(p.key)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5542
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5543
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5544
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5545
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5546
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5547
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5548
                    MapReduceKeysTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5549
                        t = (MapReduceKeysTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5550
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5551
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5552
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5553
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5554
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5555
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5556
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5557
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5558
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5559
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5560
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5561
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5562
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5563
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5564
    static final class MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5565
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5566
        final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5567
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5568
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5569
        MapReduceValuesTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5570
        MapReduceValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5571
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5572
             MapReduceValuesTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5573
             Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5574
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5575
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5576
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5577
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5578
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5579
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5580
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5581
            final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5582
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5583
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5584
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5585
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5586
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5587
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5588
                    (rights = new MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5589
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5590
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5591
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5592
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5593
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5594
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5595
                    if ((u = transformer.apply(p.val)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5596
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5597
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5598
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5599
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5600
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5601
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5602
                    MapReduceValuesTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5603
                        t = (MapReduceValuesTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5604
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5605
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5606
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5607
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5608
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5609
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5610
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5611
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5612
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5613
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5614
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5615
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5616
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5617
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5618
    static final class MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5619
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5620
        final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5621
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5622
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5623
        MapReduceEntriesTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5624
        MapReduceEntriesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5625
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5626
             MapReduceEntriesTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5627
             Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5628
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5629
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5630
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5631
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5632
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5633
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5634
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5635
            final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5636
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5637
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5638
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5639
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5640
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5641
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5642
                    (rights = new MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5643
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5644
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5645
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5646
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5647
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5648
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5649
                    if ((u = transformer.apply(p)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5650
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5651
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5652
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5653
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5654
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5655
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5656
                    MapReduceEntriesTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5657
                        t = (MapReduceEntriesTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5658
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5659
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5660
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5661
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5662
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5663
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5664
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5665
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5666
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5667
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5668
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5669
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5670
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5671
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5672
    static final class MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5673
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5674
        final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5675
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5676
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5677
        MapReduceMappingsTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5678
        MapReduceMappingsTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5679
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5680
             MapReduceMappingsTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5681
             BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5682
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5683
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5684
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5685
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5686
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5687
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5688
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5689
            final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5690
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5691
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5692
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5693
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5694
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5695
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5696
                    (rights = new MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5697
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5698
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5699
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5700
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5701
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5702
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5703
                    if ((u = transformer.apply(p.key, p.val)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5704
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5705
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5706
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5707
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5708
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5709
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5710
                    MapReduceMappingsTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5711
                        t = (MapReduceMappingsTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5712
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5713
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5714
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5715
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5716
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5717
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5718
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5719
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5720
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5721
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5722
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5723
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5724
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5725
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5726
    static final class MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5727
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5728
        final ToDoubleFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5729
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5730
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5731
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5732
        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5733
        MapReduceKeysToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5734
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5735
             MapReduceKeysToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5736
             ToDoubleFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5737
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5738
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5739
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5740
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5741
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5742
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5743
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5744
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5745
            final ToDoubleFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5746
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5747
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5748
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5749
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5750
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5751
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5752
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5753
                    (rights = new MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5754
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5755
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5756
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5757
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5758
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5759
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5760
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5761
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5762
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5763
                    MapReduceKeysToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5764
                        t = (MapReduceKeysToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5765
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5766
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5767
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5768
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5769
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5770
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5771
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5772
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5773
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5774
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5775
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5776
    static final class MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5777
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5778
        final ToDoubleFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5779
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5780
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5781
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5782
        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5783
        MapReduceValuesToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5784
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5785
             MapReduceValuesToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5786
             ToDoubleFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5787
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5788
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5789
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5790
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5791
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5792
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5793
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5794
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5795
            final ToDoubleFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5796
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5797
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5798
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5799
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5800
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5801
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5802
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5803
                    (rights = new MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5804
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5805
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5806
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5807
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5808
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5809
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5810
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5811
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5812
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5813
                    MapReduceValuesToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5814
                        t = (MapReduceValuesToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5815
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5816
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5817
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5818
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5819
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5820
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5821
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5822
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5823
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5824
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5825
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5826
    static final class MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5827
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5828
        final ToDoubleFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5829
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5830
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5831
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5832
        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5833
        MapReduceEntriesToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5834
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5835
             MapReduceEntriesToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5836
             ToDoubleFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5837
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5838
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5839
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5840
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5841
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5842
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5843
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5844
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5845
            final ToDoubleFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5846
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5847
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5848
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5849
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5850
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5851
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5852
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5853
                    (rights = new MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5854
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5855
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5856
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5857
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5858
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5859
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5860
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5861
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5862
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5863
                    MapReduceEntriesToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5864
                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5865
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5866
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5867
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5868
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5869
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5870
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5871
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5872
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5873
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5874
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5875
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5876
    static final class MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5877
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5878
        final ToDoubleBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5879
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5880
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5881
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5882
        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5883
        MapReduceMappingsToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5884
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5885
             MapReduceMappingsToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5886
             ToDoubleBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5887
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5888
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5889
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5890
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5891
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5892
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5893
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5894
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5895
            final ToDoubleBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5896
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5897
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5898
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5899
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5900
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5901
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5902
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5903
                    (rights = new MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5904
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5905
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5906
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5907
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5908
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5909
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5910
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5911
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5912
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5913
                    MapReduceMappingsToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5914
                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5915
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5916
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5917
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5918
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5919
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5920
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5921
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5922
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5923
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5924
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5925
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5926
    static final class MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5927
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5928
        final ToLongFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5929
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5930
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5931
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5932
        MapReduceKeysToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5933
        MapReduceKeysToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5934
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5935
             MapReduceKeysToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5936
             ToLongFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5937
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5938
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5939
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5940
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5941
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5942
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5943
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5944
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5945
            final ToLongFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5946
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5947
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5948
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5949
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5950
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5951
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5952
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5953
                    (rights = new MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5954
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5955
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5956
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5957
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5958
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5959
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5960
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5961
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5962
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5963
                    MapReduceKeysToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5964
                        t = (MapReduceKeysToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5965
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5966
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5967
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5968
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5969
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5970
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5971
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5972
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5973
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5974
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5975
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5976
    static final class MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5977
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5978
        final ToLongFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5979
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5980
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5981
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5982
        MapReduceValuesToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5983
        MapReduceValuesToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5984
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5985
             MapReduceValuesToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5986
             ToLongFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5987
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5988
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5989
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5990
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5991
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5992
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5993
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5994
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5995
            final ToLongFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5996
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5997
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5998
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5999
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6000
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6001
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6002
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6003
                    (rights = new MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6004
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6005
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6006
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6007
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6008
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6009
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6010
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6011
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6012
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6013
                    MapReduceValuesToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6014
                        t = (MapReduceValuesToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6015
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6016
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6017
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6018
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6019
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6020
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6021
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6022
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6023
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6024
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6025
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6026
    static final class MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6027
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6028
        final ToLongFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6029
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6030
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6031
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6032
        MapReduceEntriesToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6033
        MapReduceEntriesToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6034
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6035
             MapReduceEntriesToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6036
             ToLongFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6037
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6038
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6039
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6040
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6041
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6042
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6043
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6044
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6045
            final ToLongFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6046
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6047
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6048
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6049
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6050
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6051
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6052
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6053
                    (rights = new MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6054
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6055
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6056
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6057
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6058
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6059
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6060
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6061
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6062
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6063
                    MapReduceEntriesToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6064
                        t = (MapReduceEntriesToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6065
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6066
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6067
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6068
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6069
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6070
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6071
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6072
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6073
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6074
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6075
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6076
    static final class MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6077
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6078
        final ToLongBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6079
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6080
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6081
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6082
        MapReduceMappingsToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6083
        MapReduceMappingsToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6084
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6085
             MapReduceMappingsToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6086
             ToLongBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6087
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6088
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6089
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6090
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6091
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6092
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6093
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6094
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6095
            final ToLongBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6096
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6097
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6098
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6099
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6100
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6101
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6102
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6103
                    (rights = new MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6104
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6105
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6106
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6107
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6108
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6109
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6110
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6111
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6112
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6113
                    MapReduceMappingsToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6114
                        t = (MapReduceMappingsToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6115
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6116
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6117
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6118
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6119
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6120
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6121
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6122
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6123
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6124
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6125
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6126
    static final class MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6127
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6128
        final ToIntFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6129
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6130
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6131
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6132
        MapReduceKeysToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6133
        MapReduceKeysToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6134
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6135
             MapReduceKeysToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6136
             ToIntFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6137
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6138
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6139
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6140
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6141
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6142
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6143
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6144
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6145
            final ToIntFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6146
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6147
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6148
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6149
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6150
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6151
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6152
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6153
                    (rights = new MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6154
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6155
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6156
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6157
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6158
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6159
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6160
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6161
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6162
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6163
                    MapReduceKeysToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6164
                        t = (MapReduceKeysToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6165
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6166
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6167
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6168
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6169
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6170
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6171
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6172
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6173
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6174
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6175
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6176
    static final class MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6177
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6178
        final ToIntFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6179
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6180
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6181
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6182
        MapReduceValuesToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6183
        MapReduceValuesToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6184
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6185
             MapReduceValuesToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6186
             ToIntFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6187
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6188
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6189
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6190
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6191
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6192
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6193
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6194
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6195
            final ToIntFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6196
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6197
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6198
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6199
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6200
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6201
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6202
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6203
                    (rights = new MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6204
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6205
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6206
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6207
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6208
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6209
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6210
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6211
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6212
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6213
                    MapReduceValuesToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6214
                        t = (MapReduceValuesToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6215
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6216
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6217
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6218
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6219
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6220
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6221
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6222
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6223
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6224
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6225
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6226
    static final class MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6227
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6228
        final ToIntFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6229
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6230
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6231
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6232
        MapReduceEntriesToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6233
        MapReduceEntriesToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6234
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6235
             MapReduceEntriesToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6236
             ToIntFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6237
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6238
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6239
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6240
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6241
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6242
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6243
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6244
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6245
            final ToIntFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6246
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6247
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6248
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6249
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6250
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6251
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6252
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6253
                    (rights = new MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6254
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6255
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6256
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6257
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6258
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6259
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6260
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6261
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6262
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6263
                    MapReduceEntriesToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6264
                        t = (MapReduceEntriesToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6265
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6266
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6267
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6268
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6269
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6270
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6271
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6272
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6273
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6274
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6275
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6276
    static final class MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6277
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6278
        final ToIntBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6279
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6280
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6281
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6282
        MapReduceMappingsToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6283
        MapReduceMappingsToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6284
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6285
             MapReduceMappingsToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6286
             ToIntBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6287
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6288
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6289
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6290
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6291
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6292
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6293
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6294
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6295
            final ToIntBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6296
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6297
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6298
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6299
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6300
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6301
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6302
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6303
                    (rights = new MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6304
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6305
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6306
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6307
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6308
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6309
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6310
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6311
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6312
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6313
                    MapReduceMappingsToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6314
                        t = (MapReduceMappingsToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6315
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6316
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6317
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6318
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6319
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6320
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6321
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6323
    }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6324
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6325
    // Unsafe mechanics
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6326
    private static final sun.misc.Unsafe U;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6327
    private static final long SIZECTL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6328
    private static final long TRANSFERINDEX;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6329
    private static final long BASECOUNT;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6330
    private static final long CELLSBUSY;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6331
    private static final long CELLVALUE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6332
    private static final long ABASE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6333
    private static final int ASHIFT;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6334
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6335
    static {
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6336
        try {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6337
            U = sun.misc.Unsafe.getUnsafe();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6338
            Class<?> k = ConcurrentHashMap.class;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6339
            SIZECTL = U.objectFieldOffset
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6340
                (k.getDeclaredField("sizeCtl"));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6341
            TRANSFERINDEX = U.objectFieldOffset
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6342
                (k.getDeclaredField("transferIndex"));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6343
            BASECOUNT = U.objectFieldOffset
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6344
                (k.getDeclaredField("baseCount"));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6345
            CELLSBUSY = U.objectFieldOffset
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6346
                (k.getDeclaredField("cellsBusy"));
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6347
            Class<?> ck = CounterCell.class;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6348
            CELLVALUE = U.objectFieldOffset
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6349
                (ck.getDeclaredField("value"));
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6350
            Class<?> ak = Node[].class;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6351
            ABASE = U.arrayBaseOffset(ak);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6352
            int scale = U.arrayIndexScale(ak);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6353
            if ((scale & (scale - 1)) != 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6354
                throw new Error("data type scale not a power of two");
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6355
            ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6356
        } catch (Exception e) {
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6357
            throw new Error(e);
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6358
        }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6359
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6360
}