ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java (file contents):
Revision 1.83 by jsr166, Mon Aug 22 03:42:10 2005 UTC vs.
Revision 1.142 by dl, Wed Oct 31 12:49:18 2012 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   package java.util.concurrent;
8 < import java.util.concurrent.locks.*;
9 < import java.util.*;
8 > import java.util.concurrent.atomic.LongAdder;
9 > import java.util.concurrent.ForkJoinPool;
10 > import java.util.concurrent.ForkJoinTask;
11 >
12 > import java.util.Comparator;
13 > import java.util.Arrays;
14 > import java.util.Map;
15 > import java.util.Set;
16 > import java.util.Collection;
17 > import java.util.AbstractMap;
18 > import java.util.AbstractSet;
19 > import java.util.AbstractCollection;
20 > import java.util.Hashtable;
21 > import java.util.HashMap;
22 > import java.util.Iterator;
23 > import java.util.Enumeration;
24 > import java.util.ConcurrentModificationException;
25 > import java.util.NoSuchElementException;
26 > import java.util.concurrent.ConcurrentMap;
27 > import java.util.concurrent.ThreadLocalRandom;
28 > import java.util.concurrent.locks.LockSupport;
29 > import java.util.concurrent.locks.AbstractQueuedSynchronizer;
30 > import java.util.concurrent.atomic.AtomicReference;
31 >
32   import java.io.Serializable;
11 import java.io.IOException;
12 import java.io.ObjectInputStream;
13 import java.io.ObjectOutputStream;
33  
34   /**
35   * A hash table supporting full concurrency of retrievals and
36 < * adjustable expected concurrency for updates. This class obeys the
36 > * high expected concurrency for updates. This class obeys the
37   * same functional specification as {@link java.util.Hashtable}, and
38   * includes versions of methods corresponding to each method of
39 < * <tt>Hashtable</tt>. However, even though all operations are
39 > * {@code Hashtable}. However, even though all operations are
40   * thread-safe, retrieval operations do <em>not</em> entail locking,
41   * and there is <em>not</em> any support for locking the entire table
42   * in a way that prevents all access.  This class is fully
43 < * interoperable with <tt>Hashtable</tt> in programs that rely on its
43 > * interoperable with {@code Hashtable} in programs that rely on its
44   * thread safety but not on its synchronization details.
45   *
46 < * <p> Retrieval operations (including <tt>get</tt>) generally do not
47 < * block, so may overlap with update operations (including
48 < * <tt>put</tt> and <tt>remove</tt>). Retrievals reflect the results
49 < * of the most recently <em>completed</em> update operations holding
50 < * upon their onset.  For aggregate operations such as <tt>putAll</tt>
51 < * and <tt>clear</tt>, concurrent retrievals may reflect insertion or
52 < * removal of only some entries.  Similarly, Iterators and
53 < * Enumerations return elements reflecting the state of the hash table
54 < * at some point at or since the creation of the iterator/enumeration.
55 < * They do <em>not</em> throw {@link ConcurrentModificationException}.
56 < * However, iterators are designed to be used by only one thread at a time.
57 < *
58 < * <p> The allowed concurrency among update operations is guided by
59 < * the optional <tt>concurrencyLevel</tt> constructor argument
60 < * (default <tt>16</tt>), which is used as a hint for internal sizing.  The
61 < * table is internally partitioned to try to permit the indicated
62 < * number of concurrent updates without contention. Because placement
63 < * in hash tables is essentially random, the actual concurrency will
64 < * vary.  Ideally, you should choose a value to accommodate as many
65 < * threads as will ever concurrently modify the table. Using a
66 < * significantly higher value than you need can waste space and time,
67 < * and a significantly lower value can lead to thread contention. But
68 < * overestimates and underestimates within an order of magnitude do
69 < * not usually have much noticeable impact. A value of one is
70 < * appropriate when it is known that only one thread will modify and
71 < * all others will only read. Also, resizing this or any other kind of
72 < * hash table is a relatively slow operation, so, when possible, it is
73 < * a good idea to provide estimates of expected table sizes in
74 < * constructors.
46 > * <p> Retrieval operations (including {@code get}) generally do not
47 > * block, so may overlap with update operations (including {@code put}
48 > * and {@code remove}). Retrievals reflect the results of the most
49 > * recently <em>completed</em> update operations holding upon their
50 > * onset. (More formally, an update operation for a given key bears a
51 > * <em>happens-before</em> relation with any (non-null) retrieval for
52 > * that key reporting the updated value.)  For aggregate operations
53 > * such as {@code putAll} and {@code clear}, concurrent retrievals may
54 > * reflect insertion or removal of only some entries.  Similarly,
55 > * Iterators and Enumerations return elements reflecting the state of
56 > * the hash table at some point at or since the creation of the
57 > * iterator/enumeration.  They do <em>not</em> throw {@link
58 > * ConcurrentModificationException}.  However, iterators are designed
59 > * to be used by only one thread at a time.  Bear in mind that the
60 > * results of aggregate status methods including {@code size}, {@code
61 > * isEmpty}, and {@code containsValue} are typically useful only when
62 > * a map is not undergoing concurrent updates in other threads.
63 > * Otherwise the results of these methods reflect transient states
64 > * that may be adequate for monitoring or estimation purposes, but not
65 > * for program control.
66 > *
67 > * <p> The table is dynamically expanded when there are too many
68 > * collisions (i.e., keys that have distinct hash codes but fall into
69 > * the same slot modulo the table size), with the expected average
70 > * effect of maintaining roughly two bins per mapping (corresponding
71 > * to a 0.75 load factor threshold for resizing). There may be much
72 > * variance around this average as mappings are added and removed, but
73 > * overall, this maintains a commonly accepted time/space tradeoff for
74 > * hash tables.  However, resizing this or any other kind of hash
75 > * table may be a relatively slow operation. When possible, it is a
76 > * good idea to provide a size estimate as an optional {@code
77 > * initialCapacity} constructor argument. An additional optional
78 > * {@code loadFactor} constructor argument provides a further means of
79 > * customizing initial table capacity by specifying the table density
80 > * to be used in calculating the amount of space to allocate for the
81 > * given number of elements.  Also, for compatibility with previous
82 > * versions of this class, constructors may optionally specify an
83 > * expected {@code concurrencyLevel} as an additional hint for
84 > * internal sizing.  Note that using many keys with exactly the same
85 > * {@code hashCode()} is a sure way to slow down performance of any
86 > * hash table.
87 > *
88 > * <p> A {@link Set} projection of a ConcurrentHashMap may be created
89 > * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
90 > * (using {@link #keySet(Object)} when only keys are of interest, and the
91 > * mapped values are (perhaps transiently) not used or all take the
92 > * same mapping value.
93 > *
94 > * <p> A ConcurrentHashMap can be used as scalable frequency map (a
95 > * form of histogram or multiset) by using {@link LongAdder} values
96 > * and initializing via {@link #computeIfAbsent}. For example, to add
97 > * a count to a {@code ConcurrentHashMap<String,LongAdder> freqs}, you
98 > * can use {@code freqs.computeIfAbsent(k -> new
99 > * LongAdder()).increment();}
100   *
101   * <p>This class and its views and iterators implement all of the
102   * <em>optional</em> methods of the {@link Map} and {@link Iterator}
103   * interfaces.
104   *
105   * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
106 < * does <em>not</em> allow <tt>null</tt> to be used as a key or value.
106 > * does <em>not</em> allow {@code null} to be used as a key or value.
107 > *
108 > * <p>ConcurrentHashMaps support parallel operations using the {@link
109 > * ForkJoinPool#commonPool}. (Tasks that may be used in other contexts
110 > * are available in class {@link ForkJoinTasks}). These operations are
111 > * designed to be safely, and often sensibly, applied even with maps
112 > * that are being concurrently updated by other threads; for example,
113 > * when computing a snapshot summary of the values in a shared
114 > * registry.  There are three kinds of operation, each with four
115 > * forms, accepting functions with Keys, Values, Entries, and (Key,
116 > * Value) arguments and/or return values. (The first three forms are
117 > * also available via the {@link #keySet()}, {@link #values()} and
118 > * {@link #entrySet()} views). Because the elements of a
119 > * ConcurrentHashMap are not ordered in any particular way, and may be
120 > * processed in different orders in different parallel executions, the
121 > * correctness of supplied functions should not depend on any
122 > * ordering, or on any other objects or values that may transiently
123 > * change while computation is in progress; and except for forEach
124 > * actions, should ideally be side-effect-free.
125 > *
126 > * <ul>
127 > * <li> forEach: Perform a given action on each element.
128 > * A variant form applies a given transformation on each element
129 > * before performing the action.</li>
130 > *
131 > * <li> search: Return the first available non-null result of
132 > * applying a given function on each element; skipping further
133 > * search when a result is found.</li>
134 > *
135 > * <li> reduce: Accumulate each element.  The supplied reduction
136 > * function cannot rely on ordering (more formally, it should be
137 > * both associative and commutative).  There are five variants:
138 > *
139 > * <ul>
140 > *
141 > * <li> Plain reductions. (There is not a form of this method for
142 > * (key, value) function arguments since there is no corresponding
143 > * return type.)</li>
144 > *
145 > * <li> Mapped reductions that accumulate the results of a given
146 > * function applied to each element.</li>
147 > *
148 > * <li> Reductions to scalar doubles, longs, and ints, using a
149 > * given basis value.</li>
150 > *
151 > * </li>
152 > * </ul>
153 > * </ul>
154 > *
155 > * <p>The concurrency properties of bulk operations follow
156 > * from those of ConcurrentHashMap: Any non-null result returned
157 > * from {@code get(key)} and related access methods bears a
158 > * happens-before relation with the associated insertion or
159 > * update.  The result of any bulk operation reflects the
160 > * composition of these per-element relations (but is not
161 > * necessarily atomic with respect to the map as a whole unless it
162 > * is somehow known to be quiescent).  Conversely, because keys
163 > * and values in the map are never null, null serves as a reliable
164 > * atomic indicator of the current lack of any result.  To
165 > * maintain this property, null serves as an implicit basis for
166 > * all non-scalar reduction operations. For the double, long, and
167 > * int versions, the basis should be one that, when combined with
168 > * any other value, returns that other value (more formally, it
169 > * should be the identity element for the reduction). Most common
170 > * reductions have these properties; for example, computing a sum
171 > * with basis 0 or a minimum with basis MAX_VALUE.
172 > *
173 > * <p>Search and transformation functions provided as arguments
174 > * should similarly return null to indicate the lack of any result
175 > * (in which case it is not used). In the case of mapped
176 > * reductions, this also enables transformations to serve as
177 > * filters, returning null (or, in the case of primitive
178 > * specializations, the identity basis) if the element should not
179 > * be combined. You can create compound transformations and
180 > * filterings by composing them yourself under this "null means
181 > * there is nothing there now" rule before using them in search or
182 > * reduce operations.
183 > *
184 > * <p>Methods accepting and/or returning Entry arguments maintain
185 > * key-value associations. They may be useful for example when
186 > * finding the key for the greatest value. Note that "plain" Entry
187 > * arguments can be supplied using {@code new
188 > * AbstractMap.SimpleEntry(k,v)}.
189 > *
190 > * <p> Bulk operations may complete abruptly, throwing an
191 > * exception encountered in the application of a supplied
192 > * function. Bear in mind when handling such exceptions that other
193 > * concurrently executing functions could also have thrown
194 > * exceptions, or would have done so if the first exception had
195 > * not occurred.
196 > *
197 > * <p>Parallel speedups for bulk operations compared to sequential
198 > * processing are common but not guaranteed.  Operations involving
199 > * brief functions on small maps may execute more slowly than
200 > * sequential loops if the underlying work to parallelize the
201 > * computation is more expensive than the computation itself.
202 > * Similarly, parallelization may not lead to much actual parallelism
203 > * if all processors are busy performing unrelated tasks.
204 > *
205 > * <p> All arguments to all task methods must be non-null.
206 > *
207 > * <p><em>jsr166e note: During transition, this class
208 > * uses nested functional interfaces with different names but the
209 > * same forms as those expected for JDK8.<em>
210   *
211   * <p>This class is a member of the
212 < * <a href="{@docRoot}/../guide/collections/index.html">
212 > * <a href="{@docRoot}/../technotes/guides/collections/index.html">
213   * Java Collections Framework</a>.
214   *
215   * @since 1.5
# Line 70 | Line 217 | import java.io.ObjectOutputStream;
217   * @param <K> the type of keys maintained by this map
218   * @param <V> the type of mapped values
219   */
220 < public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
221 <        implements ConcurrentMap<K, V>, Serializable {
220 > public class ConcurrentHashMap<K, V>
221 >    implements ConcurrentMap<K, V>, Serializable {
222      private static final long serialVersionUID = 7249069246763182397L;
223  
224 +    /**
225 +     * A partitionable iterator. A Spliterator can be traversed
226 +     * directly, but can also be partitioned (before traversal) by
227 +     * creating another Spliterator that covers a non-overlapping
228 +     * portion of the elements, and so may be amenable to parallel
229 +     * execution.
230 +     *
231 +     * <p> This interface exports a subset of expected JDK8
232 +     * functionality.
233 +     *
234 +     * <p>Sample usage: Here is one (of the several) ways to compute
235 +     * the sum of the values held in a map using the ForkJoin
236 +     * framework. As illustrated here, Spliterators are well suited to
237 +     * designs in which a task repeatedly splits off half its work
238 +     * into forked subtasks until small enough to process directly,
239 +     * and then joins these subtasks. Variants of this style can also
240 +     * be used in completion-based designs.
241 +     *
242 +     * <pre>
243 +     * {@code ConcurrentHashMap<String, Long> m = ...
244 +     * // split as if have 8 * parallelism, for load balance
245 +     * int n = m.size();
246 +     * int p = aForkJoinPool.getParallelism() * 8;
247 +     * int split = (n < p)? n : p;
248 +     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
249 +     * // ...
250 +     * static class SumValues extends RecursiveTask<Long> {
251 +     *   final Spliterator<Long> s;
252 +     *   final int split;             // split while > 1
253 +     *   final SumValues nextJoin;    // records forked subtasks to join
254 +     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
255 +     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
256 +     *   }
257 +     *   public Long compute() {
258 +     *     long sum = 0;
259 +     *     SumValues subtasks = null; // fork subtasks
260 +     *     for (int s = split >>> 1; s > 0; s >>>= 1)
261 +     *       (subtasks = new SumValues(s.split(), s, subtasks)).fork();
262 +     *     while (s.hasNext())        // directly process remaining elements
263 +     *       sum += s.next();
264 +     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
265 +     *       sum += t.join();         // collect subtask results
266 +     *     return sum;
267 +     *   }
268 +     * }
269 +     * }</pre>
270 +     */
271 +    public static interface Spliterator<T> extends Iterator<T> {
272 +        /**
273 +         * Returns a Spliterator covering approximately half of the
274 +         * elements, guaranteed not to overlap with those subsequently
275 +         * returned by this Spliterator.  After invoking this method,
276 +         * the current Spliterator will <em>not</em> produce any of
277 +         * the elements of the returned Spliterator, but the two
278 +         * Spliterators together will produce all of the elements that
279 +         * would have been produced by this Spliterator had this
280 +         * method not been called. The exact number of elements
281 +         * produced by the returned Spliterator is not guaranteed, and
282 +         * may be zero (i.e., with {@code hasNext()} reporting {@code
283 +         * false}) if this Spliterator cannot be further split.
284 +         *
285 +         * @return a Spliterator covering approximately half of the
286 +         * elements
287 +         * @throws IllegalStateException if this Spliterator has
288 +         * already commenced traversing elements
289 +         */
290 +        Spliterator<T> split();
291 +    }
292 +
293 +
294      /*
295 <     * The basic strategy is to subdivide the table among Segments,
296 <     * each of which itself is a concurrently readable hash table.
295 >     * Overview:
296 >     *
297 >     * The primary design goal of this hash table is to maintain
298 >     * concurrent readability (typically method get(), but also
299 >     * iterators and related methods) while minimizing update
300 >     * contention. Secondary goals are to keep space consumption about
301 >     * the same or better than java.util.HashMap, and to support high
302 >     * initial insertion rates on an empty table by many threads.
303 >     *
304 >     * Each key-value mapping is held in a Node.  Because Node fields
305 >     * can contain special values, they are defined using plain Object
306 >     * types. Similarly in turn, all internal methods that use them
307 >     * work off Object types. And similarly, so do the internal
308 >     * methods of auxiliary iterator and view classes.  All public
309 >     * generic typed methods relay in/out of these internal methods,
310 >     * supplying null-checks and casts as needed. This also allows
311 >     * many of the public methods to be factored into a smaller number
312 >     * of internal methods (although sadly not so for the five
313 >     * variants of put-related operations). The validation-based
314 >     * approach explained below leads to a lot of code sprawl because
315 >     * retry-control precludes factoring into smaller methods.
316 >     *
317 >     * The table is lazily initialized to a power-of-two size upon the
318 >     * first insertion.  Each bin in the table normally contains a
319 >     * list of Nodes (most often, the list has only zero or one Node).
320 >     * Table accesses require volatile/atomic reads, writes, and
321 >     * CASes.  Because there is no other way to arrange this without
322 >     * adding further indirections, we use intrinsics
323 >     * (sun.misc.Unsafe) operations.  The lists of nodes within bins
324 >     * are always accurately traversable under volatile reads, so long
325 >     * as lookups check hash code and non-nullness of value before
326 >     * checking key equality.
327 >     *
328 >     * We use the top two bits of Node hash fields for control
329 >     * purposes -- they are available anyway because of addressing
330 >     * constraints.  As explained further below, these top bits are
331 >     * used as follows:
332 >     *  00 - Normal
333 >     *  01 - Locked
334 >     *  11 - Locked and may have a thread waiting for lock
335 >     *  10 - Node is a forwarding node
336 >     *
337 >     * The lower 30 bits of each Node's hash field contain a
338 >     * transformation of the key's hash code, except for forwarding
339 >     * nodes, for which the lower bits are zero (and so always have
340 >     * hash field == MOVED).
341 >     *
342 >     * Insertion (via put or its variants) of the first node in an
343 >     * empty bin is performed by just CASing it to the bin.  This is
344 >     * by far the most common case for put operations under most
345 >     * key/hash distributions.  Other update operations (insert,
346 >     * delete, and replace) require locks.  We do not want to waste
347 >     * the space required to associate a distinct lock object with
348 >     * each bin, so instead use the first node of a bin list itself as
349 >     * a lock. Blocking support for these locks relies on the builtin
350 >     * "synchronized" monitors.  However, we also need a tryLock
351 >     * construction, so we overlay these by using bits of the Node
352 >     * hash field for lock control (see above), and so normally use
353 >     * builtin monitors only for blocking and signalling using
354 >     * wait/notifyAll constructions. See Node.tryAwaitLock.
355 >     *
356 >     * Using the first node of a list as a lock does not by itself
357 >     * suffice though: When a node is locked, any update must first
358 >     * validate that it is still the first node after locking it, and
359 >     * retry if not. Because new nodes are always appended to lists,
360 >     * once a node is first in a bin, it remains first until deleted
361 >     * or the bin becomes invalidated (upon resizing).  However,
362 >     * operations that only conditionally update may inspect nodes
363 >     * until the point of update. This is a converse of sorts to the
364 >     * lazy locking technique described by Herlihy & Shavit.
365 >     *
366 >     * The main disadvantage of per-bin locks is that other update
367 >     * operations on other nodes in a bin list protected by the same
368 >     * lock can stall, for example when user equals() or mapping
369 >     * functions take a long time.  However, statistically, under
370 >     * random hash codes, this is not a common problem.  Ideally, the
371 >     * frequency of nodes in bins follows a Poisson distribution
372 >     * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
373 >     * parameter of about 0.5 on average, given the resizing threshold
374 >     * of 0.75, although with a large variance because of resizing
375 >     * granularity. Ignoring variance, the expected occurrences of
376 >     * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
377 >     * first values are:
378 >     *
379 >     * 0:    0.60653066
380 >     * 1:    0.30326533
381 >     * 2:    0.07581633
382 >     * 3:    0.01263606
383 >     * 4:    0.00157952
384 >     * 5:    0.00015795
385 >     * 6:    0.00001316
386 >     * 7:    0.00000094
387 >     * 8:    0.00000006
388 >     * more: less than 1 in ten million
389 >     *
390 >     * Lock contention probability for two threads accessing distinct
391 >     * elements is roughly 1 / (8 * #elements) under random hashes.
392 >     *
393 >     * Actual hash code distributions encountered in practice
394 >     * sometimes deviate significantly from uniform randomness.  This
395 >     * includes the case when N > (1<<30), so some keys MUST collide.
396 >     * Similarly for dumb or hostile usages in which multiple keys are
397 >     * designed to have identical hash codes. Also, although we guard
398 >     * against the worst effects of this (see method spread), sets of
399 >     * hashes may differ only in bits that do not impact their bin
400 >     * index for a given power-of-two mask.  So we use a secondary
401 >     * strategy that applies when the number of nodes in a bin exceeds
402 >     * a threshold, and at least one of the keys implements
403 >     * Comparable.  These TreeBins use a balanced tree to hold nodes
404 >     * (a specialized form of red-black trees), bounding search time
405 >     * to O(log N).  Each search step in a TreeBin is around twice as
406 >     * slow as in a regular list, but given that N cannot exceed
407 >     * (1<<64) (before running out of addresses) this bounds search
408 >     * steps, lock hold times, etc, to reasonable constants (roughly
409 >     * 100 nodes inspected per operation worst case) so long as keys
410 >     * are Comparable (which is very common -- String, Long, etc).
411 >     * TreeBin nodes (TreeNodes) also maintain the same "next"
412 >     * traversal pointers as regular nodes, so can be traversed in
413 >     * iterators in the same way.
414 >     *
415 >     * The table is resized when occupancy exceeds a percentage
416 >     * threshold (nominally, 0.75, but see below).  Only a single
417 >     * thread performs the resize (using field "sizeCtl", to arrange
418 >     * exclusion), but the table otherwise remains usable for reads
419 >     * and updates. Resizing proceeds by transferring bins, one by
420 >     * one, from the table to the next table.  Because we are using
421 >     * power-of-two expansion, the elements from each bin must either
422 >     * stay at same index, or move with a power of two offset. We
423 >     * eliminate unnecessary node creation by catching cases where old
424 >     * nodes can be reused because their next fields won't change.  On
425 >     * average, only about one-sixth of them need cloning when a table
426 >     * doubles. The nodes they replace will be garbage collectable as
427 >     * soon as they are no longer referenced by any reader thread that
428 >     * may be in the midst of concurrently traversing table.  Upon
429 >     * transfer, the old table bin contains only a special forwarding
430 >     * node (with hash field "MOVED") that contains the next table as
431 >     * its key. On encountering a forwarding node, access and update
432 >     * operations restart, using the new table.
433 >     *
434 >     * Each bin transfer requires its bin lock. However, unlike other
435 >     * cases, a transfer can skip a bin if it fails to acquire its
436 >     * lock, and revisit it later (unless it is a TreeBin). Method
437 >     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
438 >     * have been skipped because of failure to acquire a lock, and
439 >     * blocks only if none are available (i.e., only very rarely).
440 >     * The transfer operation must also ensure that all accessible
441 >     * bins in both the old and new table are usable by any traversal.
442 >     * When there are no lock acquisition failures, this is arranged
443 >     * simply by proceeding from the last bin (table.length - 1) up
444 >     * towards the first.  Upon seeing a forwarding node, traversals
445 >     * (see class Iter) arrange to move to the new table
446 >     * without revisiting nodes.  However, when any node is skipped
447 >     * during a transfer, all earlier table bins may have become
448 >     * visible, so are initialized with a reverse-forwarding node back
449 >     * to the old table until the new ones are established. (This
450 >     * sometimes requires transiently locking a forwarding node, which
451 >     * is possible under the above encoding.) These more expensive
452 >     * mechanics trigger only when necessary.
453 >     *
454 >     * The traversal scheme also applies to partial traversals of
455 >     * ranges of bins (via an alternate Traverser constructor)
456 >     * to support partitioned aggregate operations.  Also, read-only
457 >     * operations give up if ever forwarded to a null table, which
458 >     * provides support for shutdown-style clearing, which is also not
459 >     * currently implemented.
460 >     *
461 >     * Lazy table initialization minimizes footprint until first use,
462 >     * and also avoids resizings when the first operation is from a
463 >     * putAll, constructor with map argument, or deserialization.
464 >     * These cases attempt to override the initial capacity settings,
465 >     * but harmlessly fail to take effect in cases of races.
466 >     *
467 >     * The element count is maintained using a LongAdder, which avoids
468 >     * contention on updates but can encounter cache thrashing if read
469 >     * too frequently during concurrent access. To avoid reading so
470 >     * often, resizing is attempted either when a bin lock is
471 >     * contended, or upon adding to a bin already holding two or more
472 >     * nodes (checked before adding in the xIfAbsent methods, after
473 >     * adding in others). Under uniform hash distributions, the
474 >     * probability of this occurring at threshold is around 13%,
475 >     * meaning that only about 1 in 8 puts check threshold (and after
476 >     * resizing, many fewer do so). But this approximation has high
477 >     * variance for small table sizes, so we check on any collision
478 >     * for sizes <= 64. The bulk putAll operation further reduces
479 >     * contention by only committing count updates upon these size
480 >     * checks.
481 >     *
482 >     * Maintaining API and serialization compatibility with previous
483 >     * versions of this class introduces several oddities. Mainly: We
484 >     * leave untouched but unused constructor arguments refering to
485 >     * concurrencyLevel. We accept a loadFactor constructor argument,
486 >     * but apply it only to initial table capacity (which is the only
487 >     * time that we can guarantee to honor it.) We also declare an
488 >     * unused "Segment" class that is instantiated in minimal form
489 >     * only when serializing.
490       */
491  
492      /* ---------------- Constants -------------- */
493  
494      /**
495 <     * The default initial capacity for this table,
496 <     * used when not otherwise specified in a constructor.
495 >     * The largest possible table capacity.  This value must be
496 >     * exactly 1<<30 to stay within Java array allocation and indexing
497 >     * bounds for power of two table sizes, and is further required
498 >     * because the top two bits of 32bit hash fields are used for
499 >     * control purposes.
500 >     */
501 >    private static final int MAXIMUM_CAPACITY = 1 << 30;
502 >
503 >    /**
504 >     * The default initial table capacity.  Must be a power of 2
505 >     * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
506       */
507 <    static final int DEFAULT_INITIAL_CAPACITY = 16;
507 >    private static final int DEFAULT_CAPACITY = 16;
508  
509      /**
510 <     * The default load factor for this table, used when not
511 <     * otherwise specified in a constructor.
510 >     * The largest possible (non-power of two) array size.
511 >     * Needed by toArray and related methods.
512       */
513 <    static final float DEFAULT_LOAD_FACTOR = 0.75f;
513 >    static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
514  
515      /**
516 <     * The default concurrency level for this table, used when not
517 <     * otherwise specified in a constructor.
516 >     * The default concurrency level for this table. Unused but
517 >     * defined for compatibility with previous versions of this class.
518       */
519 <    static final int DEFAULT_CONCURRENCY_LEVEL = 16;
519 >    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
520  
521      /**
522 <     * The maximum capacity, used if a higher value is implicitly
523 <     * specified by either of the constructors with arguments.  MUST
524 <     * be a power of two <= 1<<30 to ensure that entries are indexable
525 <     * using ints.
522 >     * The load factor for this table. Overrides of this value in
523 >     * constructors affect only the initial table capacity.  The
524 >     * actual floating point value isn't normally used -- it is
525 >     * simpler to use expressions such as {@code n - (n >>> 2)} for
526 >     * the associated resizing threshold.
527       */
528 <    static final int MAXIMUM_CAPACITY = 1 << 30;
528 >    private static final float LOAD_FACTOR = 0.75f;
529  
530      /**
531 <     * The maximum number of segments to allow; used to bound
532 <     * constructor arguments.
531 >     * The buffer size for skipped bins during transfers. The
532 >     * value is arbitrary but should be large enough to avoid
533 >     * most locking stalls during resizes.
534       */
535 <    static final int MAX_SEGMENTS = 1 << 16; // slightly conservative
535 >    private static final int TRANSFER_BUFFER_SIZE = 32;
536  
537      /**
538 <     * Number of unsynchronized retries in size and containsValue
539 <     * methods before resorting to locking. This is used to avoid
540 <     * unbounded retries if tables undergo continuous modification
541 <     * which would make it impossible to obtain an accurate result.
538 >     * The bin count threshold for using a tree rather than list for a
539 >     * bin.  The value reflects the approximate break-even point for
540 >     * using tree-based operations.
541 >     */
542 >    private static final int TREE_THRESHOLD = 8;
543 >
544 >    /*
545 >     * Encodings for special uses of Node hash fields. See above for
546 >     * explanation.
547       */
548 <    static final int RETRIES_BEFORE_LOCK = 2;
548 >    static final int MOVED     = 0x80000000; // hash field for forwarding nodes
549 >    static final int LOCKED    = 0x40000000; // set/tested only as a bit
550 >    static final int WAITING   = 0xc0000000; // both bits set/tested together
551 >    static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
552  
553      /* ---------------- Fields -------------- */
554  
555      /**
556 <     * Mask value for indexing into segments. The upper bits of a
557 <     * key's hash code are used to choose the segment.
556 >     * The array of bins. Lazily initialized upon first insertion.
557 >     * Size is always a power of two. Accessed directly by iterators.
558       */
559 <    final int segmentMask;
559 >    transient volatile Node[] table;
560  
561      /**
562 <     * Shift value for indexing within segments.
562 >     * The counter maintaining number of elements.
563       */
564 <    final int segmentShift;
564 >    private transient final LongAdder counter;
565  
566      /**
567 <     * The segments, each of which is a specialized hash table
568 <     */
569 <    final Segment<K,V>[] segments;
567 >     * Table initialization and resizing control.  When negative, the
568 >     * table is being initialized or resized. Otherwise, when table is
569 >     * null, holds the initial table size to use upon creation, or 0
570 >     * for default. After initialization, holds the next element count
571 >     * value upon which to resize the table.
572 >     */
573 >    private transient volatile int sizeCtl;
574 >
575 >    // views
576 >    private transient KeySetView<K,V> keySet;
577 >    private transient ValuesView<K,V> values;
578 >    private transient EntrySetView<K,V> entrySet;
579  
580 <    transient Set<K> keySet;
581 <    transient Set<Map.Entry<K,V>> entrySet;
144 <    transient Collection<V> values;
580 >    /** For serialization compatibility. Null unless serialized; see below */
581 >    private Segment<K,V>[] segments;
582  
583 <    /* ---------------- Small Utilities -------------- */
583 >    /* ---------------- Table element access -------------- */
584  
585 <    /**
586 <     * Returns a hash code for non-null Object x.
587 <     * Uses the same hash code spreader as most other java.util hash tables.
588 <     * @param x the object serving as a key
589 <     * @return the hash code
585 >    /*
586 >     * Volatile access methods are used for table elements as well as
587 >     * elements of in-progress next table while resizing.  Uses are
588 >     * null checked by callers, and implicitly bounds-checked, relying
589 >     * on the invariants that tab arrays have non-zero size, and all
590 >     * indices are masked with (tab.length - 1) which is never
591 >     * negative and always less than length. Note that, to be correct
592 >     * wrt arbitrary concurrency errors by users, bounds checks must
593 >     * operate on local variables, which accounts for some odd-looking
594 >     * inline assignments below.
595       */
596 <    static int hash(Object x) {
597 <        int h = x.hashCode();
598 <        h += ~(h << 9);
157 <        h ^=  (h >>> 14);
158 <        h +=  (h << 4);
159 <        h ^=  (h >>> 10);
160 <        return h;
596 >
597 >    static final Node tabAt(Node[] tab, int i) { // used by Iter
598 >        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
599      }
600  
601 <    /**
602 <     * Returns the segment that should be used for key with given hash
603 <     * @param hash the hash code for the key
604 <     * @return the segment
605 <     */
606 <    final Segment<K,V> segmentFor(int hash) {
169 <        return segments[(hash >>> segmentShift) & segmentMask];
601 >    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
602 >        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
603 >    }
604 >
605 >    private static final void setTabAt(Node[] tab, int i, Node v) {
606 >        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
607      }
608  
609 <    /* ---------------- Inner Classes -------------- */
609 >    /* ---------------- Nodes -------------- */
610  
611      /**
612 <     * ConcurrentHashMap list entry. Note that this is never exported
613 <     * out as a user-visible Map.Entry.
614 <     *
615 <     * Because the value field is volatile, not final, it is legal wrt
616 <     * the Java Memory Model for an unsynchronized reader to see null
617 <     * instead of initial value when read via a data race.  Although a
618 <     * reordering leading to this is not likely to ever actually
619 <     * occur, the Segment.readValueUnderLock method is used as a
620 <     * backup in case a null (pre-initialized) value is ever seen in
621 <     * an unsynchronized access method.
622 <     */
623 <    static final class HashEntry<K,V> {
624 <        final K key;
625 <        final int hash;
189 <        volatile V value;
190 <        final HashEntry<K,V> next;
612 >     * Key-value entry. Note that this is never exported out as a
613 >     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
614 >     * field of MOVED are special, and do not contain user keys or
615 >     * values.  Otherwise, keys are never null, and null val fields
616 >     * indicate that a node is in the process of being deleted or
617 >     * created. For purposes of read-only access, a key may be read
618 >     * before a val, but can only be used after checking val to be
619 >     * non-null.
620 >     */
621 >    static class Node {
622 >        volatile int hash;
623 >        final Object key;
624 >        volatile Object val;
625 >        volatile Node next;
626  
627 <        HashEntry(K key, int hash, HashEntry<K,V> next, V value) {
193 <            this.key = key;
627 >        Node(int hash, Object key, Object val, Node next) {
628              this.hash = hash;
629 +            this.key = key;
630 +            this.val = val;
631              this.next = next;
196            this.value = value;
632          }
633  
634 <        @SuppressWarnings("unchecked")
635 <        static final <K,V> HashEntry<K,V>[] newArray(int i) {
636 <            return new HashEntry[i];
637 <        }
203 <    }
204 <
205 <    /**
206 <     * Segments are specialized versions of hash tables.  This
207 <     * subclasses from ReentrantLock opportunistically, just to
208 <     * simplify some locking and avoid separate construction.
209 <     */
210 <    static final class Segment<K,V> extends ReentrantLock implements Serializable {
211 <        /*
212 <         * Segments maintain a table of entry lists that are ALWAYS
213 <         * kept in a consistent state, so can be read without locking.
214 <         * Next fields of nodes are immutable (final).  All list
215 <         * additions are performed at the front of each bin. This
216 <         * makes it easy to check changes, and also fast to traverse.
217 <         * When nodes would otherwise be changed, new nodes are
218 <         * created to replace them. This works well for hash tables
219 <         * since the bin lists tend to be short. (The average length
220 <         * is less than two for the default load factor threshold.)
221 <         *
222 <         * Read operations can thus proceed without locking, but rely
223 <         * on selected uses of volatiles to ensure that completed
224 <         * write operations performed by other threads are
225 <         * noticed. For most purposes, the "count" field, tracking the
226 <         * number of elements, serves as that volatile variable
227 <         * ensuring visibility.  This is convenient because this field
228 <         * needs to be read in many read operations anyway:
229 <         *
230 <         *   - All (unsynchronized) read operations must first read the
231 <         *     "count" field, and should not look at table entries if
232 <         *     it is 0.
233 <         *
234 <         *   - All (synchronized) write operations should write to
235 <         *     the "count" field after structurally changing any bin.
236 <         *     The operations must not take any action that could even
237 <         *     momentarily cause a concurrent read operation to see
238 <         *     inconsistent data. This is made easier by the nature of
239 <         *     the read operations in Map. For example, no operation
240 <         *     can reveal that the table has grown but the threshold
241 <         *     has not yet been updated, so there are no atomicity
242 <         *     requirements for this with respect to reads.
243 <         *
244 <         * As a guide, all critical volatile reads and writes to the
245 <         * count field are marked in code comments.
246 <         */
634 >        /** CompareAndSet the hash field */
635 >        final boolean casHash(int cmp, int val) {
636 >            return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
637 >        }
638  
639 <        private static final long serialVersionUID = 2249069246763182397L;
639 >        /** The number of spins before blocking for a lock */
640 >        static final int MAX_SPINS =
641 >            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
642  
643          /**
644 <         * The number of elements in this segment's region.
644 >         * Spins a while if LOCKED bit set and this node is the first
645 >         * of its bin, and then sets WAITING bits on hash field and
646 >         * blocks (once) if they are still set.  It is OK for this
647 >         * method to return even if lock is not available upon exit,
648 >         * which enables these simple single-wait mechanics.
649 >         *
650 >         * The corresponding signalling operation is performed within
651 >         * callers: Upon detecting that WAITING has been set when
652 >         * unlocking lock (via a failed CAS from non-waiting LOCKED
653 >         * state), unlockers acquire the sync lock and perform a
654 >         * notifyAll.
655 >         *
656 >         * The initial sanity check on tab and bounds is not currently
657 >         * necessary in the only usages of this method, but enables
658 >         * use in other future contexts.
659           */
660 <        transient volatile int count;
660 >        final void tryAwaitLock(Node[] tab, int i) {
661 >            if (tab != null && i >= 0 && i < tab.length) { // sanity check
662 >                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
663 >                int spins = MAX_SPINS, h;
664 >                while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
665 >                    if (spins >= 0) {
666 >                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
667 >                        if (r >= 0 && --spins == 0)
668 >                            Thread.yield();  // yield before block
669 >                    }
670 >                    else if (casHash(h, h | WAITING)) {
671 >                        synchronized (this) {
672 >                            if (tabAt(tab, i) == this &&
673 >                                (hash & WAITING) == WAITING) {
674 >                                try {
675 >                                    wait();
676 >                                } catch (InterruptedException ie) {
677 >                                    try {
678 >                                        Thread.currentThread().interrupt();
679 >                                    } catch (SecurityException ignore) {
680 >                                    }
681 >                                }
682 >                            }
683 >                            else
684 >                                notifyAll(); // possibly won race vs signaller
685 >                        }
686 >                        break;
687 >                    }
688 >                }
689 >            }
690 >        }
691  
692 <        /**
693 <         * Number of updates that alter the size of the table. This is
694 <         * used during bulk-read methods to make sure they see a
258 <         * consistent snapshot: If modCounts change during a traversal
259 <         * of segments computing size or checking containsValue, then
260 <         * we might have an inconsistent view of state so (usually)
261 <         * must retry.
262 <         */
263 <        transient int modCount;
692 >        // Unsafe mechanics for casHash
693 >        private static final sun.misc.Unsafe UNSAFE;
694 >        private static final long hashOffset;
695  
696 <        /**
697 <         * The table is rehashed when its size exceeds this threshold.
698 <         * (The value of this field is always <tt>(int)(capacity *
699 <         * loadFactor)</tt>.)
700 <         */
701 <        transient int threshold;
696 >        static {
697 >            try {
698 >                UNSAFE = sun.misc.Unsafe.getUnsafe();
699 >                Class<?> k = Node.class;
700 >                hashOffset = UNSAFE.objectFieldOffset
701 >                    (k.getDeclaredField("hash"));
702 >            } catch (Exception e) {
703 >                throw new Error(e);
704 >            }
705 >        }
706 >    }
707  
708 <        /**
273 <         * The per-segment table.
274 <         */
275 <        transient volatile HashEntry<K,V>[] table;
708 >    /* ---------------- TreeBins -------------- */
709  
710 <        /**
711 <         * The load factor for the hash table.  Even though this value
712 <         * is same for all segments, it is replicated to avoid needing
713 <         * links to outer object.
714 <         * @serial
715 <         */
716 <        final float loadFactor;
710 >    /**
711 >     * Nodes for use in TreeBins
712 >     */
713 >    static final class TreeNode extends Node {
714 >        TreeNode parent;  // red-black tree links
715 >        TreeNode left;
716 >        TreeNode right;
717 >        TreeNode prev;    // needed to unlink next upon deletion
718 >        boolean red;
719 >
720 >        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
721 >            super(hash, key, val, next);
722 >            this.parent = parent;
723 >        }
724 >    }
725  
726 <        Segment(int initialCapacity, float lf) {
727 <            loadFactor = lf;
728 <            setTable(HashEntry.<K,V>newArray(initialCapacity));
726 >    /**
727 >     * A specialized form of red-black tree for use in bins
728 >     * whose size exceeds a threshold.
729 >     *
730 >     * TreeBins use a special form of comparison for search and
731 >     * related operations (which is the main reason we cannot use
732 >     * existing collections such as TreeMaps). TreeBins contain
733 >     * Comparable elements, but may contain others, as well as
734 >     * elements that are Comparable but not necessarily Comparable<T>
735 >     * for the same T, so we cannot invoke compareTo among them. To
736 >     * handle this, the tree is ordered primarily by hash value, then
737 >     * by getClass().getName() order, and then by Comparator order
738 >     * among elements of the same class.  On lookup at a node, if
739 >     * elements are not comparable or compare as 0, both left and
740 >     * right children may need to be searched in the case of tied hash
741 >     * values. (This corresponds to the full list search that would be
742 >     * necessary if all elements were non-Comparable and had tied
743 >     * hashes.)  The red-black balancing code is updated from
744 >     * pre-jdk-collections
745 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
746 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
747 >     * Algorithms" (CLR).
748 >     *
749 >     * TreeBins also maintain a separate locking discipline than
750 >     * regular bins. Because they are forwarded via special MOVED
751 >     * nodes at bin heads (which can never change once established),
752 >     * we cannot use those nodes as locks. Instead, TreeBin
753 >     * extends AbstractQueuedSynchronizer to support a simple form of
754 >     * read-write lock. For update operations and table validation,
755 >     * the exclusive form of lock behaves in the same way as bin-head
756 >     * locks. However, lookups use shared read-lock mechanics to allow
757 >     * multiple readers in the absence of writers.  Additionally,
758 >     * these lookups do not ever block: While the lock is not
759 >     * available, they proceed along the slow traversal path (via
760 >     * next-pointers) until the lock becomes available or the list is
761 >     * exhausted, whichever comes first. (These cases are not fast,
762 >     * but maximize aggregate expected throughput.)  The AQS mechanics
763 >     * for doing this are straightforward.  The lock state is held as
764 >     * AQS getState().  Read counts are negative; the write count (1)
765 >     * is positive.  There are no signalling preferences among readers
766 >     * and writers. Since we don't need to export full Lock API, we
767 >     * just override the minimal AQS methods and use them directly.
768 >     */
769 >    static final class TreeBin extends AbstractQueuedSynchronizer {
770 >        private static final long serialVersionUID = 2249069246763182397L;
771 >        transient TreeNode root;  // root of tree
772 >        transient TreeNode first; // head of next-pointer list
773 >
774 >        /* AQS overrides */
775 >        public final boolean isHeldExclusively() { return getState() > 0; }
776 >        public final boolean tryAcquire(int ignore) {
777 >            if (compareAndSetState(0, 1)) {
778 >                setExclusiveOwnerThread(Thread.currentThread());
779 >                return true;
780 >            }
781 >            return false;
782 >        }
783 >        public final boolean tryRelease(int ignore) {
784 >            setExclusiveOwnerThread(null);
785 >            setState(0);
786 >            return true;
787 >        }
788 >        public final int tryAcquireShared(int ignore) {
789 >            for (int c;;) {
790 >                if ((c = getState()) > 0)
791 >                    return -1;
792 >                if (compareAndSetState(c, c -1))
793 >                    return 1;
794 >            }
795 >        }
796 >        public final boolean tryReleaseShared(int ignore) {
797 >            int c;
798 >            do {} while (!compareAndSetState(c = getState(), c + 1));
799 >            return c == -1;
800 >        }
801 >
802 >        /** From CLR */
803 >        private void rotateLeft(TreeNode p) {
804 >            if (p != null) {
805 >                TreeNode r = p.right, pp, rl;
806 >                if ((rl = p.right = r.left) != null)
807 >                    rl.parent = p;
808 >                if ((pp = r.parent = p.parent) == null)
809 >                    root = r;
810 >                else if (pp.left == p)
811 >                    pp.left = r;
812 >                else
813 >                    pp.right = r;
814 >                r.left = p;
815 >                p.parent = r;
816 >            }
817          }
818  
819 <        @SuppressWarnings("unchecked")
820 <        static final <K,V> Segment<K,V>[] newArray(int i) {
821 <            return new Segment[i];
819 >        /** From CLR */
820 >        private void rotateRight(TreeNode p) {
821 >            if (p != null) {
822 >                TreeNode l = p.left, pp, lr;
823 >                if ((lr = p.left = l.right) != null)
824 >                    lr.parent = p;
825 >                if ((pp = l.parent = p.parent) == null)
826 >                    root = l;
827 >                else if (pp.right == p)
828 >                    pp.right = l;
829 >                else
830 >                    pp.left = l;
831 >                l.right = p;
832 >                p.parent = l;
833 >            }
834          }
835  
836          /**
837 <         * Sets table to new HashEntry array.
838 <         * Call only while holding lock or in constructor.
837 >         * Returns the TreeNode (or null if not found) for the given key
838 >         * starting at given root.
839           */
840 <        void setTable(HashEntry<K,V>[] newTable) {
841 <            threshold = (int)(newTable.length * loadFactor);
842 <            table = newTable;
840 >        @SuppressWarnings("unchecked") final TreeNode getTreeNode
841 >            (int h, Object k, TreeNode p) {
842 >            Class<?> c = k.getClass();
843 >            while (p != null) {
844 >                int dir, ph;  Object pk; Class<?> pc;
845 >                if ((ph = p.hash) == h) {
846 >                    if ((pk = p.key) == k || k.equals(pk))
847 >                        return p;
848 >                    if (c != (pc = pk.getClass()) ||
849 >                        !(k instanceof Comparable) ||
850 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
851 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
852 >                        TreeNode r = null, s = null, pl, pr;
853 >                        if (dir >= 0) {
854 >                            if ((pl = p.left) != null && h <= pl.hash)
855 >                                s = pl;
856 >                        }
857 >                        else if ((pr = p.right) != null && h >= pr.hash)
858 >                            s = pr;
859 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
860 >                            return r;
861 >                    }
862 >                }
863 >                else
864 >                    dir = (h < ph) ? -1 : 1;
865 >                p = (dir > 0) ? p.right : p.left;
866 >            }
867 >            return null;
868          }
869  
870          /**
871 <         * Returns properly casted first entry of bin for given hash.
871 >         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
872 >         * read-lock to call getTreeNode, but during failure to get
873 >         * lock, searches along next links.
874           */
875 <        HashEntry<K,V> getFirst(int hash) {
876 <            HashEntry<K,V>[] tab = table;
877 <            return tab[hash & (tab.length - 1)];
875 >        final Object getValue(int h, Object k) {
876 >            Node r = null;
877 >            int c = getState(); // Must read lock state first
878 >            for (Node e = first; e != null; e = e.next) {
879 >                if (c <= 0 && compareAndSetState(c, c - 1)) {
880 >                    try {
881 >                        r = getTreeNode(h, k, root);
882 >                    } finally {
883 >                        releaseShared(0);
884 >                    }
885 >                    break;
886 >                }
887 >                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
888 >                    r = e;
889 >                    break;
890 >                }
891 >                else
892 >                    c = getState();
893 >            }
894 >            return r == null ? null : r.val;
895          }
896  
897          /**
898 <         * Reads value field of an entry under lock. Called if value
899 <         * field ever appears to be null. This is possible only if a
315 <         * compiler happens to reorder a HashEntry initialization with
316 <         * its table assignment, which is legal under memory model
317 <         * but is not known to ever occur.
898 >         * Finds or adds a node.
899 >         * @return null if added
900           */
901 <        V readValueUnderLock(HashEntry<K,V> e) {
902 <            lock();
903 <            try {
904 <                return e.value;
905 <            } finally {
906 <                unlock();
901 >        @SuppressWarnings("unchecked") final TreeNode putTreeNode
902 >            (int h, Object k, Object v) {
903 >            Class<?> c = k.getClass();
904 >            TreeNode pp = root, p = null;
905 >            int dir = 0;
906 >            while (pp != null) { // find existing node or leaf to insert at
907 >                int ph;  Object pk; Class<?> pc;
908 >                p = pp;
909 >                if ((ph = p.hash) == h) {
910 >                    if ((pk = p.key) == k || k.equals(pk))
911 >                        return p;
912 >                    if (c != (pc = pk.getClass()) ||
913 >                        !(k instanceof Comparable) ||
914 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
915 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
916 >                        TreeNode r = null, s = null, pl, pr;
917 >                        if (dir >= 0) {
918 >                            if ((pl = p.left) != null && h <= pl.hash)
919 >                                s = pl;
920 >                        }
921 >                        else if ((pr = p.right) != null && h >= pr.hash)
922 >                            s = pr;
923 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
924 >                            return r;
925 >                    }
926 >                }
927 >                else
928 >                    dir = (h < ph) ? -1 : 1;
929 >                pp = (dir > 0) ? p.right : p.left;
930              }
326        }
931  
932 <        /* Specialized implementations of map methods */
933 <
934 <        V get(Object key, int hash) {
935 <            if (count != 0) { // read-volatile
936 <                HashEntry<K,V> e = getFirst(hash);
937 <                while (e != null) {
938 <                    if (e.hash == hash && key.equals(e.key)) {
939 <                        V v = e.value;
940 <                        if (v != null)
941 <                            return v;
942 <                        return readValueUnderLock(e); // recheck
932 >            TreeNode f = first;
933 >            TreeNode x = first = new TreeNode(h, k, v, f, p);
934 >            if (p == null)
935 >                root = x;
936 >            else { // attach and rebalance; adapted from CLR
937 >                TreeNode xp, xpp;
938 >                if (f != null)
939 >                    f.prev = x;
940 >                if (dir <= 0)
941 >                    p.left = x;
942 >                else
943 >                    p.right = x;
944 >                x.red = true;
945 >                while (x != null && (xp = x.parent) != null && xp.red &&
946 >                       (xpp = xp.parent) != null) {
947 >                    TreeNode xppl = xpp.left;
948 >                    if (xp == xppl) {
949 >                        TreeNode y = xpp.right;
950 >                        if (y != null && y.red) {
951 >                            y.red = false;
952 >                            xp.red = false;
953 >                            xpp.red = true;
954 >                            x = xpp;
955 >                        }
956 >                        else {
957 >                            if (x == xp.right) {
958 >                                rotateLeft(x = xp);
959 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
960 >                            }
961 >                            if (xp != null) {
962 >                                xp.red = false;
963 >                                if (xpp != null) {
964 >                                    xpp.red = true;
965 >                                    rotateRight(xpp);
966 >                                }
967 >                            }
968 >                        }
969 >                    }
970 >                    else {
971 >                        TreeNode y = xppl;
972 >                        if (y != null && y.red) {
973 >                            y.red = false;
974 >                            xp.red = false;
975 >                            xpp.red = true;
976 >                            x = xpp;
977 >                        }
978 >                        else {
979 >                            if (x == xp.left) {
980 >                                rotateRight(x = xp);
981 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
982 >                            }
983 >                            if (xp != null) {
984 >                                xp.red = false;
985 >                                if (xpp != null) {
986 >                                    xpp.red = true;
987 >                                    rotateLeft(xpp);
988 >                                }
989 >                            }
990 >                        }
991                      }
340                    e = e.next;
992                  }
993 +                TreeNode r = root;
994 +                if (r != null && r.red)
995 +                    r.red = false;
996              }
997              return null;
998          }
999  
1000 <        boolean containsKey(Object key, int hash) {
1001 <            if (count != 0) { // read-volatile
1002 <                HashEntry<K,V> e = getFirst(hash);
1003 <                while (e != null) {
1004 <                    if (e.hash == hash && key.equals(e.key))
1005 <                        return true;
1006 <                    e = e.next;
1000 >        /**
1001 >         * Removes the given node, that must be present before this
1002 >         * call.  This is messier than typical red-black deletion code
1003 >         * because we cannot swap the contents of an interior node
1004 >         * with a leaf successor that is pinned by "next" pointers
1005 >         * that are accessible independently of lock. So instead we
1006 >         * swap the tree linkages.
1007 >         */
1008 >        final void deleteTreeNode(TreeNode p) {
1009 >            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
1010 >            TreeNode pred = p.prev;
1011 >            if (pred == null)
1012 >                first = next;
1013 >            else
1014 >                pred.next = next;
1015 >            if (next != null)
1016 >                next.prev = pred;
1017 >            TreeNode replacement;
1018 >            TreeNode pl = p.left;
1019 >            TreeNode pr = p.right;
1020 >            if (pl != null && pr != null) {
1021 >                TreeNode s = pr, sl;
1022 >                while ((sl = s.left) != null) // find successor
1023 >                    s = sl;
1024 >                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
1025 >                TreeNode sr = s.right;
1026 >                TreeNode pp = p.parent;
1027 >                if (s == pr) { // p was s's direct parent
1028 >                    p.parent = s;
1029 >                    s.right = p;
1030 >                }
1031 >                else {
1032 >                    TreeNode sp = s.parent;
1033 >                    if ((p.parent = sp) != null) {
1034 >                        if (s == sp.left)
1035 >                            sp.left = p;
1036 >                        else
1037 >                            sp.right = p;
1038 >                    }
1039 >                    if ((s.right = pr) != null)
1040 >                        pr.parent = s;
1041                  }
1042 +                p.left = null;
1043 +                if ((p.right = sr) != null)
1044 +                    sr.parent = p;
1045 +                if ((s.left = pl) != null)
1046 +                    pl.parent = s;
1047 +                if ((s.parent = pp) == null)
1048 +                    root = s;
1049 +                else if (p == pp.left)
1050 +                    pp.left = s;
1051 +                else
1052 +                    pp.right = s;
1053 +                replacement = sr;
1054              }
1055 <            return false;
1055 >            else
1056 >                replacement = (pl != null) ? pl : pr;
1057 >            TreeNode pp = p.parent;
1058 >            if (replacement == null) {
1059 >                if (pp == null) {
1060 >                    root = null;
1061 >                    return;
1062 >                }
1063 >                replacement = p;
1064 >            }
1065 >            else {
1066 >                replacement.parent = pp;
1067 >                if (pp == null)
1068 >                    root = replacement;
1069 >                else if (p == pp.left)
1070 >                    pp.left = replacement;
1071 >                else
1072 >                    pp.right = replacement;
1073 >                p.left = p.right = p.parent = null;
1074 >            }
1075 >            if (!p.red) { // rebalance, from CLR
1076 >                TreeNode x = replacement;
1077 >                while (x != null) {
1078 >                    TreeNode xp, xpl;
1079 >                    if (x.red || (xp = x.parent) == null) {
1080 >                        x.red = false;
1081 >                        break;
1082 >                    }
1083 >                    if (x == (xpl = xp.left)) {
1084 >                        TreeNode sib = xp.right;
1085 >                        if (sib != null && sib.red) {
1086 >                            sib.red = false;
1087 >                            xp.red = true;
1088 >                            rotateLeft(xp);
1089 >                            sib = (xp = x.parent) == null ? null : xp.right;
1090 >                        }
1091 >                        if (sib == null)
1092 >                            x = xp;
1093 >                        else {
1094 >                            TreeNode sl = sib.left, sr = sib.right;
1095 >                            if ((sr == null || !sr.red) &&
1096 >                                (sl == null || !sl.red)) {
1097 >                                sib.red = true;
1098 >                                x = xp;
1099 >                            }
1100 >                            else {
1101 >                                if (sr == null || !sr.red) {
1102 >                                    if (sl != null)
1103 >                                        sl.red = false;
1104 >                                    sib.red = true;
1105 >                                    rotateRight(sib);
1106 >                                    sib = (xp = x.parent) == null ? null : xp.right;
1107 >                                }
1108 >                                if (sib != null) {
1109 >                                    sib.red = (xp == null) ? false : xp.red;
1110 >                                    if ((sr = sib.right) != null)
1111 >                                        sr.red = false;
1112 >                                }
1113 >                                if (xp != null) {
1114 >                                    xp.red = false;
1115 >                                    rotateLeft(xp);
1116 >                                }
1117 >                                x = root;
1118 >                            }
1119 >                        }
1120 >                    }
1121 >                    else { // symmetric
1122 >                        TreeNode sib = xpl;
1123 >                        if (sib != null && sib.red) {
1124 >                            sib.red = false;
1125 >                            xp.red = true;
1126 >                            rotateRight(xp);
1127 >                            sib = (xp = x.parent) == null ? null : xp.left;
1128 >                        }
1129 >                        if (sib == null)
1130 >                            x = xp;
1131 >                        else {
1132 >                            TreeNode sl = sib.left, sr = sib.right;
1133 >                            if ((sl == null || !sl.red) &&
1134 >                                (sr == null || !sr.red)) {
1135 >                                sib.red = true;
1136 >                                x = xp;
1137 >                            }
1138 >                            else {
1139 >                                if (sl == null || !sl.red) {
1140 >                                    if (sr != null)
1141 >                                        sr.red = false;
1142 >                                    sib.red = true;
1143 >                                    rotateLeft(sib);
1144 >                                    sib = (xp = x.parent) == null ? null : xp.left;
1145 >                                }
1146 >                                if (sib != null) {
1147 >                                    sib.red = (xp == null) ? false : xp.red;
1148 >                                    if ((sl = sib.left) != null)
1149 >                                        sl.red = false;
1150 >                                }
1151 >                                if (xp != null) {
1152 >                                    xp.red = false;
1153 >                                    rotateRight(xp);
1154 >                                }
1155 >                                x = root;
1156 >                            }
1157 >                        }
1158 >                    }
1159 >                }
1160 >            }
1161 >            if (p == replacement && (pp = p.parent) != null) {
1162 >                if (p == pp.left) // detach pointers
1163 >                    pp.left = null;
1164 >                else if (p == pp.right)
1165 >                    pp.right = null;
1166 >                p.parent = null;
1167 >            }
1168 >        }
1169 >    }
1170 >
1171 >    /* ---------------- Collision reduction methods -------------- */
1172 >
1173 >    /**
1174 >     * Spreads higher bits to lower, and also forces top 2 bits to 0.
1175 >     * Because the table uses power-of-two masking, sets of hashes
1176 >     * that vary only in bits above the current mask will always
1177 >     * collide. (Among known examples are sets of Float keys holding
1178 >     * consecutive whole numbers in small tables.)  To counter this,
1179 >     * we apply a transform that spreads the impact of higher bits
1180 >     * downward. There is a tradeoff between speed, utility, and
1181 >     * quality of bit-spreading. Because many common sets of hashes
1182 >     * are already reasonably distributed across bits (so don't benefit
1183 >     * from spreading), and because we use trees to handle large sets
1184 >     * of collisions in bins, we don't need excessively high quality.
1185 >     */
1186 >    private static final int spread(int h) {
1187 >        h ^= (h >>> 18) ^ (h >>> 12);
1188 >        return (h ^ (h >>> 10)) & HASH_BITS;
1189 >    }
1190 >
1191 >    /**
1192 >     * Replaces a list bin with a tree bin. Call only when locked.
1193 >     * Fails to replace if the given key is non-comparable or table
1194 >     * is, or needs, resizing.
1195 >     */
1196 >    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
1197 >        if ((key instanceof Comparable) &&
1198 >            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1199 >            TreeBin t = new TreeBin();
1200 >            for (Node e = tabAt(tab, index); e != null; e = e.next)
1201 >                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1202 >            setTabAt(tab, index, new Node(MOVED, t, null, null));
1203          }
1204 +    }
1205  
1206 <        boolean containsValue(Object value) {
1207 <            if (count != 0) { // read-volatile
1208 <                HashEntry<K,V>[] tab = table;
1209 <                int len = tab.length;
1210 <                for (int i = 0 ; i < len; i++) {
1211 <                    for (HashEntry<K,V> e = tab[i]; e != null; e = e.next) {
1212 <                        V v = e.value;
1213 <                        if (v == null) // recheck
1214 <                            v = readValueUnderLock(e);
1215 <                        if (value.equals(v))
1216 <                            return true;
1206 >    /* ---------------- Internal access and update methods -------------- */
1207 >
1208 >    /** Implementation for get and containsKey */
1209 >    private final Object internalGet(Object k) {
1210 >        int h = spread(k.hashCode());
1211 >        retry: for (Node[] tab = table; tab != null;) {
1212 >            Node e, p; Object ek, ev; int eh;      // locals to read fields once
1213 >            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1214 >                if ((eh = e.hash) == MOVED) {
1215 >                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
1216 >                        return ((TreeBin)ek).getValue(h, k);
1217 >                    else {                        // restart with new table
1218 >                        tab = (Node[])ek;
1219 >                        continue retry;
1220                      }
1221                  }
1222 +                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1223 +                         ((ek = e.key) == k || k.equals(ek)))
1224 +                    return ev;
1225              }
1226 <            return false;
1226 >            break;
1227          }
1228 +        return null;
1229 +    }
1230  
1231 <        boolean replace(K key, int hash, V oldValue, V newValue) {
1232 <            lock();
1233 <            try {
1234 <                HashEntry<K,V> e = getFirst(hash);
1235 <                while (e != null && (e.hash != hash || !key.equals(e.key)))
1236 <                    e = e.next;
1237 <
1238 <                boolean replaced = false;
1239 <                if (e != null && oldValue.equals(e.value)) {
1240 <                    replaced = true;
1241 <                    e.value = newValue;
1231 >    /**
1232 >     * Implementation for the four public remove/replace methods:
1233 >     * Replaces node value with v, conditional upon match of cv if
1234 >     * non-null.  If resulting value is null, delete.
1235 >     */
1236 >    private final Object internalReplace(Object k, Object v, Object cv) {
1237 >        int h = spread(k.hashCode());
1238 >        Object oldVal = null;
1239 >        for (Node[] tab = table;;) {
1240 >            Node f; int i, fh; Object fk;
1241 >            if (tab == null ||
1242 >                (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1243 >                break;
1244 >            else if ((fh = f.hash) == MOVED) {
1245 >                if ((fk = f.key) instanceof TreeBin) {
1246 >                    TreeBin t = (TreeBin)fk;
1247 >                    boolean validated = false;
1248 >                    boolean deleted = false;
1249 >                    t.acquire(0);
1250 >                    try {
1251 >                        if (tabAt(tab, i) == f) {
1252 >                            validated = true;
1253 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1254 >                            if (p != null) {
1255 >                                Object pv = p.val;
1256 >                                if (cv == null || cv == pv || cv.equals(pv)) {
1257 >                                    oldVal = pv;
1258 >                                    if ((p.val = v) == null) {
1259 >                                        deleted = true;
1260 >                                        t.deleteTreeNode(p);
1261 >                                    }
1262 >                                }
1263 >                            }
1264 >                        }
1265 >                    } finally {
1266 >                        t.release(0);
1267 >                    }
1268 >                    if (validated) {
1269 >                        if (deleted)
1270 >                            counter.add(-1L);
1271 >                        break;
1272 >                    }
1273 >                }
1274 >                else
1275 >                    tab = (Node[])fk;
1276 >            }
1277 >            else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1278 >                break;                          // rules out possible existence
1279 >            else if ((fh & LOCKED) != 0) {
1280 >                checkForResize();               // try resizing if can't get lock
1281 >                f.tryAwaitLock(tab, i);
1282 >            }
1283 >            else if (f.casHash(fh, fh | LOCKED)) {
1284 >                boolean validated = false;
1285 >                boolean deleted = false;
1286 >                try {
1287 >                    if (tabAt(tab, i) == f) {
1288 >                        validated = true;
1289 >                        for (Node e = f, pred = null;;) {
1290 >                            Object ek, ev;
1291 >                            if ((e.hash & HASH_BITS) == h &&
1292 >                                ((ev = e.val) != null) &&
1293 >                                ((ek = e.key) == k || k.equals(ek))) {
1294 >                                if (cv == null || cv == ev || cv.equals(ev)) {
1295 >                                    oldVal = ev;
1296 >                                    if ((e.val = v) == null) {
1297 >                                        deleted = true;
1298 >                                        Node en = e.next;
1299 >                                        if (pred != null)
1300 >                                            pred.next = en;
1301 >                                        else
1302 >                                            setTabAt(tab, i, en);
1303 >                                    }
1304 >                                }
1305 >                                break;
1306 >                            }
1307 >                            pred = e;
1308 >                            if ((e = e.next) == null)
1309 >                                break;
1310 >                        }
1311 >                    }
1312 >                } finally {
1313 >                    if (!f.casHash(fh | LOCKED, fh)) {
1314 >                        f.hash = fh;
1315 >                        synchronized (f) { f.notifyAll(); };
1316 >                    }
1317 >                }
1318 >                if (validated) {
1319 >                    if (deleted)
1320 >                        counter.add(-1L);
1321 >                    break;
1322                  }
387                return replaced;
388            } finally {
389                unlock();
1323              }
1324          }
1325 +        return oldVal;
1326 +    }
1327  
1328 <        V replace(K key, int hash, V newValue) {
1329 <            lock();
1330 <            try {
1331 <                HashEntry<K,V> e = getFirst(hash);
1332 <                while (e != null && (e.hash != hash || !key.equals(e.key)))
1333 <                    e = e.next;
1328 >    /*
1329 >     * Internal versions of the six insertion methods, each a
1330 >     * little more complicated than the last. All have
1331 >     * the same basic structure as the first (internalPut):
1332 >     *  1. If table uninitialized, create
1333 >     *  2. If bin empty, try to CAS new node
1334 >     *  3. If bin stale, use new table
1335 >     *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1336 >     *  5. Lock and validate; if valid, scan and add or update
1337 >     *
1338 >     * The others interweave other checks and/or alternative actions:
1339 >     *  * Plain put checks for and performs resize after insertion.
1340 >     *  * putIfAbsent prescans for mapping without lock (and fails to add
1341 >     *    if present), which also makes pre-emptive resize checks worthwhile.
1342 >     *  * computeIfAbsent extends form used in putIfAbsent with additional
1343 >     *    mechanics to deal with, calls, potential exceptions and null
1344 >     *    returns from function call.
1345 >     *  * compute uses the same function-call mechanics, but without
1346 >     *    the prescans
1347 >     *  * merge acts as putIfAbsent in the absent case, but invokes the
1348 >     *    update function if present
1349 >     *  * putAll attempts to pre-allocate enough table space
1350 >     *    and more lazily performs count updates and checks.
1351 >     *
1352 >     * Someday when details settle down a bit more, it might be worth
1353 >     * some factoring to reduce sprawl.
1354 >     */
1355  
1356 <                V oldValue = null;
1357 <                if (e != null) {
1358 <                    oldValue = e.value;
1359 <                    e.value = newValue;
1356 >    /** Implementation for put */
1357 >    private final Object internalPut(Object k, Object v) {
1358 >        int h = spread(k.hashCode());
1359 >        int count = 0;
1360 >        for (Node[] tab = table;;) {
1361 >            int i; Node f; int fh; Object fk;
1362 >            if (tab == null)
1363 >                tab = initTable();
1364 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1365 >                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1366 >                    break;                   // no lock when adding to empty bin
1367 >            }
1368 >            else if ((fh = f.hash) == MOVED) {
1369 >                if ((fk = f.key) instanceof TreeBin) {
1370 >                    TreeBin t = (TreeBin)fk;
1371 >                    Object oldVal = null;
1372 >                    t.acquire(0);
1373 >                    try {
1374 >                        if (tabAt(tab, i) == f) {
1375 >                            count = 2;
1376 >                            TreeNode p = t.putTreeNode(h, k, v);
1377 >                            if (p != null) {
1378 >                                oldVal = p.val;
1379 >                                p.val = v;
1380 >                            }
1381 >                        }
1382 >                    } finally {
1383 >                        t.release(0);
1384 >                    }
1385 >                    if (count != 0) {
1386 >                        if (oldVal != null)
1387 >                            return oldVal;
1388 >                        break;
1389 >                    }
1390 >                }
1391 >                else
1392 >                    tab = (Node[])fk;
1393 >            }
1394 >            else if ((fh & LOCKED) != 0) {
1395 >                checkForResize();
1396 >                f.tryAwaitLock(tab, i);
1397 >            }
1398 >            else if (f.casHash(fh, fh | LOCKED)) {
1399 >                Object oldVal = null;
1400 >                try {                        // needed in case equals() throws
1401 >                    if (tabAt(tab, i) == f) {
1402 >                        count = 1;
1403 >                        for (Node e = f;; ++count) {
1404 >                            Object ek, ev;
1405 >                            if ((e.hash & HASH_BITS) == h &&
1406 >                                (ev = e.val) != null &&
1407 >                                ((ek = e.key) == k || k.equals(ek))) {
1408 >                                oldVal = ev;
1409 >                                e.val = v;
1410 >                                break;
1411 >                            }
1412 >                            Node last = e;
1413 >                            if ((e = e.next) == null) {
1414 >                                last.next = new Node(h, k, v, null);
1415 >                                if (count >= TREE_THRESHOLD)
1416 >                                    replaceWithTreeBin(tab, i, k);
1417 >                                break;
1418 >                            }
1419 >                        }
1420 >                    }
1421 >                } finally {                  // unlock and signal if needed
1422 >                    if (!f.casHash(fh | LOCKED, fh)) {
1423 >                        f.hash = fh;
1424 >                        synchronized (f) { f.notifyAll(); };
1425 >                    }
1426 >                }
1427 >                if (count != 0) {
1428 >                    if (oldVal != null)
1429 >                        return oldVal;
1430 >                    if (tab.length <= 64)
1431 >                        count = 2;
1432 >                    break;
1433                  }
405                return oldValue;
406            } finally {
407                unlock();
1434              }
1435          }
1436 +        counter.add(1L);
1437 +        if (count > 1)
1438 +            checkForResize();
1439 +        return null;
1440 +    }
1441  
1442 +    /** Implementation for putIfAbsent */
1443 +    private final Object internalPutIfAbsent(Object k, Object v) {
1444 +        int h = spread(k.hashCode());
1445 +        int count = 0;
1446 +        for (Node[] tab = table;;) {
1447 +            int i; Node f; int fh; Object fk, fv;
1448 +            if (tab == null)
1449 +                tab = initTable();
1450 +            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1451 +                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1452 +                    break;
1453 +            }
1454 +            else if ((fh = f.hash) == MOVED) {
1455 +                if ((fk = f.key) instanceof TreeBin) {
1456 +                    TreeBin t = (TreeBin)fk;
1457 +                    Object oldVal = null;
1458 +                    t.acquire(0);
1459 +                    try {
1460 +                        if (tabAt(tab, i) == f) {
1461 +                            count = 2;
1462 +                            TreeNode p = t.putTreeNode(h, k, v);
1463 +                            if (p != null)
1464 +                                oldVal = p.val;
1465 +                        }
1466 +                    } finally {
1467 +                        t.release(0);
1468 +                    }
1469 +                    if (count != 0) {
1470 +                        if (oldVal != null)
1471 +                            return oldVal;
1472 +                        break;
1473 +                    }
1474 +                }
1475 +                else
1476 +                    tab = (Node[])fk;
1477 +            }
1478 +            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1479 +                     ((fk = f.key) == k || k.equals(fk)))
1480 +                return fv;
1481 +            else {
1482 +                Node g = f.next;
1483 +                if (g != null) { // at least 2 nodes -- search and maybe resize
1484 +                    for (Node e = g;;) {
1485 +                        Object ek, ev;
1486 +                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1487 +                            ((ek = e.key) == k || k.equals(ek)))
1488 +                            return ev;
1489 +                        if ((e = e.next) == null) {
1490 +                            checkForResize();
1491 +                            break;
1492 +                        }
1493 +                    }
1494 +                }
1495 +                if (((fh = f.hash) & LOCKED) != 0) {
1496 +                    checkForResize();
1497 +                    f.tryAwaitLock(tab, i);
1498 +                }
1499 +                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1500 +                    Object oldVal = null;
1501 +                    try {
1502 +                        if (tabAt(tab, i) == f) {
1503 +                            count = 1;
1504 +                            for (Node e = f;; ++count) {
1505 +                                Object ek, ev;
1506 +                                if ((e.hash & HASH_BITS) == h &&
1507 +                                    (ev = e.val) != null &&
1508 +                                    ((ek = e.key) == k || k.equals(ek))) {
1509 +                                    oldVal = ev;
1510 +                                    break;
1511 +                                }
1512 +                                Node last = e;
1513 +                                if ((e = e.next) == null) {
1514 +                                    last.next = new Node(h, k, v, null);
1515 +                                    if (count >= TREE_THRESHOLD)
1516 +                                        replaceWithTreeBin(tab, i, k);
1517 +                                    break;
1518 +                                }
1519 +                            }
1520 +                        }
1521 +                    } finally {
1522 +                        if (!f.casHash(fh | LOCKED, fh)) {
1523 +                            f.hash = fh;
1524 +                            synchronized (f) { f.notifyAll(); };
1525 +                        }
1526 +                    }
1527 +                    if (count != 0) {
1528 +                        if (oldVal != null)
1529 +                            return oldVal;
1530 +                        if (tab.length <= 64)
1531 +                            count = 2;
1532 +                        break;
1533 +                    }
1534 +                }
1535 +            }
1536 +        }
1537 +        counter.add(1L);
1538 +        if (count > 1)
1539 +            checkForResize();
1540 +        return null;
1541 +    }
1542  
1543 <        V put(K key, int hash, V value, boolean onlyIfAbsent) {
1544 <            lock();
1545 <            try {
1546 <                int c = count;
1547 <                if (c++ > threshold) // ensure capacity
1548 <                    rehash();
1549 <                HashEntry<K,V>[] tab = table;
1550 <                int index = hash & (tab.length - 1);
1551 <                HashEntry<K,V> first = tab[index];
1552 <                HashEntry<K,V> e = first;
1553 <                while (e != null && (e.hash != hash || !key.equals(e.key)))
1554 <                    e = e.next;
1555 <
1556 <                V oldValue;
1557 <                if (e != null) {
1558 <                    oldValue = e.value;
1559 <                    if (!onlyIfAbsent)
1560 <                        e.value = value;
1543 >    /** Implementation for computeIfAbsent */
1544 >    private final Object internalComputeIfAbsent(K k,
1545 >                                                 Fun<? super K, ?> mf) {
1546 >        int h = spread(k.hashCode());
1547 >        Object val = null;
1548 >        int count = 0;
1549 >        for (Node[] tab = table;;) {
1550 >            Node f; int i, fh; Object fk, fv;
1551 >            if (tab == null)
1552 >                tab = initTable();
1553 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1554 >                Node node = new Node(fh = h | LOCKED, k, null, null);
1555 >                if (casTabAt(tab, i, null, node)) {
1556 >                    count = 1;
1557 >                    try {
1558 >                        if ((val = mf.apply(k)) != null)
1559 >                            node.val = val;
1560 >                    } finally {
1561 >                        if (val == null)
1562 >                            setTabAt(tab, i, null);
1563 >                        if (!node.casHash(fh, h)) {
1564 >                            node.hash = h;
1565 >                            synchronized (node) { node.notifyAll(); };
1566 >                        }
1567 >                    }
1568                  }
1569 <                else {
1570 <                    oldValue = null;
1571 <                    ++modCount;
1572 <                    tab[index] = new HashEntry<K,V>(key, hash, first, value);
1573 <                    count = c; // write-volatile
1569 >                if (count != 0)
1570 >                    break;
1571 >            }
1572 >            else if ((fh = f.hash) == MOVED) {
1573 >                if ((fk = f.key) instanceof TreeBin) {
1574 >                    TreeBin t = (TreeBin)fk;
1575 >                    boolean added = false;
1576 >                    t.acquire(0);
1577 >                    try {
1578 >                        if (tabAt(tab, i) == f) {
1579 >                            count = 1;
1580 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1581 >                            if (p != null)
1582 >                                val = p.val;
1583 >                            else if ((val = mf.apply(k)) != null) {
1584 >                                added = true;
1585 >                                count = 2;
1586 >                                t.putTreeNode(h, k, val);
1587 >                            }
1588 >                        }
1589 >                    } finally {
1590 >                        t.release(0);
1591 >                    }
1592 >                    if (count != 0) {
1593 >                        if (!added)
1594 >                            return val;
1595 >                        break;
1596 >                    }
1597 >                }
1598 >                else
1599 >                    tab = (Node[])fk;
1600 >            }
1601 >            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1602 >                     ((fk = f.key) == k || k.equals(fk)))
1603 >                return fv;
1604 >            else {
1605 >                Node g = f.next;
1606 >                if (g != null) {
1607 >                    for (Node e = g;;) {
1608 >                        Object ek, ev;
1609 >                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1610 >                            ((ek = e.key) == k || k.equals(ek)))
1611 >                            return ev;
1612 >                        if ((e = e.next) == null) {
1613 >                            checkForResize();
1614 >                            break;
1615 >                        }
1616 >                    }
1617 >                }
1618 >                if (((fh = f.hash) & LOCKED) != 0) {
1619 >                    checkForResize();
1620 >                    f.tryAwaitLock(tab, i);
1621 >                }
1622 >                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1623 >                    boolean added = false;
1624 >                    try {
1625 >                        if (tabAt(tab, i) == f) {
1626 >                            count = 1;
1627 >                            for (Node e = f;; ++count) {
1628 >                                Object ek, ev;
1629 >                                if ((e.hash & HASH_BITS) == h &&
1630 >                                    (ev = e.val) != null &&
1631 >                                    ((ek = e.key) == k || k.equals(ek))) {
1632 >                                    val = ev;
1633 >                                    break;
1634 >                                }
1635 >                                Node last = e;
1636 >                                if ((e = e.next) == null) {
1637 >                                    if ((val = mf.apply(k)) != null) {
1638 >                                        added = true;
1639 >                                        last.next = new Node(h, k, val, null);
1640 >                                        if (count >= TREE_THRESHOLD)
1641 >                                            replaceWithTreeBin(tab, i, k);
1642 >                                    }
1643 >                                    break;
1644 >                                }
1645 >                            }
1646 >                        }
1647 >                    } finally {
1648 >                        if (!f.casHash(fh | LOCKED, fh)) {
1649 >                            f.hash = fh;
1650 >                            synchronized (f) { f.notifyAll(); };
1651 >                        }
1652 >                    }
1653 >                    if (count != 0) {
1654 >                        if (!added)
1655 >                            return val;
1656 >                        if (tab.length <= 64)
1657 >                            count = 2;
1658 >                        break;
1659 >                    }
1660                  }
437                return oldValue;
438            } finally {
439                unlock();
1661              }
1662          }
1663 +        if (val != null) {
1664 +            counter.add(1L);
1665 +            if (count > 1)
1666 +                checkForResize();
1667 +        }
1668 +        return val;
1669 +    }
1670  
1671 <        void rehash() {
1672 <            HashEntry<K,V>[] oldTable = table;
1673 <            int oldCapacity = oldTable.length;
1674 <            if (oldCapacity >= MAXIMUM_CAPACITY)
1675 <                return;
1676 <
1677 <            /*
1678 <             * Reclassify nodes in each list to new Map.  Because we are
1679 <             * using power-of-two expansion, the elements from each bin
1680 <             * must either stay at same index, or move with a power of two
1681 <             * offset. We eliminate unnecessary node creation by catching
1682 <             * cases where old nodes can be reused because their next
1683 <             * fields won't change. Statistically, at the default
1684 <             * threshold, only about one-sixth of them need cloning when
1685 <             * a table doubles. The nodes they replace will be garbage
1686 <             * collectable as soon as they are no longer referenced by any
1687 <             * reader thread that may be in the midst of traversing table
1688 <             * right now.
1689 <             */
1690 <
1691 <            HashEntry<K,V>[] newTable = HashEntry.newArray(oldCapacity<<1);
1692 <            threshold = (int)(newTable.length * loadFactor);
1693 <            int sizeMask = newTable.length - 1;
1694 <            for (int i = 0; i < oldCapacity ; i++) {
1695 <                // We need to guarantee that any existing reads of old Map can
1696 <                //  proceed. So we cannot yet null out each bin.
1697 <                HashEntry<K,V> e = oldTable[i];
1698 <
1699 <                if (e != null) {
1700 <                    HashEntry<K,V> next = e.next;
1701 <                    int idx = e.hash & sizeMask;
1702 <
1703 <                    //  Single node on list
1704 <                    if (next == null)
1705 <                        newTable[idx] = e;
1671 >    /** Implementation for compute */
1672 >    @SuppressWarnings("unchecked") private final Object internalCompute
1673 >        (K k, boolean onlyIfPresent, BiFun<? super K, ? super V, ? extends V> mf) {
1674 >        int h = spread(k.hashCode());
1675 >        Object val = null;
1676 >        int delta = 0;
1677 >        int count = 0;
1678 >        for (Node[] tab = table;;) {
1679 >            Node f; int i, fh; Object fk;
1680 >            if (tab == null)
1681 >                tab = initTable();
1682 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1683 >                if (onlyIfPresent)
1684 >                    break;
1685 >                Node node = new Node(fh = h | LOCKED, k, null, null);
1686 >                if (casTabAt(tab, i, null, node)) {
1687 >                    try {
1688 >                        count = 1;
1689 >                        if ((val = mf.apply(k, null)) != null) {
1690 >                            node.val = val;
1691 >                            delta = 1;
1692 >                        }
1693 >                    } finally {
1694 >                        if (delta == 0)
1695 >                            setTabAt(tab, i, null);
1696 >                        if (!node.casHash(fh, h)) {
1697 >                            node.hash = h;
1698 >                            synchronized (node) { node.notifyAll(); };
1699 >                        }
1700 >                    }
1701 >                }
1702 >                if (count != 0)
1703 >                    break;
1704 >            }
1705 >            else if ((fh = f.hash) == MOVED) {
1706 >                if ((fk = f.key) instanceof TreeBin) {
1707 >                    TreeBin t = (TreeBin)fk;
1708 >                    t.acquire(0);
1709 >                    try {
1710 >                        if (tabAt(tab, i) == f) {
1711 >                            count = 1;
1712 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1713 >                            Object pv = (p == null) ? null : p.val;
1714 >                            if ((val = mf.apply(k, (V)pv)) != null) {
1715 >                                if (p != null)
1716 >                                    p.val = val;
1717 >                                else {
1718 >                                    count = 2;
1719 >                                    delta = 1;
1720 >                                    t.putTreeNode(h, k, val);
1721 >                                }
1722 >                            }
1723 >                            else if (p != null) {
1724 >                                delta = -1;
1725 >                                t.deleteTreeNode(p);
1726 >                            }
1727 >                        }
1728 >                    } finally {
1729 >                        t.release(0);
1730 >                    }
1731 >                    if (count != 0)
1732 >                        break;
1733 >                }
1734 >                else
1735 >                    tab = (Node[])fk;
1736 >            }
1737 >            else if ((fh & LOCKED) != 0) {
1738 >                checkForResize();
1739 >                f.tryAwaitLock(tab, i);
1740 >            }
1741 >            else if (f.casHash(fh, fh | LOCKED)) {
1742 >                try {
1743 >                    if (tabAt(tab, i) == f) {
1744 >                        count = 1;
1745 >                        for (Node e = f, pred = null;; ++count) {
1746 >                            Object ek, ev;
1747 >                            if ((e.hash & HASH_BITS) == h &&
1748 >                                (ev = e.val) != null &&
1749 >                                ((ek = e.key) == k || k.equals(ek))) {
1750 >                                val = mf.apply(k, (V)ev);
1751 >                                if (val != null)
1752 >                                    e.val = val;
1753 >                                else {
1754 >                                    delta = -1;
1755 >                                    Node en = e.next;
1756 >                                    if (pred != null)
1757 >                                        pred.next = en;
1758 >                                    else
1759 >                                        setTabAt(tab, i, en);
1760 >                                }
1761 >                                break;
1762 >                            }
1763 >                            pred = e;
1764 >                            if ((e = e.next) == null) {
1765 >                                if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1766 >                                    pred.next = new Node(h, k, val, null);
1767 >                                    delta = 1;
1768 >                                    if (count >= TREE_THRESHOLD)
1769 >                                        replaceWithTreeBin(tab, i, k);
1770 >                                }
1771 >                                break;
1772 >                            }
1773 >                        }
1774 >                    }
1775 >                } finally {
1776 >                    if (!f.casHash(fh | LOCKED, fh)) {
1777 >                        f.hash = fh;
1778 >                        synchronized (f) { f.notifyAll(); };
1779 >                    }
1780 >                }
1781 >                if (count != 0) {
1782 >                    if (tab.length <= 64)
1783 >                        count = 2;
1784 >                    break;
1785 >                }
1786 >            }
1787 >        }
1788 >        if (delta != 0) {
1789 >            counter.add((long)delta);
1790 >            if (count > 1)
1791 >                checkForResize();
1792 >        }
1793 >        return val;
1794 >    }
1795  
1796 <                    else {
1797 <                        // Reuse trailing consecutive sequence at same slot
1798 <                        HashEntry<K,V> lastRun = e;
1799 <                        int lastIdx = idx;
1800 <                        for (HashEntry<K,V> last = next;
1801 <                             last != null;
1802 <                             last = last.next) {
1803 <                            int k = last.hash & sizeMask;
1804 <                            if (k != lastIdx) {
1805 <                                lastIdx = k;
1806 <                                lastRun = last;
1796 >    /** Implementation for merge */
1797 >    @SuppressWarnings("unchecked") private final Object internalMerge
1798 >        (K k, V v, BiFun<? super V, ? super V, ? extends V> mf) {
1799 >        int h = spread(k.hashCode());
1800 >        Object val = null;
1801 >        int delta = 0;
1802 >        int count = 0;
1803 >        for (Node[] tab = table;;) {
1804 >            int i; Node f; int fh; Object fk, fv;
1805 >            if (tab == null)
1806 >                tab = initTable();
1807 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1808 >                if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1809 >                    delta = 1;
1810 >                    val = v;
1811 >                    break;
1812 >                }
1813 >            }
1814 >            else if ((fh = f.hash) == MOVED) {
1815 >                if ((fk = f.key) instanceof TreeBin) {
1816 >                    TreeBin t = (TreeBin)fk;
1817 >                    t.acquire(0);
1818 >                    try {
1819 >                        if (tabAt(tab, i) == f) {
1820 >                            count = 1;
1821 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1822 >                            val = (p == null) ? v : mf.apply((V)p.val, v);
1823 >                            if (val != null) {
1824 >                                if (p != null)
1825 >                                    p.val = val;
1826 >                                else {
1827 >                                    count = 2;
1828 >                                    delta = 1;
1829 >                                    t.putTreeNode(h, k, val);
1830 >                                }
1831 >                            }
1832 >                            else if (p != null) {
1833 >                                delta = -1;
1834 >                                t.deleteTreeNode(p);
1835 >                            }
1836 >                        }
1837 >                    } finally {
1838 >                        t.release(0);
1839 >                    }
1840 >                    if (count != 0)
1841 >                        break;
1842 >                }
1843 >                else
1844 >                    tab = (Node[])fk;
1845 >            }
1846 >            else if ((fh & LOCKED) != 0) {
1847 >                checkForResize();
1848 >                f.tryAwaitLock(tab, i);
1849 >            }
1850 >            else if (f.casHash(fh, fh | LOCKED)) {
1851 >                try {
1852 >                    if (tabAt(tab, i) == f) {
1853 >                        count = 1;
1854 >                        for (Node e = f, pred = null;; ++count) {
1855 >                            Object ek, ev;
1856 >                            if ((e.hash & HASH_BITS) == h &&
1857 >                                (ev = e.val) != null &&
1858 >                                ((ek = e.key) == k || k.equals(ek))) {
1859 >                                val = mf.apply(v, (V)ev);
1860 >                                if (val != null)
1861 >                                    e.val = val;
1862 >                                else {
1863 >                                    delta = -1;
1864 >                                    Node en = e.next;
1865 >                                    if (pred != null)
1866 >                                        pred.next = en;
1867 >                                    else
1868 >                                        setTabAt(tab, i, en);
1869 >                                }
1870 >                                break;
1871 >                            }
1872 >                            pred = e;
1873 >                            if ((e = e.next) == null) {
1874 >                                val = v;
1875 >                                pred.next = new Node(h, k, val, null);
1876 >                                delta = 1;
1877 >                                if (count >= TREE_THRESHOLD)
1878 >                                    replaceWithTreeBin(tab, i, k);
1879 >                                break;
1880                              }
1881                          }
1882 <                        newTable[lastIdx] = lastRun;
1882 >                    }
1883 >                } finally {
1884 >                    if (!f.casHash(fh | LOCKED, fh)) {
1885 >                        f.hash = fh;
1886 >                        synchronized (f) { f.notifyAll(); };
1887 >                    }
1888 >                }
1889 >                if (count != 0) {
1890 >                    if (tab.length <= 64)
1891 >                        count = 2;
1892 >                    break;
1893 >                }
1894 >            }
1895 >        }
1896 >        if (delta != 0) {
1897 >            counter.add((long)delta);
1898 >            if (count > 1)
1899 >                checkForResize();
1900 >        }
1901 >        return val;
1902 >    }
1903  
1904 <                        // Clone all remaining nodes
1905 <                        for (HashEntry<K,V> p = e; p != lastRun; p = p.next) {
1906 <                            int k = p.hash & sizeMask;
1907 <                            HashEntry<K,V> n = newTable[k];
1908 <                            newTable[k] = new HashEntry<K,V>(p.key, p.hash,
1909 <                                                             n, p.value);
1904 >    /** Implementation for putAll */
1905 >    private final void internalPutAll(Map<?, ?> m) {
1906 >        tryPresize(m.size());
1907 >        long delta = 0L;     // number of uncommitted additions
1908 >        boolean npe = false; // to throw exception on exit for nulls
1909 >        try {                // to clean up counts on other exceptions
1910 >            for (Map.Entry<?, ?> entry : m.entrySet()) {
1911 >                Object k, v;
1912 >                if (entry == null || (k = entry.getKey()) == null ||
1913 >                    (v = entry.getValue()) == null) {
1914 >                    npe = true;
1915 >                    break;
1916 >                }
1917 >                int h = spread(k.hashCode());
1918 >                for (Node[] tab = table;;) {
1919 >                    int i; Node f; int fh; Object fk;
1920 >                    if (tab == null)
1921 >                        tab = initTable();
1922 >                    else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1923 >                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1924 >                            ++delta;
1925 >                            break;
1926 >                        }
1927 >                    }
1928 >                    else if ((fh = f.hash) == MOVED) {
1929 >                        if ((fk = f.key) instanceof TreeBin) {
1930 >                            TreeBin t = (TreeBin)fk;
1931 >                            boolean validated = false;
1932 >                            t.acquire(0);
1933 >                            try {
1934 >                                if (tabAt(tab, i) == f) {
1935 >                                    validated = true;
1936 >                                    TreeNode p = t.getTreeNode(h, k, t.root);
1937 >                                    if (p != null)
1938 >                                        p.val = v;
1939 >                                    else {
1940 >                                        t.putTreeNode(h, k, v);
1941 >                                        ++delta;
1942 >                                    }
1943 >                                }
1944 >                            } finally {
1945 >                                t.release(0);
1946 >                            }
1947 >                            if (validated)
1948 >                                break;
1949 >                        }
1950 >                        else
1951 >                            tab = (Node[])fk;
1952 >                    }
1953 >                    else if ((fh & LOCKED) != 0) {
1954 >                        counter.add(delta);
1955 >                        delta = 0L;
1956 >                        checkForResize();
1957 >                        f.tryAwaitLock(tab, i);
1958 >                    }
1959 >                    else if (f.casHash(fh, fh | LOCKED)) {
1960 >                        int count = 0;
1961 >                        try {
1962 >                            if (tabAt(tab, i) == f) {
1963 >                                count = 1;
1964 >                                for (Node e = f;; ++count) {
1965 >                                    Object ek, ev;
1966 >                                    if ((e.hash & HASH_BITS) == h &&
1967 >                                        (ev = e.val) != null &&
1968 >                                        ((ek = e.key) == k || k.equals(ek))) {
1969 >                                        e.val = v;
1970 >                                        break;
1971 >                                    }
1972 >                                    Node last = e;
1973 >                                    if ((e = e.next) == null) {
1974 >                                        ++delta;
1975 >                                        last.next = new Node(h, k, v, null);
1976 >                                        if (count >= TREE_THRESHOLD)
1977 >                                            replaceWithTreeBin(tab, i, k);
1978 >                                        break;
1979 >                                    }
1980 >                                }
1981 >                            }
1982 >                        } finally {
1983 >                            if (!f.casHash(fh | LOCKED, fh)) {
1984 >                                f.hash = fh;
1985 >                                synchronized (f) { f.notifyAll(); };
1986 >                            }
1987 >                        }
1988 >                        if (count != 0) {
1989 >                            if (count > 1) {
1990 >                                counter.add(delta);
1991 >                                delta = 0L;
1992 >                                checkForResize();
1993 >                            }
1994 >                            break;
1995                          }
1996                      }
1997                  }
1998              }
1999 <            table = newTable;
1999 >        } finally {
2000 >            if (delta != 0)
2001 >                counter.add(delta);
2002          }
2003 +        if (npe)
2004 +            throw new NullPointerException();
2005 +    }
2006  
2007 <        /**
508 <         * Remove; match on key only if value null, else match both.
509 <         */
510 <        V remove(Object key, int hash, Object value) {
511 <            lock();
512 <            try {
513 <                int c = count - 1;
514 <                HashEntry<K,V>[] tab = table;
515 <                int index = hash & (tab.length - 1);
516 <                HashEntry<K,V> first = tab[index];
517 <                HashEntry<K,V> e = first;
518 <                while (e != null && (e.hash != hash || !key.equals(e.key)))
519 <                    e = e.next;
2007 >    /* ---------------- Table Initialization and Resizing -------------- */
2008  
2009 <                V oldValue = null;
2010 <                if (e != null) {
2011 <                    V v = e.value;
2012 <                    if (value == null || value.equals(v)) {
2013 <                        oldValue = v;
2014 <                        // All entries following removed node can stay
2015 <                        // in list, but all preceding ones need to be
2016 <                        // cloned.
2017 <                        ++modCount;
2018 <                        HashEntry<K,V> newFirst = e.next;
2019 <                        for (HashEntry<K,V> p = first; p != e; p = p.next)
2020 <                            newFirst = new HashEntry<K,V>(p.key, p.hash,
2021 <                                                          newFirst, p.value);
2022 <                        tab[index] = newFirst;
2023 <                        count = c; // write-volatile
2009 >    /**
2010 >     * Returns a power of two table size for the given desired capacity.
2011 >     * See Hackers Delight, sec 3.2
2012 >     */
2013 >    private static final int tableSizeFor(int c) {
2014 >        int n = c - 1;
2015 >        n |= n >>> 1;
2016 >        n |= n >>> 2;
2017 >        n |= n >>> 4;
2018 >        n |= n >>> 8;
2019 >        n |= n >>> 16;
2020 >        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
2021 >    }
2022 >
2023 >    /**
2024 >     * Initializes table, using the size recorded in sizeCtl.
2025 >     */
2026 >    private final Node[] initTable() {
2027 >        Node[] tab; int sc;
2028 >        while ((tab = table) == null) {
2029 >            if ((sc = sizeCtl) < 0)
2030 >                Thread.yield(); // lost initialization race; just spin
2031 >            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2032 >                try {
2033 >                    if ((tab = table) == null) {
2034 >                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2035 >                        tab = table = new Node[n];
2036 >                        sc = n - (n >>> 2);
2037                      }
2038 +                } finally {
2039 +                    sizeCtl = sc;
2040 +                }
2041 +                break;
2042 +            }
2043 +        }
2044 +        return tab;
2045 +    }
2046 +
2047 +    /**
2048 +     * If table is too small and not already resizing, creates next
2049 +     * table and transfers bins.  Rechecks occupancy after a transfer
2050 +     * to see if another resize is already needed because resizings
2051 +     * are lagging additions.
2052 +     */
2053 +    private final void checkForResize() {
2054 +        Node[] tab; int n, sc;
2055 +        while ((tab = table) != null &&
2056 +               (n = tab.length) < MAXIMUM_CAPACITY &&
2057 +               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
2058 +               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2059 +            try {
2060 +                if (tab == table) {
2061 +                    table = rebuild(tab);
2062 +                    sc = (n << 1) - (n >>> 1);
2063                  }
538                return oldValue;
2064              } finally {
2065 <                unlock();
2065 >                sizeCtl = sc;
2066              }
2067          }
2068 +    }
2069  
2070 <        void clear() {
2071 <            if (count != 0) {
2072 <                lock();
2070 >    /**
2071 >     * Tries to presize table to accommodate the given number of elements.
2072 >     *
2073 >     * @param size number of elements (doesn't need to be perfectly accurate)
2074 >     */
2075 >    private final void tryPresize(int size) {
2076 >        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
2077 >            tableSizeFor(size + (size >>> 1) + 1);
2078 >        int sc;
2079 >        while ((sc = sizeCtl) >= 0) {
2080 >            Node[] tab = table; int n;
2081 >            if (tab == null || (n = tab.length) == 0) {
2082 >                n = (sc > c) ? sc : c;
2083 >                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2084 >                    try {
2085 >                        if (table == tab) {
2086 >                            table = new Node[n];
2087 >                            sc = n - (n >>> 2);
2088 >                        }
2089 >                    } finally {
2090 >                        sizeCtl = sc;
2091 >                    }
2092 >                }
2093 >            }
2094 >            else if (c <= sc || n >= MAXIMUM_CAPACITY)
2095 >                break;
2096 >            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2097                  try {
2098 <                    HashEntry<K,V>[] tab = table;
2099 <                    for (int i = 0; i < tab.length ; i++)
2100 <                        tab[i] = null;
2101 <                    ++modCount;
552 <                    count = 0; // write-volatile
2098 >                    if (table == tab) {
2099 >                        table = rebuild(tab);
2100 >                        sc = (n << 1) - (n >>> 1);
2101 >                    }
2102                  } finally {
2103 <                    unlock();
2103 >                    sizeCtl = sc;
2104 >                }
2105 >            }
2106 >        }
2107 >    }
2108 >
2109 >    /*
2110 >     * Moves and/or copies the nodes in each bin to new table. See
2111 >     * above for explanation.
2112 >     *
2113 >     * @return the new table
2114 >     */
2115 >    private static final Node[] rebuild(Node[] tab) {
2116 >        int n = tab.length;
2117 >        Node[] nextTab = new Node[n << 1];
2118 >        Node fwd = new Node(MOVED, nextTab, null, null);
2119 >        int[] buffer = null;       // holds bins to revisit; null until needed
2120 >        Node rev = null;           // reverse forwarder; null until needed
2121 >        int nbuffered = 0;         // the number of bins in buffer list
2122 >        int bufferIndex = 0;       // buffer index of current buffered bin
2123 >        int bin = n - 1;           // current non-buffered bin or -1 if none
2124 >
2125 >        for (int i = bin;;) {      // start upwards sweep
2126 >            int fh; Node f;
2127 >            if ((f = tabAt(tab, i)) == null) {
2128 >                if (bin >= 0) {    // Unbuffered; no lock needed (or available)
2129 >                    if (!casTabAt(tab, i, f, fwd))
2130 >                        continue;
2131 >                }
2132 >                else {             // transiently use a locked forwarding node
2133 >                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
2134 >                    if (!casTabAt(tab, i, f, g))
2135 >                        continue;
2136 >                    setTabAt(nextTab, i, null);
2137 >                    setTabAt(nextTab, i + n, null);
2138 >                    setTabAt(tab, i, fwd);
2139 >                    if (!g.casHash(MOVED|LOCKED, MOVED)) {
2140 >                        g.hash = MOVED;
2141 >                        synchronized (g) { g.notifyAll(); }
2142 >                    }
2143 >                }
2144 >            }
2145 >            else if ((fh = f.hash) == MOVED) {
2146 >                Object fk = f.key;
2147 >                if (fk instanceof TreeBin) {
2148 >                    TreeBin t = (TreeBin)fk;
2149 >                    boolean validated = false;
2150 >                    t.acquire(0);
2151 >                    try {
2152 >                        if (tabAt(tab, i) == f) {
2153 >                            validated = true;
2154 >                            splitTreeBin(nextTab, i, t);
2155 >                            setTabAt(tab, i, fwd);
2156 >                        }
2157 >                    } finally {
2158 >                        t.release(0);
2159 >                    }
2160 >                    if (!validated)
2161 >                        continue;
2162 >                }
2163 >            }
2164 >            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
2165 >                boolean validated = false;
2166 >                try {              // split to lo and hi lists; copying as needed
2167 >                    if (tabAt(tab, i) == f) {
2168 >                        validated = true;
2169 >                        splitBin(nextTab, i, f);
2170 >                        setTabAt(tab, i, fwd);
2171 >                    }
2172 >                } finally {
2173 >                    if (!f.casHash(fh | LOCKED, fh)) {
2174 >                        f.hash = fh;
2175 >                        synchronized (f) { f.notifyAll(); };
2176 >                    }
2177 >                }
2178 >                if (!validated)
2179 >                    continue;
2180 >            }
2181 >            else {
2182 >                if (buffer == null) // initialize buffer for revisits
2183 >                    buffer = new int[TRANSFER_BUFFER_SIZE];
2184 >                if (bin < 0 && bufferIndex > 0) {
2185 >                    int j = buffer[--bufferIndex];
2186 >                    buffer[bufferIndex] = i;
2187 >                    i = j;         // swap with another bin
2188 >                    continue;
2189 >                }
2190 >                if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) {
2191 >                    f.tryAwaitLock(tab, i);
2192 >                    continue;      // no other options -- block
2193                  }
2194 +                if (rev == null)   // initialize reverse-forwarder
2195 +                    rev = new Node(MOVED, tab, null, null);
2196 +                if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0)
2197 +                    continue;      // recheck before adding to list
2198 +                buffer[nbuffered++] = i;
2199 +                setTabAt(nextTab, i, rev);     // install place-holders
2200 +                setTabAt(nextTab, i + n, rev);
2201 +            }
2202 +
2203 +            if (bin > 0)
2204 +                i = --bin;
2205 +            else if (buffer != null && nbuffered > 0) {
2206 +                bin = -1;
2207 +                i = buffer[bufferIndex = --nbuffered];
2208              }
2209 +            else
2210 +                return nextTab;
2211          }
2212      }
2213  
2214 +    /**
2215 +     * Splits a normal bin with list headed by e into lo and hi parts;
2216 +     * installs in given table.
2217 +     */
2218 +    private static void splitBin(Node[] nextTab, int i, Node e) {
2219 +        int bit = nextTab.length >>> 1; // bit to split on
2220 +        int runBit = e.hash & bit;
2221 +        Node lastRun = e, lo = null, hi = null;
2222 +        for (Node p = e.next; p != null; p = p.next) {
2223 +            int b = p.hash & bit;
2224 +            if (b != runBit) {
2225 +                runBit = b;
2226 +                lastRun = p;
2227 +            }
2228 +        }
2229 +        if (runBit == 0)
2230 +            lo = lastRun;
2231 +        else
2232 +            hi = lastRun;
2233 +        for (Node p = e; p != lastRun; p = p.next) {
2234 +            int ph = p.hash & HASH_BITS;
2235 +            Object pk = p.key, pv = p.val;
2236 +            if ((ph & bit) == 0)
2237 +                lo = new Node(ph, pk, pv, lo);
2238 +            else
2239 +                hi = new Node(ph, pk, pv, hi);
2240 +        }
2241 +        setTabAt(nextTab, i, lo);
2242 +        setTabAt(nextTab, i + bit, hi);
2243 +    }
2244  
2245 +    /**
2246 +     * Splits a tree bin into lo and hi parts; installs in given table.
2247 +     */
2248 +    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2249 +        int bit = nextTab.length >>> 1;
2250 +        TreeBin lt = new TreeBin();
2251 +        TreeBin ht = new TreeBin();
2252 +        int lc = 0, hc = 0;
2253 +        for (Node e = t.first; e != null; e = e.next) {
2254 +            int h = e.hash & HASH_BITS;
2255 +            Object k = e.key, v = e.val;
2256 +            if ((h & bit) == 0) {
2257 +                ++lc;
2258 +                lt.putTreeNode(h, k, v);
2259 +            }
2260 +            else {
2261 +                ++hc;
2262 +                ht.putTreeNode(h, k, v);
2263 +            }
2264 +        }
2265 +        Node ln, hn; // throw away trees if too small
2266 +        if (lc <= (TREE_THRESHOLD >>> 1)) {
2267 +            ln = null;
2268 +            for (Node p = lt.first; p != null; p = p.next)
2269 +                ln = new Node(p.hash, p.key, p.val, ln);
2270 +        }
2271 +        else
2272 +            ln = new Node(MOVED, lt, null, null);
2273 +        setTabAt(nextTab, i, ln);
2274 +        if (hc <= (TREE_THRESHOLD >>> 1)) {
2275 +            hn = null;
2276 +            for (Node p = ht.first; p != null; p = p.next)
2277 +                hn = new Node(p.hash, p.key, p.val, hn);
2278 +        }
2279 +        else
2280 +            hn = new Node(MOVED, ht, null, null);
2281 +        setTabAt(nextTab, i + bit, hn);
2282 +    }
2283  
2284 <    /* ---------------- Public operations -------------- */
2284 >    /**
2285 >     * Implementation for clear. Steps through each bin, removing all
2286 >     * nodes.
2287 >     */
2288 >    private final void internalClear() {
2289 >        long delta = 0L; // negative number of deletions
2290 >        int i = 0;
2291 >        Node[] tab = table;
2292 >        while (tab != null && i < tab.length) {
2293 >            int fh; Object fk;
2294 >            Node f = tabAt(tab, i);
2295 >            if (f == null)
2296 >                ++i;
2297 >            else if ((fh = f.hash) == MOVED) {
2298 >                if ((fk = f.key) instanceof TreeBin) {
2299 >                    TreeBin t = (TreeBin)fk;
2300 >                    t.acquire(0);
2301 >                    try {
2302 >                        if (tabAt(tab, i) == f) {
2303 >                            for (Node p = t.first; p != null; p = p.next) {
2304 >                                if (p.val != null) { // (currently always true)
2305 >                                    p.val = null;
2306 >                                    --delta;
2307 >                                }
2308 >                            }
2309 >                            t.first = null;
2310 >                            t.root = null;
2311 >                            ++i;
2312 >                        }
2313 >                    } finally {
2314 >                        t.release(0);
2315 >                    }
2316 >                }
2317 >                else
2318 >                    tab = (Node[])fk;
2319 >            }
2320 >            else if ((fh & LOCKED) != 0) {
2321 >                counter.add(delta); // opportunistically update count
2322 >                delta = 0L;
2323 >                f.tryAwaitLock(tab, i);
2324 >            }
2325 >            else if (f.casHash(fh, fh | LOCKED)) {
2326 >                try {
2327 >                    if (tabAt(tab, i) == f) {
2328 >                        for (Node e = f; e != null; e = e.next) {
2329 >                            if (e.val != null) {  // (currently always true)
2330 >                                e.val = null;
2331 >                                --delta;
2332 >                            }
2333 >                        }
2334 >                        setTabAt(tab, i, null);
2335 >                        ++i;
2336 >                    }
2337 >                } finally {
2338 >                    if (!f.casHash(fh | LOCKED, fh)) {
2339 >                        f.hash = fh;
2340 >                        synchronized (f) { f.notifyAll(); };
2341 >                    }
2342 >                }
2343 >            }
2344 >        }
2345 >        if (delta != 0)
2346 >            counter.add(delta);
2347 >    }
2348 >
2349 >    /* ----------------Table Traversal -------------- */
2350  
2351      /**
2352 <     * Creates a new, empty map with the specified initial
2353 <     * capacity, load factor and concurrency level.
2352 >     * Encapsulates traversal for methods such as containsValue; also
2353 >     * serves as a base class for other iterators and bulk tasks.
2354       *
2355 <     * @param initialCapacity the initial capacity. The implementation
2356 <     * performs internal sizing to accommodate this many elements.
2357 <     * @param loadFactor  the load factor threshold, used to control resizing.
2358 <     * Resizing may be performed when the average number of elements per
2359 <     * bin exceeds this threshold.
2360 <     * @param concurrencyLevel the estimated number of concurrently
2361 <     * updating threads. The implementation performs internal sizing
2362 <     * to try to accommodate this many threads.
2363 <     * @throws IllegalArgumentException if the initial capacity is
2364 <     * negative or the load factor or concurrencyLevel are
2365 <     * nonpositive.
2366 <     */
2367 <    public ConcurrentHashMap(int initialCapacity,
2368 <                             float loadFactor, int concurrencyLevel) {
2369 <        if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
2370 <            throw new IllegalArgumentException();
2355 >     * At each step, the iterator snapshots the key ("nextKey") and
2356 >     * value ("nextVal") of a valid node (i.e., one that, at point of
2357 >     * snapshot, has a non-null user value). Because val fields can
2358 >     * change (including to null, indicating deletion), field nextVal
2359 >     * might not be accurate at point of use, but still maintains the
2360 >     * weak consistency property of holding a value that was once
2361 >     * valid. To support iterator.remove, the nextKey field is not
2362 >     * updated (nulled out) when the iterator cannot advance.
2363 >     *
2364 >     * Internal traversals directly access these fields, as in:
2365 >     * {@code while (it.advance() != null) { process(it.nextKey); }}
2366 >     *
2367 >     * Exported iterators must track whether the iterator has advanced
2368 >     * (in hasNext vs next) (by setting/checking/nulling field
2369 >     * nextVal), and then extract key, value, or key-value pairs as
2370 >     * return values of next().
2371 >     *
2372 >     * The iterator visits once each still-valid node that was
2373 >     * reachable upon iterator construction. It might miss some that
2374 >     * were added to a bin after the bin was visited, which is OK wrt
2375 >     * consistency guarantees. Maintaining this property in the face
2376 >     * of possible ongoing resizes requires a fair amount of
2377 >     * bookkeeping state that is difficult to optimize away amidst
2378 >     * volatile accesses.  Even so, traversal maintains reasonable
2379 >     * throughput.
2380 >     *
2381 >     * Normally, iteration proceeds bin-by-bin traversing lists.
2382 >     * However, if the table has been resized, then all future steps
2383 >     * must traverse both the bin at the current index as well as at
2384 >     * (index + baseSize); and so on for further resizings. To
2385 >     * paranoically cope with potential sharing by users of iterators
2386 >     * across threads, iteration terminates if a bounds checks fails
2387 >     * for a table read.
2388 >     *
2389 >     * This class extends ForkJoinTask to streamline parallel
2390 >     * iteration in bulk operations (see BulkTask). This adds only an
2391 >     * int of space overhead, which is close enough to negligible in
2392 >     * cases where it is not needed to not worry about it.  Because
2393 >     * ForkJoinTask is Serializable, but iterators need not be, we
2394 >     * need to add warning suppressions.
2395 >     */
2396 >    @SuppressWarnings("serial") static class Traverser<K,V,R> extends ForkJoinTask<R> {
2397 >        final ConcurrentHashMap<K, V> map;
2398 >        Node next;           // the next entry to use
2399 >        Object nextKey;      // cached key field of next
2400 >        Object nextVal;      // cached val field of next
2401 >        Node[] tab;          // current table; updated if resized
2402 >        int index;           // index of bin to use next
2403 >        int baseIndex;       // current index of initial table
2404 >        int baseLimit;       // index bound for initial table
2405 >        int baseSize;        // initial table size
2406 >
2407 >        /** Creates iterator for all entries in the table. */
2408 >        Traverser(ConcurrentHashMap<K, V> map) {
2409 >            this.map = map;
2410 >        }
2411 >
2412 >        /** Creates iterator for split() methods */
2413 >        Traverser(Traverser<K,V,?> it) {
2414 >            ConcurrentHashMap<K, V> m; Node[] t;
2415 >            if ((m = this.map = it.map) == null)
2416 >                t = null;
2417 >            else if ((t = it.tab) == null && // force parent tab initialization
2418 >                     (t = it.tab = m.table) != null)
2419 >                it.baseLimit = it.baseSize = t.length;
2420 >            this.tab = t;
2421 >            this.baseSize = it.baseSize;
2422 >            it.baseLimit = this.index = this.baseIndex =
2423 >                ((this.baseLimit = it.baseLimit) + it.baseIndex + 1) >>> 1;
2424 >        }
2425  
2426 <        if (concurrencyLevel > MAX_SEGMENTS)
2427 <            concurrencyLevel = MAX_SEGMENTS;
2426 >        /**
2427 >         * Advances next; returns nextVal or null if terminated.
2428 >         * See above for explanation.
2429 >         */
2430 >        final Object advance() {
2431 >            Node e = next;
2432 >            Object ev = null;
2433 >            outer: do {
2434 >                if (e != null)                  // advance past used/skipped node
2435 >                    e = e.next;
2436 >                while (e == null) {             // get to next non-null bin
2437 >                    ConcurrentHashMap<K, V> m;
2438 >                    Node[] t; int b, i, n; Object ek; // checks must use locals
2439 >                    if ((t = tab) != null)
2440 >                        n = t.length;
2441 >                    else if ((m = map) != null && (t = tab = m.table) != null)
2442 >                        n = baseLimit = baseSize = t.length;
2443 >                    else
2444 >                        break outer;
2445 >                    if ((b = baseIndex) >= baseLimit ||
2446 >                        (i = index) < 0 || i >= n)
2447 >                        break outer;
2448 >                    if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2449 >                        if ((ek = e.key) instanceof TreeBin)
2450 >                            e = ((TreeBin)ek).first;
2451 >                        else {
2452 >                            tab = (Node[])ek;
2453 >                            continue;           // restarts due to null val
2454 >                        }
2455 >                    }                           // visit upper slots if present
2456 >                    index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2457 >                }
2458 >                nextKey = e.key;
2459 >            } while ((ev = e.val) == null);    // skip deleted or special nodes
2460 >            next = e;
2461 >            return nextVal = ev;
2462 >        }
2463  
2464 <        // Find power-of-two sizes best matching arguments
2465 <        int sshift = 0;
2466 <        int ssize = 1;
2467 <        while (ssize < concurrencyLevel) {
2468 <            ++sshift;
2469 <            ssize <<= 1;
2470 <        }
2471 <        segmentShift = 32 - sshift;
2472 <        segmentMask = ssize - 1;
2473 <        this.segments = Segment.newArray(ssize);
2474 <
2475 <        if (initialCapacity > MAXIMUM_CAPACITY)
2476 <            initialCapacity = MAXIMUM_CAPACITY;
2477 <        int c = initialCapacity / ssize;
2478 <        if (c * ssize < initialCapacity)
2479 <            ++c;
2480 <        int cap = 1;
2481 <        while (cap < c)
606 <            cap <<= 1;
2464 >        public final void remove() {
2465 >            Object k = nextKey;
2466 >            if (k == null && (advance() == null || (k = nextKey) == null))
2467 >                throw new IllegalStateException();
2468 >            map.internalReplace(k, null, null);
2469 >        }
2470 >
2471 >        public final boolean hasNext() {
2472 >            return nextVal != null || advance() != null;
2473 >        }
2474 >
2475 >        public final boolean hasMoreElements() { return hasNext(); }
2476 >        public final void setRawResult(Object x) { }
2477 >        public R getRawResult() { return null; }
2478 >        public boolean exec() { return true; }
2479 >    }
2480 >
2481 >    /* ---------------- Public operations -------------- */
2482  
2483 <        for (int i = 0; i < this.segments.length; ++i)
2484 <            this.segments[i] = new Segment<K,V>(cap, loadFactor);
2483 >    /**
2484 >     * Creates a new, empty map with the default initial table size (16).
2485 >     */
2486 >    public ConcurrentHashMap() {
2487 >        this.counter = new LongAdder();
2488      }
2489  
2490      /**
2491 <     * Creates a new, empty map with the specified initial capacity
2492 <     * and load factor and with the default concurrencyLevel (16).
2491 >     * Creates a new, empty map with an initial table size
2492 >     * accommodating the specified number of elements without the need
2493 >     * to dynamically resize.
2494       *
2495       * @param initialCapacity The implementation performs internal
2496       * sizing to accommodate this many elements.
2497 <     * @param loadFactor  the load factor threshold, used to control resizing.
2498 <     * Resizing may be performed when the average number of elements per
2499 <     * bin exceeds this threshold.
2497 >     * @throws IllegalArgumentException if the initial capacity of
2498 >     * elements is negative
2499 >     */
2500 >    public ConcurrentHashMap(int initialCapacity) {
2501 >        if (initialCapacity < 0)
2502 >            throw new IllegalArgumentException();
2503 >        int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
2504 >                   MAXIMUM_CAPACITY :
2505 >                   tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2506 >        this.counter = new LongAdder();
2507 >        this.sizeCtl = cap;
2508 >    }
2509 >
2510 >    /**
2511 >     * Creates a new map with the same mappings as the given map.
2512 >     *
2513 >     * @param m the map
2514 >     */
2515 >    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
2516 >        this.counter = new LongAdder();
2517 >        this.sizeCtl = DEFAULT_CAPACITY;
2518 >        internalPutAll(m);
2519 >    }
2520 >
2521 >    /**
2522 >     * Creates a new, empty map with an initial table size based on
2523 >     * the given number of elements ({@code initialCapacity}) and
2524 >     * initial table density ({@code loadFactor}).
2525 >     *
2526 >     * @param initialCapacity the initial capacity. The implementation
2527 >     * performs internal sizing to accommodate this many elements,
2528 >     * given the specified load factor.
2529 >     * @param loadFactor the load factor (table density) for
2530 >     * establishing the initial table size
2531       * @throws IllegalArgumentException if the initial capacity of
2532       * elements is negative or the load factor is nonpositive
2533       *
2534       * @since 1.6
2535       */
2536      public ConcurrentHashMap(int initialCapacity, float loadFactor) {
2537 <        this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL);
2537 >        this(initialCapacity, loadFactor, 1);
2538      }
2539  
2540      /**
2541 <     * Creates a new, empty map with the specified initial capacity,
2542 <     * and with default load factor (0.75) and concurrencyLevel (16).
2541 >     * Creates a new, empty map with an initial table size based on
2542 >     * the given number of elements ({@code initialCapacity}), table
2543 >     * density ({@code loadFactor}), and number of concurrently
2544 >     * updating threads ({@code concurrencyLevel}).
2545       *
2546       * @param initialCapacity the initial capacity. The implementation
2547 <     * performs internal sizing to accommodate this many elements.
2548 <     * @throws IllegalArgumentException if the initial capacity of
2549 <     * elements is negative.
2547 >     * performs internal sizing to accommodate this many elements,
2548 >     * given the specified load factor.
2549 >     * @param loadFactor the load factor (table density) for
2550 >     * establishing the initial table size
2551 >     * @param concurrencyLevel the estimated number of concurrently
2552 >     * updating threads. The implementation may use this value as
2553 >     * a sizing hint.
2554 >     * @throws IllegalArgumentException if the initial capacity is
2555 >     * negative or the load factor or concurrencyLevel are
2556 >     * nonpositive
2557       */
2558 <    public ConcurrentHashMap(int initialCapacity) {
2559 <        this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
2558 >    public ConcurrentHashMap(int initialCapacity,
2559 >                               float loadFactor, int concurrencyLevel) {
2560 >        if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
2561 >            throw new IllegalArgumentException();
2562 >        if (initialCapacity < concurrencyLevel)   // Use at least as many bins
2563 >            initialCapacity = concurrencyLevel;   // as estimated threads
2564 >        long size = (long)(1.0 + (long)initialCapacity / loadFactor);
2565 >        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
2566 >            MAXIMUM_CAPACITY : tableSizeFor((int)size);
2567 >        this.counter = new LongAdder();
2568 >        this.sizeCtl = cap;
2569      }
2570  
2571      /**
2572 <     * Creates a new, empty map with a default initial capacity (16),
2573 <     * load factor (0.75) and concurrencyLevel (16).
2572 >     * Creates a new {@link Set} backed by a ConcurrentHashMap
2573 >     * from the given type to {@code Boolean.TRUE}.
2574 >     *
2575 >     * @return the new set
2576       */
2577 <    public ConcurrentHashMap() {
2578 <        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
2577 >    public static <K> KeySetView<K,Boolean> newKeySet() {
2578 >        return new KeySetView<K,Boolean>(new ConcurrentHashMap<K,Boolean>(),
2579 >                                      Boolean.TRUE);
2580      }
2581  
2582      /**
2583 <     * Creates a new map with the same mappings as the given map.
2584 <     * The map is created with a capacity of 1.5 times the number
654 <     * of mappings in the given map or 16 (whichever is greater),
655 <     * and a default load factor (0.75) and concurrencyLevel (16).
2583 >     * Creates a new {@link Set} backed by a ConcurrentHashMap
2584 >     * from the given type to {@code Boolean.TRUE}.
2585       *
2586 <     * @param m the map
2586 >     * @param initialCapacity The implementation performs internal
2587 >     * sizing to accommodate this many elements.
2588 >     * @throws IllegalArgumentException if the initial capacity of
2589 >     * elements is negative
2590 >     * @return the new set
2591       */
2592 <    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
2593 <        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
2594 <                      DEFAULT_INITIAL_CAPACITY),
662 <             DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
663 <        putAll(m);
2592 >    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2593 >        return new KeySetView<K,Boolean>(new ConcurrentHashMap<K,Boolean>(initialCapacity),
2594 >                                      Boolean.TRUE);
2595      }
2596  
2597      /**
2598 <     * Returns <tt>true</tt> if this map contains no key-value mappings.
668 <     *
669 <     * @return <tt>true</tt> if this map contains no key-value mappings
2598 >     * {@inheritDoc}
2599       */
2600      public boolean isEmpty() {
2601 <        final Segment<K,V>[] segments = this.segments;
673 <        /*
674 <         * We keep track of per-segment modCounts to avoid ABA
675 <         * problems in which an element in one segment was added and
676 <         * in another removed during traversal, in which case the
677 <         * table was never actually empty at any point. Note the
678 <         * similar use of modCounts in the size() and containsValue()
679 <         * methods, which are the only other methods also susceptible
680 <         * to ABA problems.
681 <         */
682 <        int[] mc = new int[segments.length];
683 <        int mcsum = 0;
684 <        for (int i = 0; i < segments.length; ++i) {
685 <            if (segments[i].count != 0)
686 <                return false;
687 <            else
688 <                mcsum += mc[i] = segments[i].modCount;
689 <        }
690 <        // If mcsum happens to be zero, then we know we got a snapshot
691 <        // before any modifications at all were made.  This is
692 <        // probably common enough to bother tracking.
693 <        if (mcsum != 0) {
694 <            for (int i = 0; i < segments.length; ++i) {
695 <                if (segments[i].count != 0 ||
696 <                    mc[i] != segments[i].modCount)
697 <                    return false;
698 <            }
699 <        }
700 <        return true;
2601 >        return counter.sum() <= 0L; // ignore transient negative values
2602      }
2603  
2604      /**
2605 <     * Returns the number of key-value mappings in this map.  If the
705 <     * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
706 <     * <tt>Integer.MAX_VALUE</tt>.
707 <     *
708 <     * @return the number of key-value mappings in this map
2605 >     * {@inheritDoc}
2606       */
2607      public int size() {
2608 <        final Segment<K,V>[] segments = this.segments;
2609 <        long sum = 0;
2610 <        long check = 0;
2611 <        int[] mc = new int[segments.length];
2612 <        // Try a few times to get accurate count. On failure due to
2613 <        // continuous async changes in table, resort to locking.
2614 <        for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) {
2615 <            check = 0;
2616 <            sum = 0;
2617 <            int mcsum = 0;
2618 <            for (int i = 0; i < segments.length; ++i) {
2619 <                sum += segments[i].count;
2620 <                mcsum += mc[i] = segments[i].modCount;
2621 <            }
2622 <            if (mcsum != 0) {
2623 <                for (int i = 0; i < segments.length; ++i) {
2624 <                    check += segments[i].count;
2625 <                    if (mc[i] != segments[i].modCount) {
729 <                        check = -1; // force retry
730 <                        break;
731 <                    }
732 <                }
733 <            }
734 <            if (check == sum)
735 <                break;
736 <        }
737 <        if (check != sum) { // Resort to locking all segments
738 <            sum = 0;
739 <            for (int i = 0; i < segments.length; ++i)
740 <                segments[i].lock();
741 <            for (int i = 0; i < segments.length; ++i)
742 <                sum += segments[i].count;
743 <            for (int i = 0; i < segments.length; ++i)
744 <                segments[i].unlock();
745 <        }
746 <        if (sum > Integer.MAX_VALUE)
747 <            return Integer.MAX_VALUE;
748 <        else
749 <            return (int)sum;
2608 >        long n = counter.sum();
2609 >        return ((n < 0L) ? 0 :
2610 >                (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
2611 >                (int)n);
2612 >    }
2613 >
2614 >    /**
2615 >     * Returns the number of mappings. This method should be used
2616 >     * instead of {@link #size} because a ConcurrentHashMap may
2617 >     * contain more mappings than can be represented as an int. The
2618 >     * value returned is a snapshot; the actual count may differ if
2619 >     * there are ongoing concurrent insertions or removals.
2620 >     *
2621 >     * @return the number of mappings
2622 >     */
2623 >    public long mappingCount() {
2624 >        long n = counter.sum();
2625 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2626      }
2627  
2628      /**
2629 <     * Returns the value to which this map maps the specified key, or
2630 <     * <tt>null</tt> if the map contains no mapping for the key.
2629 >     * Returns the value to which the specified key is mapped,
2630 >     * or {@code null} if this map contains no mapping for the key.
2631 >     *
2632 >     * <p>More formally, if this map contains a mapping from a key
2633 >     * {@code k} to a value {@code v} such that {@code key.equals(k)},
2634 >     * then this method returns {@code v}; otherwise it returns
2635 >     * {@code null}.  (There can be at most one such mapping.)
2636       *
756     * @param key key whose associated value is to be returned
757     * @return the value to which this map maps the specified key, or
758     *         <tt>null</tt> if the map contains no mapping for the key
2637       * @throws NullPointerException if the specified key is null
2638       */
2639 <    public V get(Object key) {
2640 <        int hash = hash(key); // throws NullPointerException if key null
2641 <        return segmentFor(hash).get(key, hash);
2639 >    @SuppressWarnings("unchecked") public V get(Object key) {
2640 >        if (key == null)
2641 >            throw new NullPointerException();
2642 >        return (V)internalGet(key);
2643 >    }
2644 >
2645 >    /**
2646 >     * Returns the value to which the specified key is mapped,
2647 >     * or the given defaultValue if this map contains no mapping for the key.
2648 >     *
2649 >     * @param key the key
2650 >     * @param defaultValue the value to return if this map contains
2651 >     * no mapping for the given key
2652 >     * @return the mapping for the key, if present; else the defaultValue
2653 >     * @throws NullPointerException if the specified key is null
2654 >     */
2655 >    @SuppressWarnings("unchecked") public V getValueOrDefault(Object key, V defaultValue) {
2656 >        if (key == null)
2657 >            throw new NullPointerException();
2658 >        V v = (V) internalGet(key);
2659 >        return v == null ? defaultValue : v;
2660      }
2661  
2662      /**
2663       * Tests if the specified object is a key in this table.
2664       *
2665       * @param  key   possible key
2666 <     * @return <tt>true</tt> if and only if the specified object
2666 >     * @return {@code true} if and only if the specified object
2667       *         is a key in this table, as determined by the
2668 <     *         <tt>equals</tt> method; <tt>false</tt> otherwise.
2668 >     *         {@code equals} method; {@code false} otherwise
2669       * @throws NullPointerException if the specified key is null
2670       */
2671      public boolean containsKey(Object key) {
2672 <        int hash = hash(key); // throws NullPointerException if key null
2673 <        return segmentFor(hash).containsKey(key, hash);
2672 >        if (key == null)
2673 >            throw new NullPointerException();
2674 >        return internalGet(key) != null;
2675      }
2676  
2677      /**
2678 <     * Returns <tt>true</tt> if this map maps one or more keys to the
2679 <     * specified value. Note: This method requires a full internal
2680 <     * traversal of the hash table, and so is much slower than
784 <     * method <tt>containsKey</tt>.
2678 >     * Returns {@code true} if this map maps one or more keys to the
2679 >     * specified value. Note: This method may require a full traversal
2680 >     * of the map, and is much slower than method {@code containsKey}.
2681       *
2682       * @param value value whose presence in this map is to be tested
2683 <     * @return <tt>true</tt> if this map maps one or more keys to the
2683 >     * @return {@code true} if this map maps one or more keys to the
2684       *         specified value
2685       * @throws NullPointerException if the specified value is null
2686       */
2687      public boolean containsValue(Object value) {
2688          if (value == null)
2689              throw new NullPointerException();
2690 <
2691 <        // See explanation of modCount use above
2692 <
2693 <        final Segment<K,V>[] segments = this.segments;
2694 <        int[] mc = new int[segments.length];
799 <
800 <        // Try a few times without locking
801 <        for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) {
802 <            int sum = 0;
803 <            int mcsum = 0;
804 <            for (int i = 0; i < segments.length; ++i) {
805 <                int c = segments[i].count;
806 <                mcsum += mc[i] = segments[i].modCount;
807 <                if (segments[i].containsValue(value))
808 <                    return true;
809 <            }
810 <            boolean cleanSweep = true;
811 <            if (mcsum != 0) {
812 <                for (int i = 0; i < segments.length; ++i) {
813 <                    int c = segments[i].count;
814 <                    if (mc[i] != segments[i].modCount) {
815 <                        cleanSweep = false;
816 <                        break;
817 <                    }
818 <                }
819 <            }
820 <            if (cleanSweep)
821 <                return false;
2690 >        Object v;
2691 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2692 >        while ((v = it.advance()) != null) {
2693 >            if (v == value || value.equals(v))
2694 >                return true;
2695          }
2696 <        // Resort to locking all segments
824 <        for (int i = 0; i < segments.length; ++i)
825 <            segments[i].lock();
826 <        boolean found = false;
827 <        try {
828 <            for (int i = 0; i < segments.length; ++i) {
829 <                if (segments[i].containsValue(value)) {
830 <                    found = true;
831 <                    break;
832 <                }
833 <            }
834 <        } finally {
835 <            for (int i = 0; i < segments.length; ++i)
836 <                segments[i].unlock();
837 <        }
838 <        return found;
2696 >        return false;
2697      }
2698  
2699      /**
# Line 845 | Line 2703 | public class ConcurrentHashMap<K, V> ext
2703       * full compatibility with class {@link java.util.Hashtable},
2704       * which supported this method prior to introduction of the
2705       * Java Collections framework.
2706 <
2706 >     *
2707       * @param  value a value to search for
2708 <     * @return <tt>true</tt> if and only if some key maps to the
2709 <     *         <tt>value</tt> argument in this table as
2710 <     *         determined by the <tt>equals</tt> method;
2711 <     *         <tt>false</tt> otherwise
2708 >     * @return {@code true} if and only if some key maps to the
2709 >     *         {@code value} argument in this table as
2710 >     *         determined by the {@code equals} method;
2711 >     *         {@code false} otherwise
2712       * @throws NullPointerException if the specified value is null
2713       */
2714      public boolean contains(Object value) {
# Line 861 | Line 2719 | public class ConcurrentHashMap<K, V> ext
2719       * Maps the specified key to the specified value in this table.
2720       * Neither the key nor the value can be null.
2721       *
2722 <     * <p> The value can be retrieved by calling the <tt>get</tt> method
2722 >     * <p> The value can be retrieved by calling the {@code get} method
2723       * with a key that is equal to the original key.
2724       *
2725       * @param key key with which the specified value is to be associated
2726       * @param value value to be associated with the specified key
2727 <     * @return the previous value associated with <tt>key</tt>, or
2728 <     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
2727 >     * @return the previous value associated with {@code key}, or
2728 >     *         {@code null} if there was no mapping for {@code key}
2729       * @throws NullPointerException if the specified key or value is null
2730       */
2731 <    public V put(K key, V value) {
2732 <        if (value == null)
2731 >    @SuppressWarnings("unchecked") public V put(K key, V value) {
2732 >        if (key == null || value == null)
2733              throw new NullPointerException();
2734 <        int hash = hash(key);
877 <        return segmentFor(hash).put(key, hash, value, false);
2734 >        return (V)internalPut(key, value);
2735      }
2736  
2737      /**
2738       * {@inheritDoc}
2739       *
2740       * @return the previous value associated with the specified key,
2741 <     *         or <tt>null</tt> if there was no mapping for the key
2741 >     *         or {@code null} if there was no mapping for the key
2742       * @throws NullPointerException if the specified key or value is null
2743       */
2744 <    public V putIfAbsent(K key, V value) {
2745 <        if (value == null)
2744 >    @SuppressWarnings("unchecked") public V putIfAbsent(K key, V value) {
2745 >        if (key == null || value == null)
2746              throw new NullPointerException();
2747 <        int hash = hash(key);
891 <        return segmentFor(hash).put(key, hash, value, true);
2747 >        return (V)internalPutIfAbsent(key, value);
2748      }
2749  
2750      /**
# Line 899 | Line 2755 | public class ConcurrentHashMap<K, V> ext
2755       * @param m mappings to be stored in this map
2756       */
2757      public void putAll(Map<? extends K, ? extends V> m) {
2758 <        for (Iterator<? extends Map.Entry<? extends K, ? extends V>> it = (Iterator<? extends Map.Entry<? extends K, ? extends V>>) m.entrySet().iterator(); it.hasNext(); ) {
2759 <            Entry<? extends K, ? extends V> e = it.next();
2760 <            put(e.getKey(), e.getValue());
2761 <        }
2758 >        internalPutAll(m);
2759 >    }
2760 >
2761 >    /**
2762 >     * If the specified key is not already associated with a value,
2763 >     * computes its value using the given mappingFunction and enters
2764 >     * it into the map unless null.  This is equivalent to
2765 >     * <pre> {@code
2766 >     * if (map.containsKey(key))
2767 >     *   return map.get(key);
2768 >     * value = mappingFunction.apply(key);
2769 >     * if (value != null)
2770 >     *   map.put(key, value);
2771 >     * return value;}</pre>
2772 >     *
2773 >     * except that the action is performed atomically.  If the
2774 >     * function returns {@code null} no mapping is recorded. If the
2775 >     * function itself throws an (unchecked) exception, the exception
2776 >     * is rethrown to its caller, and no mapping is recorded.  Some
2777 >     * attempted update operations on this map by other threads may be
2778 >     * blocked while computation is in progress, so the computation
2779 >     * should be short and simple, and must not attempt to update any
2780 >     * other mappings of this Map. The most appropriate usage is to
2781 >     * construct a new object serving as an initial mapped value, or
2782 >     * memoized result, as in:
2783 >     *
2784 >     *  <pre> {@code
2785 >     * map.computeIfAbsent(key, new Fun<K, V>() {
2786 >     *   public V map(K k) { return new Value(f(k)); }});}</pre>
2787 >     *
2788 >     * @param key key with which the specified value is to be associated
2789 >     * @param mappingFunction the function to compute a value
2790 >     * @return the current (existing or computed) value associated with
2791 >     *         the specified key, or null if the computed value is null
2792 >     * @throws NullPointerException if the specified key or mappingFunction
2793 >     *         is null
2794 >     * @throws IllegalStateException if the computation detectably
2795 >     *         attempts a recursive update to this map that would
2796 >     *         otherwise never complete
2797 >     * @throws RuntimeException or Error if the mappingFunction does so,
2798 >     *         in which case the mapping is left unestablished
2799 >     */
2800 >    @SuppressWarnings("unchecked") public V computeIfAbsent
2801 >        (K key, Fun<? super K, ? extends V> mappingFunction) {
2802 >        if (key == null || mappingFunction == null)
2803 >            throw new NullPointerException();
2804 >        return (V)internalComputeIfAbsent(key, mappingFunction);
2805 >    }
2806 >
2807 >    /**
2808 >     * If the given key is present, computes a new mapping value given a key and
2809 >     * its current mapped value. This is equivalent to
2810 >     *  <pre> {@code
2811 >     *   if (map.containsKey(key)) {
2812 >     *     value = remappingFunction.apply(key, map.get(key));
2813 >     *     if (value != null)
2814 >     *       map.put(key, value);
2815 >     *     else
2816 >     *       map.remove(key);
2817 >     *   }
2818 >     * }</pre>
2819 >     *
2820 >     * except that the action is performed atomically.  If the
2821 >     * function returns {@code null}, the mapping is removed.  If the
2822 >     * function itself throws an (unchecked) exception, the exception
2823 >     * is rethrown to its caller, and the current mapping is left
2824 >     * unchanged.  Some attempted update operations on this map by
2825 >     * other threads may be blocked while computation is in progress,
2826 >     * so the computation should be short and simple, and must not
2827 >     * attempt to update any other mappings of this Map. For example,
2828 >     * to either create or append new messages to a value mapping:
2829 >     *
2830 >     * @param key key with which the specified value is to be associated
2831 >     * @param remappingFunction the function to compute a value
2832 >     * @return the new value associated with the specified key, or null if none
2833 >     * @throws NullPointerException if the specified key or remappingFunction
2834 >     *         is null
2835 >     * @throws IllegalStateException if the computation detectably
2836 >     *         attempts a recursive update to this map that would
2837 >     *         otherwise never complete
2838 >     * @throws RuntimeException or Error if the remappingFunction does so,
2839 >     *         in which case the mapping is unchanged
2840 >     */
2841 >    @SuppressWarnings("unchecked") public V computeIfPresent
2842 >        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2843 >        if (key == null || remappingFunction == null)
2844 >            throw new NullPointerException();
2845 >        return (V)internalCompute(key, true, remappingFunction);
2846 >    }
2847 >
2848 >    /**
2849 >     * Computes a new mapping value given a key and
2850 >     * its current mapped value (or {@code null} if there is no current
2851 >     * mapping). This is equivalent to
2852 >     *  <pre> {@code
2853 >     *   value = remappingFunction.apply(key, map.get(key));
2854 >     *   if (value != null)
2855 >     *     map.put(key, value);
2856 >     *   else
2857 >     *     map.remove(key);
2858 >     * }</pre>
2859 >     *
2860 >     * except that the action is performed atomically.  If the
2861 >     * function returns {@code null}, the mapping is removed.  If the
2862 >     * function itself throws an (unchecked) exception, the exception
2863 >     * is rethrown to its caller, and the current mapping is left
2864 >     * unchanged.  Some attempted update operations on this map by
2865 >     * other threads may be blocked while computation is in progress,
2866 >     * so the computation should be short and simple, and must not
2867 >     * attempt to update any other mappings of this Map. For example,
2868 >     * to either create or append new messages to a value mapping:
2869 >     *
2870 >     * <pre> {@code
2871 >     * Map<Key, String> map = ...;
2872 >     * final String msg = ...;
2873 >     * map.compute(key, new BiFun<Key, String, String>() {
2874 >     *   public String apply(Key k, String v) {
2875 >     *    return (v == null) ? msg : v + msg;});}}</pre>
2876 >     *
2877 >     * @param key key with which the specified value is to be associated
2878 >     * @param remappingFunction the function to compute a value
2879 >     * @return the new value associated with the specified key, or null if none
2880 >     * @throws NullPointerException if the specified key or remappingFunction
2881 >     *         is null
2882 >     * @throws IllegalStateException if the computation detectably
2883 >     *         attempts a recursive update to this map that would
2884 >     *         otherwise never complete
2885 >     * @throws RuntimeException or Error if the remappingFunction does so,
2886 >     *         in which case the mapping is unchanged
2887 >     */
2888 >    @SuppressWarnings("unchecked") public V compute
2889 >        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2890 >        if (key == null || remappingFunction == null)
2891 >            throw new NullPointerException();
2892 >        return (V)internalCompute(key, false, remappingFunction);
2893 >    }
2894 >
2895 >    /**
2896 >     * If the specified key is not already associated
2897 >     * with a value, associate it with the given value.
2898 >     * Otherwise, replace the value with the results of
2899 >     * the given remapping function. This is equivalent to:
2900 >     *  <pre> {@code
2901 >     *   if (!map.containsKey(key))
2902 >     *     map.put(value);
2903 >     *   else {
2904 >     *     newValue = remappingFunction.apply(map.get(key), value);
2905 >     *     if (value != null)
2906 >     *       map.put(key, value);
2907 >     *     else
2908 >     *       map.remove(key);
2909 >     *   }
2910 >     * }</pre>
2911 >     * except that the action is performed atomically.  If the
2912 >     * function returns {@code null}, the mapping is removed.  If the
2913 >     * function itself throws an (unchecked) exception, the exception
2914 >     * is rethrown to its caller, and the current mapping is left
2915 >     * unchanged.  Some attempted update operations on this map by
2916 >     * other threads may be blocked while computation is in progress,
2917 >     * so the computation should be short and simple, and must not
2918 >     * attempt to update any other mappings of this Map.
2919 >     */
2920 >    @SuppressWarnings("unchecked") public V merge
2921 >        (K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2922 >        if (key == null || value == null || remappingFunction == null)
2923 >            throw new NullPointerException();
2924 >        return (V)internalMerge(key, value, remappingFunction);
2925      }
2926  
2927      /**
# Line 910 | Line 2929 | public class ConcurrentHashMap<K, V> ext
2929       * This method does nothing if the key is not in the map.
2930       *
2931       * @param  key the key that needs to be removed
2932 <     * @return the previous value associated with <tt>key</tt>, or
2933 <     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
2932 >     * @return the previous value associated with {@code key}, or
2933 >     *         {@code null} if there was no mapping for {@code key}
2934       * @throws NullPointerException if the specified key is null
2935       */
2936 <    public V remove(Object key) {
2937 <        int hash = hash(key);
2938 <        return segmentFor(hash).remove(key, hash, null);
2936 >    @SuppressWarnings("unchecked") public V remove(Object key) {
2937 >        if (key == null)
2938 >            throw new NullPointerException();
2939 >        return (V)internalReplace(key, null, null);
2940      }
2941  
2942      /**
# Line 925 | Line 2945 | public class ConcurrentHashMap<K, V> ext
2945       * @throws NullPointerException if the specified key is null
2946       */
2947      public boolean remove(Object key, Object value) {
2948 +        if (key == null)
2949 +            throw new NullPointerException();
2950          if (value == null)
2951              return false;
2952 <        int hash = hash(key);
931 <        return segmentFor(hash).remove(key, hash, value) != null;
2952 >        return internalReplace(key, null, value) != null;
2953      }
2954  
2955      /**
# Line 937 | Line 2958 | public class ConcurrentHashMap<K, V> ext
2958       * @throws NullPointerException if any of the arguments are null
2959       */
2960      public boolean replace(K key, V oldValue, V newValue) {
2961 <        if (oldValue == null || newValue == null)
2961 >        if (key == null || oldValue == null || newValue == null)
2962              throw new NullPointerException();
2963 <        int hash = hash(key);
943 <        return segmentFor(hash).replace(key, hash, oldValue, newValue);
2963 >        return internalReplace(key, newValue, oldValue) != null;
2964      }
2965  
2966      /**
2967       * {@inheritDoc}
2968       *
2969       * @return the previous value associated with the specified key,
2970 <     *         or <tt>null</tt> if there was no mapping for the key
2970 >     *         or {@code null} if there was no mapping for the key
2971       * @throws NullPointerException if the specified key or value is null
2972       */
2973 <    public V replace(K key, V value) {
2974 <        if (value == null)
2973 >    @SuppressWarnings("unchecked") public V replace(K key, V value) {
2974 >        if (key == null || value == null)
2975              throw new NullPointerException();
2976 <        int hash = hash(key);
957 <        return segmentFor(hash).replace(key, hash, value);
2976 >        return (V)internalReplace(key, value, null);
2977      }
2978  
2979      /**
2980       * Removes all of the mappings from this map.
2981       */
2982      public void clear() {
2983 <        for (int i = 0; i < segments.length; ++i)
965 <            segments[i].clear();
2983 >        internalClear();
2984      }
2985  
2986      /**
2987       * Returns a {@link Set} view of the keys contained in this map.
2988       * The set is backed by the map, so changes to the map are
2989 <     * reflected in the set, and vice-versa.  The set supports element
972 <     * removal, which removes the corresponding mapping from this map,
973 <     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
974 <     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
975 <     * operations.  It does not support the <tt>add</tt> or
976 <     * <tt>addAll</tt> operations.
2989 >     * reflected in the set, and vice-versa.
2990       *
2991 <     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
2992 <     * that will never throw {@link ConcurrentModificationException},
2993 <     * and guarantees to traverse elements as they existed upon
2994 <     * construction of the iterator, and may (but is not guaranteed to)
2995 <     * reflect any modifications subsequent to construction.
2991 >     * @return the set view
2992 >     */
2993 >    public KeySetView<K,V> keySet() {
2994 >        KeySetView<K,V> ks = keySet;
2995 >        return (ks != null) ? ks : (keySet = new KeySetView<K,V>(this, null));
2996 >    }
2997 >
2998 >    /**
2999 >     * Returns a {@link Set} view of the keys in this map, using the
3000 >     * given common mapped value for any additions (i.e., {@link
3001 >     * Collection#add} and {@link Collection#addAll}). This is of
3002 >     * course only appropriate if it is acceptable to use the same
3003 >     * value for all additions from this view.
3004 >     *
3005 >     * @param mappedValue the mapped value to use for any
3006 >     * additions.
3007 >     * @return the set view
3008 >     * @throws NullPointerException if the mappedValue is null
3009       */
3010 <    public Set<K> keySet() {
3011 <        Set<K> ks = keySet;
3012 <        return (ks != null) ? ks : (keySet = new KeySet());
3010 >    public KeySetView<K,V> keySet(V mappedValue) {
3011 >        if (mappedValue == null)
3012 >            throw new NullPointerException();
3013 >        return new KeySetView<K,V>(this, mappedValue);
3014      }
3015  
3016      /**
3017       * Returns a {@link Collection} view of the values contained in this map.
3018       * The collection is backed by the map, so changes to the map are
3019 <     * reflected in the collection, and vice-versa.  The collection
993 <     * supports element removal, which removes the corresponding
994 <     * mapping from this map, via the <tt>Iterator.remove</tt>,
995 <     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
996 <     * <tt>retainAll</tt>, and <tt>clear</tt> operations.  It does not
997 <     * support the <tt>add</tt> or <tt>addAll</tt> operations.
998 <     *
999 <     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
1000 <     * that will never throw {@link ConcurrentModificationException},
1001 <     * and guarantees to traverse elements as they existed upon
1002 <     * construction of the iterator, and may (but is not guaranteed to)
1003 <     * reflect any modifications subsequent to construction.
3019 >     * reflected in the collection, and vice-versa.
3020       */
3021 <    public Collection<V> values() {
3022 <        Collection<V> vs = values;
3023 <        return (vs != null) ? vs : (values = new Values());
3021 >    public ValuesView<K,V> values() {
3022 >        ValuesView<K,V> vs = values;
3023 >        return (vs != null) ? vs : (values = new ValuesView<K,V>(this));
3024      }
3025  
3026      /**
# Line 1012 | Line 3028 | public class ConcurrentHashMap<K, V> ext
3028       * The set is backed by the map, so changes to the map are
3029       * reflected in the set, and vice-versa.  The set supports element
3030       * removal, which removes the corresponding mapping from the map,
3031 <     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
3032 <     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
3033 <     * operations.  It does not support the <tt>add</tt> or
3034 <     * <tt>addAll</tt> operations.
3031 >     * via the {@code Iterator.remove}, {@code Set.remove},
3032 >     * {@code removeAll}, {@code retainAll}, and {@code clear}
3033 >     * operations.  It does not support the {@code add} or
3034 >     * {@code addAll} operations.
3035       *
3036 <     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
3036 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
3037       * that will never throw {@link ConcurrentModificationException},
3038       * and guarantees to traverse elements as they existed upon
3039       * construction of the iterator, and may (but is not guaranteed to)
3040       * reflect any modifications subsequent to construction.
3041       */
3042      public Set<Map.Entry<K,V>> entrySet() {
3043 <        Set<Map.Entry<K,V>> es = entrySet;
3044 <        return (es != null) ? es : (entrySet = new EntrySet());
3043 >        EntrySetView<K,V> es = entrySet;
3044 >        return (es != null) ? es : (entrySet = new EntrySetView<K,V>(this));
3045      }
3046  
3047      /**
3048       * Returns an enumeration of the keys in this table.
3049       *
3050       * @return an enumeration of the keys in this table
3051 <     * @see #keySet
3051 >     * @see #keySet()
3052       */
3053      public Enumeration<K> keys() {
3054 <        return new KeyIterator();
3054 >        return new KeyIterator<K,V>(this);
3055      }
3056  
3057      /**
3058       * Returns an enumeration of the values in this table.
3059       *
3060       * @return an enumeration of the values in this table
3061 <     * @see #values
3061 >     * @see #values()
3062       */
3063      public Enumeration<V> elements() {
3064 <        return new ValueIterator();
3064 >        return new ValueIterator<K,V>(this);
3065      }
3066  
3067 <    /* ---------------- Iterator Support -------------- */
3068 <
3069 <    abstract class HashIterator {
3070 <        int nextSegmentIndex;
3071 <        int nextTableIndex;
3072 <        HashEntry<K,V>[] currentTable;
3073 <        HashEntry<K, V> nextEntry;
3074 <        HashEntry<K, V> lastReturned;
3067 >    /**
3068 >     * Returns a partitionable iterator of the keys in this map.
3069 >     *
3070 >     * @return a partitionable iterator of the keys in this map
3071 >     */
3072 >    public Spliterator<K> keySpliterator() {
3073 >        return new KeyIterator<K,V>(this);
3074 >    }
3075  
3076 <        HashIterator() {
3077 <            nextSegmentIndex = segments.length - 1;
3078 <            nextTableIndex = -1;
3079 <            advance();
3080 <        }
3076 >    /**
3077 >     * Returns a partitionable iterator of the values in this map.
3078 >     *
3079 >     * @return a partitionable iterator of the values in this map
3080 >     */
3081 >    public Spliterator<V> valueSpliterator() {
3082 >        return new ValueIterator<K,V>(this);
3083 >    }
3084  
3085 <        public boolean hasMoreElements() { return hasNext(); }
3085 >    /**
3086 >     * Returns a partitionable iterator of the entries in this map.
3087 >     *
3088 >     * @return a partitionable iterator of the entries in this map
3089 >     */
3090 >    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
3091 >        return new EntryIterator<K,V>(this);
3092 >    }
3093  
3094 <        final void advance() {
3095 <            if (nextEntry != null && (nextEntry = nextEntry.next) != null)
3096 <                return;
3094 >    /**
3095 >     * Returns the hash code value for this {@link Map}, i.e.,
3096 >     * the sum of, for each key-value pair in the map,
3097 >     * {@code key.hashCode() ^ value.hashCode()}.
3098 >     *
3099 >     * @return the hash code value for this map
3100 >     */
3101 >    public int hashCode() {
3102 >        int h = 0;
3103 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3104 >        Object v;
3105 >        while ((v = it.advance()) != null) {
3106 >            h += it.nextKey.hashCode() ^ v.hashCode();
3107 >        }
3108 >        return h;
3109 >    }
3110  
3111 <            while (nextTableIndex >= 0) {
3112 <                if ( (nextEntry = currentTable[nextTableIndex--]) != null)
3113 <                    return;
3111 >    /**
3112 >     * Returns a string representation of this map.  The string
3113 >     * representation consists of a list of key-value mappings (in no
3114 >     * particular order) enclosed in braces ("{@code {}}").  Adjacent
3115 >     * mappings are separated by the characters {@code ", "} (comma
3116 >     * and space).  Each key-value mapping is rendered as the key
3117 >     * followed by an equals sign ("{@code =}") followed by the
3118 >     * associated value.
3119 >     *
3120 >     * @return a string representation of this map
3121 >     */
3122 >    public String toString() {
3123 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3124 >        StringBuilder sb = new StringBuilder();
3125 >        sb.append('{');
3126 >        Object v;
3127 >        if ((v = it.advance()) != null) {
3128 >            for (;;) {
3129 >                Object k = it.nextKey;
3130 >                sb.append(k == this ? "(this Map)" : k);
3131 >                sb.append('=');
3132 >                sb.append(v == this ? "(this Map)" : v);
3133 >                if ((v = it.advance()) == null)
3134 >                    break;
3135 >                sb.append(',').append(' ');
3136              }
3137 +        }
3138 +        return sb.append('}').toString();
3139 +    }
3140  
3141 <            while (nextSegmentIndex >= 0) {
3142 <                Segment<K,V> seg = segments[nextSegmentIndex--];
3143 <                if (seg.count != 0) {
3144 <                    currentTable = seg.table;
3145 <                    for (int j = currentTable.length - 1; j >= 0; --j) {
3146 <                        if ( (nextEntry = currentTable[j]) != null) {
3147 <                            nextTableIndex = j - 1;
3148 <                            return;
3149 <                        }
3150 <                    }
3151 <                }
3141 >    /**
3142 >     * Compares the specified object with this map for equality.
3143 >     * Returns {@code true} if the given object is a map with the same
3144 >     * mappings as this map.  This operation may return misleading
3145 >     * results if either map is concurrently modified during execution
3146 >     * of this method.
3147 >     *
3148 >     * @param o object to be compared for equality with this map
3149 >     * @return {@code true} if the specified object is equal to this map
3150 >     */
3151 >    public boolean equals(Object o) {
3152 >        if (o != this) {
3153 >            if (!(o instanceof Map))
3154 >                return false;
3155 >            Map<?,?> m = (Map<?,?>) o;
3156 >            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3157 >            Object val;
3158 >            while ((val = it.advance()) != null) {
3159 >                Object v = m.get(it.nextKey);
3160 >                if (v == null || (v != val && !v.equals(val)))
3161 >                    return false;
3162 >            }
3163 >            for (Map.Entry<?,?> e : m.entrySet()) {
3164 >                Object mk, mv, v;
3165 >                if ((mk = e.getKey()) == null ||
3166 >                    (mv = e.getValue()) == null ||
3167 >                    (v = internalGet(mk)) == null ||
3168 >                    (mv != v && !mv.equals(v)))
3169 >                    return false;
3170              }
3171          }
3172 +        return true;
3173 +    }
3174  
3175 <        public boolean hasNext() { return nextEntry != null; }
3175 >    /* ----------------Iterators -------------- */
3176  
3177 <        HashEntry<K,V> nextEntry() {
3178 <            if (nextEntry == null)
3177 >    @SuppressWarnings("serial") static final class KeyIterator<K,V> extends Traverser<K,V,Object>
3178 >        implements Spliterator<K>, Enumeration<K> {
3179 >        KeyIterator(ConcurrentHashMap<K, V> map) { super(map); }
3180 >        KeyIterator(Traverser<K,V,Object> it) {
3181 >            super(it);
3182 >        }
3183 >        public KeyIterator<K,V> split() {
3184 >            if (nextKey != null)
3185 >                throw new IllegalStateException();
3186 >            return new KeyIterator<K,V>(this);
3187 >        }
3188 >        @SuppressWarnings("unchecked") public final K next() {
3189 >            if (nextVal == null && advance() == null)
3190                  throw new NoSuchElementException();
3191 <            lastReturned = nextEntry;
3192 <            advance();
3193 <            return lastReturned;
3191 >            Object k = nextKey;
3192 >            nextVal = null;
3193 >            return (K) k;
3194          }
3195  
3196 <        public void remove() {
3197 <            if (lastReturned == null)
3196 >        public final K nextElement() { return next(); }
3197 >    }
3198 >
3199 >    @SuppressWarnings("serial") static final class ValueIterator<K,V> extends Traverser<K,V,Object>
3200 >        implements Spliterator<V>, Enumeration<V> {
3201 >        ValueIterator(ConcurrentHashMap<K, V> map) { super(map); }
3202 >        ValueIterator(Traverser<K,V,Object> it) {
3203 >            super(it);
3204 >        }
3205 >        public ValueIterator<K,V> split() {
3206 >            if (nextKey != null)
3207                  throw new IllegalStateException();
3208 <            ConcurrentHashMap.this.remove(lastReturned.key);
1105 <            lastReturned = null;
3208 >            return new ValueIterator<K,V>(this);
3209          }
1107    }
3210  
3211 <    final class KeyIterator
3212 <        extends HashIterator
3213 <        implements Iterator<K>, Enumeration<K>
3214 <    {
3215 <        public K next()        { return super.nextEntry().key; }
3216 <        public K nextElement() { return super.nextEntry().key; }
3211 >        @SuppressWarnings("unchecked") public final V next() {
3212 >            Object v;
3213 >            if ((v = nextVal) == null && (v = advance()) == null)
3214 >                throw new NoSuchElementException();
3215 >            nextVal = null;
3216 >            return (V) v;
3217 >        }
3218 >
3219 >        public final V nextElement() { return next(); }
3220      }
3221  
3222 <    final class ValueIterator
3223 <        extends HashIterator
3224 <        implements Iterator<V>, Enumeration<V>
3225 <    {
3226 <        public V next()        { return super.nextEntry().value; }
3227 <        public V nextElement() { return super.nextEntry().value; }
3222 >    @SuppressWarnings("serial") static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3223 >        implements Spliterator<Map.Entry<K,V>> {
3224 >        EntryIterator(ConcurrentHashMap<K, V> map) { super(map); }
3225 >        EntryIterator(Traverser<K,V,Object> it) {
3226 >            super(it);
3227 >        }
3228 >        public EntryIterator<K,V> split() {
3229 >            if (nextKey != null)
3230 >                throw new IllegalStateException();
3231 >            return new EntryIterator<K,V>(this);
3232 >        }
3233 >
3234 >        @SuppressWarnings("unchecked") public final Map.Entry<K,V> next() {
3235 >            Object v;
3236 >            if ((v = nextVal) == null && (v = advance()) == null)
3237 >                throw new NoSuchElementException();
3238 >            Object k = nextKey;
3239 >            nextVal = null;
3240 >            return new MapEntry<K,V>((K)k, (V)v, map);
3241 >        }
3242      }
3243  
3244      /**
3245 <     * Custom Entry class used by EntryIterator.next(), that relays
1127 <     * setValue changes to the underlying map.
3245 >     * Exported Entry for iterators
3246       */
3247 <    final class WriteThroughEntry
3248 <        extends AbstractMap.SimpleEntry<K,V>
3249 <    {
3250 <        WriteThroughEntry(K k, V v) {
3251 <            super(k,v);
3247 >    static final class MapEntry<K,V> implements Map.Entry<K, V> {
3248 >        final K key; // non-null
3249 >        V val;       // non-null
3250 >        final ConcurrentHashMap<K, V> map;
3251 >        MapEntry(K key, V val, ConcurrentHashMap<K, V> map) {
3252 >            this.key = key;
3253 >            this.val = val;
3254 >            this.map = map;
3255 >        }
3256 >        public final K getKey()       { return key; }
3257 >        public final V getValue()     { return val; }
3258 >        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
3259 >        public final String toString(){ return key + "=" + val; }
3260 >
3261 >        public final boolean equals(Object o) {
3262 >            Object k, v; Map.Entry<?,?> e;
3263 >            return ((o instanceof Map.Entry) &&
3264 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3265 >                    (v = e.getValue()) != null &&
3266 >                    (k == key || k.equals(key)) &&
3267 >                    (v == val || v.equals(val)));
3268          }
3269  
3270          /**
3271 <         * Set our entry's value and write through to the map. The
3272 <         * value to return is somewhat arbitrary here. Since a
3273 <         * WriteThroughEntry does not necessarily track asynchronous
3274 <         * changes, the most recent "previous" value could be
3275 <         * different from what we return (or could even have been
3276 <         * removed in which case the put will re-establish). We do not
1143 <         * and cannot guarantee more.
3271 >         * Sets our entry's value and writes through to the map. The
3272 >         * value to return is somewhat arbitrary here. Since we do not
3273 >         * necessarily track asynchronous changes, the most recent
3274 >         * "previous" value could be different from what we return (or
3275 >         * could even have been removed in which case the put will
3276 >         * re-establish). We do not and cannot guarantee more.
3277           */
3278 <        public V setValue(V value) {
3278 >        public final V setValue(V value) {
3279              if (value == null) throw new NullPointerException();
3280 <            V v = super.setValue(value);
3281 <            ConcurrentHashMap.this.put(getKey(), value);
3280 >            V v = val;
3281 >            val = value;
3282 >            map.put(key, value);
3283              return v;
3284          }
3285      }
3286  
3287 <    final class EntryIterator
3288 <        extends HashIterator
3289 <        implements Iterator<Entry<K,V>>
3290 <    {
3291 <        public Map.Entry<K,V> next() {
3292 <            HashEntry<K,V> e = super.nextEntry();
3293 <            return new WriteThroughEntry(e.key, e.value);
3287 >    /* ---------------- Serialization Support -------------- */
3288 >
3289 >    /**
3290 >     * Stripped-down version of helper class used in previous version,
3291 >     * declared for the sake of serialization compatibility
3292 >     */
3293 >    static class Segment<K,V> implements Serializable {
3294 >        private static final long serialVersionUID = 2249069246763182397L;
3295 >        final float loadFactor;
3296 >        Segment(float lf) { this.loadFactor = lf; }
3297 >    }
3298 >
3299 >    /**
3300 >     * Saves the state of the {@code ConcurrentHashMap} instance to a
3301 >     * stream (i.e., serializes it).
3302 >     * @param s the stream
3303 >     * @serialData
3304 >     * the key (Object) and value (Object)
3305 >     * for each key-value mapping, followed by a null pair.
3306 >     * The key-value mappings are emitted in no particular order.
3307 >     */
3308 >    @SuppressWarnings("unchecked") private void writeObject(java.io.ObjectOutputStream s)
3309 >        throws java.io.IOException {
3310 >        if (segments == null) { // for serialization compatibility
3311 >            segments = (Segment<K,V>[])
3312 >                new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
3313 >            for (int i = 0; i < segments.length; ++i)
3314 >                segments[i] = new Segment<K,V>(LOAD_FACTOR);
3315          }
3316 +        s.defaultWriteObject();
3317 +        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3318 +        Object v;
3319 +        while ((v = it.advance()) != null) {
3320 +            s.writeObject(it.nextKey);
3321 +            s.writeObject(v);
3322 +        }
3323 +        s.writeObject(null);
3324 +        s.writeObject(null);
3325 +        segments = null; // throw away
3326      }
3327  
3328 <    final class KeySet extends AbstractSet<K> {
3329 <        public Iterator<K> iterator() {
3330 <            return new KeyIterator();
3328 >    /**
3329 >     * Reconstitutes the instance from a stream (that is, deserializes it).
3330 >     * @param s the stream
3331 >     */
3332 >    @SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s)
3333 >        throws java.io.IOException, ClassNotFoundException {
3334 >        s.defaultReadObject();
3335 >        this.segments = null; // unneeded
3336 >        // initialize transient final field
3337 >        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
3338 >
3339 >        // Create all nodes, then place in table once size is known
3340 >        long size = 0L;
3341 >        Node p = null;
3342 >        for (;;) {
3343 >            K k = (K) s.readObject();
3344 >            V v = (V) s.readObject();
3345 >            if (k != null && v != null) {
3346 >                int h = spread(k.hashCode());
3347 >                p = new Node(h, k, v, p);
3348 >                ++size;
3349 >            }
3350 >            else
3351 >                break;
3352          }
3353 <        public int size() {
3354 <            return ConcurrentHashMap.this.size();
3353 >        if (p != null) {
3354 >            boolean init = false;
3355 >            int n;
3356 >            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
3357 >                n = MAXIMUM_CAPACITY;
3358 >            else {
3359 >                int sz = (int)size;
3360 >                n = tableSizeFor(sz + (sz >>> 1) + 1);
3361 >            }
3362 >            int sc = sizeCtl;
3363 >            boolean collide = false;
3364 >            if (n > sc &&
3365 >                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
3366 >                try {
3367 >                    if (table == null) {
3368 >                        init = true;
3369 >                        Node[] tab = new Node[n];
3370 >                        int mask = n - 1;
3371 >                        while (p != null) {
3372 >                            int j = p.hash & mask;
3373 >                            Node next = p.next;
3374 >                            Node q = p.next = tabAt(tab, j);
3375 >                            setTabAt(tab, j, p);
3376 >                            if (!collide && q != null && q.hash == p.hash)
3377 >                                collide = true;
3378 >                            p = next;
3379 >                        }
3380 >                        table = tab;
3381 >                        counter.add(size);
3382 >                        sc = n - (n >>> 2);
3383 >                    }
3384 >                } finally {
3385 >                    sizeCtl = sc;
3386 >                }
3387 >                if (collide) { // rescan and convert to TreeBins
3388 >                    Node[] tab = table;
3389 >                    for (int i = 0; i < tab.length; ++i) {
3390 >                        int c = 0;
3391 >                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
3392 >                            if (++c > TREE_THRESHOLD &&
3393 >                                (e.key instanceof Comparable)) {
3394 >                                replaceWithTreeBin(tab, i, e.key);
3395 >                                break;
3396 >                            }
3397 >                        }
3398 >                    }
3399 >                }
3400 >            }
3401 >            if (!init) { // Can only happen if unsafely published.
3402 >                while (p != null) {
3403 >                    internalPut(p.key, p.val);
3404 >                    p = p.next;
3405 >                }
3406 >            }
3407          }
3408 <        public boolean contains(Object o) {
3409 <            return ConcurrentHashMap.this.containsKey(o);
3408 >    }
3409 >
3410 >
3411 >    // -------------------------------------------------------
3412 >
3413 >    // Sams
3414 >    /** Interface describing a void action of one argument */
3415 >    public interface Action<A> { void apply(A a); }
3416 >    /** Interface describing a void action of two arguments */
3417 >    public interface BiAction<A,B> { void apply(A a, B b); }
3418 >    /** Interface describing a function of one argument */
3419 >    public interface Fun<A,T> { T apply(A a); }
3420 >    /** Interface describing a function of two arguments */
3421 >    public interface BiFun<A,B,T> { T apply(A a, B b); }
3422 >    /** Interface describing a function of no arguments */
3423 >    public interface Generator<T> { T apply(); }
3424 >    /** Interface describing a function mapping its argument to a double */
3425 >    public interface ObjectToDouble<A> { double apply(A a); }
3426 >    /** Interface describing a function mapping its argument to a long */
3427 >    public interface ObjectToLong<A> { long apply(A a); }
3428 >    /** Interface describing a function mapping its argument to an int */
3429 >    public interface ObjectToInt<A> {int apply(A a); }
3430 >    /** Interface describing a function mapping two arguments to a double */
3431 >    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
3432 >    /** Interface describing a function mapping two arguments to a long */
3433 >    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
3434 >    /** Interface describing a function mapping two arguments to an int */
3435 >    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
3436 >    /** Interface describing a function mapping a double to a double */
3437 >    public interface DoubleToDouble { double apply(double a); }
3438 >    /** Interface describing a function mapping a long to a long */
3439 >    public interface LongToLong { long apply(long a); }
3440 >    /** Interface describing a function mapping an int to an int */
3441 >    public interface IntToInt { int apply(int a); }
3442 >    /** Interface describing a function mapping two doubles to a double */
3443 >    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
3444 >    /** Interface describing a function mapping two longs to a long */
3445 >    public interface LongByLongToLong { long apply(long a, long b); }
3446 >    /** Interface describing a function mapping two ints to an int */
3447 >    public interface IntByIntToInt { int apply(int a, int b); }
3448 >
3449 >
3450 >    // -------------------------------------------------------
3451 >
3452 >    /**
3453 >     * Performs the given action for each (key, value).
3454 >     *
3455 >     * @param action the action
3456 >     */
3457 >    public void forEach(BiAction<K,V> action) {
3458 >        ForkJoinTasks.forEach
3459 >            (this, action).invoke();
3460 >    }
3461 >
3462 >    /**
3463 >     * Performs the given action for each non-null transformation
3464 >     * of each (key, value).
3465 >     *
3466 >     * @param transformer a function returning the transformation
3467 >     * for an element, or null of there is no transformation (in
3468 >     * which case the action is not applied).
3469 >     * @param action the action
3470 >     */
3471 >    public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
3472 >                            Action<U> action) {
3473 >        ForkJoinTasks.forEach
3474 >            (this, transformer, action).invoke();
3475 >    }
3476 >
3477 >    /**
3478 >     * Returns a non-null result from applying the given search
3479 >     * function on each (key, value), or null if none.  Upon
3480 >     * success, further element processing is suppressed and the
3481 >     * results of any other parallel invocations of the search
3482 >     * function are ignored.
3483 >     *
3484 >     * @param searchFunction a function returning a non-null
3485 >     * result on success, else null
3486 >     * @return a non-null result from applying the given search
3487 >     * function on each (key, value), or null if none
3488 >     */
3489 >    public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
3490 >        return ForkJoinTasks.search
3491 >            (this, searchFunction).invoke();
3492 >    }
3493 >
3494 >    /**
3495 >     * Returns the result of accumulating the given transformation
3496 >     * of all (key, value) pairs using the given reducer to
3497 >     * combine values, or null if none.
3498 >     *
3499 >     * @param transformer a function returning the transformation
3500 >     * for an element, or null of there is no transformation (in
3501 >     * which case it is not combined).
3502 >     * @param reducer a commutative associative combining function
3503 >     * @return the result of accumulating the given transformation
3504 >     * of all (key, value) pairs
3505 >     */
3506 >    public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
3507 >                        BiFun<? super U, ? super U, ? extends U> reducer) {
3508 >        return ForkJoinTasks.reduce
3509 >            (this, transformer, reducer).invoke();
3510 >    }
3511 >
3512 >    /**
3513 >     * Returns the result of accumulating the given transformation
3514 >     * of all (key, value) pairs using the given reducer to
3515 >     * combine values, and the given basis as an identity value.
3516 >     *
3517 >     * @param transformer a function returning the transformation
3518 >     * for an element
3519 >     * @param basis the identity (initial default value) for the reduction
3520 >     * @param reducer a commutative associative combining function
3521 >     * @return the result of accumulating the given transformation
3522 >     * of all (key, value) pairs
3523 >     */
3524 >    public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
3525 >                                 double basis,
3526 >                                 DoubleByDoubleToDouble reducer) {
3527 >        return ForkJoinTasks.reduceToDouble
3528 >            (this, transformer, basis, reducer).invoke();
3529 >    }
3530 >
3531 >    /**
3532 >     * Returns the result of accumulating the given transformation
3533 >     * of all (key, value) pairs using the given reducer to
3534 >     * combine values, and the given basis as an identity value.
3535 >     *
3536 >     * @param transformer a function returning the transformation
3537 >     * for an element
3538 >     * @param basis the identity (initial default value) for the reduction
3539 >     * @param reducer a commutative associative combining function
3540 >     * @return the result of accumulating the given transformation
3541 >     * of all (key, value) pairs
3542 >     */
3543 >    public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
3544 >                             long basis,
3545 >                             LongByLongToLong reducer) {
3546 >        return ForkJoinTasks.reduceToLong
3547 >            (this, transformer, basis, reducer).invoke();
3548 >    }
3549 >
3550 >    /**
3551 >     * Returns the result of accumulating the given transformation
3552 >     * of all (key, value) pairs using the given reducer to
3553 >     * combine values, and the given basis as an identity value.
3554 >     *
3555 >     * @param transformer a function returning the transformation
3556 >     * for an element
3557 >     * @param basis the identity (initial default value) for the reduction
3558 >     * @param reducer a commutative associative combining function
3559 >     * @return the result of accumulating the given transformation
3560 >     * of all (key, value) pairs
3561 >     */
3562 >    public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
3563 >                           int basis,
3564 >                           IntByIntToInt reducer) {
3565 >        return ForkJoinTasks.reduceToInt
3566 >            (this, transformer, basis, reducer).invoke();
3567 >    }
3568 >
3569 >    /**
3570 >     * Performs the given action for each key.
3571 >     *
3572 >     * @param action the action
3573 >     */
3574 >    public void forEachKey(Action<K> action) {
3575 >        ForkJoinTasks.forEachKey
3576 >            (this, action).invoke();
3577 >    }
3578 >
3579 >    /**
3580 >     * Performs the given action for each non-null transformation
3581 >     * of each key.
3582 >     *
3583 >     * @param transformer a function returning the transformation
3584 >     * for an element, or null of there is no transformation (in
3585 >     * which case the action is not applied).
3586 >     * @param action the action
3587 >     */
3588 >    public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
3589 >                               Action<U> action) {
3590 >        ForkJoinTasks.forEachKey
3591 >            (this, transformer, action).invoke();
3592 >    }
3593 >
3594 >    /**
3595 >     * Returns a non-null result from applying the given search
3596 >     * function on each key, or null if none. Upon success,
3597 >     * further element processing is suppressed and the results of
3598 >     * any other parallel invocations of the search function are
3599 >     * ignored.
3600 >     *
3601 >     * @param searchFunction a function returning a non-null
3602 >     * result on success, else null
3603 >     * @return a non-null result from applying the given search
3604 >     * function on each key, or null if none
3605 >     */
3606 >    public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
3607 >        return ForkJoinTasks.searchKeys
3608 >            (this, searchFunction).invoke();
3609 >    }
3610 >
3611 >    /**
3612 >     * Returns the result of accumulating all keys using the given
3613 >     * reducer to combine values, or null if none.
3614 >     *
3615 >     * @param reducer a commutative associative combining function
3616 >     * @return the result of accumulating all keys using the given
3617 >     * reducer to combine values, or null if none
3618 >     */
3619 >    public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
3620 >        return ForkJoinTasks.reduceKeys
3621 >            (this, reducer).invoke();
3622 >    }
3623 >
3624 >    /**
3625 >     * Returns the result of accumulating the given transformation
3626 >     * of all keys using the given reducer to combine values, or
3627 >     * null if none.
3628 >     *
3629 >     * @param transformer a function returning the transformation
3630 >     * for an element, or null of there is no transformation (in
3631 >     * which case it is not combined).
3632 >     * @param reducer a commutative associative combining function
3633 >     * @return the result of accumulating the given transformation
3634 >     * of all keys
3635 >     */
3636 >    public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
3637 >                            BiFun<? super U, ? super U, ? extends U> reducer) {
3638 >        return ForkJoinTasks.reduceKeys
3639 >            (this, transformer, reducer).invoke();
3640 >    }
3641 >
3642 >    /**
3643 >     * Returns the result of accumulating the given transformation
3644 >     * of all keys using the given reducer to combine values, and
3645 >     * the given basis as an identity value.
3646 >     *
3647 >     * @param transformer a function returning the transformation
3648 >     * for an element
3649 >     * @param basis the identity (initial default value) for the reduction
3650 >     * @param reducer a commutative associative combining function
3651 >     * @return  the result of accumulating the given transformation
3652 >     * of all keys
3653 >     */
3654 >    public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3655 >                                     double basis,
3656 >                                     DoubleByDoubleToDouble reducer) {
3657 >        return ForkJoinTasks.reduceKeysToDouble
3658 >            (this, transformer, basis, reducer).invoke();
3659 >    }
3660 >
3661 >    /**
3662 >     * Returns the result of accumulating the given transformation
3663 >     * of all keys using the given reducer to combine values, and
3664 >     * the given basis as an identity value.
3665 >     *
3666 >     * @param transformer a function returning the transformation
3667 >     * for an element
3668 >     * @param basis the identity (initial default value) for the reduction
3669 >     * @param reducer a commutative associative combining function
3670 >     * @return the result of accumulating the given transformation
3671 >     * of all keys
3672 >     */
3673 >    public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3674 >                                 long basis,
3675 >                                 LongByLongToLong reducer) {
3676 >        return ForkJoinTasks.reduceKeysToLong
3677 >            (this, transformer, basis, reducer).invoke();
3678 >    }
3679 >
3680 >    /**
3681 >     * Returns the result of accumulating the given transformation
3682 >     * of all keys using the given reducer to combine values, and
3683 >     * the given basis as an identity value.
3684 >     *
3685 >     * @param transformer a function returning the transformation
3686 >     * for an element
3687 >     * @param basis the identity (initial default value) for the reduction
3688 >     * @param reducer a commutative associative combining function
3689 >     * @return the result of accumulating the given transformation
3690 >     * of all keys
3691 >     */
3692 >    public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3693 >                               int basis,
3694 >                               IntByIntToInt reducer) {
3695 >        return ForkJoinTasks.reduceKeysToInt
3696 >            (this, transformer, basis, reducer).invoke();
3697 >    }
3698 >
3699 >    /**
3700 >     * Performs the given action for each value.
3701 >     *
3702 >     * @param action the action
3703 >     */
3704 >    public void forEachValue(Action<V> action) {
3705 >        ForkJoinTasks.forEachValue
3706 >            (this, action).invoke();
3707 >    }
3708 >
3709 >    /**
3710 >     * Performs the given action for each non-null transformation
3711 >     * of each value.
3712 >     *
3713 >     * @param transformer a function returning the transformation
3714 >     * for an element, or null of there is no transformation (in
3715 >     * which case the action is not applied).
3716 >     */
3717 >    public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3718 >                                 Action<U> action) {
3719 >        ForkJoinTasks.forEachValue
3720 >            (this, transformer, action).invoke();
3721 >    }
3722 >
3723 >    /**
3724 >     * Returns a non-null result from applying the given search
3725 >     * function on each value, or null if none.  Upon success,
3726 >     * further element processing is suppressed and the results of
3727 >     * any other parallel invocations of the search function are
3728 >     * ignored.
3729 >     *
3730 >     * @param searchFunction a function returning a non-null
3731 >     * result on success, else null
3732 >     * @return a non-null result from applying the given search
3733 >     * function on each value, or null if none
3734 >     *
3735 >     */
3736 >    public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
3737 >        return ForkJoinTasks.searchValues
3738 >            (this, searchFunction).invoke();
3739 >    }
3740 >
3741 >    /**
3742 >     * Returns the result of accumulating all values using the
3743 >     * given reducer to combine values, or null if none.
3744 >     *
3745 >     * @param reducer a commutative associative combining function
3746 >     * @return  the result of accumulating all values
3747 >     */
3748 >    public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3749 >        return ForkJoinTasks.reduceValues
3750 >            (this, reducer).invoke();
3751 >    }
3752 >
3753 >    /**
3754 >     * Returns the result of accumulating the given transformation
3755 >     * of all values using the given reducer to combine values, or
3756 >     * null if none.
3757 >     *
3758 >     * @param transformer a function returning the transformation
3759 >     * for an element, or null of there is no transformation (in
3760 >     * which case it is not combined).
3761 >     * @param reducer a commutative associative combining function
3762 >     * @return the result of accumulating the given transformation
3763 >     * of all values
3764 >     */
3765 >    public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
3766 >                              BiFun<? super U, ? super U, ? extends U> reducer) {
3767 >        return ForkJoinTasks.reduceValues
3768 >            (this, transformer, reducer).invoke();
3769 >    }
3770 >
3771 >    /**
3772 >     * Returns the result of accumulating the given transformation
3773 >     * of all values using the given reducer to combine values,
3774 >     * and the given basis as an identity value.
3775 >     *
3776 >     * @param transformer a function returning the transformation
3777 >     * for an element
3778 >     * @param basis the identity (initial default value) for the reduction
3779 >     * @param reducer a commutative associative combining function
3780 >     * @return the result of accumulating the given transformation
3781 >     * of all values
3782 >     */
3783 >    public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
3784 >                                       double basis,
3785 >                                       DoubleByDoubleToDouble reducer) {
3786 >        return ForkJoinTasks.reduceValuesToDouble
3787 >            (this, transformer, basis, reducer).invoke();
3788 >    }
3789 >
3790 >    /**
3791 >     * Returns the result of accumulating the given transformation
3792 >     * of all values using the given reducer to combine values,
3793 >     * and the given basis as an identity value.
3794 >     *
3795 >     * @param transformer a function returning the transformation
3796 >     * for an element
3797 >     * @param basis the identity (initial default value) for the reduction
3798 >     * @param reducer a commutative associative combining function
3799 >     * @return the result of accumulating the given transformation
3800 >     * of all values
3801 >     */
3802 >    public long reduceValuesToLong(ObjectToLong<? super V> transformer,
3803 >                                   long basis,
3804 >                                   LongByLongToLong reducer) {
3805 >        return ForkJoinTasks.reduceValuesToLong
3806 >            (this, transformer, basis, reducer).invoke();
3807 >    }
3808 >
3809 >    /**
3810 >     * Returns the result of accumulating the given transformation
3811 >     * of all values using the given reducer to combine values,
3812 >     * and the given basis as an identity value.
3813 >     *
3814 >     * @param transformer a function returning the transformation
3815 >     * for an element
3816 >     * @param basis the identity (initial default value) for the reduction
3817 >     * @param reducer a commutative associative combining function
3818 >     * @return the result of accumulating the given transformation
3819 >     * of all values
3820 >     */
3821 >    public int reduceValuesToInt(ObjectToInt<? super V> transformer,
3822 >                                 int basis,
3823 >                                 IntByIntToInt reducer) {
3824 >        return ForkJoinTasks.reduceValuesToInt
3825 >            (this, transformer, basis, reducer).invoke();
3826 >    }
3827 >
3828 >    /**
3829 >     * Performs the given action for each entry.
3830 >     *
3831 >     * @param action the action
3832 >     */
3833 >    public void forEachEntry(Action<Map.Entry<K,V>> action) {
3834 >        ForkJoinTasks.forEachEntry
3835 >            (this, action).invoke();
3836 >    }
3837 >
3838 >    /**
3839 >     * Performs the given action for each non-null transformation
3840 >     * of each entry.
3841 >     *
3842 >     * @param transformer a function returning the transformation
3843 >     * for an element, or null of there is no transformation (in
3844 >     * which case the action is not applied).
3845 >     * @param action the action
3846 >     */
3847 >    public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
3848 >                                 Action<U> action) {
3849 >        ForkJoinTasks.forEachEntry
3850 >            (this, transformer, action).invoke();
3851 >    }
3852 >
3853 >    /**
3854 >     * Returns a non-null result from applying the given search
3855 >     * function on each entry, or null if none.  Upon success,
3856 >     * further element processing is suppressed and the results of
3857 >     * any other parallel invocations of the search function are
3858 >     * ignored.
3859 >     *
3860 >     * @param searchFunction a function returning a non-null
3861 >     * result on success, else null
3862 >     * @return a non-null result from applying the given search
3863 >     * function on each entry, or null if none
3864 >     */
3865 >    public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
3866 >        return ForkJoinTasks.searchEntries
3867 >            (this, searchFunction).invoke();
3868 >    }
3869 >
3870 >    /**
3871 >     * Returns the result of accumulating all entries using the
3872 >     * given reducer to combine values, or null if none.
3873 >     *
3874 >     * @param reducer a commutative associative combining function
3875 >     * @return the result of accumulating all entries
3876 >     */
3877 >    public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
3878 >        return ForkJoinTasks.reduceEntries
3879 >            (this, reducer).invoke();
3880 >    }
3881 >
3882 >    /**
3883 >     * Returns the result of accumulating the given transformation
3884 >     * of all entries using the given reducer to combine values,
3885 >     * or null if none.
3886 >     *
3887 >     * @param transformer a function returning the transformation
3888 >     * for an element, or null of there is no transformation (in
3889 >     * which case it is not combined).
3890 >     * @param reducer a commutative associative combining function
3891 >     * @return the result of accumulating the given transformation
3892 >     * of all entries
3893 >     */
3894 >    public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
3895 >                               BiFun<? super U, ? super U, ? extends U> reducer) {
3896 >        return ForkJoinTasks.reduceEntries
3897 >            (this, transformer, reducer).invoke();
3898 >    }
3899 >
3900 >    /**
3901 >     * Returns the result of accumulating the given transformation
3902 >     * of all entries using the given reducer to combine values,
3903 >     * and the given basis as an identity value.
3904 >     *
3905 >     * @param transformer a function returning the transformation
3906 >     * for an element
3907 >     * @param basis the identity (initial default value) for the reduction
3908 >     * @param reducer a commutative associative combining function
3909 >     * @return the result of accumulating the given transformation
3910 >     * of all entries
3911 >     */
3912 >    public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
3913 >                                        double basis,
3914 >                                        DoubleByDoubleToDouble reducer) {
3915 >        return ForkJoinTasks.reduceEntriesToDouble
3916 >            (this, transformer, basis, reducer).invoke();
3917 >    }
3918 >
3919 >    /**
3920 >     * Returns the result of accumulating the given transformation
3921 >     * of all entries using the given reducer to combine values,
3922 >     * and the given basis as an identity value.
3923 >     *
3924 >     * @param transformer a function returning the transformation
3925 >     * for an element
3926 >     * @param basis the identity (initial default value) for the reduction
3927 >     * @param reducer a commutative associative combining function
3928 >     * @return  the result of accumulating the given transformation
3929 >     * of all entries
3930 >     */
3931 >    public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
3932 >                                    long basis,
3933 >                                    LongByLongToLong reducer) {
3934 >        return ForkJoinTasks.reduceEntriesToLong
3935 >            (this, transformer, basis, reducer).invoke();
3936 >    }
3937 >
3938 >    /**
3939 >     * Returns the result of accumulating the given transformation
3940 >     * of all entries using the given reducer to combine values,
3941 >     * and the given basis as an identity value.
3942 >     *
3943 >     * @param transformer a function returning the transformation
3944 >     * for an element
3945 >     * @param basis the identity (initial default value) for the reduction
3946 >     * @param reducer a commutative associative combining function
3947 >     * @return the result of accumulating the given transformation
3948 >     * of all entries
3949 >     */
3950 >    public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
3951 >                                  int basis,
3952 >                                  IntByIntToInt reducer) {
3953 >        return ForkJoinTasks.reduceEntriesToInt
3954 >            (this, transformer, basis, reducer).invoke();
3955 >    }
3956 >
3957 >    /* ----------------Views -------------- */
3958 >
3959 >    /**
3960 >     * Base class for views.
3961 >     */
3962 >    static abstract class CHMView<K, V> {
3963 >        final ConcurrentHashMap<K, V> map;
3964 >        CHMView(ConcurrentHashMap<K, V> map)  { this.map = map; }
3965 >
3966 >        /**
3967 >         * Returns the map backing this view.
3968 >         *
3969 >         * @return the map backing this view
3970 >         */
3971 >        public ConcurrentHashMap<K,V> getMap() { return map; }
3972 >
3973 >        public final int size()                 { return map.size(); }
3974 >        public final boolean isEmpty()          { return map.isEmpty(); }
3975 >        public final void clear()               { map.clear(); }
3976 >
3977 >        // implementations below rely on concrete classes supplying these
3978 >        abstract public Iterator<?> iterator();
3979 >        abstract public boolean contains(Object o);
3980 >        abstract public boolean remove(Object o);
3981 >
3982 >        private static final String oomeMsg = "Required array size too large";
3983 >
3984 >        public final Object[] toArray() {
3985 >            long sz = map.mappingCount();
3986 >            if (sz > (long)(MAX_ARRAY_SIZE))
3987 >                throw new OutOfMemoryError(oomeMsg);
3988 >            int n = (int)sz;
3989 >            Object[] r = new Object[n];
3990 >            int i = 0;
3991 >            Iterator<?> it = iterator();
3992 >            while (it.hasNext()) {
3993 >                if (i == n) {
3994 >                    if (n >= MAX_ARRAY_SIZE)
3995 >                        throw new OutOfMemoryError(oomeMsg);
3996 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3997 >                        n = MAX_ARRAY_SIZE;
3998 >                    else
3999 >                        n += (n >>> 1) + 1;
4000 >                    r = Arrays.copyOf(r, n);
4001 >                }
4002 >                r[i++] = it.next();
4003 >            }
4004 >            return (i == n) ? r : Arrays.copyOf(r, i);
4005          }
4006 <        public boolean remove(Object o) {
4007 <            return ConcurrentHashMap.this.remove(o) != null;
4006 >
4007 >        @SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) {
4008 >            long sz = map.mappingCount();
4009 >            if (sz > (long)(MAX_ARRAY_SIZE))
4010 >                throw new OutOfMemoryError(oomeMsg);
4011 >            int m = (int)sz;
4012 >            T[] r = (a.length >= m) ? a :
4013 >                (T[])java.lang.reflect.Array
4014 >                .newInstance(a.getClass().getComponentType(), m);
4015 >            int n = r.length;
4016 >            int i = 0;
4017 >            Iterator<?> it = iterator();
4018 >            while (it.hasNext()) {
4019 >                if (i == n) {
4020 >                    if (n >= MAX_ARRAY_SIZE)
4021 >                        throw new OutOfMemoryError(oomeMsg);
4022 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4023 >                        n = MAX_ARRAY_SIZE;
4024 >                    else
4025 >                        n += (n >>> 1) + 1;
4026 >                    r = Arrays.copyOf(r, n);
4027 >                }
4028 >                r[i++] = (T)it.next();
4029 >            }
4030 >            if (a == r && i < n) {
4031 >                r[i] = null; // null-terminate
4032 >                return r;
4033 >            }
4034 >            return (i == n) ? r : Arrays.copyOf(r, i);
4035          }
4036 <        public void clear() {
4037 <            ConcurrentHashMap.this.clear();
4036 >
4037 >        public final int hashCode() {
4038 >            int h = 0;
4039 >            for (Iterator<?> it = iterator(); it.hasNext();)
4040 >                h += it.next().hashCode();
4041 >            return h;
4042 >        }
4043 >
4044 >        public final String toString() {
4045 >            StringBuilder sb = new StringBuilder();
4046 >            sb.append('[');
4047 >            Iterator<?> it = iterator();
4048 >            if (it.hasNext()) {
4049 >                for (;;) {
4050 >                    Object e = it.next();
4051 >                    sb.append(e == this ? "(this Collection)" : e);
4052 >                    if (!it.hasNext())
4053 >                        break;
4054 >                    sb.append(',').append(' ');
4055 >                }
4056 >            }
4057 >            return sb.append(']').toString();
4058 >        }
4059 >
4060 >        public final boolean containsAll(Collection<?> c) {
4061 >            if (c != this) {
4062 >                for (Iterator<?> it = c.iterator(); it.hasNext();) {
4063 >                    Object e = it.next();
4064 >                    if (e == null || !contains(e))
4065 >                        return false;
4066 >                }
4067 >            }
4068 >            return true;
4069          }
4070 <        public Object[] toArray() {
4071 <            Collection<K> c = new ArrayList<K>(size());
4072 <            for (K k : this)
4073 <                c.add(k);
4074 <            return c.toArray();
4070 >
4071 >        public final boolean removeAll(Collection<?> c) {
4072 >            boolean modified = false;
4073 >            for (Iterator<?> it = iterator(); it.hasNext();) {
4074 >                if (c.contains(it.next())) {
4075 >                    it.remove();
4076 >                    modified = true;
4077 >                }
4078 >            }
4079 >            return modified;
4080          }
4081 <        public <T> T[] toArray(T[] a) {
4082 <            Collection<K> c = new ArrayList<K>();
4083 <            for (K k : this)
4084 <                c.add(k);
4085 <            return c.toArray(a);
4081 >
4082 >        public final boolean retainAll(Collection<?> c) {
4083 >            boolean modified = false;
4084 >            for (Iterator<?> it = iterator(); it.hasNext();) {
4085 >                if (!c.contains(it.next())) {
4086 >                    it.remove();
4087 >                    modified = true;
4088 >                }
4089 >            }
4090 >            return modified;
4091          }
4092 +
4093      }
4094  
4095 <    final class Values extends AbstractCollection<V> {
4096 <        public Iterator<V> iterator() {
4097 <            return new ValueIterator();
4095 >    /**
4096 >     * A view of a ConcurrentHashMap as a {@link Set} of keys, in
4097 >     * which additions may optionally be enabled by mapping to a
4098 >     * common value.  This class cannot be directly instantiated. See
4099 >     * {@link #keySet}, {@link #keySet(Object)}, {@link #newKeySet()},
4100 >     * {@link #newKeySet(int)}.
4101 >     */
4102 >    public static class KeySetView<K,V> extends CHMView<K,V> implements Set<K>, java.io.Serializable {
4103 >        private static final long serialVersionUID = 7249069246763182397L;
4104 >        private final V value;
4105 >        KeySetView(ConcurrentHashMap<K, V> map, V value) {  // non-public
4106 >            super(map);
4107 >            this.value = value;
4108 >        }
4109 >
4110 >        /**
4111 >         * Returns the default mapped value for additions,
4112 >         * or {@code null} if additions are not supported.
4113 >         *
4114 >         * @return the default mapped value for additions, or {@code null}
4115 >         * if not supported.
4116 >         */
4117 >        public V getMappedValue() { return value; }
4118 >
4119 >        // implement Set API
4120 >
4121 >        public boolean contains(Object o) { return map.containsKey(o); }
4122 >        public boolean remove(Object o)   { return map.remove(o) != null; }
4123 >
4124 >        /**
4125 >         * Returns a "weakly consistent" iterator that will never
4126 >         * throw {@link ConcurrentModificationException}, and
4127 >         * guarantees to traverse elements as they existed upon
4128 >         * construction of the iterator, and may (but is not
4129 >         * guaranteed to) reflect any modifications subsequent to
4130 >         * construction.
4131 >         *
4132 >         * @return an iterator over the keys of this map
4133 >         */
4134 >        public Iterator<K> iterator()     { return new KeyIterator<K,V>(map); }
4135 >        public boolean add(K e) {
4136 >            V v;
4137 >            if ((v = value) == null)
4138 >                throw new UnsupportedOperationException();
4139 >            if (e == null)
4140 >                throw new NullPointerException();
4141 >            return map.internalPutIfAbsent(e, v) == null;
4142 >        }
4143 >        public boolean addAll(Collection<? extends K> c) {
4144 >            boolean added = false;
4145 >            V v;
4146 >            if ((v = value) == null)
4147 >                throw new UnsupportedOperationException();
4148 >            for (K e : c) {
4149 >                if (e == null)
4150 >                    throw new NullPointerException();
4151 >                if (map.internalPutIfAbsent(e, v) == null)
4152 >                    added = true;
4153 >            }
4154 >            return added;
4155 >        }
4156 >        public boolean equals(Object o) {
4157 >            Set<?> c;
4158 >            return ((o instanceof Set) &&
4159 >                    ((c = (Set<?>)o) == this ||
4160 >                     (containsAll(c) && c.containsAll(this))));
4161 >        }
4162 >
4163 >        /**
4164 >         * Performs the given action for each key.
4165 >         *
4166 >         * @param action the action
4167 >         */
4168 >        public void forEach(Action<K> action) {
4169 >            ForkJoinTasks.forEachKey
4170 >                (map, action).invoke();
4171 >        }
4172 >
4173 >        /**
4174 >         * Performs the given action for each non-null transformation
4175 >         * of each key.
4176 >         *
4177 >         * @param transformer a function returning the transformation
4178 >         * for an element, or null of there is no transformation (in
4179 >         * which case the action is not applied).
4180 >         * @param action the action
4181 >         */
4182 >        public <U> void forEach(Fun<? super K, ? extends U> transformer,
4183 >                                Action<U> action) {
4184 >            ForkJoinTasks.forEachKey
4185 >                (map, transformer, action).invoke();
4186          }
4187 <        public int size() {
4188 <            return ConcurrentHashMap.this.size();
4187 >
4188 >        /**
4189 >         * Returns a non-null result from applying the given search
4190 >         * function on each key, or null if none. Upon success,
4191 >         * further element processing is suppressed and the results of
4192 >         * any other parallel invocations of the search function are
4193 >         * ignored.
4194 >         *
4195 >         * @param searchFunction a function returning a non-null
4196 >         * result on success, else null
4197 >         * @return a non-null result from applying the given search
4198 >         * function on each key, or null if none
4199 >         */
4200 >        public <U> U search(Fun<? super K, ? extends U> searchFunction) {
4201 >            return ForkJoinTasks.searchKeys
4202 >                (map, searchFunction).invoke();
4203          }
4204 <        public boolean contains(Object o) {
4205 <            return ConcurrentHashMap.this.containsValue(o);
4204 >
4205 >        /**
4206 >         * Returns the result of accumulating all keys using the given
4207 >         * reducer to combine values, or null if none.
4208 >         *
4209 >         * @param reducer a commutative associative combining function
4210 >         * @return the result of accumulating all keys using the given
4211 >         * reducer to combine values, or null if none
4212 >         */
4213 >        public K reduce(BiFun<? super K, ? super K, ? extends K> reducer) {
4214 >            return ForkJoinTasks.reduceKeys
4215 >                (map, reducer).invoke();
4216          }
4217 <        public void clear() {
4218 <            ConcurrentHashMap.this.clear();
4217 >
4218 >        /**
4219 >         * Returns the result of accumulating the given transformation
4220 >         * of all keys using the given reducer to combine values, and
4221 >         * the given basis as an identity value.
4222 >         *
4223 >         * @param transformer a function returning the transformation
4224 >         * for an element
4225 >         * @param basis the identity (initial default value) for the reduction
4226 >         * @param reducer a commutative associative combining function
4227 >         * @return  the result of accumulating the given transformation
4228 >         * of all keys
4229 >         */
4230 >        public double reduceToDouble(ObjectToDouble<? super K> transformer,
4231 >                                     double basis,
4232 >                                     DoubleByDoubleToDouble reducer) {
4233 >            return ForkJoinTasks.reduceKeysToDouble
4234 >                (map, transformer, basis, reducer).invoke();
4235          }
4236 <        public Object[] toArray() {
4237 <            Collection<V> c = new ArrayList<V>(size());
4238 <            for (V v : this)
4239 <                c.add(v);
4240 <            return c.toArray();
4236 >
4237 >
4238 >        /**
4239 >         * Returns the result of accumulating the given transformation
4240 >         * of all keys using the given reducer to combine values, and
4241 >         * the given basis as an identity value.
4242 >         *
4243 >         * @param transformer a function returning the transformation
4244 >         * for an element
4245 >         * @param basis the identity (initial default value) for the reduction
4246 >         * @param reducer a commutative associative combining function
4247 >         * @return the result of accumulating the given transformation
4248 >         * of all keys
4249 >         */
4250 >        public long reduceToLong(ObjectToLong<? super K> transformer,
4251 >                                 long basis,
4252 >                                 LongByLongToLong reducer) {
4253 >            return ForkJoinTasks.reduceKeysToLong
4254 >                (map, transformer, basis, reducer).invoke();
4255          }
4256 <        public <T> T[] toArray(T[] a) {
4257 <            Collection<V> c = new ArrayList<V>(size());
4258 <            for (V v : this)
4259 <                c.add(v);
4260 <            return c.toArray(a);
4256 >        
4257 >        /**
4258 >         * Returns the result of accumulating the given transformation
4259 >         * of all keys using the given reducer to combine values, and
4260 >         * the given basis as an identity value.
4261 >         *
4262 >         * @param transformer a function returning the transformation
4263 >         * for an element
4264 >         * @param basis the identity (initial default value) for the reduction
4265 >         * @param reducer a commutative associative combining function
4266 >         * @return the result of accumulating the given transformation
4267 >         * of all keys
4268 >         */
4269 >        public int reduceToInt(ObjectToInt<? super K> transformer,
4270 >                               int basis,
4271 >                               IntByIntToInt reducer) {
4272 >            return ForkJoinTasks.reduceKeysToInt
4273 >                (map, transformer, basis, reducer).invoke();
4274          }
4275 +        
4276      }
4277  
4278 <    final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
4279 <        public Iterator<Map.Entry<K,V>> iterator() {
4280 <            return new EntryIterator();
4278 >    /**
4279 >     * A view of a ConcurrentHashMap as a {@link Collection} of
4280 >     * values, in which additions are disabled. This class cannot be
4281 >     * directly instantiated. See {@link #values},
4282 >     *
4283 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
4284 >     * that will never throw {@link ConcurrentModificationException},
4285 >     * and guarantees to traverse elements as they existed upon
4286 >     * construction of the iterator, and may (but is not guaranteed to)
4287 >     * reflect any modifications subsequent to construction.
4288 >     */
4289 >    public static final class ValuesView<K,V> extends CHMView<K,V>
4290 >        implements Collection<V> {
4291 >        ValuesView(ConcurrentHashMap<K, V> map)   { super(map); }
4292 >        public final boolean contains(Object o) { return map.containsValue(o); }
4293 >        public final boolean remove(Object o) {
4294 >            if (o != null) {
4295 >                Iterator<V> it = new ValueIterator<K,V>(map);
4296 >                while (it.hasNext()) {
4297 >                    if (o.equals(it.next())) {
4298 >                        it.remove();
4299 >                        return true;
4300 >                    }
4301 >                }
4302 >            }
4303 >            return false;
4304 >        }
4305 >
4306 >        /**
4307 >         * Returns a "weakly consistent" iterator that will never
4308 >         * throw {@link ConcurrentModificationException}, and
4309 >         * guarantees to traverse elements as they existed upon
4310 >         * construction of the iterator, and may (but is not
4311 >         * guaranteed to) reflect any modifications subsequent to
4312 >         * construction.
4313 >         *
4314 >         * @return an iterator over the values of this map
4315 >         */
4316 >        public final Iterator<V> iterator() {
4317 >            return new ValueIterator<K,V>(map);
4318          }
4319 <        public boolean contains(Object o) {
4320 <            if (!(o instanceof Map.Entry))
1226 <                return false;
1227 <            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
1228 <            V v = ConcurrentHashMap.this.get(e.getKey());
1229 <            return v != null && v.equals(e.getValue());
4319 >        public final boolean add(V e) {
4320 >            throw new UnsupportedOperationException();
4321          }
4322 <        public boolean remove(Object o) {
4323 <            if (!(o instanceof Map.Entry))
4324 <                return false;
4325 <            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
4326 <            return ConcurrentHashMap.this.remove(e.getKey(), e.getValue());
4322 >        public final boolean addAll(Collection<? extends V> c) {
4323 >            throw new UnsupportedOperationException();
4324 >        }
4325 >
4326 >        /**
4327 >         * Performs the given action for each value.
4328 >         *
4329 >         * @param action the action
4330 >         */
4331 >        public void forEach(Action<V> action) {
4332 >            ForkJoinTasks.forEachValue
4333 >                (map, action).invoke();
4334 >        }
4335 >
4336 >        /**
4337 >         * Performs the given action for each non-null transformation
4338 >         * of each value.
4339 >         *
4340 >         * @param transformer a function returning the transformation
4341 >         * for an element, or null of there is no transformation (in
4342 >         * which case the action is not applied).
4343 >         */
4344 >        public <U> void forEach(Fun<? super V, ? extends U> transformer,
4345 >                                     Action<U> action) {
4346 >            ForkJoinTasks.forEachValue
4347 >                (map, transformer, action).invoke();
4348 >        }
4349 >
4350 >        /**
4351 >         * Returns a non-null result from applying the given search
4352 >         * function on each value, or null if none.  Upon success,
4353 >         * further element processing is suppressed and the results of
4354 >         * any other parallel invocations of the search function are
4355 >         * ignored.
4356 >         *
4357 >         * @param searchFunction a function returning a non-null
4358 >         * result on success, else null
4359 >         * @return a non-null result from applying the given search
4360 >         * function on each value, or null if none
4361 >         *
4362 >         */
4363 >        public <U> U search(Fun<? super V, ? extends U> searchFunction) {
4364 >            return ForkJoinTasks.searchValues
4365 >                (map, searchFunction).invoke();
4366          }
4367 <        public int size() {
4368 <            return ConcurrentHashMap.this.size();
4367 >
4368 >        /**
4369 >         * Returns the result of accumulating all values using the
4370 >         * given reducer to combine values, or null if none.
4371 >         *
4372 >         * @param reducer a commutative associative combining function
4373 >         * @return  the result of accumulating all values
4374 >         */
4375 >        public V reduce(BiFun<? super V, ? super V, ? extends V> reducer) {
4376 >            return ForkJoinTasks.reduceValues
4377 >                (map, reducer).invoke();
4378          }
4379 <        public void clear() {
4380 <            ConcurrentHashMap.this.clear();
4379 >
4380 >        /**
4381 >         * Returns the result of accumulating the given transformation
4382 >         * of all values using the given reducer to combine values, or
4383 >         * null if none.
4384 >         *
4385 >         * @param transformer a function returning the transformation
4386 >         * for an element, or null of there is no transformation (in
4387 >         * which case it is not combined).
4388 >         * @param reducer a commutative associative combining function
4389 >         * @return the result of accumulating the given transformation
4390 >         * of all values
4391 >         */
4392 >        public <U> U reduce(Fun<? super V, ? extends U> transformer,
4393 >                            BiFun<? super U, ? super U, ? extends U> reducer) {
4394 >            return ForkJoinTasks.reduceValues
4395 >                (map, transformer, reducer).invoke();
4396          }
4397 <        public Object[] toArray() {
4398 <            Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size());
4399 <            for (Map.Entry<K,V> e : this)
4400 <                c.add(e);
4401 <            return c.toArray();
4402 <        }
4403 <        public <T> T[] toArray(T[] a) {
4404 <            Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size());
4405 <            for (Map.Entry<K,V> e : this)
4406 <                c.add(e);
4407 <            return c.toArray(a);
4397 >        
4398 >        /**
4399 >         * Returns the result of accumulating the given transformation
4400 >         * of all values using the given reducer to combine values,
4401 >         * and the given basis as an identity value.
4402 >         *
4403 >         * @param transformer a function returning the transformation
4404 >         * for an element
4405 >         * @param basis the identity (initial default value) for the reduction
4406 >         * @param reducer a commutative associative combining function
4407 >         * @return the result of accumulating the given transformation
4408 >         * of all values
4409 >         */
4410 >        public double reduceToDouble(ObjectToDouble<? super V> transformer,
4411 >                                     double basis,
4412 >                                     DoubleByDoubleToDouble reducer) {
4413 >            return ForkJoinTasks.reduceValuesToDouble
4414 >                (map, transformer, basis, reducer).invoke();
4415 >        }
4416 >
4417 >        /**
4418 >         * Returns the result of accumulating the given transformation
4419 >         * of all values using the given reducer to combine values,
4420 >         * and the given basis as an identity value.
4421 >         *
4422 >         * @param transformer a function returning the transformation
4423 >         * for an element
4424 >         * @param basis the identity (initial default value) for the reduction
4425 >         * @param reducer a commutative associative combining function
4426 >         * @return the result of accumulating the given transformation
4427 >         * of all values
4428 >         */
4429 >        public long reduceToLong(ObjectToLong<? super V> transformer,
4430 >                                 long basis,
4431 >                                 LongByLongToLong reducer) {
4432 >            return ForkJoinTasks.reduceValuesToLong
4433 >                (map, transformer, basis, reducer).invoke();
4434 >        }
4435 >
4436 >        /**
4437 >         * Returns the result of accumulating the given transformation
4438 >         * of all values using the given reducer to combine values,
4439 >         * and the given basis as an identity value.
4440 >         *
4441 >         * @param transformer a function returning the transformation
4442 >         * for an element
4443 >         * @param basis the identity (initial default value) for the reduction
4444 >         * @param reducer a commutative associative combining function
4445 >         * @return the result of accumulating the given transformation
4446 >         * of all values
4447 >         */
4448 >        public int reduceToInt(ObjectToInt<? super V> transformer,
4449 >                               int basis,
4450 >                               IntByIntToInt reducer) {
4451 >            return ForkJoinTasks.reduceValuesToInt
4452 >                (map, transformer, basis, reducer).invoke();
4453          }
4454  
4455      }
4456  
4457 <    /* ---------------- Serialization Support -------------- */
4457 >    /**
4458 >     * A view of a ConcurrentHashMap as a {@link Set} of (key, value)
4459 >     * entries.  This class cannot be directly instantiated. See
4460 >     * {@link #entrySet}.
4461 >     */
4462 >    public static final class EntrySetView<K,V> extends CHMView<K,V>
4463 >        implements Set<Map.Entry<K,V>> {
4464 >        EntrySetView(ConcurrentHashMap<K, V> map) { super(map); }
4465 >        public final boolean contains(Object o) {
4466 >            Object k, v, r; Map.Entry<?,?> e;
4467 >            return ((o instanceof Map.Entry) &&
4468 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4469 >                    (r = map.get(k)) != null &&
4470 >                    (v = e.getValue()) != null &&
4471 >                    (v == r || v.equals(r)));
4472 >        }
4473 >        public final boolean remove(Object o) {
4474 >            Object k, v; Map.Entry<?,?> e;
4475 >            return ((o instanceof Map.Entry) &&
4476 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4477 >                    (v = e.getValue()) != null &&
4478 >                    map.remove(k, v));
4479 >        }
4480 >
4481 >        /**
4482 >         * Returns a "weakly consistent" iterator that will never
4483 >         * throw {@link ConcurrentModificationException}, and
4484 >         * guarantees to traverse elements as they existed upon
4485 >         * construction of the iterator, and may (but is not
4486 >         * guaranteed to) reflect any modifications subsequent to
4487 >         * construction.
4488 >         *
4489 >         * @return an iterator over the entries of this map
4490 >         */
4491 >        public final Iterator<Map.Entry<K,V>> iterator() {
4492 >            return new EntryIterator<K,V>(map);
4493 >        }
4494 >
4495 >        public final boolean add(Entry<K,V> e) {
4496 >            K key = e.getKey();
4497 >            V value = e.getValue();
4498 >            if (key == null || value == null)
4499 >                throw new NullPointerException();
4500 >            return map.internalPut(key, value) == null;
4501 >        }
4502 >        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
4503 >            boolean added = false;
4504 >            for (Entry<K,V> e : c) {
4505 >                if (add(e))
4506 >                    added = true;
4507 >            }
4508 >            return added;
4509 >        }
4510 >        public boolean equals(Object o) {
4511 >            Set<?> c;
4512 >            return ((o instanceof Set) &&
4513 >                    ((c = (Set<?>)o) == this ||
4514 >                     (containsAll(c) && c.containsAll(this))));
4515 >        }
4516 >
4517 >        /**
4518 >         * Performs the given action for each entry.
4519 >         *
4520 >         * @param action the action
4521 >         */
4522 >        public void forEach(Action<Map.Entry<K,V>> action) {
4523 >            ForkJoinTasks.forEachEntry
4524 >                (map, action).invoke();
4525 >        }
4526 >
4527 >        /**
4528 >         * Performs the given action for each non-null transformation
4529 >         * of each entry.
4530 >         *
4531 >         * @param transformer a function returning the transformation
4532 >         * for an element, or null of there is no transformation (in
4533 >         * which case the action is not applied).
4534 >         * @param action the action
4535 >         */
4536 >        public <U> void forEach(Fun<Map.Entry<K,V>, ? extends U> transformer,
4537 >                                Action<U> action) {
4538 >            ForkJoinTasks.forEachEntry
4539 >                (map, transformer, action).invoke();
4540 >        }
4541 >
4542 >        /**
4543 >         * Returns a non-null result from applying the given search
4544 >         * function on each entry, or null if none.  Upon success,
4545 >         * further element processing is suppressed and the results of
4546 >         * any other parallel invocations of the search function are
4547 >         * ignored.
4548 >         *
4549 >         * @param searchFunction a function returning a non-null
4550 >         * result on success, else null
4551 >         * @return a non-null result from applying the given search
4552 >         * function on each entry, or null if none
4553 >         */
4554 >        public <U> U search(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4555 >            return ForkJoinTasks.searchEntries
4556 >                (map, searchFunction).invoke();
4557 >        }
4558 >
4559 >        /**
4560 >         * Returns the result of accumulating all entries using the
4561 >         * given reducer to combine values, or null if none.
4562 >         *
4563 >         * @param reducer a commutative associative combining function
4564 >         * @return the result of accumulating all entries
4565 >         */
4566 >        public Map.Entry<K,V> reduce(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4567 >            return ForkJoinTasks.reduceEntries
4568 >                (map, reducer).invoke();
4569 >        }
4570 >
4571 >        /**
4572 >         * Returns the result of accumulating the given transformation
4573 >         * of all entries using the given reducer to combine values,
4574 >         * or null if none.
4575 >         *
4576 >         * @param transformer a function returning the transformation
4577 >         * for an element, or null of there is no transformation (in
4578 >         * which case it is not combined).
4579 >         * @param reducer a commutative associative combining function
4580 >         * @return the result of accumulating the given transformation
4581 >         * of all entries
4582 >         */
4583 >        public <U> U reduce(Fun<Map.Entry<K,V>, ? extends U> transformer,
4584 >                            BiFun<? super U, ? super U, ? extends U> reducer) {
4585 >            return ForkJoinTasks.reduceEntries
4586 >                (map, transformer, reducer).invoke();
4587 >        }
4588 >
4589 >        /**
4590 >         * Returns the result of accumulating the given transformation
4591 >         * of all entries using the given reducer to combine values,
4592 >         * and the given basis as an identity value.
4593 >         *
4594 >         * @param transformer a function returning the transformation
4595 >         * for an element
4596 >         * @param basis the identity (initial default value) for the reduction
4597 >         * @param reducer a commutative associative combining function
4598 >         * @return the result of accumulating the given transformation
4599 >         * of all entries
4600 >         */
4601 >        public double reduceToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4602 >                                     double basis,
4603 >                                     DoubleByDoubleToDouble reducer) {
4604 >            return ForkJoinTasks.reduceEntriesToDouble
4605 >                (map, transformer, basis, reducer).invoke();
4606 >        }
4607 >
4608 >        /**
4609 >         * Returns the result of accumulating the given transformation
4610 >         * of all entries using the given reducer to combine values,
4611 >         * and the given basis as an identity value.
4612 >         *
4613 >         * @param transformer a function returning the transformation
4614 >         * for an element
4615 >         * @param basis the identity (initial default value) for the reduction
4616 >         * @param reducer a commutative associative combining function
4617 >         * @return  the result of accumulating the given transformation
4618 >         * of all entries
4619 >         */
4620 >        public long reduceToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4621 >                                 long basis,
4622 >                                 LongByLongToLong reducer) {
4623 >            return ForkJoinTasks.reduceEntriesToLong
4624 >                (map, transformer, basis, reducer).invoke();
4625 >        }
4626 >
4627 >        /**
4628 >         * Returns the result of accumulating the given transformation
4629 >         * of all entries using the given reducer to combine values,
4630 >         * and the given basis as an identity value.
4631 >         *
4632 >         * @param transformer a function returning the transformation
4633 >         * for an element
4634 >         * @param basis the identity (initial default value) for the reduction
4635 >         * @param reducer a commutative associative combining function
4636 >         * @return the result of accumulating the given transformation
4637 >         * of all entries
4638 >         */
4639 >        public int reduceToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4640 >                               int basis,
4641 >                               IntByIntToInt reducer) {
4642 >            return ForkJoinTasks.reduceEntriesToInt
4643 >                (map, transformer, basis, reducer).invoke();
4644 >        }
4645 >
4646 >    }
4647 >
4648 >    // ---------------------------------------------------------------------
4649  
4650      /**
4651 <     * Save the state of the <tt>ConcurrentHashMap</tt> instance to a
4652 <     * stream (i.e., serialize it).
4653 <     * @param s the stream
4654 <     * @serialData
4655 <     * the key (Object) and value (Object)
4656 <     * for each key-value mapping, followed by a null pair.
4657 <     * The key-value mappings are emitted in no particular order.
4651 >     * Predefined tasks for performing bulk parallel operations on
4652 >     * ConcurrentHashMaps. These tasks follow the forms and rules used
4653 >     * for bulk operations. Each method has the same name, but returns
4654 >     * a task rather than invoking it. These methods may be useful in
4655 >     * custom applications such as submitting a task without waiting
4656 >     * for completion, using a custom pool, or combining with other
4657 >     * tasks.
4658       */
4659 <    private void writeObject(java.io.ObjectOutputStream s) throws IOException  {
4660 <        s.defaultWriteObject();
4659 >    public static class ForkJoinTasks {
4660 >        private ForkJoinTasks() {}
4661  
4662 <        for (int k = 0; k < segments.length; ++k) {
4663 <            Segment<K,V> seg = segments[k];
4664 <            seg.lock();
4665 <            try {
4666 <                HashEntry<K,V>[] tab = seg.table;
4667 <                for (int i = 0; i < tab.length; ++i) {
4668 <                    for (HashEntry<K,V> e = tab[i]; e != null; e = e.next) {
4669 <                        s.writeObject(e.key);
4670 <                        s.writeObject(e.value);
4671 <                    }
4662 >        /**
4663 >         * Returns a task that when invoked, performs the given
4664 >         * action for each (key, value)
4665 >         *
4666 >         * @param map the map
4667 >         * @param action the action
4668 >         * @return the task
4669 >         */
4670 >        public static <K,V> ForkJoinTask<Void> forEach
4671 >            (ConcurrentHashMap<K,V> map,
4672 >             BiAction<K,V> action) {
4673 >            if (action == null) throw new NullPointerException();
4674 >            return new ForEachMappingTask<K,V>(map, null, -1, null, action);
4675 >        }
4676 >
4677 >        /**
4678 >         * Returns a task that when invoked, performs the given
4679 >         * action for each non-null transformation of each (key, value)
4680 >         *
4681 >         * @param map the map
4682 >         * @param transformer a function returning the transformation
4683 >         * for an element, or null if there is no transformation (in
4684 >         * which case the action is not applied)
4685 >         * @param action the action
4686 >         * @return the task
4687 >         */
4688 >        public static <K,V,U> ForkJoinTask<Void> forEach
4689 >            (ConcurrentHashMap<K,V> map,
4690 >             BiFun<? super K, ? super V, ? extends U> transformer,
4691 >             Action<U> action) {
4692 >            if (transformer == null || action == null)
4693 >                throw new NullPointerException();
4694 >            return new ForEachTransformedMappingTask<K,V,U>
4695 >                (map, null, -1, null, transformer, action);
4696 >        }
4697 >
4698 >        /**
4699 >         * Returns a task that when invoked, returns a non-null result
4700 >         * from applying the given search function on each (key,
4701 >         * value), or null if none. Upon success, further element
4702 >         * processing is suppressed and the results of any other
4703 >         * parallel invocations of the search function are ignored.
4704 >         *
4705 >         * @param map the map
4706 >         * @param searchFunction a function returning a non-null
4707 >         * result on success, else null
4708 >         * @return the task
4709 >         */
4710 >        public static <K,V,U> ForkJoinTask<U> search
4711 >            (ConcurrentHashMap<K,V> map,
4712 >             BiFun<? super K, ? super V, ? extends U> searchFunction) {
4713 >            if (searchFunction == null) throw new NullPointerException();
4714 >            return new SearchMappingsTask<K,V,U>
4715 >                (map, null, -1, null, searchFunction,
4716 >                 new AtomicReference<U>());
4717 >        }
4718 >
4719 >        /**
4720 >         * Returns a task that when invoked, returns the result of
4721 >         * accumulating the given transformation of all (key, value) pairs
4722 >         * using the given reducer to combine values, or null if none.
4723 >         *
4724 >         * @param map the map
4725 >         * @param transformer a function returning the transformation
4726 >         * for an element, or null if there is no transformation (in
4727 >         * which case it is not combined).
4728 >         * @param reducer a commutative associative combining function
4729 >         * @return the task
4730 >         */
4731 >        public static <K,V,U> ForkJoinTask<U> reduce
4732 >            (ConcurrentHashMap<K,V> map,
4733 >             BiFun<? super K, ? super V, ? extends U> transformer,
4734 >             BiFun<? super U, ? super U, ? extends U> reducer) {
4735 >            if (transformer == null || reducer == null)
4736 >                throw new NullPointerException();
4737 >            return new MapReduceMappingsTask<K,V,U>
4738 >                (map, null, -1, null, transformer, reducer);
4739 >        }
4740 >
4741 >        /**
4742 >         * Returns a task that when invoked, returns the result of
4743 >         * accumulating the given transformation of all (key, value) pairs
4744 >         * using the given reducer to combine values, and the given
4745 >         * basis as an identity value.
4746 >         *
4747 >         * @param map the map
4748 >         * @param transformer a function returning the transformation
4749 >         * for an element
4750 >         * @param basis the identity (initial default value) for the reduction
4751 >         * @param reducer a commutative associative combining function
4752 >         * @return the task
4753 >         */
4754 >        public static <K,V> ForkJoinTask<Double> reduceToDouble
4755 >            (ConcurrentHashMap<K,V> map,
4756 >             ObjectByObjectToDouble<? super K, ? super V> transformer,
4757 >             double basis,
4758 >             DoubleByDoubleToDouble reducer) {
4759 >            if (transformer == null || reducer == null)
4760 >                throw new NullPointerException();
4761 >            return new MapReduceMappingsToDoubleTask<K,V>
4762 >                (map, null, -1, null, transformer, basis, reducer);
4763 >        }
4764 >
4765 >        /**
4766 >         * Returns a task that when invoked, returns the result of
4767 >         * accumulating the given transformation of all (key, value) pairs
4768 >         * using the given reducer to combine values, and the given
4769 >         * basis as an identity value.
4770 >         *
4771 >         * @param map the map
4772 >         * @param transformer a function returning the transformation
4773 >         * for an element
4774 >         * @param basis the identity (initial default value) for the reduction
4775 >         * @param reducer a commutative associative combining function
4776 >         * @return the task
4777 >         */
4778 >        public static <K,V> ForkJoinTask<Long> reduceToLong
4779 >            (ConcurrentHashMap<K,V> map,
4780 >             ObjectByObjectToLong<? super K, ? super V> transformer,
4781 >             long basis,
4782 >             LongByLongToLong reducer) {
4783 >            if (transformer == null || reducer == null)
4784 >                throw new NullPointerException();
4785 >            return new MapReduceMappingsToLongTask<K,V>
4786 >                (map, null, -1, null, transformer, basis, reducer);
4787 >        }
4788 >
4789 >        /**
4790 >         * Returns a task that when invoked, returns the result of
4791 >         * accumulating the given transformation of all (key, value) pairs
4792 >         * using the given reducer to combine values, and the given
4793 >         * basis as an identity value.
4794 >         *
4795 >         * @param transformer a function returning the transformation
4796 >         * for an element
4797 >         * @param basis the identity (initial default value) for the reduction
4798 >         * @param reducer a commutative associative combining function
4799 >         * @return the task
4800 >         */
4801 >        public static <K,V> ForkJoinTask<Integer> reduceToInt
4802 >            (ConcurrentHashMap<K,V> map,
4803 >             ObjectByObjectToInt<? super K, ? super V> transformer,
4804 >             int basis,
4805 >             IntByIntToInt reducer) {
4806 >            if (transformer == null || reducer == null)
4807 >                throw new NullPointerException();
4808 >            return new MapReduceMappingsToIntTask<K,V>
4809 >                (map, null, -1, null, transformer, basis, reducer);
4810 >        }
4811 >
4812 >        /**
4813 >         * Returns a task that when invoked, performs the given action
4814 >         * for each key.
4815 >         *
4816 >         * @param map the map
4817 >         * @param action the action
4818 >         * @return the task
4819 >         */
4820 >        public static <K,V> ForkJoinTask<Void> forEachKey
4821 >            (ConcurrentHashMap<K,V> map,
4822 >             Action<K> action) {
4823 >            if (action == null) throw new NullPointerException();
4824 >            return new ForEachKeyTask<K,V>(map, null, -1, null, action);
4825 >        }
4826 >
4827 >        /**
4828 >         * Returns a task that when invoked, performs the given action
4829 >         * for each non-null transformation of each key.
4830 >         *
4831 >         * @param map the map
4832 >         * @param transformer a function returning the transformation
4833 >         * for an element, or null if there is no transformation (in
4834 >         * which case the action is not applied)
4835 >         * @param action the action
4836 >         * @return the task
4837 >         */
4838 >        public static <K,V,U> ForkJoinTask<Void> forEachKey
4839 >            (ConcurrentHashMap<K,V> map,
4840 >             Fun<? super K, ? extends U> transformer,
4841 >             Action<U> action) {
4842 >            if (transformer == null || action == null)
4843 >                throw new NullPointerException();
4844 >            return new ForEachTransformedKeyTask<K,V,U>
4845 >                (map, null, -1, null, transformer, action);
4846 >        }
4847 >
4848 >        /**
4849 >         * Returns a task that when invoked, returns a non-null result
4850 >         * from applying the given search function on each key, or
4851 >         * null if none.  Upon success, further element processing is
4852 >         * suppressed and the results of any other parallel
4853 >         * invocations of the search function are ignored.
4854 >         *
4855 >         * @param map the map
4856 >         * @param searchFunction a function returning a non-null
4857 >         * result on success, else null
4858 >         * @return the task
4859 >         */
4860 >        public static <K,V,U> ForkJoinTask<U> searchKeys
4861 >            (ConcurrentHashMap<K,V> map,
4862 >             Fun<? super K, ? extends U> searchFunction) {
4863 >            if (searchFunction == null) throw new NullPointerException();
4864 >            return new SearchKeysTask<K,V,U>
4865 >                (map, null, -1, null, searchFunction,
4866 >                 new AtomicReference<U>());
4867 >        }
4868 >
4869 >        /**
4870 >         * Returns a task that when invoked, returns the result of
4871 >         * accumulating all keys using the given reducer to combine
4872 >         * values, or null if none.
4873 >         *
4874 >         * @param map the map
4875 >         * @param reducer a commutative associative combining function
4876 >         * @return the task
4877 >         */
4878 >        public static <K,V> ForkJoinTask<K> reduceKeys
4879 >            (ConcurrentHashMap<K,V> map,
4880 >             BiFun<? super K, ? super K, ? extends K> reducer) {
4881 >            if (reducer == null) throw new NullPointerException();
4882 >            return new ReduceKeysTask<K,V>
4883 >                (map, null, -1, null, reducer);
4884 >        }
4885 >
4886 >        /**
4887 >         * Returns a task that when invoked, returns the result of
4888 >         * accumulating the given transformation of all keys using the given
4889 >         * reducer to combine values, or null if none.
4890 >         *
4891 >         * @param map the map
4892 >         * @param transformer a function returning the transformation
4893 >         * for an element, or null if there is no transformation (in
4894 >         * which case it is not combined).
4895 >         * @param reducer a commutative associative combining function
4896 >         * @return the task
4897 >         */
4898 >        public static <K,V,U> ForkJoinTask<U> reduceKeys
4899 >            (ConcurrentHashMap<K,V> map,
4900 >             Fun<? super K, ? extends U> transformer,
4901 >             BiFun<? super U, ? super U, ? extends U> reducer) {
4902 >            if (transformer == null || reducer == null)
4903 >                throw new NullPointerException();
4904 >            return new MapReduceKeysTask<K,V,U>
4905 >                (map, null, -1, null, transformer, reducer);
4906 >        }
4907 >
4908 >        /**
4909 >         * Returns a task that when invoked, returns the result of
4910 >         * accumulating the given transformation of all keys using the given
4911 >         * reducer to combine values, and the given basis as an
4912 >         * identity value.
4913 >         *
4914 >         * @param map the map
4915 >         * @param transformer a function returning the transformation
4916 >         * for an element
4917 >         * @param basis the identity (initial default value) for the reduction
4918 >         * @param reducer a commutative associative combining function
4919 >         * @return the task
4920 >         */
4921 >        public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4922 >            (ConcurrentHashMap<K,V> map,
4923 >             ObjectToDouble<? super K> transformer,
4924 >             double basis,
4925 >             DoubleByDoubleToDouble reducer) {
4926 >            if (transformer == null || reducer == null)
4927 >                throw new NullPointerException();
4928 >            return new MapReduceKeysToDoubleTask<K,V>
4929 >                (map, null, -1, null, transformer, basis, reducer);
4930 >        }
4931 >
4932 >        /**
4933 >         * Returns a task that when invoked, returns the result of
4934 >         * accumulating the given transformation of all keys using the given
4935 >         * reducer to combine values, and the given basis as an
4936 >         * identity value.
4937 >         *
4938 >         * @param map the map
4939 >         * @param transformer a function returning the transformation
4940 >         * for an element
4941 >         * @param basis the identity (initial default value) for the reduction
4942 >         * @param reducer a commutative associative combining function
4943 >         * @return the task
4944 >         */
4945 >        public static <K,V> ForkJoinTask<Long> reduceKeysToLong
4946 >            (ConcurrentHashMap<K,V> map,
4947 >             ObjectToLong<? super K> transformer,
4948 >             long basis,
4949 >             LongByLongToLong reducer) {
4950 >            if (transformer == null || reducer == null)
4951 >                throw new NullPointerException();
4952 >            return new MapReduceKeysToLongTask<K,V>
4953 >                (map, null, -1, null, transformer, basis, reducer);
4954 >        }
4955 >
4956 >        /**
4957 >         * Returns a task that when invoked, returns the result of
4958 >         * accumulating the given transformation of all keys using the given
4959 >         * reducer to combine values, and the given basis as an
4960 >         * identity value.
4961 >         *
4962 >         * @param map the map
4963 >         * @param transformer a function returning the transformation
4964 >         * for an element
4965 >         * @param basis the identity (initial default value) for the reduction
4966 >         * @param reducer a commutative associative combining function
4967 >         * @return the task
4968 >         */
4969 >        public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
4970 >            (ConcurrentHashMap<K,V> map,
4971 >             ObjectToInt<? super K> transformer,
4972 >             int basis,
4973 >             IntByIntToInt reducer) {
4974 >            if (transformer == null || reducer == null)
4975 >                throw new NullPointerException();
4976 >            return new MapReduceKeysToIntTask<K,V>
4977 >                (map, null, -1, null, transformer, basis, reducer);
4978 >        }
4979 >
4980 >        /**
4981 >         * Returns a task that when invoked, performs the given action
4982 >         * for each value.
4983 >         *
4984 >         * @param map the map
4985 >         * @param action the action
4986 >         */
4987 >        public static <K,V> ForkJoinTask<Void> forEachValue
4988 >            (ConcurrentHashMap<K,V> map,
4989 >             Action<V> action) {
4990 >            if (action == null) throw new NullPointerException();
4991 >            return new ForEachValueTask<K,V>(map, null, -1, null, action);
4992 >        }
4993 >
4994 >        /**
4995 >         * Returns a task that when invoked, performs the given action
4996 >         * for each non-null transformation of each value.
4997 >         *
4998 >         * @param map the map
4999 >         * @param transformer a function returning the transformation
5000 >         * for an element, or null if there is no transformation (in
5001 >         * which case the action is not applied)
5002 >         * @param action the action
5003 >         */
5004 >        public static <K,V,U> ForkJoinTask<Void> forEachValue
5005 >            (ConcurrentHashMap<K,V> map,
5006 >             Fun<? super V, ? extends U> transformer,
5007 >             Action<U> action) {
5008 >            if (transformer == null || action == null)
5009 >                throw new NullPointerException();
5010 >            return new ForEachTransformedValueTask<K,V,U>
5011 >                (map, null, -1, null, transformer, action);
5012 >        }
5013 >
5014 >        /**
5015 >         * Returns a task that when invoked, returns a non-null result
5016 >         * from applying the given search function on each value, or
5017 >         * null if none.  Upon success, further element processing is
5018 >         * suppressed and the results of any other parallel
5019 >         * invocations of the search function are ignored.
5020 >         *
5021 >         * @param map the map
5022 >         * @param searchFunction a function returning a non-null
5023 >         * result on success, else null
5024 >         * @return the task
5025 >         */
5026 >        public static <K,V,U> ForkJoinTask<U> searchValues
5027 >            (ConcurrentHashMap<K,V> map,
5028 >             Fun<? super V, ? extends U> searchFunction) {
5029 >            if (searchFunction == null) throw new NullPointerException();
5030 >            return new SearchValuesTask<K,V,U>
5031 >                (map, null, -1, null, searchFunction,
5032 >                 new AtomicReference<U>());
5033 >        }
5034 >
5035 >        /**
5036 >         * Returns a task that when invoked, returns the result of
5037 >         * accumulating all values using the given reducer to combine
5038 >         * values, or null if none.
5039 >         *
5040 >         * @param map the map
5041 >         * @param reducer a commutative associative combining function
5042 >         * @return the task
5043 >         */
5044 >        public static <K,V> ForkJoinTask<V> reduceValues
5045 >            (ConcurrentHashMap<K,V> map,
5046 >             BiFun<? super V, ? super V, ? extends V> reducer) {
5047 >            if (reducer == null) throw new NullPointerException();
5048 >            return new ReduceValuesTask<K,V>
5049 >                (map, null, -1, null, reducer);
5050 >        }
5051 >
5052 >        /**
5053 >         * Returns a task that when invoked, returns the result of
5054 >         * accumulating the given transformation of all values using the
5055 >         * given reducer to combine values, or null if none.
5056 >         *
5057 >         * @param map the map
5058 >         * @param transformer a function returning the transformation
5059 >         * for an element, or null if there is no transformation (in
5060 >         * which case it is not combined).
5061 >         * @param reducer a commutative associative combining function
5062 >         * @return the task
5063 >         */
5064 >        public static <K,V,U> ForkJoinTask<U> reduceValues
5065 >            (ConcurrentHashMap<K,V> map,
5066 >             Fun<? super V, ? extends U> transformer,
5067 >             BiFun<? super U, ? super U, ? extends U> reducer) {
5068 >            if (transformer == null || reducer == null)
5069 >                throw new NullPointerException();
5070 >            return new MapReduceValuesTask<K,V,U>
5071 >                (map, null, -1, null, transformer, reducer);
5072 >        }
5073 >
5074 >        /**
5075 >         * Returns a task that when invoked, returns the result of
5076 >         * accumulating the given transformation of all values using the
5077 >         * given reducer to combine values, and the given basis as an
5078 >         * identity value.
5079 >         *
5080 >         * @param map the map
5081 >         * @param transformer a function returning the transformation
5082 >         * for an element
5083 >         * @param basis the identity (initial default value) for the reduction
5084 >         * @param reducer a commutative associative combining function
5085 >         * @return the task
5086 >         */
5087 >        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
5088 >            (ConcurrentHashMap<K,V> map,
5089 >             ObjectToDouble<? super V> transformer,
5090 >             double basis,
5091 >             DoubleByDoubleToDouble reducer) {
5092 >            if (transformer == null || reducer == null)
5093 >                throw new NullPointerException();
5094 >            return new MapReduceValuesToDoubleTask<K,V>
5095 >                (map, null, -1, null, transformer, basis, reducer);
5096 >        }
5097 >
5098 >        /**
5099 >         * Returns a task that when invoked, returns the result of
5100 >         * accumulating the given transformation of all values using the
5101 >         * given reducer to combine values, and the given basis as an
5102 >         * identity value.
5103 >         *
5104 >         * @param map the map
5105 >         * @param transformer a function returning the transformation
5106 >         * for an element
5107 >         * @param basis the identity (initial default value) for the reduction
5108 >         * @param reducer a commutative associative combining function
5109 >         * @return the task
5110 >         */
5111 >        public static <K,V> ForkJoinTask<Long> reduceValuesToLong
5112 >            (ConcurrentHashMap<K,V> map,
5113 >             ObjectToLong<? super V> transformer,
5114 >             long basis,
5115 >             LongByLongToLong reducer) {
5116 >            if (transformer == null || reducer == null)
5117 >                throw new NullPointerException();
5118 >            return new MapReduceValuesToLongTask<K,V>
5119 >                (map, null, -1, null, transformer, basis, reducer);
5120 >        }
5121 >
5122 >        /**
5123 >         * Returns a task that when invoked, returns the result of
5124 >         * accumulating the given transformation of all values using the
5125 >         * given reducer to combine values, and the given basis as an
5126 >         * identity value.
5127 >         *
5128 >         * @param map the map
5129 >         * @param transformer a function returning the transformation
5130 >         * for an element
5131 >         * @param basis the identity (initial default value) for the reduction
5132 >         * @param reducer a commutative associative combining function
5133 >         * @return the task
5134 >         */
5135 >        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
5136 >            (ConcurrentHashMap<K,V> map,
5137 >             ObjectToInt<? super V> transformer,
5138 >             int basis,
5139 >             IntByIntToInt reducer) {
5140 >            if (transformer == null || reducer == null)
5141 >                throw new NullPointerException();
5142 >            return new MapReduceValuesToIntTask<K,V>
5143 >                (map, null, -1, null, transformer, basis, reducer);
5144 >        }
5145 >
5146 >        /**
5147 >         * Returns a task that when invoked, perform the given action
5148 >         * for each entry.
5149 >         *
5150 >         * @param map the map
5151 >         * @param action the action
5152 >         */
5153 >        public static <K,V> ForkJoinTask<Void> forEachEntry
5154 >            (ConcurrentHashMap<K,V> map,
5155 >             Action<Map.Entry<K,V>> action) {
5156 >            if (action == null) throw new NullPointerException();
5157 >            return new ForEachEntryTask<K,V>(map, null, -1, null, action);
5158 >        }
5159 >
5160 >        /**
5161 >         * Returns a task that when invoked, perform the given action
5162 >         * for each non-null transformation of each entry.
5163 >         *
5164 >         * @param map the map
5165 >         * @param transformer a function returning the transformation
5166 >         * for an element, or null if there is no transformation (in
5167 >         * which case the action is not applied)
5168 >         * @param action the action
5169 >         */
5170 >        public static <K,V,U> ForkJoinTask<Void> forEachEntry
5171 >            (ConcurrentHashMap<K,V> map,
5172 >             Fun<Map.Entry<K,V>, ? extends U> transformer,
5173 >             Action<U> action) {
5174 >            if (transformer == null || action == null)
5175 >                throw new NullPointerException();
5176 >            return new ForEachTransformedEntryTask<K,V,U>
5177 >                (map, null, -1, null, transformer, action);
5178 >        }
5179 >
5180 >        /**
5181 >         * Returns a task that when invoked, returns a non-null result
5182 >         * from applying the given search function on each entry, or
5183 >         * null if none.  Upon success, further element processing is
5184 >         * suppressed and the results of any other parallel
5185 >         * invocations of the search function are ignored.
5186 >         *
5187 >         * @param map the map
5188 >         * @param searchFunction a function returning a non-null
5189 >         * result on success, else null
5190 >         * @return the task
5191 >         */
5192 >        public static <K,V,U> ForkJoinTask<U> searchEntries
5193 >            (ConcurrentHashMap<K,V> map,
5194 >             Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
5195 >            if (searchFunction == null) throw new NullPointerException();
5196 >            return new SearchEntriesTask<K,V,U>
5197 >                (map, null, -1, null, searchFunction,
5198 >                 new AtomicReference<U>());
5199 >        }
5200 >
5201 >        /**
5202 >         * Returns a task that when invoked, returns the result of
5203 >         * accumulating all entries using the given reducer to combine
5204 >         * values, or null if none.
5205 >         *
5206 >         * @param map the map
5207 >         * @param reducer a commutative associative combining function
5208 >         * @return the task
5209 >         */
5210 >        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
5211 >            (ConcurrentHashMap<K,V> map,
5212 >             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5213 >            if (reducer == null) throw new NullPointerException();
5214 >            return new ReduceEntriesTask<K,V>
5215 >                (map, null, -1, null, reducer);
5216 >        }
5217 >
5218 >        /**
5219 >         * Returns a task that when invoked, returns the result of
5220 >         * accumulating the given transformation of all entries using the
5221 >         * given reducer to combine values, or null if none.
5222 >         *
5223 >         * @param map the map
5224 >         * @param transformer a function returning the transformation
5225 >         * for an element, or null if there is no transformation (in
5226 >         * which case it is not combined).
5227 >         * @param reducer a commutative associative combining function
5228 >         * @return the task
5229 >         */
5230 >        public static <K,V,U> ForkJoinTask<U> reduceEntries
5231 >            (ConcurrentHashMap<K,V> map,
5232 >             Fun<Map.Entry<K,V>, ? extends U> transformer,
5233 >             BiFun<? super U, ? super U, ? extends U> reducer) {
5234 >            if (transformer == null || reducer == null)
5235 >                throw new NullPointerException();
5236 >            return new MapReduceEntriesTask<K,V,U>
5237 >                (map, null, -1, null, transformer, reducer);
5238 >        }
5239 >
5240 >        /**
5241 >         * Returns a task that when invoked, returns the result of
5242 >         * accumulating the given transformation of all entries using the
5243 >         * given reducer to combine values, and the given basis as an
5244 >         * identity value.
5245 >         *
5246 >         * @param map the map
5247 >         * @param transformer a function returning the transformation
5248 >         * for an element
5249 >         * @param basis the identity (initial default value) for the reduction
5250 >         * @param reducer a commutative associative combining function
5251 >         * @return the task
5252 >         */
5253 >        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
5254 >            (ConcurrentHashMap<K,V> map,
5255 >             ObjectToDouble<Map.Entry<K,V>> transformer,
5256 >             double basis,
5257 >             DoubleByDoubleToDouble reducer) {
5258 >            if (transformer == null || reducer == null)
5259 >                throw new NullPointerException();
5260 >            return new MapReduceEntriesToDoubleTask<K,V>
5261 >                (map, null, -1, null, transformer, basis, reducer);
5262 >        }
5263 >
5264 >        /**
5265 >         * Returns a task that when invoked, returns the result of
5266 >         * accumulating the given transformation of all entries using the
5267 >         * given reducer to combine values, and the given basis as an
5268 >         * identity value.
5269 >         *
5270 >         * @param map the map
5271 >         * @param transformer a function returning the transformation
5272 >         * for an element
5273 >         * @param basis the identity (initial default value) for the reduction
5274 >         * @param reducer a commutative associative combining function
5275 >         * @return the task
5276 >         */
5277 >        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
5278 >            (ConcurrentHashMap<K,V> map,
5279 >             ObjectToLong<Map.Entry<K,V>> transformer,
5280 >             long basis,
5281 >             LongByLongToLong reducer) {
5282 >            if (transformer == null || reducer == null)
5283 >                throw new NullPointerException();
5284 >            return new MapReduceEntriesToLongTask<K,V>
5285 >                (map, null, -1, null, transformer, basis, reducer);
5286 >        }
5287 >
5288 >        /**
5289 >         * Returns a task that when invoked, returns the result of
5290 >         * accumulating the given transformation of all entries using the
5291 >         * given reducer to combine values, and the given basis as an
5292 >         * identity value.
5293 >         *
5294 >         * @param map the map
5295 >         * @param transformer a function returning the transformation
5296 >         * for an element
5297 >         * @param basis the identity (initial default value) for the reduction
5298 >         * @param reducer a commutative associative combining function
5299 >         * @return the task
5300 >         */
5301 >        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
5302 >            (ConcurrentHashMap<K,V> map,
5303 >             ObjectToInt<Map.Entry<K,V>> transformer,
5304 >             int basis,
5305 >             IntByIntToInt reducer) {
5306 >            if (transformer == null || reducer == null)
5307 >                throw new NullPointerException();
5308 >            return new MapReduceEntriesToIntTask<K,V>
5309 >                (map, null, -1, null, transformer, basis, reducer);
5310 >        }
5311 >    }
5312 >
5313 >    // -------------------------------------------------------
5314 >
5315 >    /**
5316 >     * Base for FJ tasks for bulk operations. This adds a variant of
5317 >     * CountedCompleters and some split and merge bookkeeping to
5318 >     * iterator functionality. The forEach and reduce methods are
5319 >     * similar to those illustrated in CountedCompleter documentation,
5320 >     * except that bottom-up reduction completions perform them within
5321 >     * their compute methods. The search methods are like forEach
5322 >     * except they continually poll for success and exit early.  Also,
5323 >     * exceptions are handled in a simpler manner, by just trying to
5324 >     * complete root task exceptionally.
5325 >     */
5326 >    @SuppressWarnings("serial") static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
5327 >        final BulkTask<K,V,?> parent;  // completion target
5328 >        int batch;                     // split control; -1 for unknown
5329 >        int pending;                   // completion control
5330 >
5331 >        BulkTask(ConcurrentHashMap<K,V> map, BulkTask<K,V,?> parent,
5332 >                 int batch) {
5333 >            super(map);
5334 >            this.parent = parent;
5335 >            this.batch = batch;
5336 >            if (parent != null && map != null) { // split parent
5337 >                Node[] t;
5338 >                if ((t = parent.tab) == null &&
5339 >                    (t = parent.tab = map.table) != null)
5340 >                    parent.baseLimit = parent.baseSize = t.length;
5341 >                this.tab = t;
5342 >                this.baseSize = parent.baseSize;
5343 >                int hi = this.baseLimit = parent.baseLimit;
5344 >                parent.baseLimit = this.index = this.baseIndex =
5345 >                    (hi + parent.baseIndex + 1) >>> 1;
5346 >            }
5347 >        }
5348 >
5349 >        /**
5350 >         * Forces root task to complete.
5351 >         * @param ex if null, complete normally, else exceptionally
5352 >         * @return false to simplify use
5353 >         */
5354 >        final boolean tryCompleteComputation(Throwable ex) {
5355 >            for (BulkTask<K,V,?> a = this;;) {
5356 >                BulkTask<K,V,?> p = a.parent;
5357 >                if (p == null) {
5358 >                    if (ex != null)
5359 >                        a.completeExceptionally(ex);
5360 >                    else
5361 >                        a.quietlyComplete();
5362 >                    return false;
5363                  }
5364 <            } finally {
5365 <                seg.unlock();
5364 >                a = p;
5365 >            }
5366 >        }
5367 >
5368 >        /**
5369 >         * Version of tryCompleteComputation for function screening checks
5370 >         */
5371 >        final boolean abortOnNullFunction() {
5372 >            return tryCompleteComputation(new Error("Unexpected null function"));
5373 >        }
5374 >
5375 >        // utilities
5376 >
5377 >        /** CompareAndSet pending count */
5378 >        final boolean casPending(int cmp, int val) {
5379 >            return U.compareAndSwapInt(this, PENDING, cmp, val);
5380 >        }
5381 >
5382 >        /**
5383 >         * Returns approx exp2 of the number of times (minus one) to
5384 >         * split task by two before executing leaf action. This value
5385 >         * is faster to compute and more convenient to use as a guide
5386 >         * to splitting than is the depth, since it is used while
5387 >         * dividing by two anyway.
5388 >         */
5389 >        final int batch() {
5390 >            ConcurrentHashMap<K, V> m; int b; Node[] t;  ForkJoinPool pool;
5391 >            if ((b = batch) < 0 && (m = map) != null) { // force initialization
5392 >                if ((t = tab) == null && (t = tab = m.table) != null)
5393 >                    baseLimit = baseSize = t.length;
5394 >                if (t != null) {
5395 >                    long n = m.counter.sum();
5396 >                    int par = ((pool = getPool()) == null) ?
5397 >                        ForkJoinPool.getCommonPoolParallelism() :
5398 >                        pool.getParallelism();
5399 >                    int sp = par << 3; // slack of 8
5400 >                    b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
5401 >                }
5402 >            }
5403 >            return b;
5404 >        }
5405 >
5406 >        /**
5407 >         * Returns exportable snapshot entry.
5408 >         */
5409 >        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
5410 >            return new AbstractMap.SimpleEntry<K,V>(k, v);
5411 >        }
5412 >
5413 >        // Unsafe mechanics
5414 >        private static final sun.misc.Unsafe U;
5415 >        private static final long PENDING;
5416 >        static {
5417 >            try {
5418 >                U = sun.misc.Unsafe.getUnsafe();
5419 >                PENDING = U.objectFieldOffset
5420 >                    (BulkTask.class.getDeclaredField("pending"));
5421 >            } catch (Exception e) {
5422 >                throw new Error(e);
5423              }
5424          }
1287        s.writeObject(null);
1288        s.writeObject(null);
5425      }
5426  
5427      /**
5428 <     * Reconstitute the <tt>ConcurrentHashMap</tt> instance from a
1293 <     * stream (i.e., deserialize it).
1294 <     * @param s the stream
5428 >     * Base class for non-reductive actions
5429       */
5430 <    private void readObject(java.io.ObjectInputStream s)
5431 <        throws IOException, ClassNotFoundException  {
5432 <        s.defaultReadObject();
5430 >    @SuppressWarnings("serial") static abstract class BulkAction<K,V,R> extends BulkTask<K,V,R> {
5431 >        BulkAction<K,V,?> nextTask;
5432 >        BulkAction(ConcurrentHashMap<K,V> map, BulkTask<K,V,?> parent,
5433 >                   int batch, BulkAction<K,V,?> nextTask) {
5434 >            super(map, parent, batch);
5435 >            this.nextTask = nextTask;
5436 >        }
5437  
5438 <        // Initialize each segment to be minimally sized, and let grow.
5439 <        for (int i = 0; i < segments.length; ++i) {
5440 <            segments[i].setTable(new HashEntry[1]);
5438 >        /**
5439 >         * Try to complete task and upward parents. Upon hitting
5440 >         * non-completed parent, if a non-FJ task, try to help out the
5441 >         * computation.
5442 >         */
5443 >        final void tryComplete(BulkAction<K,V,?> subtasks) {
5444 >            BulkTask<K,V,?> a = this, s = a;
5445 >            for (int c;;) {
5446 >                if ((c = a.pending) == 0) {
5447 >                    if ((a = (s = a).parent) == null) {
5448 >                        s.quietlyComplete();
5449 >                        break;
5450 >                    }
5451 >                }
5452 >                else if (a.casPending(c, c - 1)) {
5453 >                    if (subtasks != null && !inForkJoinPool()) {
5454 >                        while ((s = a.parent) != null)
5455 >                            a = s;
5456 >                        while (!a.isDone()) {
5457 >                            BulkAction<K,V,?> next = subtasks.nextTask;
5458 >                            if (subtasks.tryUnfork())
5459 >                                subtasks.exec();
5460 >                            if ((subtasks = next) == null)
5461 >                                break;
5462 >                        }
5463 >                    }
5464 >                    break;
5465 >                }
5466 >            }
5467          }
5468  
5469 <        // Read the keys and values, and put the mappings in the table
5470 <        for (;;) {
5471 <            K key = (K) s.readObject();
5472 <            V value = (V) s.readObject();
5473 <            if (key == null)
5474 <                break;
5475 <            put(key, value);
5469 >    }
5470 >
5471 >    /*
5472 >     * Task classes. Coded in a regular but ugly format/style to
5473 >     * simplify checks that each variant differs in the right way from
5474 >     * others.
5475 >     */
5476 >
5477 >    @SuppressWarnings("serial") static final class ForEachKeyTask<K,V>
5478 >        extends BulkAction<K,V,Void> {
5479 >        final Action<K> action;
5480 >        ForEachKeyTask
5481 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5482 >             ForEachKeyTask<K,V> nextTask,
5483 >             Action<K> action) {
5484 >            super(m, p, b, nextTask);
5485 >            this.action = action;
5486 >        }
5487 >        @SuppressWarnings("unchecked") public final boolean exec() {
5488 >            final Action<K> action = this.action;
5489 >            if (action == null)
5490 >                return abortOnNullFunction();
5491 >            ForEachKeyTask<K,V> subtasks = null;
5492 >            try {
5493 >                int b = batch(), c;
5494 >                while (b > 1 && baseIndex != baseLimit) {
5495 >                    do {} while (!casPending(c = pending, c+1));
5496 >                    (subtasks = new ForEachKeyTask<K,V>
5497 >                     (map, this, b >>>= 1, subtasks, action)).fork();
5498 >                }
5499 >                while (advance() != null)
5500 >                    action.apply((K)nextKey);
5501 >            } catch (Throwable ex) {
5502 >                return tryCompleteComputation(ex);
5503 >            }
5504 >            tryComplete(subtasks);
5505 >            return false;
5506 >        }
5507 >    }
5508 >
5509 >    @SuppressWarnings("serial") static final class ForEachValueTask<K,V>
5510 >        extends BulkAction<K,V,Void> {
5511 >        final Action<V> action;
5512 >        ForEachValueTask
5513 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5514 >             ForEachValueTask<K,V> nextTask,
5515 >             Action<V> action) {
5516 >            super(m, p, b, nextTask);
5517 >            this.action = action;
5518 >        }
5519 >        @SuppressWarnings("unchecked") public final boolean exec() {
5520 >            final Action<V> action = this.action;
5521 >            if (action == null)
5522 >                return abortOnNullFunction();
5523 >            ForEachValueTask<K,V> subtasks = null;
5524 >            try {
5525 >                int b = batch(), c;
5526 >                while (b > 1 && baseIndex != baseLimit) {
5527 >                    do {} while (!casPending(c = pending, c+1));
5528 >                    (subtasks = new ForEachValueTask<K,V>
5529 >                     (map, this, b >>>= 1, subtasks, action)).fork();
5530 >                }
5531 >                Object v;
5532 >                while ((v = advance()) != null)
5533 >                    action.apply((V)v);
5534 >            } catch (Throwable ex) {
5535 >                return tryCompleteComputation(ex);
5536 >            }
5537 >            tryComplete(subtasks);
5538 >            return false;
5539 >        }
5540 >    }
5541 >
5542 >    @SuppressWarnings("serial") static final class ForEachEntryTask<K,V>
5543 >        extends BulkAction<K,V,Void> {
5544 >        final Action<Entry<K,V>> action;
5545 >        ForEachEntryTask
5546 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5547 >             ForEachEntryTask<K,V> nextTask,
5548 >             Action<Entry<K,V>> action) {
5549 >            super(m, p, b, nextTask);
5550 >            this.action = action;
5551 >        }
5552 >        @SuppressWarnings("unchecked") public final boolean exec() {
5553 >            final Action<Entry<K,V>> action = this.action;
5554 >            if (action == null)
5555 >                return abortOnNullFunction();
5556 >            ForEachEntryTask<K,V> subtasks = null;
5557 >            try {
5558 >                int b = batch(), c;
5559 >                while (b > 1 && baseIndex != baseLimit) {
5560 >                    do {} while (!casPending(c = pending, c+1));
5561 >                    (subtasks = new ForEachEntryTask<K,V>
5562 >                     (map, this, b >>>= 1, subtasks, action)).fork();
5563 >                }
5564 >                Object v;
5565 >                while ((v = advance()) != null)
5566 >                    action.apply(entryFor((K)nextKey, (V)v));
5567 >            } catch (Throwable ex) {
5568 >                return tryCompleteComputation(ex);
5569 >            }
5570 >            tryComplete(subtasks);
5571 >            return false;
5572 >        }
5573 >    }
5574 >
5575 >    @SuppressWarnings("serial") static final class ForEachMappingTask<K,V>
5576 >        extends BulkAction<K,V,Void> {
5577 >        final BiAction<K,V> action;
5578 >        ForEachMappingTask
5579 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5580 >             ForEachMappingTask<K,V> nextTask,
5581 >             BiAction<K,V> action) {
5582 >            super(m, p, b, nextTask);
5583 >            this.action = action;
5584 >        }
5585 >        @SuppressWarnings("unchecked") public final boolean exec() {
5586 >            final BiAction<K,V> action = this.action;
5587 >            if (action == null)
5588 >                return abortOnNullFunction();
5589 >            ForEachMappingTask<K,V> subtasks = null;
5590 >            try {
5591 >                int b = batch(), c;
5592 >                while (b > 1 && baseIndex != baseLimit) {
5593 >                    do {} while (!casPending(c = pending, c+1));
5594 >                    (subtasks = new ForEachMappingTask<K,V>
5595 >                     (map, this, b >>>= 1, subtasks, action)).fork();
5596 >                }
5597 >                Object v;
5598 >                while ((v = advance()) != null)
5599 >                    action.apply((K)nextKey, (V)v);
5600 >            } catch (Throwable ex) {
5601 >                return tryCompleteComputation(ex);
5602 >            }
5603 >            tryComplete(subtasks);
5604 >            return false;
5605 >        }
5606 >    }
5607 >
5608 >    @SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U>
5609 >        extends BulkAction<K,V,Void> {
5610 >        final Fun<? super K, ? extends U> transformer;
5611 >        final Action<U> action;
5612 >        ForEachTransformedKeyTask
5613 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5614 >             ForEachTransformedKeyTask<K,V,U> nextTask,
5615 >             Fun<? super K, ? extends U> transformer,
5616 >             Action<U> action) {
5617 >            super(m, p, b, nextTask);
5618 >            this.transformer = transformer;
5619 >            this.action = action;
5620 >
5621 >        }
5622 >        @SuppressWarnings("unchecked") public final boolean exec() {
5623 >            final Fun<? super K, ? extends U> transformer =
5624 >                this.transformer;
5625 >            final Action<U> action = this.action;
5626 >            if (transformer == null || action == null)
5627 >                return abortOnNullFunction();
5628 >            ForEachTransformedKeyTask<K,V,U> subtasks = null;
5629 >            try {
5630 >                int b = batch(), c;
5631 >                while (b > 1 && baseIndex != baseLimit) {
5632 >                    do {} while (!casPending(c = pending, c+1));
5633 >                    (subtasks = new ForEachTransformedKeyTask<K,V,U>
5634 >                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5635 >                }
5636 >                U u;
5637 >                while (advance() != null) {
5638 >                    if ((u = transformer.apply((K)nextKey)) != null)
5639 >                        action.apply(u);
5640 >                }
5641 >            } catch (Throwable ex) {
5642 >                return tryCompleteComputation(ex);
5643 >            }
5644 >            tryComplete(subtasks);
5645 >            return false;
5646 >        }
5647 >    }
5648 >
5649 >    @SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U>
5650 >        extends BulkAction<K,V,Void> {
5651 >        final Fun<? super V, ? extends U> transformer;
5652 >        final Action<U> action;
5653 >        ForEachTransformedValueTask
5654 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5655 >             ForEachTransformedValueTask<K,V,U> nextTask,
5656 >             Fun<? super V, ? extends U> transformer,
5657 >             Action<U> action) {
5658 >            super(m, p, b, nextTask);
5659 >            this.transformer = transformer;
5660 >            this.action = action;
5661 >
5662 >        }
5663 >        @SuppressWarnings("unchecked") public final boolean exec() {
5664 >            final Fun<? super V, ? extends U> transformer =
5665 >                this.transformer;
5666 >            final Action<U> action = this.action;
5667 >            if (transformer == null || action == null)
5668 >                return abortOnNullFunction();
5669 >            ForEachTransformedValueTask<K,V,U> subtasks = null;
5670 >            try {
5671 >                int b = batch(), c;
5672 >                while (b > 1 && baseIndex != baseLimit) {
5673 >                    do {} while (!casPending(c = pending, c+1));
5674 >                    (subtasks = new ForEachTransformedValueTask<K,V,U>
5675 >                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5676 >                }
5677 >                Object v; U u;
5678 >                while ((v = advance()) != null) {
5679 >                    if ((u = transformer.apply((V)v)) != null)
5680 >                        action.apply(u);
5681 >                }
5682 >            } catch (Throwable ex) {
5683 >                return tryCompleteComputation(ex);
5684 >            }
5685 >            tryComplete(subtasks);
5686 >            return false;
5687 >        }
5688 >    }
5689 >
5690 >    @SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U>
5691 >        extends BulkAction<K,V,Void> {
5692 >        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5693 >        final Action<U> action;
5694 >        ForEachTransformedEntryTask
5695 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5696 >             ForEachTransformedEntryTask<K,V,U> nextTask,
5697 >             Fun<Map.Entry<K,V>, ? extends U> transformer,
5698 >             Action<U> action) {
5699 >            super(m, p, b, nextTask);
5700 >            this.transformer = transformer;
5701 >            this.action = action;
5702 >
5703 >        }
5704 >        @SuppressWarnings("unchecked") public final boolean exec() {
5705 >            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5706 >                this.transformer;
5707 >            final Action<U> action = this.action;
5708 >            if (transformer == null || action == null)
5709 >                return abortOnNullFunction();
5710 >            ForEachTransformedEntryTask<K,V,U> subtasks = null;
5711 >            try {
5712 >                int b = batch(), c;
5713 >                while (b > 1 && baseIndex != baseLimit) {
5714 >                    do {} while (!casPending(c = pending, c+1));
5715 >                    (subtasks = new ForEachTransformedEntryTask<K,V,U>
5716 >                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5717 >                }
5718 >                Object v; U u;
5719 >                while ((v = advance()) != null) {
5720 >                    if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5721 >                        action.apply(u);
5722 >                }
5723 >            } catch (Throwable ex) {
5724 >                return tryCompleteComputation(ex);
5725 >            }
5726 >            tryComplete(subtasks);
5727 >            return false;
5728 >        }
5729 >    }
5730 >
5731 >    @SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U>
5732 >        extends BulkAction<K,V,Void> {
5733 >        final BiFun<? super K, ? super V, ? extends U> transformer;
5734 >        final Action<U> action;
5735 >        ForEachTransformedMappingTask
5736 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5737 >             ForEachTransformedMappingTask<K,V,U> nextTask,
5738 >             BiFun<? super K, ? super V, ? extends U> transformer,
5739 >             Action<U> action) {
5740 >            super(m, p, b, nextTask);
5741 >            this.transformer = transformer;
5742 >            this.action = action;
5743 >
5744 >        }
5745 >        @SuppressWarnings("unchecked") public final boolean exec() {
5746 >            final BiFun<? super K, ? super V, ? extends U> transformer =
5747 >                this.transformer;
5748 >            final Action<U> action = this.action;
5749 >            if (transformer == null || action == null)
5750 >                return abortOnNullFunction();
5751 >            ForEachTransformedMappingTask<K,V,U> subtasks = null;
5752 >            try {
5753 >                int b = batch(), c;
5754 >                while (b > 1 && baseIndex != baseLimit) {
5755 >                    do {} while (!casPending(c = pending, c+1));
5756 >                    (subtasks = new ForEachTransformedMappingTask<K,V,U>
5757 >                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5758 >                }
5759 >                Object v; U u;
5760 >                while ((v = advance()) != null) {
5761 >                    if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5762 >                        action.apply(u);
5763 >                }
5764 >            } catch (Throwable ex) {
5765 >                return tryCompleteComputation(ex);
5766 >            }
5767 >            tryComplete(subtasks);
5768 >            return false;
5769 >        }
5770 >    }
5771 >
5772 >    @SuppressWarnings("serial") static final class SearchKeysTask<K,V,U>
5773 >        extends BulkAction<K,V,U> {
5774 >        final Fun<? super K, ? extends U> searchFunction;
5775 >        final AtomicReference<U> result;
5776 >        SearchKeysTask
5777 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5778 >             SearchKeysTask<K,V,U> nextTask,
5779 >             Fun<? super K, ? extends U> searchFunction,
5780 >             AtomicReference<U> result) {
5781 >            super(m, p, b, nextTask);
5782 >            this.searchFunction = searchFunction; this.result = result;
5783 >        }
5784 >        @SuppressWarnings("unchecked") public final boolean exec() {
5785 >            AtomicReference<U> result = this.result;
5786 >            final Fun<? super K, ? extends U> searchFunction =
5787 >                this.searchFunction;
5788 >            if (searchFunction == null || result == null)
5789 >                return abortOnNullFunction();
5790 >            SearchKeysTask<K,V,U> subtasks = null;
5791 >            try {
5792 >                int b = batch(), c;
5793 >                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5794 >                    do {} while (!casPending(c = pending, c+1));
5795 >                    (subtasks = new SearchKeysTask<K,V,U>
5796 >                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5797 >                }
5798 >                U u;
5799 >                while (result.get() == null && advance() != null) {
5800 >                    if ((u = searchFunction.apply((K)nextKey)) != null) {
5801 >                        if (result.compareAndSet(null, u))
5802 >                            tryCompleteComputation(null);
5803 >                        break;
5804 >                    }
5805 >                }
5806 >            } catch (Throwable ex) {
5807 >                return tryCompleteComputation(ex);
5808 >            }
5809 >            tryComplete(subtasks);
5810 >            return false;
5811 >        }
5812 >        public final U getRawResult() { return result.get(); }
5813 >    }
5814 >
5815 >    @SuppressWarnings("serial") static final class SearchValuesTask<K,V,U>
5816 >        extends BulkAction<K,V,U> {
5817 >        final Fun<? super V, ? extends U> searchFunction;
5818 >        final AtomicReference<U> result;
5819 >        SearchValuesTask
5820 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5821 >             SearchValuesTask<K,V,U> nextTask,
5822 >             Fun<? super V, ? extends U> searchFunction,
5823 >             AtomicReference<U> result) {
5824 >            super(m, p, b, nextTask);
5825 >            this.searchFunction = searchFunction; this.result = result;
5826 >        }
5827 >        @SuppressWarnings("unchecked") public final boolean exec() {
5828 >            AtomicReference<U> result = this.result;
5829 >            final Fun<? super V, ? extends U> searchFunction =
5830 >                this.searchFunction;
5831 >            if (searchFunction == null || result == null)
5832 >                return abortOnNullFunction();
5833 >            SearchValuesTask<K,V,U> subtasks = null;
5834 >            try {
5835 >                int b = batch(), c;
5836 >                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5837 >                    do {} while (!casPending(c = pending, c+1));
5838 >                    (subtasks = new SearchValuesTask<K,V,U>
5839 >                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5840 >                }
5841 >                Object v; U u;
5842 >                while (result.get() == null && (v = advance()) != null) {
5843 >                    if ((u = searchFunction.apply((V)v)) != null) {
5844 >                        if (result.compareAndSet(null, u))
5845 >                            tryCompleteComputation(null);
5846 >                        break;
5847 >                    }
5848 >                }
5849 >            } catch (Throwable ex) {
5850 >                return tryCompleteComputation(ex);
5851 >            }
5852 >            tryComplete(subtasks);
5853 >            return false;
5854 >        }
5855 >        public final U getRawResult() { return result.get(); }
5856 >    }
5857 >
5858 >    @SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U>
5859 >        extends BulkAction<K,V,U> {
5860 >        final Fun<Entry<K,V>, ? extends U> searchFunction;
5861 >        final AtomicReference<U> result;
5862 >        SearchEntriesTask
5863 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5864 >             SearchEntriesTask<K,V,U> nextTask,
5865 >             Fun<Entry<K,V>, ? extends U> searchFunction,
5866 >             AtomicReference<U> result) {
5867 >            super(m, p, b, nextTask);
5868 >            this.searchFunction = searchFunction; this.result = result;
5869 >        }
5870 >        @SuppressWarnings("unchecked") public final boolean exec() {
5871 >            AtomicReference<U> result = this.result;
5872 >            final Fun<Entry<K,V>, ? extends U> searchFunction =
5873 >                this.searchFunction;
5874 >            if (searchFunction == null || result == null)
5875 >                return abortOnNullFunction();
5876 >            SearchEntriesTask<K,V,U> subtasks = null;
5877 >            try {
5878 >                int b = batch(), c;
5879 >                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5880 >                    do {} while (!casPending(c = pending, c+1));
5881 >                    (subtasks = new SearchEntriesTask<K,V,U>
5882 >                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5883 >                }
5884 >                Object v; U u;
5885 >                while (result.get() == null && (v = advance()) != null) {
5886 >                    if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5887 >                        if (result.compareAndSet(null, u))
5888 >                            tryCompleteComputation(null);
5889 >                        break;
5890 >                    }
5891 >                }
5892 >            } catch (Throwable ex) {
5893 >                return tryCompleteComputation(ex);
5894 >            }
5895 >            tryComplete(subtasks);
5896 >            return false;
5897 >        }
5898 >        public final U getRawResult() { return result.get(); }
5899 >    }
5900 >
5901 >    @SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U>
5902 >        extends BulkAction<K,V,U> {
5903 >        final BiFun<? super K, ? super V, ? extends U> searchFunction;
5904 >        final AtomicReference<U> result;
5905 >        SearchMappingsTask
5906 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5907 >             SearchMappingsTask<K,V,U> nextTask,
5908 >             BiFun<? super K, ? super V, ? extends U> searchFunction,
5909 >             AtomicReference<U> result) {
5910 >            super(m, p, b, nextTask);
5911 >            this.searchFunction = searchFunction; this.result = result;
5912 >        }
5913 >        @SuppressWarnings("unchecked") public final boolean exec() {
5914 >            AtomicReference<U> result = this.result;
5915 >            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5916 >                this.searchFunction;
5917 >            if (searchFunction == null || result == null)
5918 >                return abortOnNullFunction();
5919 >            SearchMappingsTask<K,V,U> subtasks = null;
5920 >            try {
5921 >                int b = batch(), c;
5922 >                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5923 >                    do {} while (!casPending(c = pending, c+1));
5924 >                    (subtasks = new SearchMappingsTask<K,V,U>
5925 >                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5926 >                }
5927 >                Object v; U u;
5928 >                while (result.get() == null && (v = advance()) != null) {
5929 >                    if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5930 >                        if (result.compareAndSet(null, u))
5931 >                            tryCompleteComputation(null);
5932 >                        break;
5933 >                    }
5934 >                }
5935 >            } catch (Throwable ex) {
5936 >                return tryCompleteComputation(ex);
5937 >            }
5938 >            tryComplete(subtasks);
5939 >            return false;
5940 >        }
5941 >        public final U getRawResult() { return result.get(); }
5942 >    }
5943 >
5944 >    @SuppressWarnings("serial") static final class ReduceKeysTask<K,V>
5945 >        extends BulkTask<K,V,K> {
5946 >        final BiFun<? super K, ? super K, ? extends K> reducer;
5947 >        K result;
5948 >        ReduceKeysTask<K,V> rights, nextRight;
5949 >        ReduceKeysTask
5950 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5951 >             ReduceKeysTask<K,V> nextRight,
5952 >             BiFun<? super K, ? super K, ? extends K> reducer) {
5953 >            super(m, p, b); this.nextRight = nextRight;
5954 >            this.reducer = reducer;
5955 >        }
5956 >        @SuppressWarnings("unchecked") public final boolean exec() {
5957 >            final BiFun<? super K, ? super K, ? extends K> reducer =
5958 >                this.reducer;
5959 >            if (reducer == null)
5960 >                return abortOnNullFunction();
5961 >            try {
5962 >                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5963 >                    do {} while (!casPending(c = pending, c+1));
5964 >                    (rights = new ReduceKeysTask<K,V>
5965 >                     (map, this, b >>>= 1, rights, reducer)).fork();
5966 >                }
5967 >                K r = null;
5968 >                while (advance() != null) {
5969 >                    K u = (K)nextKey;
5970 >                    r = (r == null) ? u : reducer.apply(r, u);
5971 >                }
5972 >                result = r;
5973 >                for (ReduceKeysTask<K,V> t = this, s;;) {
5974 >                    int c; BulkTask<K,V,?> par; K tr, sr;
5975 >                    if ((c = t.pending) == 0) {
5976 >                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5977 >                            if ((sr = s.result) != null)
5978 >                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5979 >                        }
5980 >                        if ((par = t.parent) == null ||
5981 >                            !(par instanceof ReduceKeysTask)) {
5982 >                            t.quietlyComplete();
5983 >                            break;
5984 >                        }
5985 >                        t = (ReduceKeysTask<K,V>)par;
5986 >                    }
5987 >                    else if (t.casPending(c, c - 1))
5988 >                        break;
5989 >                }
5990 >            } catch (Throwable ex) {
5991 >                return tryCompleteComputation(ex);
5992 >            }
5993 >            ReduceKeysTask<K,V> s = rights;
5994 >            if (s != null && !inForkJoinPool()) {
5995 >                do  {
5996 >                    if (s.tryUnfork())
5997 >                        s.exec();
5998 >                } while ((s = s.nextRight) != null);
5999 >            }
6000 >            return false;
6001 >        }
6002 >        public final K getRawResult() { return result; }
6003 >    }
6004 >
6005 >    @SuppressWarnings("serial") static final class ReduceValuesTask<K,V>
6006 >        extends BulkTask<K,V,V> {
6007 >        final BiFun<? super V, ? super V, ? extends V> reducer;
6008 >        V result;
6009 >        ReduceValuesTask<K,V> rights, nextRight;
6010 >        ReduceValuesTask
6011 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6012 >             ReduceValuesTask<K,V> nextRight,
6013 >             BiFun<? super V, ? super V, ? extends V> reducer) {
6014 >            super(m, p, b); this.nextRight = nextRight;
6015 >            this.reducer = reducer;
6016 >        }
6017 >        @SuppressWarnings("unchecked") public final boolean exec() {
6018 >            final BiFun<? super V, ? super V, ? extends V> reducer =
6019 >                this.reducer;
6020 >            if (reducer == null)
6021 >                return abortOnNullFunction();
6022 >            try {
6023 >                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6024 >                    do {} while (!casPending(c = pending, c+1));
6025 >                    (rights = new ReduceValuesTask<K,V>
6026 >                     (map, this, b >>>= 1, rights, reducer)).fork();
6027 >                }
6028 >                V r = null;
6029 >                Object v;
6030 >                while ((v = advance()) != null) {
6031 >                    V u = (V)v;
6032 >                    r = (r == null) ? u : reducer.apply(r, u);
6033 >                }
6034 >                result = r;
6035 >                for (ReduceValuesTask<K,V> t = this, s;;) {
6036 >                    int c; BulkTask<K,V,?> par; V tr, sr;
6037 >                    if ((c = t.pending) == 0) {
6038 >                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6039 >                            if ((sr = s.result) != null)
6040 >                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
6041 >                        }
6042 >                        if ((par = t.parent) == null ||
6043 >                            !(par instanceof ReduceValuesTask)) {
6044 >                            t.quietlyComplete();
6045 >                            break;
6046 >                        }
6047 >                        t = (ReduceValuesTask<K,V>)par;
6048 >                    }
6049 >                    else if (t.casPending(c, c - 1))
6050 >                        break;
6051 >                }
6052 >            } catch (Throwable ex) {
6053 >                return tryCompleteComputation(ex);
6054 >            }
6055 >            ReduceValuesTask<K,V> s = rights;
6056 >            if (s != null && !inForkJoinPool()) {
6057 >                do  {
6058 >                    if (s.tryUnfork())
6059 >                        s.exec();
6060 >                } while ((s = s.nextRight) != null);
6061 >            }
6062 >            return false;
6063 >        }
6064 >        public final V getRawResult() { return result; }
6065 >    }
6066 >
6067 >    @SuppressWarnings("serial") static final class ReduceEntriesTask<K,V>
6068 >        extends BulkTask<K,V,Map.Entry<K,V>> {
6069 >        final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
6070 >        Map.Entry<K,V> result;
6071 >        ReduceEntriesTask<K,V> rights, nextRight;
6072 >        ReduceEntriesTask
6073 >            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6074 >             ReduceEntriesTask<K,V> nextRight,
6075 >             BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
6076 >            super(m, p, b); this.nextRight = nextRight;
6077 >            this.reducer = reducer;
6078 >        }
6079 >        @SuppressWarnings("unchecked") public final boolean exec() {
6080 >            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
6081 >                this.reducer;
6082 >            if (reducer == null)
6083 >                return abortOnNullFunction();
6084 >            try {
6085 >                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6086 >                    do {} while (!casPending(c = pending, c+1));
6087 >                    (rights = new ReduceEntriesTask<K,V>
6088 >                     (map, this, b >>>= 1, rights, reducer)).fork();
6089 >                }
6090 >                Map.Entry<K,V> r = null;
6091 >                Object v;
6092 >                while ((v = advance()) != null) {
6093 >                    Map.Entry<K,V> u = entryFor((K)nextKey, (V)v);
6094 >                    r = (r == null) ? u : reducer.apply(r, u);
6095 >                }
6096 >                result = r;
6097 >                for (ReduceEntriesTask<K,V> t = this, s;;) {
6098 >                    int c; BulkTask<K,V,?> par; Map.Entry<K,V> tr, sr;
6099 >                    if ((c = t.pending) == 0) {
6100 >                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6101 >                            if ((sr = s.result) != null)
6102 >                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
6103 >                        }
6104 >                        if ((par = t.parent) == null ||
6105 >                            !(par instanceof ReduceEntriesTask)) {
6106 >                            t.quietlyComplete();
6107 >                            break;
6108 >                        }
6109 >                        t = (ReduceEntriesTask<K,V>)par;
6110 >                    }
6111 >                    else if (t.casPending(c, c - 1))
6112 >                        break;
6113 >                }
6114 >            } catch (Throwable ex) {
6115 >                return tryCompleteComputation(ex);
6116 >            }
6117 >            ReduceEntriesTask<K,V> s = rights;
6118 >            if (s != null && !inForkJoinPool()) {
6119 >                do  {
6120 >                    if (s.tryUnfork())
6121 >                        s.exec();
6122 >                } while ((s = s.nextRight) != null);
6123 >            }
6124 >            return false;
6125          }
6126 +        public final Map.Entry<K,V> getRawResult() { return result; }
6127 +    }
6128 +
6129 +    @SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U>
6130 +        extends BulkTask<K,V,U> {
6131 +        final Fun<? super K, ? extends U> transformer;
6132 +        final BiFun<? super U, ? super U, ? extends U> reducer;
6133 +        U result;
6134 +        MapReduceKeysTask<K,V,U> rights, nextRight;
6135 +        MapReduceKeysTask
6136 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6137 +             MapReduceKeysTask<K,V,U> nextRight,
6138 +             Fun<? super K, ? extends U> transformer,
6139 +             BiFun<? super U, ? super U, ? extends U> reducer) {
6140 +            super(m, p, b); this.nextRight = nextRight;
6141 +            this.transformer = transformer;
6142 +            this.reducer = reducer;
6143 +        }
6144 +        @SuppressWarnings("unchecked") public final boolean exec() {
6145 +            final Fun<? super K, ? extends U> transformer =
6146 +                this.transformer;
6147 +            final BiFun<? super U, ? super U, ? extends U> reducer =
6148 +                this.reducer;
6149 +            if (transformer == null || reducer == null)
6150 +                return abortOnNullFunction();
6151 +            try {
6152 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6153 +                    do {} while (!casPending(c = pending, c+1));
6154 +                    (rights = new MapReduceKeysTask<K,V,U>
6155 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
6156 +                }
6157 +                U r = null, u;
6158 +                while (advance() != null) {
6159 +                    if ((u = transformer.apply((K)nextKey)) != null)
6160 +                        r = (r == null) ? u : reducer.apply(r, u);
6161 +                }
6162 +                result = r;
6163 +                for (MapReduceKeysTask<K,V,U> t = this, s;;) {
6164 +                    int c; BulkTask<K,V,?> par; U tr, sr;
6165 +                    if ((c = t.pending) == 0) {
6166 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6167 +                            if ((sr = s.result) != null)
6168 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
6169 +                        }
6170 +                        if ((par = t.parent) == null ||
6171 +                            !(par instanceof MapReduceKeysTask)) {
6172 +                            t.quietlyComplete();
6173 +                            break;
6174 +                        }
6175 +                        t = (MapReduceKeysTask<K,V,U>)par;
6176 +                    }
6177 +                    else if (t.casPending(c, c - 1))
6178 +                        break;
6179 +                }
6180 +            } catch (Throwable ex) {
6181 +                return tryCompleteComputation(ex);
6182 +            }
6183 +            MapReduceKeysTask<K,V,U> s = rights;
6184 +            if (s != null && !inForkJoinPool()) {
6185 +                do  {
6186 +                    if (s.tryUnfork())
6187 +                        s.exec();
6188 +                } while ((s = s.nextRight) != null);
6189 +            }
6190 +            return false;
6191 +        }
6192 +        public final U getRawResult() { return result; }
6193 +    }
6194 +
6195 +    @SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U>
6196 +        extends BulkTask<K,V,U> {
6197 +        final Fun<? super V, ? extends U> transformer;
6198 +        final BiFun<? super U, ? super U, ? extends U> reducer;
6199 +        U result;
6200 +        MapReduceValuesTask<K,V,U> rights, nextRight;
6201 +        MapReduceValuesTask
6202 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6203 +             MapReduceValuesTask<K,V,U> nextRight,
6204 +             Fun<? super V, ? extends U> transformer,
6205 +             BiFun<? super U, ? super U, ? extends U> reducer) {
6206 +            super(m, p, b); this.nextRight = nextRight;
6207 +            this.transformer = transformer;
6208 +            this.reducer = reducer;
6209 +        }
6210 +        @SuppressWarnings("unchecked") public final boolean exec() {
6211 +            final Fun<? super V, ? extends U> transformer =
6212 +                this.transformer;
6213 +            final BiFun<? super U, ? super U, ? extends U> reducer =
6214 +                this.reducer;
6215 +            if (transformer == null || reducer == null)
6216 +                return abortOnNullFunction();
6217 +            try {
6218 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6219 +                    do {} while (!casPending(c = pending, c+1));
6220 +                    (rights = new MapReduceValuesTask<K,V,U>
6221 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
6222 +                }
6223 +                U r = null, u;
6224 +                Object v;
6225 +                while ((v = advance()) != null) {
6226 +                    if ((u = transformer.apply((V)v)) != null)
6227 +                        r = (r == null) ? u : reducer.apply(r, u);
6228 +                }
6229 +                result = r;
6230 +                for (MapReduceValuesTask<K,V,U> t = this, s;;) {
6231 +                    int c; BulkTask<K,V,?> par; U tr, sr;
6232 +                    if ((c = t.pending) == 0) {
6233 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6234 +                            if ((sr = s.result) != null)
6235 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
6236 +                        }
6237 +                        if ((par = t.parent) == null ||
6238 +                            !(par instanceof MapReduceValuesTask)) {
6239 +                            t.quietlyComplete();
6240 +                            break;
6241 +                        }
6242 +                        t = (MapReduceValuesTask<K,V,U>)par;
6243 +                    }
6244 +                    else if (t.casPending(c, c - 1))
6245 +                        break;
6246 +                }
6247 +            } catch (Throwable ex) {
6248 +                return tryCompleteComputation(ex);
6249 +            }
6250 +            MapReduceValuesTask<K,V,U> s = rights;
6251 +            if (s != null && !inForkJoinPool()) {
6252 +                do  {
6253 +                    if (s.tryUnfork())
6254 +                        s.exec();
6255 +                } while ((s = s.nextRight) != null);
6256 +            }
6257 +            return false;
6258 +        }
6259 +        public final U getRawResult() { return result; }
6260 +    }
6261 +
6262 +    @SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U>
6263 +        extends BulkTask<K,V,U> {
6264 +        final Fun<Map.Entry<K,V>, ? extends U> transformer;
6265 +        final BiFun<? super U, ? super U, ? extends U> reducer;
6266 +        U result;
6267 +        MapReduceEntriesTask<K,V,U> rights, nextRight;
6268 +        MapReduceEntriesTask
6269 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6270 +             MapReduceEntriesTask<K,V,U> nextRight,
6271 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
6272 +             BiFun<? super U, ? super U, ? extends U> reducer) {
6273 +            super(m, p, b); this.nextRight = nextRight;
6274 +            this.transformer = transformer;
6275 +            this.reducer = reducer;
6276 +        }
6277 +        @SuppressWarnings("unchecked") public final boolean exec() {
6278 +            final Fun<Map.Entry<K,V>, ? extends U> transformer =
6279 +                this.transformer;
6280 +            final BiFun<? super U, ? super U, ? extends U> reducer =
6281 +                this.reducer;
6282 +            if (transformer == null || reducer == null)
6283 +                return abortOnNullFunction();
6284 +            try {
6285 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6286 +                    do {} while (!casPending(c = pending, c+1));
6287 +                    (rights = new MapReduceEntriesTask<K,V,U>
6288 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
6289 +                }
6290 +                U r = null, u;
6291 +                Object v;
6292 +                while ((v = advance()) != null) {
6293 +                    if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
6294 +                        r = (r == null) ? u : reducer.apply(r, u);
6295 +                }
6296 +                result = r;
6297 +                for (MapReduceEntriesTask<K,V,U> t = this, s;;) {
6298 +                    int c; BulkTask<K,V,?> par; U tr, sr;
6299 +                    if ((c = t.pending) == 0) {
6300 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6301 +                            if ((sr = s.result) != null)
6302 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
6303 +                        }
6304 +                        if ((par = t.parent) == null ||
6305 +                            !(par instanceof MapReduceEntriesTask)) {
6306 +                            t.quietlyComplete();
6307 +                            break;
6308 +                        }
6309 +                        t = (MapReduceEntriesTask<K,V,U>)par;
6310 +                    }
6311 +                    else if (t.casPending(c, c - 1))
6312 +                        break;
6313 +                }
6314 +            } catch (Throwable ex) {
6315 +                return tryCompleteComputation(ex);
6316 +            }
6317 +            MapReduceEntriesTask<K,V,U> s = rights;
6318 +            if (s != null && !inForkJoinPool()) {
6319 +                do  {
6320 +                    if (s.tryUnfork())
6321 +                        s.exec();
6322 +                } while ((s = s.nextRight) != null);
6323 +            }
6324 +            return false;
6325 +        }
6326 +        public final U getRawResult() { return result; }
6327 +    }
6328 +
6329 +    @SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U>
6330 +        extends BulkTask<K,V,U> {
6331 +        final BiFun<? super K, ? super V, ? extends U> transformer;
6332 +        final BiFun<? super U, ? super U, ? extends U> reducer;
6333 +        U result;
6334 +        MapReduceMappingsTask<K,V,U> rights, nextRight;
6335 +        MapReduceMappingsTask
6336 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6337 +             MapReduceMappingsTask<K,V,U> nextRight,
6338 +             BiFun<? super K, ? super V, ? extends U> transformer,
6339 +             BiFun<? super U, ? super U, ? extends U> reducer) {
6340 +            super(m, p, b); this.nextRight = nextRight;
6341 +            this.transformer = transformer;
6342 +            this.reducer = reducer;
6343 +        }
6344 +        @SuppressWarnings("unchecked") public final boolean exec() {
6345 +            final BiFun<? super K, ? super V, ? extends U> transformer =
6346 +                this.transformer;
6347 +            final BiFun<? super U, ? super U, ? extends U> reducer =
6348 +                this.reducer;
6349 +            if (transformer == null || reducer == null)
6350 +                return abortOnNullFunction();
6351 +            try {
6352 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6353 +                    do {} while (!casPending(c = pending, c+1));
6354 +                    (rights = new MapReduceMappingsTask<K,V,U>
6355 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
6356 +                }
6357 +                U r = null, u;
6358 +                Object v;
6359 +                while ((v = advance()) != null) {
6360 +                    if ((u = transformer.apply((K)nextKey, (V)v)) != null)
6361 +                        r = (r == null) ? u : reducer.apply(r, u);
6362 +                }
6363 +                result = r;
6364 +                for (MapReduceMappingsTask<K,V,U> t = this, s;;) {
6365 +                    int c; BulkTask<K,V,?> par; U tr, sr;
6366 +                    if ((c = t.pending) == 0) {
6367 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6368 +                            if ((sr = s.result) != null)
6369 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
6370 +                        }
6371 +                        if ((par = t.parent) == null ||
6372 +                            !(par instanceof MapReduceMappingsTask)) {
6373 +                            t.quietlyComplete();
6374 +                            break;
6375 +                        }
6376 +                        t = (MapReduceMappingsTask<K,V,U>)par;
6377 +                    }
6378 +                    else if (t.casPending(c, c - 1))
6379 +                        break;
6380 +                }
6381 +            } catch (Throwable ex) {
6382 +                return tryCompleteComputation(ex);
6383 +            }
6384 +            MapReduceMappingsTask<K,V,U> s = rights;
6385 +            if (s != null && !inForkJoinPool()) {
6386 +                do  {
6387 +                    if (s.tryUnfork())
6388 +                        s.exec();
6389 +                } while ((s = s.nextRight) != null);
6390 +            }
6391 +            return false;
6392 +        }
6393 +        public final U getRawResult() { return result; }
6394 +    }
6395 +
6396 +    @SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V>
6397 +        extends BulkTask<K,V,Double> {
6398 +        final ObjectToDouble<? super K> transformer;
6399 +        final DoubleByDoubleToDouble reducer;
6400 +        final double basis;
6401 +        double result;
6402 +        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
6403 +        MapReduceKeysToDoubleTask
6404 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6405 +             MapReduceKeysToDoubleTask<K,V> nextRight,
6406 +             ObjectToDouble<? super K> transformer,
6407 +             double basis,
6408 +             DoubleByDoubleToDouble reducer) {
6409 +            super(m, p, b); this.nextRight = nextRight;
6410 +            this.transformer = transformer;
6411 +            this.basis = basis; this.reducer = reducer;
6412 +        }
6413 +        @SuppressWarnings("unchecked") public final boolean exec() {
6414 +            final ObjectToDouble<? super K> transformer =
6415 +                this.transformer;
6416 +            final DoubleByDoubleToDouble reducer = this.reducer;
6417 +            if (transformer == null || reducer == null)
6418 +                return abortOnNullFunction();
6419 +            try {
6420 +                final double id = this.basis;
6421 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6422 +                    do {} while (!casPending(c = pending, c+1));
6423 +                    (rights = new MapReduceKeysToDoubleTask<K,V>
6424 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6425 +                }
6426 +                double r = id;
6427 +                while (advance() != null)
6428 +                    r = reducer.apply(r, transformer.apply((K)nextKey));
6429 +                result = r;
6430 +                for (MapReduceKeysToDoubleTask<K,V> t = this, s;;) {
6431 +                    int c; BulkTask<K,V,?> par;
6432 +                    if ((c = t.pending) == 0) {
6433 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6434 +                            t.result = reducer.apply(t.result, s.result);
6435 +                        }
6436 +                        if ((par = t.parent) == null ||
6437 +                            !(par instanceof MapReduceKeysToDoubleTask)) {
6438 +                            t.quietlyComplete();
6439 +                            break;
6440 +                        }
6441 +                        t = (MapReduceKeysToDoubleTask<K,V>)par;
6442 +                    }
6443 +                    else if (t.casPending(c, c - 1))
6444 +                        break;
6445 +                }
6446 +            } catch (Throwable ex) {
6447 +                return tryCompleteComputation(ex);
6448 +            }
6449 +            MapReduceKeysToDoubleTask<K,V> s = rights;
6450 +            if (s != null && !inForkJoinPool()) {
6451 +                do  {
6452 +                    if (s.tryUnfork())
6453 +                        s.exec();
6454 +                } while ((s = s.nextRight) != null);
6455 +            }
6456 +            return false;
6457 +        }
6458 +        public final Double getRawResult() { return result; }
6459 +    }
6460 +
6461 +    @SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V>
6462 +        extends BulkTask<K,V,Double> {
6463 +        final ObjectToDouble<? super V> transformer;
6464 +        final DoubleByDoubleToDouble reducer;
6465 +        final double basis;
6466 +        double result;
6467 +        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
6468 +        MapReduceValuesToDoubleTask
6469 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6470 +             MapReduceValuesToDoubleTask<K,V> nextRight,
6471 +             ObjectToDouble<? super V> transformer,
6472 +             double basis,
6473 +             DoubleByDoubleToDouble reducer) {
6474 +            super(m, p, b); this.nextRight = nextRight;
6475 +            this.transformer = transformer;
6476 +            this.basis = basis; this.reducer = reducer;
6477 +        }
6478 +        @SuppressWarnings("unchecked") public final boolean exec() {
6479 +            final ObjectToDouble<? super V> transformer =
6480 +                this.transformer;
6481 +            final DoubleByDoubleToDouble reducer = this.reducer;
6482 +            if (transformer == null || reducer == null)
6483 +                return abortOnNullFunction();
6484 +            try {
6485 +                final double id = this.basis;
6486 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6487 +                    do {} while (!casPending(c = pending, c+1));
6488 +                    (rights = new MapReduceValuesToDoubleTask<K,V>
6489 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6490 +                }
6491 +                double r = id;
6492 +                Object v;
6493 +                while ((v = advance()) != null)
6494 +                    r = reducer.apply(r, transformer.apply((V)v));
6495 +                result = r;
6496 +                for (MapReduceValuesToDoubleTask<K,V> t = this, s;;) {
6497 +                    int c; BulkTask<K,V,?> par;
6498 +                    if ((c = t.pending) == 0) {
6499 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6500 +                            t.result = reducer.apply(t.result, s.result);
6501 +                        }
6502 +                        if ((par = t.parent) == null ||
6503 +                            !(par instanceof MapReduceValuesToDoubleTask)) {
6504 +                            t.quietlyComplete();
6505 +                            break;
6506 +                        }
6507 +                        t = (MapReduceValuesToDoubleTask<K,V>)par;
6508 +                    }
6509 +                    else if (t.casPending(c, c - 1))
6510 +                        break;
6511 +                }
6512 +            } catch (Throwable ex) {
6513 +                return tryCompleteComputation(ex);
6514 +            }
6515 +            MapReduceValuesToDoubleTask<K,V> s = rights;
6516 +            if (s != null && !inForkJoinPool()) {
6517 +                do  {
6518 +                    if (s.tryUnfork())
6519 +                        s.exec();
6520 +                } while ((s = s.nextRight) != null);
6521 +            }
6522 +            return false;
6523 +        }
6524 +        public final Double getRawResult() { return result; }
6525 +    }
6526 +
6527 +    @SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V>
6528 +        extends BulkTask<K,V,Double> {
6529 +        final ObjectToDouble<Map.Entry<K,V>> transformer;
6530 +        final DoubleByDoubleToDouble reducer;
6531 +        final double basis;
6532 +        double result;
6533 +        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
6534 +        MapReduceEntriesToDoubleTask
6535 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6536 +             MapReduceEntriesToDoubleTask<K,V> nextRight,
6537 +             ObjectToDouble<Map.Entry<K,V>> transformer,
6538 +             double basis,
6539 +             DoubleByDoubleToDouble reducer) {
6540 +            super(m, p, b); this.nextRight = nextRight;
6541 +            this.transformer = transformer;
6542 +            this.basis = basis; this.reducer = reducer;
6543 +        }
6544 +        @SuppressWarnings("unchecked") public final boolean exec() {
6545 +            final ObjectToDouble<Map.Entry<K,V>> transformer =
6546 +                this.transformer;
6547 +            final DoubleByDoubleToDouble reducer = this.reducer;
6548 +            if (transformer == null || reducer == null)
6549 +                return abortOnNullFunction();
6550 +            try {
6551 +                final double id = this.basis;
6552 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6553 +                    do {} while (!casPending(c = pending, c+1));
6554 +                    (rights = new MapReduceEntriesToDoubleTask<K,V>
6555 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6556 +                }
6557 +                double r = id;
6558 +                Object v;
6559 +                while ((v = advance()) != null)
6560 +                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6561 +                result = r;
6562 +                for (MapReduceEntriesToDoubleTask<K,V> t = this, s;;) {
6563 +                    int c; BulkTask<K,V,?> par;
6564 +                    if ((c = t.pending) == 0) {
6565 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6566 +                            t.result = reducer.apply(t.result, s.result);
6567 +                        }
6568 +                        if ((par = t.parent) == null ||
6569 +                            !(par instanceof MapReduceEntriesToDoubleTask)) {
6570 +                            t.quietlyComplete();
6571 +                            break;
6572 +                        }
6573 +                        t = (MapReduceEntriesToDoubleTask<K,V>)par;
6574 +                    }
6575 +                    else if (t.casPending(c, c - 1))
6576 +                        break;
6577 +                }
6578 +            } catch (Throwable ex) {
6579 +                return tryCompleteComputation(ex);
6580 +            }
6581 +            MapReduceEntriesToDoubleTask<K,V> s = rights;
6582 +            if (s != null && !inForkJoinPool()) {
6583 +                do  {
6584 +                    if (s.tryUnfork())
6585 +                        s.exec();
6586 +                } while ((s = s.nextRight) != null);
6587 +            }
6588 +            return false;
6589 +        }
6590 +        public final Double getRawResult() { return result; }
6591 +    }
6592 +
6593 +    @SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V>
6594 +        extends BulkTask<K,V,Double> {
6595 +        final ObjectByObjectToDouble<? super K, ? super V> transformer;
6596 +        final DoubleByDoubleToDouble reducer;
6597 +        final double basis;
6598 +        double result;
6599 +        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
6600 +        MapReduceMappingsToDoubleTask
6601 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6602 +             MapReduceMappingsToDoubleTask<K,V> nextRight,
6603 +             ObjectByObjectToDouble<? super K, ? super V> transformer,
6604 +             double basis,
6605 +             DoubleByDoubleToDouble reducer) {
6606 +            super(m, p, b); this.nextRight = nextRight;
6607 +            this.transformer = transformer;
6608 +            this.basis = basis; this.reducer = reducer;
6609 +        }
6610 +        @SuppressWarnings("unchecked") public final boolean exec() {
6611 +            final ObjectByObjectToDouble<? super K, ? super V> transformer =
6612 +                this.transformer;
6613 +            final DoubleByDoubleToDouble reducer = this.reducer;
6614 +            if (transformer == null || reducer == null)
6615 +                return abortOnNullFunction();
6616 +            try {
6617 +                final double id = this.basis;
6618 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6619 +                    do {} while (!casPending(c = pending, c+1));
6620 +                    (rights = new MapReduceMappingsToDoubleTask<K,V>
6621 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6622 +                }
6623 +                double r = id;
6624 +                Object v;
6625 +                while ((v = advance()) != null)
6626 +                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6627 +                result = r;
6628 +                for (MapReduceMappingsToDoubleTask<K,V> t = this, s;;) {
6629 +                    int c; BulkTask<K,V,?> par;
6630 +                    if ((c = t.pending) == 0) {
6631 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6632 +                            t.result = reducer.apply(t.result, s.result);
6633 +                        }
6634 +                        if ((par = t.parent) == null ||
6635 +                            !(par instanceof MapReduceMappingsToDoubleTask)) {
6636 +                            t.quietlyComplete();
6637 +                            break;
6638 +                        }
6639 +                        t = (MapReduceMappingsToDoubleTask<K,V>)par;
6640 +                    }
6641 +                    else if (t.casPending(c, c - 1))
6642 +                        break;
6643 +                }
6644 +            } catch (Throwable ex) {
6645 +                return tryCompleteComputation(ex);
6646 +            }
6647 +            MapReduceMappingsToDoubleTask<K,V> s = rights;
6648 +            if (s != null && !inForkJoinPool()) {
6649 +                do  {
6650 +                    if (s.tryUnfork())
6651 +                        s.exec();
6652 +                } while ((s = s.nextRight) != null);
6653 +            }
6654 +            return false;
6655 +        }
6656 +        public final Double getRawResult() { return result; }
6657 +    }
6658 +
6659 +    @SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V>
6660 +        extends BulkTask<K,V,Long> {
6661 +        final ObjectToLong<? super K> transformer;
6662 +        final LongByLongToLong reducer;
6663 +        final long basis;
6664 +        long result;
6665 +        MapReduceKeysToLongTask<K,V> rights, nextRight;
6666 +        MapReduceKeysToLongTask
6667 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6668 +             MapReduceKeysToLongTask<K,V> nextRight,
6669 +             ObjectToLong<? super K> transformer,
6670 +             long basis,
6671 +             LongByLongToLong reducer) {
6672 +            super(m, p, b); this.nextRight = nextRight;
6673 +            this.transformer = transformer;
6674 +            this.basis = basis; this.reducer = reducer;
6675 +        }
6676 +        @SuppressWarnings("unchecked") public final boolean exec() {
6677 +            final ObjectToLong<? super K> transformer =
6678 +                this.transformer;
6679 +            final LongByLongToLong reducer = this.reducer;
6680 +            if (transformer == null || reducer == null)
6681 +                return abortOnNullFunction();
6682 +            try {
6683 +                final long id = this.basis;
6684 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6685 +                    do {} while (!casPending(c = pending, c+1));
6686 +                    (rights = new MapReduceKeysToLongTask<K,V>
6687 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6688 +                }
6689 +                long r = id;
6690 +                while (advance() != null)
6691 +                    r = reducer.apply(r, transformer.apply((K)nextKey));
6692 +                result = r;
6693 +                for (MapReduceKeysToLongTask<K,V> t = this, s;;) {
6694 +                    int c; BulkTask<K,V,?> par;
6695 +                    if ((c = t.pending) == 0) {
6696 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6697 +                            t.result = reducer.apply(t.result, s.result);
6698 +                        }
6699 +                        if ((par = t.parent) == null ||
6700 +                            !(par instanceof MapReduceKeysToLongTask)) {
6701 +                            t.quietlyComplete();
6702 +                            break;
6703 +                        }
6704 +                        t = (MapReduceKeysToLongTask<K,V>)par;
6705 +                    }
6706 +                    else if (t.casPending(c, c - 1))
6707 +                        break;
6708 +                }
6709 +            } catch (Throwable ex) {
6710 +                return tryCompleteComputation(ex);
6711 +            }
6712 +            MapReduceKeysToLongTask<K,V> s = rights;
6713 +            if (s != null && !inForkJoinPool()) {
6714 +                do  {
6715 +                    if (s.tryUnfork())
6716 +                        s.exec();
6717 +                } while ((s = s.nextRight) != null);
6718 +            }
6719 +            return false;
6720 +        }
6721 +        public final Long getRawResult() { return result; }
6722 +    }
6723 +
6724 +    @SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V>
6725 +        extends BulkTask<K,V,Long> {
6726 +        final ObjectToLong<? super V> transformer;
6727 +        final LongByLongToLong reducer;
6728 +        final long basis;
6729 +        long result;
6730 +        MapReduceValuesToLongTask<K,V> rights, nextRight;
6731 +        MapReduceValuesToLongTask
6732 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6733 +             MapReduceValuesToLongTask<K,V> nextRight,
6734 +             ObjectToLong<? super V> transformer,
6735 +             long basis,
6736 +             LongByLongToLong reducer) {
6737 +            super(m, p, b); this.nextRight = nextRight;
6738 +            this.transformer = transformer;
6739 +            this.basis = basis; this.reducer = reducer;
6740 +        }
6741 +        @SuppressWarnings("unchecked") public final boolean exec() {
6742 +            final ObjectToLong<? super V> transformer =
6743 +                this.transformer;
6744 +            final LongByLongToLong reducer = this.reducer;
6745 +            if (transformer == null || reducer == null)
6746 +                return abortOnNullFunction();
6747 +            try {
6748 +                final long id = this.basis;
6749 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6750 +                    do {} while (!casPending(c = pending, c+1));
6751 +                    (rights = new MapReduceValuesToLongTask<K,V>
6752 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6753 +                }
6754 +                long r = id;
6755 +                Object v;
6756 +                while ((v = advance()) != null)
6757 +                    r = reducer.apply(r, transformer.apply((V)v));
6758 +                result = r;
6759 +                for (MapReduceValuesToLongTask<K,V> t = this, s;;) {
6760 +                    int c; BulkTask<K,V,?> par;
6761 +                    if ((c = t.pending) == 0) {
6762 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6763 +                            t.result = reducer.apply(t.result, s.result);
6764 +                        }
6765 +                        if ((par = t.parent) == null ||
6766 +                            !(par instanceof MapReduceValuesToLongTask)) {
6767 +                            t.quietlyComplete();
6768 +                            break;
6769 +                        }
6770 +                        t = (MapReduceValuesToLongTask<K,V>)par;
6771 +                    }
6772 +                    else if (t.casPending(c, c - 1))
6773 +                        break;
6774 +                }
6775 +            } catch (Throwable ex) {
6776 +                return tryCompleteComputation(ex);
6777 +            }
6778 +            MapReduceValuesToLongTask<K,V> s = rights;
6779 +            if (s != null && !inForkJoinPool()) {
6780 +                do  {
6781 +                    if (s.tryUnfork())
6782 +                        s.exec();
6783 +                } while ((s = s.nextRight) != null);
6784 +            }
6785 +            return false;
6786 +        }
6787 +        public final Long getRawResult() { return result; }
6788 +    }
6789 +
6790 +    @SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V>
6791 +        extends BulkTask<K,V,Long> {
6792 +        final ObjectToLong<Map.Entry<K,V>> transformer;
6793 +        final LongByLongToLong reducer;
6794 +        final long basis;
6795 +        long result;
6796 +        MapReduceEntriesToLongTask<K,V> rights, nextRight;
6797 +        MapReduceEntriesToLongTask
6798 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6799 +             MapReduceEntriesToLongTask<K,V> nextRight,
6800 +             ObjectToLong<Map.Entry<K,V>> transformer,
6801 +             long basis,
6802 +             LongByLongToLong reducer) {
6803 +            super(m, p, b); this.nextRight = nextRight;
6804 +            this.transformer = transformer;
6805 +            this.basis = basis; this.reducer = reducer;
6806 +        }
6807 +        @SuppressWarnings("unchecked") public final boolean exec() {
6808 +            final ObjectToLong<Map.Entry<K,V>> transformer =
6809 +                this.transformer;
6810 +            final LongByLongToLong reducer = this.reducer;
6811 +            if (transformer == null || reducer == null)
6812 +                return abortOnNullFunction();
6813 +            try {
6814 +                final long id = this.basis;
6815 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6816 +                    do {} while (!casPending(c = pending, c+1));
6817 +                    (rights = new MapReduceEntriesToLongTask<K,V>
6818 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6819 +                }
6820 +                long r = id;
6821 +                Object v;
6822 +                while ((v = advance()) != null)
6823 +                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6824 +                result = r;
6825 +                for (MapReduceEntriesToLongTask<K,V> t = this, s;;) {
6826 +                    int c; BulkTask<K,V,?> par;
6827 +                    if ((c = t.pending) == 0) {
6828 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6829 +                            t.result = reducer.apply(t.result, s.result);
6830 +                        }
6831 +                        if ((par = t.parent) == null ||
6832 +                            !(par instanceof MapReduceEntriesToLongTask)) {
6833 +                            t.quietlyComplete();
6834 +                            break;
6835 +                        }
6836 +                        t = (MapReduceEntriesToLongTask<K,V>)par;
6837 +                    }
6838 +                    else if (t.casPending(c, c - 1))
6839 +                        break;
6840 +                }
6841 +            } catch (Throwable ex) {
6842 +                return tryCompleteComputation(ex);
6843 +            }
6844 +            MapReduceEntriesToLongTask<K,V> s = rights;
6845 +            if (s != null && !inForkJoinPool()) {
6846 +                do  {
6847 +                    if (s.tryUnfork())
6848 +                        s.exec();
6849 +                } while ((s = s.nextRight) != null);
6850 +            }
6851 +            return false;
6852 +        }
6853 +        public final Long getRawResult() { return result; }
6854 +    }
6855 +
6856 +    @SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V>
6857 +        extends BulkTask<K,V,Long> {
6858 +        final ObjectByObjectToLong<? super K, ? super V> transformer;
6859 +        final LongByLongToLong reducer;
6860 +        final long basis;
6861 +        long result;
6862 +        MapReduceMappingsToLongTask<K,V> rights, nextRight;
6863 +        MapReduceMappingsToLongTask
6864 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6865 +             MapReduceMappingsToLongTask<K,V> nextRight,
6866 +             ObjectByObjectToLong<? super K, ? super V> transformer,
6867 +             long basis,
6868 +             LongByLongToLong reducer) {
6869 +            super(m, p, b); this.nextRight = nextRight;
6870 +            this.transformer = transformer;
6871 +            this.basis = basis; this.reducer = reducer;
6872 +        }
6873 +        @SuppressWarnings("unchecked") public final boolean exec() {
6874 +            final ObjectByObjectToLong<? super K, ? super V> transformer =
6875 +                this.transformer;
6876 +            final LongByLongToLong reducer = this.reducer;
6877 +            if (transformer == null || reducer == null)
6878 +                return abortOnNullFunction();
6879 +            try {
6880 +                final long id = this.basis;
6881 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6882 +                    do {} while (!casPending(c = pending, c+1));
6883 +                    (rights = new MapReduceMappingsToLongTask<K,V>
6884 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6885 +                }
6886 +                long r = id;
6887 +                Object v;
6888 +                while ((v = advance()) != null)
6889 +                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6890 +                result = r;
6891 +                for (MapReduceMappingsToLongTask<K,V> t = this, s;;) {
6892 +                    int c; BulkTask<K,V,?> par;
6893 +                    if ((c = t.pending) == 0) {
6894 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6895 +                            t.result = reducer.apply(t.result, s.result);
6896 +                        }
6897 +                        if ((par = t.parent) == null ||
6898 +                            !(par instanceof MapReduceMappingsToLongTask)) {
6899 +                            t.quietlyComplete();
6900 +                            break;
6901 +                        }
6902 +                        t = (MapReduceMappingsToLongTask<K,V>)par;
6903 +                    }
6904 +                    else if (t.casPending(c, c - 1))
6905 +                        break;
6906 +                }
6907 +            } catch (Throwable ex) {
6908 +                return tryCompleteComputation(ex);
6909 +            }
6910 +            MapReduceMappingsToLongTask<K,V> s = rights;
6911 +            if (s != null && !inForkJoinPool()) {
6912 +                do  {
6913 +                    if (s.tryUnfork())
6914 +                        s.exec();
6915 +                } while ((s = s.nextRight) != null);
6916 +            }
6917 +            return false;
6918 +        }
6919 +        public final Long getRawResult() { return result; }
6920 +    }
6921 +
6922 +    @SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V>
6923 +        extends BulkTask<K,V,Integer> {
6924 +        final ObjectToInt<? super K> transformer;
6925 +        final IntByIntToInt reducer;
6926 +        final int basis;
6927 +        int result;
6928 +        MapReduceKeysToIntTask<K,V> rights, nextRight;
6929 +        MapReduceKeysToIntTask
6930 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6931 +             MapReduceKeysToIntTask<K,V> nextRight,
6932 +             ObjectToInt<? super K> transformer,
6933 +             int basis,
6934 +             IntByIntToInt reducer) {
6935 +            super(m, p, b); this.nextRight = nextRight;
6936 +            this.transformer = transformer;
6937 +            this.basis = basis; this.reducer = reducer;
6938 +        }
6939 +        @SuppressWarnings("unchecked") public final boolean exec() {
6940 +            final ObjectToInt<? super K> transformer =
6941 +                this.transformer;
6942 +            final IntByIntToInt reducer = this.reducer;
6943 +            if (transformer == null || reducer == null)
6944 +                return abortOnNullFunction();
6945 +            try {
6946 +                final int id = this.basis;
6947 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6948 +                    do {} while (!casPending(c = pending, c+1));
6949 +                    (rights = new MapReduceKeysToIntTask<K,V>
6950 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6951 +                }
6952 +                int r = id;
6953 +                while (advance() != null)
6954 +                    r = reducer.apply(r, transformer.apply((K)nextKey));
6955 +                result = r;
6956 +                for (MapReduceKeysToIntTask<K,V> t = this, s;;) {
6957 +                    int c; BulkTask<K,V,?> par;
6958 +                    if ((c = t.pending) == 0) {
6959 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6960 +                            t.result = reducer.apply(t.result, s.result);
6961 +                        }
6962 +                        if ((par = t.parent) == null ||
6963 +                            !(par instanceof MapReduceKeysToIntTask)) {
6964 +                            t.quietlyComplete();
6965 +                            break;
6966 +                        }
6967 +                        t = (MapReduceKeysToIntTask<K,V>)par;
6968 +                    }
6969 +                    else if (t.casPending(c, c - 1))
6970 +                        break;
6971 +                }
6972 +            } catch (Throwable ex) {
6973 +                return tryCompleteComputation(ex);
6974 +            }
6975 +            MapReduceKeysToIntTask<K,V> s = rights;
6976 +            if (s != null && !inForkJoinPool()) {
6977 +                do  {
6978 +                    if (s.tryUnfork())
6979 +                        s.exec();
6980 +                } while ((s = s.nextRight) != null);
6981 +            }
6982 +            return false;
6983 +        }
6984 +        public final Integer getRawResult() { return result; }
6985 +    }
6986 +
6987 +    @SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V>
6988 +        extends BulkTask<K,V,Integer> {
6989 +        final ObjectToInt<? super V> transformer;
6990 +        final IntByIntToInt reducer;
6991 +        final int basis;
6992 +        int result;
6993 +        MapReduceValuesToIntTask<K,V> rights, nextRight;
6994 +        MapReduceValuesToIntTask
6995 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6996 +             MapReduceValuesToIntTask<K,V> nextRight,
6997 +             ObjectToInt<? super V> transformer,
6998 +             int basis,
6999 +             IntByIntToInt reducer) {
7000 +            super(m, p, b); this.nextRight = nextRight;
7001 +            this.transformer = transformer;
7002 +            this.basis = basis; this.reducer = reducer;
7003 +        }
7004 +        @SuppressWarnings("unchecked") public final boolean exec() {
7005 +            final ObjectToInt<? super V> transformer =
7006 +                this.transformer;
7007 +            final IntByIntToInt reducer = this.reducer;
7008 +            if (transformer == null || reducer == null)
7009 +                return abortOnNullFunction();
7010 +            try {
7011 +                final int id = this.basis;
7012 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
7013 +                    do {} while (!casPending(c = pending, c+1));
7014 +                    (rights = new MapReduceValuesToIntTask<K,V>
7015 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
7016 +                }
7017 +                int r = id;
7018 +                Object v;
7019 +                while ((v = advance()) != null)
7020 +                    r = reducer.apply(r, transformer.apply((V)v));
7021 +                result = r;
7022 +                for (MapReduceValuesToIntTask<K,V> t = this, s;;) {
7023 +                    int c; BulkTask<K,V,?> par;
7024 +                    if ((c = t.pending) == 0) {
7025 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
7026 +                            t.result = reducer.apply(t.result, s.result);
7027 +                        }
7028 +                        if ((par = t.parent) == null ||
7029 +                            !(par instanceof MapReduceValuesToIntTask)) {
7030 +                            t.quietlyComplete();
7031 +                            break;
7032 +                        }
7033 +                        t = (MapReduceValuesToIntTask<K,V>)par;
7034 +                    }
7035 +                    else if (t.casPending(c, c - 1))
7036 +                        break;
7037 +                }
7038 +            } catch (Throwable ex) {
7039 +                return tryCompleteComputation(ex);
7040 +            }
7041 +            MapReduceValuesToIntTask<K,V> s = rights;
7042 +            if (s != null && !inForkJoinPool()) {
7043 +                do  {
7044 +                    if (s.tryUnfork())
7045 +                        s.exec();
7046 +                } while ((s = s.nextRight) != null);
7047 +            }
7048 +            return false;
7049 +        }
7050 +        public final Integer getRawResult() { return result; }
7051 +    }
7052 +
7053 +    @SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V>
7054 +        extends BulkTask<K,V,Integer> {
7055 +        final ObjectToInt<Map.Entry<K,V>> transformer;
7056 +        final IntByIntToInt reducer;
7057 +        final int basis;
7058 +        int result;
7059 +        MapReduceEntriesToIntTask<K,V> rights, nextRight;
7060 +        MapReduceEntriesToIntTask
7061 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
7062 +             MapReduceEntriesToIntTask<K,V> nextRight,
7063 +             ObjectToInt<Map.Entry<K,V>> transformer,
7064 +             int basis,
7065 +             IntByIntToInt reducer) {
7066 +            super(m, p, b); this.nextRight = nextRight;
7067 +            this.transformer = transformer;
7068 +            this.basis = basis; this.reducer = reducer;
7069 +        }
7070 +        @SuppressWarnings("unchecked") public final boolean exec() {
7071 +            final ObjectToInt<Map.Entry<K,V>> transformer =
7072 +                this.transformer;
7073 +            final IntByIntToInt reducer = this.reducer;
7074 +            if (transformer == null || reducer == null)
7075 +                return abortOnNullFunction();
7076 +            try {
7077 +                final int id = this.basis;
7078 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
7079 +                    do {} while (!casPending(c = pending, c+1));
7080 +                    (rights = new MapReduceEntriesToIntTask<K,V>
7081 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
7082 +                }
7083 +                int r = id;
7084 +                Object v;
7085 +                while ((v = advance()) != null)
7086 +                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
7087 +                result = r;
7088 +                for (MapReduceEntriesToIntTask<K,V> t = this, s;;) {
7089 +                    int c; BulkTask<K,V,?> par;
7090 +                    if ((c = t.pending) == 0) {
7091 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
7092 +                            t.result = reducer.apply(t.result, s.result);
7093 +                        }
7094 +                        if ((par = t.parent) == null ||
7095 +                            !(par instanceof MapReduceEntriesToIntTask)) {
7096 +                            t.quietlyComplete();
7097 +                            break;
7098 +                        }
7099 +                        t = (MapReduceEntriesToIntTask<K,V>)par;
7100 +                    }
7101 +                    else if (t.casPending(c, c - 1))
7102 +                        break;
7103 +                }
7104 +            } catch (Throwable ex) {
7105 +                return tryCompleteComputation(ex);
7106 +            }
7107 +            MapReduceEntriesToIntTask<K,V> s = rights;
7108 +            if (s != null && !inForkJoinPool()) {
7109 +                do  {
7110 +                    if (s.tryUnfork())
7111 +                        s.exec();
7112 +                } while ((s = s.nextRight) != null);
7113 +            }
7114 +            return false;
7115 +        }
7116 +        public final Integer getRawResult() { return result; }
7117 +    }
7118 +
7119 +    @SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V>
7120 +        extends BulkTask<K,V,Integer> {
7121 +        final ObjectByObjectToInt<? super K, ? super V> transformer;
7122 +        final IntByIntToInt reducer;
7123 +        final int basis;
7124 +        int result;
7125 +        MapReduceMappingsToIntTask<K,V> rights, nextRight;
7126 +        MapReduceMappingsToIntTask
7127 +            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
7128 +             MapReduceMappingsToIntTask<K,V> rights,
7129 +             ObjectByObjectToInt<? super K, ? super V> transformer,
7130 +             int basis,
7131 +             IntByIntToInt reducer) {
7132 +            super(m, p, b); this.nextRight = nextRight;
7133 +            this.transformer = transformer;
7134 +            this.basis = basis; this.reducer = reducer;
7135 +        }
7136 +        @SuppressWarnings("unchecked") public final boolean exec() {
7137 +            final ObjectByObjectToInt<? super K, ? super V> transformer =
7138 +                this.transformer;
7139 +            final IntByIntToInt reducer = this.reducer;
7140 +            if (transformer == null || reducer == null)
7141 +                return abortOnNullFunction();
7142 +            try {
7143 +                final int id = this.basis;
7144 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
7145 +                    do {} while (!casPending(c = pending, c+1));
7146 +                    (rights = new MapReduceMappingsToIntTask<K,V>
7147 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
7148 +                }
7149 +                int r = id;
7150 +                Object v;
7151 +                while ((v = advance()) != null)
7152 +                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
7153 +                result = r;
7154 +                for (MapReduceMappingsToIntTask<K,V> t = this, s;;) {
7155 +                    int c; BulkTask<K,V,?> par;
7156 +                    if ((c = t.pending) == 0) {
7157 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
7158 +                            t.result = reducer.apply(t.result, s.result);
7159 +                        }
7160 +                        if ((par = t.parent) == null ||
7161 +                            !(par instanceof MapReduceMappingsToIntTask)) {
7162 +                            t.quietlyComplete();
7163 +                            break;
7164 +                        }
7165 +                        t = (MapReduceMappingsToIntTask<K,V>)par;
7166 +                    }
7167 +                    else if (t.casPending(c, c - 1))
7168 +                        break;
7169 +                }
7170 +            } catch (Throwable ex) {
7171 +                return tryCompleteComputation(ex);
7172 +            }
7173 +            MapReduceMappingsToIntTask<K,V> s = rights;
7174 +            if (s != null && !inForkJoinPool()) {
7175 +                do  {
7176 +                    if (s.tryUnfork())
7177 +                        s.exec();
7178 +                } while ((s = s.nextRight) != null);
7179 +            }
7180 +            return false;
7181 +        }
7182 +        public final Integer getRawResult() { return result; }
7183 +    }
7184 +
7185 +    // Unsafe mechanics
7186 +    private static final sun.misc.Unsafe UNSAFE;
7187 +    private static final long counterOffset;
7188 +    private static final long sizeCtlOffset;
7189 +    private static final long ABASE;
7190 +    private static final int ASHIFT;
7191 +
7192 +    static {
7193 +        int ss;
7194 +        try {
7195 +            UNSAFE = sun.misc.Unsafe.getUnsafe();
7196 +            Class<?> k = ConcurrentHashMap.class;
7197 +            counterOffset = UNSAFE.objectFieldOffset
7198 +                (k.getDeclaredField("counter"));
7199 +            sizeCtlOffset = UNSAFE.objectFieldOffset
7200 +                (k.getDeclaredField("sizeCtl"));
7201 +            Class<?> sc = Node[].class;
7202 +            ABASE = UNSAFE.arrayBaseOffset(sc);
7203 +            ss = UNSAFE.arrayIndexScale(sc);
7204 +        } catch (Exception e) {
7205 +            throw new Error(e);
7206 +        }
7207 +        if ((ss & (ss-1)) != 0)
7208 +            throw new Error("data type scale not a power of two");
7209 +        ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
7210      }
7211   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines