ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentSkipListMap.java
Revision: 1.20
Committed: Mon May 2 22:34:56 2005 UTC (19 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.19: +1 -0 lines
Log Message:
Add missing @since 1.6

File Contents

# Content
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
5 */
6
7 package java.util.concurrent;
8 import java.util.*;
9 import java.util.concurrent.atomic.*;
10
11 /**
12 * A scalable {@link ConcurrentNavigableMap} implementation. This
13 * class maintains a map in ascending key order, sorted according to
14 * the <i>natural order</i> for the keys' class (see {@link
15 * Comparable}), or by the {@link Comparator} provided at creation
16 * time, depending on which constructor is used.
17 *
18 * <p>This class implements a concurrent variant of <a
19 * href="http://www.cs.umd.edu/~pugh/">SkipLists</a> providing
20 * expected average <i>log(n)</i> time cost for the
21 * <tt>containsKey</tt>, <tt>get</tt>, <tt>put</tt> and
22 * <tt>remove</tt> operations and their variants. Insertion, removal,
23 * update, and access operations safely execute concurrently by
24 * multiple threads. Iterators are <i>weakly consistent</i>, returning
25 * elements reflecting the state of the map at some point at or since
26 * the creation of the iterator. They do <em>not</em> throw {@link
27 * ConcurrentModificationException}, and may proceed concurrently with
28 * other operations. Ascending key ordered views and their iterators
29 * are faster than descending ones.
30 *
31 * <p>All <tt>Map.Entry</tt> pairs returned by methods in this class
32 * and its views represent snapshots of mappings at the time they were
33 * produced. They do <em>not</em> support the <tt>Entry.setValue</tt>
34 * method. (Note however that it is possible to change mappings in the
35 * associated map using <tt>put</tt>, <tt>putIfAbsent</tt>, or
36 * <tt>replace</tt>, depending on exactly which effect you need.)
37 *
38 * <p>Beware that, unlike in most collections, the <tt>size</tt>
39 * method is <em>not</em> a constant-time operation. Because of the
40 * asynchronous nature of these maps, determining the current number
41 * of elements requires a traversal of the elements. Additionally,
42 * the bulk operations <tt>putAll</tt>, <tt>equals</tt>, and
43 * <tt>clear</tt> are <em>not</em> guaranteed to be performed
44 * atomically. For example, an iterator operating concurrently with a
45 * <tt>putAll</tt> operation might view only some of the added
46 * elements.
47 *
48 * <p>This class and its views and iterators implement all of the
49 * <em>optional</em> methods of the {@link Map} and {@link Iterator}
50 * interfaces. Like most other concurrent collections, this class does
51 * not permit the use of <tt>null</tt> keys or values because some
52 * null return values cannot be reliably distinguished from the
53 * absence of elements.
54 *
55 * @author Doug Lea
56 * @param <K> the type of keys maintained by this map
57 * @param <V> the type of mapped values
58 * @since 1.6
59 */
60 public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
61 implements ConcurrentNavigableMap<K,V>,
62 Cloneable,
63 java.io.Serializable {
64 /*
65 * This class implements a tree-like two-dimensionally linked skip
66 * list in which the index levels are represented in separate
67 * nodes from the base nodes holding data. There are two reasons
68 * for taking this approach instead of the usual array-based
69 * structure: 1) Array based implementations seem to encounter
70 * more complexity and overhead 2) We can use cheaper algorithms
71 * for the heavily-traversed index lists than can be used for the
72 * base lists. Here's a picture of some of the basics for a
73 * possible list with 2 levels of index:
74 *
75 * Head nodes Index nodes
76 * +-+ right +-+ +-+
77 * |2|---------------->| |--------------------->| |->null
78 * +-+ +-+ +-+
79 * | down | |
80 * v v v
81 * +-+ +-+ +-+ +-+ +-+ +-+
82 * |1|----------->| |->| |------>| |----------->| |------>| |->null
83 * +-+ +-+ +-+ +-+ +-+ +-+
84 * v | | | | |
85 * Nodes next v v v v v
86 * +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
87 * | |->|A|->|B|->|C|->|D|->|E|->|F|->|G|->|H|->|I|->|J|->|K|->null
88 * +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
89 *
90 * The base lists use a variant of the HM linked ordered set
91 * algorithm. See Tim Harris, "A pragmatic implementation of
92 * non-blocking linked lists"
93 * http://www.cl.cam.ac.uk/~tlh20/publications.html and Maged
94 * Michael "High Performance Dynamic Lock-Free Hash Tables and
95 * List-Based Sets"
96 * http://www.research.ibm.com/people/m/michael/pubs.htm. The
97 * basic idea in these lists is to mark the "next" pointers of
98 * deleted nodes when deleting to avoid conflicts with concurrent
99 * insertions, and when traversing to keep track of triples
100 * (predecessor, node, successor) in order to detect when and how
101 * to unlink these deleted nodes.
102 *
103 * Rather than using mark-bits to mark list deletions (which can
104 * be slow and space-intensive using AtomicMarkedReference), nodes
105 * use direct CAS'able next pointers. On deletion, instead of
106 * marking a pointer, they splice in another node that can be
107 * thought of as standing for a marked pointer (indicating this by
108 * using otherwise impossible field values). Using plain nodes
109 * acts roughly like "boxed" implementations of marked pointers,
110 * but uses new nodes only when nodes are deleted, not for every
111 * link. This requires less space and supports faster
112 * traversal. Even if marked references were better supported by
113 * JVMs, traversal using this technique might still be faster
114 * because any search need only read ahead one more node than
115 * otherwise required (to check for trailing marker) rather than
116 * unmasking mark bits or whatever on each read.
117 *
118 * This approach maintains the essential property needed in the HM
119 * algorithm of changing the next-pointer of a deleted node so
120 * that any other CAS of it will fail, but implements the idea by
121 * changing the pointer to point to a different node, not by
122 * marking it. While it would be possible to further squeeze
123 * space by defining marker nodes not to have key/value fields, it
124 * isn't worth the extra type-testing overhead. The deletion
125 * markers are rarely encountered during traversal and are
126 * normally quickly garbage collected. (Note that this technique
127 * would not work well in systems without garbage collection.)
128 *
129 * In addition to using deletion markers, the lists also use
130 * nullness of value fields to indicate deletion, in a style
131 * similar to typical lazy-deletion schemes. If a node's value is
132 * null, then it is considered logically deleted and ignored even
133 * though it is still reachable. This maintains proper control of
134 * concurrent replace vs delete operations -- an attempted replace
135 * must fail if a delete beat it by nulling field, and a delete
136 * must return the last non-null value held in the field. (Note:
137 * Null, rather than some special marker, is used for value fields
138 * here because it just so happens to mesh with the Map API
139 * requirement that method get returns null if there is no
140 * mapping, which allows nodes to remain concurrently readable
141 * even when deleted. Using any other marker value here would be
142 * messy at best.)
143 *
144 * Here's the sequence of events for a deletion of node n with
145 * predecessor b and successor f, initially:
146 *
147 * +------+ +------+ +------+
148 * ... | b |------>| n |----->| f | ...
149 * +------+ +------+ +------+
150 *
151 * 1. CAS n's value field from non-null to null.
152 * From this point on, no public operations encountering
153 * the node consider this mapping to exist. However, other
154 * ongoing insertions and deletions might still modify
155 * n's next pointer.
156 *
157 * 2. CAS n's next pointer to point to a new marker node.
158 * From this point on, no other nodes can be appended to n.
159 * which avoids deletion errors in CAS-based linked lists.
160 *
161 * +------+ +------+ +------+ +------+
162 * ... | b |------>| n |----->|marker|------>| f | ...
163 * +------+ +------+ +------+ +------+
164 *
165 * 3. CAS b's next pointer over both n and its marker.
166 * From this point on, no new traversals will encounter n,
167 * and it can eventually be GCed.
168 * +------+ +------+
169 * ... | b |----------------------------------->| f | ...
170 * +------+ +------+
171 *
172 * A failure at step 1 leads to simple retry due to a lost race
173 * with another operation. Steps 2-3 can fail because some other
174 * thread noticed during a traversal a node with null value and
175 * helped out by marking and/or unlinking. This helping-out
176 * ensures that no thread can become stuck waiting for progress of
177 * the deleting thread. The use of marker nodes slightly
178 * complicates helping-out code because traversals must track
179 * consistent reads of up to four nodes (b, n, marker, f), not
180 * just (b, n, f), although the next field of a marker is
181 * immutable, and once a next field is CAS'ed to point to a
182 * marker, it never again changes, so this requires less care.
183 *
184 * Skip lists add indexing to this scheme, so that the base-level
185 * traversals start close to the locations being found, inserted
186 * or deleted -- usually base level traversals only traverse a few
187 * nodes. This doesn't change the basic algorithm except for the
188 * need to make sure base traversals start at predecessors (here,
189 * b) that are not (structurally) deleted, otherwise retrying
190 * after processing the deletion.
191 *
192 * Index levels are maintained as lists with volatile next fields,
193 * using CAS to link and unlink. Races are allowed in index-list
194 * operations that can (rarely) fail to link in a new index node
195 * or delete one. (We can't do this of course for data nodes.)
196 * However, even when this happens, the index lists remain sorted,
197 * so correctly serve as indices. This can impact performance,
198 * but since skip lists are probabilistic anyway, the net result
199 * is that under contention, the effective "p" value may be lower
200 * than its nominal value. And race windows are kept small enough
201 * that in practice these failures are rare, even under a lot of
202 * contention.
203 *
204 * The fact that retries (for both base and index lists) are
205 * relatively cheap due to indexing allows some minor
206 * simplifications of retry logic. Traversal restarts are
207 * performed after most "helping-out" CASes. This isn't always
208 * strictly necessary, but the implicit backoffs tend to help
209 * reduce other downstream failed CAS's enough to outweigh restart
210 * cost. This worsens the worst case, but seems to improve even
211 * highly contended cases.
212 *
213 * Unlike most skip-list implementations, index insertion and
214 * deletion here require a separate traversal pass occuring after
215 * the base-level action, to add or remove index nodes. This adds
216 * to single-threaded overhead, but improves contended
217 * multithreaded performance by narrowing interference windows,
218 * and allows deletion to ensure that all index nodes will be made
219 * unreachable upon return from a public remove operation, thus
220 * avoiding unwanted garbage retention. This is more important
221 * here than in some other data structures because we cannot null
222 * out node fields referencing user keys since they might still be
223 * read by other ongoing traversals.
224 *
225 * Indexing uses skip list parameters that maintain good search
226 * performance while using sparser-than-usual indices: The
227 * hardwired parameters k=1, p=0.5 (see method randomLevel) mean
228 * that about one-quarter of the nodes have indices. Of those that
229 * do, half have one level, a quarter have two, and so on (see
230 * Pugh's Skip List Cookbook, sec 3.4). The expected total space
231 * requirement for a map is slightly less than for the current
232 * implementation of java.util.TreeMap.
233 *
234 * Changing the level of the index (i.e, the height of the
235 * tree-like structure) also uses CAS. The head index has initial
236 * level/height of one. Creation of an index with height greater
237 * than the current level adds a level to the head index by
238 * CAS'ing on a new top-most head. To maintain good performance
239 * after a lot of removals, deletion methods heuristically try to
240 * reduce the height if the topmost levels appear to be empty.
241 * This may encounter races in which it possible (but rare) to
242 * reduce and "lose" a level just as it is about to contain an
243 * index (that will then never be encountered). This does no
244 * structural harm, and in practice appears to be a better option
245 * than allowing unrestrained growth of levels.
246 *
247 * The code for all this is more verbose than you'd like. Most
248 * operations entail locating an element (or position to insert an
249 * element). The code to do this can't be nicely factored out
250 * because subsequent uses require a snapshot of predecessor
251 * and/or successor and/or value fields which can't be returned
252 * all at once, at least not without creating yet another object
253 * to hold them -- creating such little objects is an especially
254 * bad idea for basic internal search operations because it adds
255 * to GC overhead. (This is one of the few times I've wished Java
256 * had macros.) Instead, some traversal code is interleaved within
257 * insertion and removal operations. The control logic to handle
258 * all the retry conditions is sometimes twisty. Most search is
259 * broken into 2 parts. findPredecessor() searches index nodes
260 * only, returning a base-level predecessor of the key. findNode()
261 * finishes out the base-level search. Even with this factoring,
262 * there is a fair amount of near-duplication of code to handle
263 * variants.
264 *
265 * For explanation of algorithms sharing at least a couple of
266 * features with this one, see Mikhail Fomitchev's thesis
267 * (http://www.cs.yorku.ca/~mikhail/), Keir Fraser's thesis
268 * (http://www.cl.cam.ac.uk/users/kaf24/), and Hakan Sundell's
269 * thesis (http://www.cs.chalmers.se/~phs/).
270 *
271 * Given the use of tree-like index nodes, you might wonder why
272 * this doesn't use some kind of search tree instead, which would
273 * support somewhat faster search operations. The reason is that
274 * there are no known efficient lock-free insertion and deletion
275 * algorithms for search trees. The immutability of the "down"
276 * links of index nodes (as opposed to mutable "left" fields in
277 * true trees) makes this tractable using only CAS operations.
278 *
279 * Notation guide for local variables
280 * Node: b, n, f for predecessor, node, successor
281 * Index: q, r, d for index node, right, down.
282 * t for another index node
283 * Head: h
284 * Levels: j
285 * Keys: k, key
286 * Values: v, value
287 * Comparisons: c
288 */
289
290 private static final long serialVersionUID = -8627078645895051609L;
291
292 /**
293 * Special value used to identify base-level header
294 */
295 private static final Object BASE_HEADER = new Object();
296
297 /**
298 * The topmost head index of the skiplist.
299 */
300 private transient volatile HeadIndex<K,V> head;
301
302 /**
303 * The comparator used to maintain order in this map, or null
304 * if using natural order.
305 * @serial
306 */
307 private final Comparator<? super K> comparator;
308
309 /**
310 * Seed for simple random number generator. Not volatile since it
311 * doesn't matter too much if different threads don't see updates.
312 */
313 private transient int randomSeed;
314
315 /** Lazily initialized key set */
316 private transient KeySet keySet;
317 /** Lazily initialized entry set */
318 private transient EntrySet entrySet;
319 /** Lazily initialized values collection */
320 private transient Values values;
321 /** Lazily initialized descending key set */
322 private transient DescendingKeySet descendingKeySet;
323 /** Lazily initialized descending entry set */
324 private transient DescendingEntrySet descendingEntrySet;
325
326 /**
327 * Initializes or resets state. Needed by constructors, clone,
328 * clear, readObject. and ConcurrentSkipListSet.clone.
329 * (Note that comparator must be separately initialized.)
330 */
331 final void initialize() {
332 keySet = null;
333 entrySet = null;
334 values = null;
335 descendingEntrySet = null;
336 descendingKeySet = null;
337 randomSeed = (int) System.nanoTime();
338 head = new HeadIndex<K,V>(new Node<K,V>(null, BASE_HEADER, null),
339 null, null, 1);
340 }
341
342 /** Updater for casHead */
343 private static final
344 AtomicReferenceFieldUpdater<ConcurrentSkipListMap, HeadIndex>
345 headUpdater = AtomicReferenceFieldUpdater.newUpdater
346 (ConcurrentSkipListMap.class, HeadIndex.class, "head");
347
348 /**
349 * compareAndSet head node
350 */
351 private boolean casHead(HeadIndex<K,V> cmp, HeadIndex<K,V> val) {
352 return headUpdater.compareAndSet(this, cmp, val);
353 }
354
355 /* ---------------- Nodes -------------- */
356
357 /**
358 * Nodes hold keys and values, and are singly linked in sorted
359 * order, possibly with some intervening marker nodes. The list is
360 * headed by a dummy node accessible as head.node. The value field
361 * is declared only as Object because it takes special non-V
362 * values for marker and header nodes.
363 */
364 static final class Node<K,V> {
365 final K key;
366 volatile Object value;
367 volatile Node<K,V> next;
368
369 /**
370 * Creates a new regular node.
371 */
372 Node(K key, Object value, Node<K,V> next) {
373 this.key = key;
374 this.value = value;
375 this.next = next;
376 }
377
378 /**
379 * Creates a new marker node. A marker is distinguished by
380 * having its value field point to itself. Marker nodes also
381 * have null keys, a fact that is exploited in a few places,
382 * but this doesn't distinguish markers from the base-level
383 * header node (head.node), which also has a null key.
384 */
385 Node(Node<K,V> next) {
386 this.key = null;
387 this.value = this;
388 this.next = next;
389 }
390
391 /** Updater for casNext */
392 static final AtomicReferenceFieldUpdater<Node, Node>
393 nextUpdater = AtomicReferenceFieldUpdater.newUpdater
394 (Node.class, Node.class, "next");
395
396 /** Updater for casValue */
397 static final AtomicReferenceFieldUpdater<Node, Object>
398 valueUpdater = AtomicReferenceFieldUpdater.newUpdater
399 (Node.class, Object.class, "value");
400
401 /**
402 * compareAndSet value field
403 */
404 boolean casValue(Object cmp, Object val) {
405 return valueUpdater.compareAndSet(this, cmp, val);
406 }
407
408 /**
409 * compareAndSet next field
410 */
411 boolean casNext(Node<K,V> cmp, Node<K,V> val) {
412 return nextUpdater.compareAndSet(this, cmp, val);
413 }
414
415 /**
416 * Returns true if this node is a marker. This method isn't
417 * actually called in any current code checking for markers
418 * because callers will have already read value field and need
419 * to use that read (not another done here) and so directly
420 * test if value points to node.
421 * @param n a possibly null reference to a node
422 * @return true if this node is a marker node
423 */
424 boolean isMarker() {
425 return value == this;
426 }
427
428 /**
429 * Returns true if this node is the header of base-level list.
430 * @return true if this node is header node
431 */
432 boolean isBaseHeader() {
433 return value == BASE_HEADER;
434 }
435
436 /**
437 * Tries to append a deletion marker to this node.
438 * @param f the assumed current successor of this node
439 * @return true if successful
440 */
441 boolean appendMarker(Node<K,V> f) {
442 return casNext(f, new Node<K,V>(f));
443 }
444
445 /**
446 * Helps out a deletion by appending marker or unlinking from
447 * predecessor. This is called during traversals when value
448 * field seen to be null.
449 * @param b predecessor
450 * @param f successor
451 */
452 void helpDelete(Node<K,V> b, Node<K,V> f) {
453 /*
454 * Rechecking links and then doing only one of the
455 * help-out stages per call tends to minimize CAS
456 * interference among helping threads.
457 */
458 if (f == next && this == b.next) {
459 if (f == null || f.value != f) // not already marked
460 appendMarker(f);
461 else
462 b.casNext(this, f.next);
463 }
464 }
465
466 /**
467 * Returns value if this node contains a valid key-value pair,
468 * else null.
469 * @return this node's value if it isn't a marker or header or
470 * is deleted, else null.
471 */
472 V getValidValue() {
473 Object v = value;
474 if (v == this || v == BASE_HEADER)
475 return null;
476 return (V)v;
477 }
478
479 /**
480 * Creates and returns a new SimpleImmutableEntry holding current
481 * mapping if this node holds a valid value, else null.
482 * @return new entry or null
483 */
484 AbstractMap.SimpleImmutableEntry<K,V> createSnapshot() {
485 V v = getValidValue();
486 if (v == null)
487 return null;
488 return new AbstractMap.SimpleImmutableEntry(key, v);
489 }
490 }
491
492 /* ---------------- Indexing -------------- */
493
494 /**
495 * Index nodes represent the levels of the skip list. To improve
496 * search performance, keys of the underlying nodes are cached.
497 * Note that even though both Nodes and Indexes have
498 * forward-pointing fields, they have different types and are
499 * handled in different ways, that can't nicely be captured by
500 * placing field in a shared abstract class.
501 */
502 static class Index<K,V> {
503 final K key;
504 final Node<K,V> node;
505 final Index<K,V> down;
506 volatile Index<K,V> right;
507
508 /**
509 * Creates index node with given values.
510 */
511 Index(Node<K,V> node, Index<K,V> down, Index<K,V> right) {
512 this.node = node;
513 this.key = node.key;
514 this.down = down;
515 this.right = right;
516 }
517
518 /** Updater for casRight */
519 static final AtomicReferenceFieldUpdater<Index, Index>
520 rightUpdater = AtomicReferenceFieldUpdater.newUpdater
521 (Index.class, Index.class, "right");
522
523 /**
524 * compareAndSet right field
525 */
526 final boolean casRight(Index<K,V> cmp, Index<K,V> val) {
527 return rightUpdater.compareAndSet(this, cmp, val);
528 }
529
530 /**
531 * Returns true if the node this indexes has been deleted.
532 * @return true if indexed node is known to be deleted
533 */
534 final boolean indexesDeletedNode() {
535 return node.value == null;
536 }
537
538 /**
539 * Tries to CAS newSucc as successor. To minimize races with
540 * unlink that may lose this index node, if the node being
541 * indexed is known to be deleted, it doesn't try to link in.
542 * @param succ the expected current successor
543 * @param newSucc the new successor
544 * @return true if successful
545 */
546 final boolean link(Index<K,V> succ, Index<K,V> newSucc) {
547 Node<K,V> n = node;
548 newSucc.right = succ;
549 return n.value != null && casRight(succ, newSucc);
550 }
551
552 /**
553 * Tries to CAS right field to skip over apparent successor
554 * succ. Fails (forcing a retraversal by caller) if this node
555 * is known to be deleted.
556 * @param succ the expected current successor
557 * @return true if successful
558 */
559 final boolean unlink(Index<K,V> succ) {
560 return !indexesDeletedNode() && casRight(succ, succ.right);
561 }
562 }
563
564 /* ---------------- Head nodes -------------- */
565
566 /**
567 * Nodes heading each level keep track of their level.
568 */
569 static final class HeadIndex<K,V> extends Index<K,V> {
570 final int level;
571 HeadIndex(Node<K,V> node, Index<K,V> down, Index<K,V> right, int level) {
572 super(node, down, right);
573 this.level = level;
574 }
575 }
576
577 /* ---------------- Comparison utilities -------------- */
578
579 /**
580 * Represents a key with a comparator as a Comparable.
581 *
582 * Because most sorted collections seem to use natural order on
583 * Comparables (Strings, Integers, etc), most internal methods are
584 * geared to use them. This is generally faster than checking
585 * per-comparison whether to use comparator or comparable because
586 * it doesn't require a (Comparable) cast for each comparison.
587 * (Optimizers can only sometimes remove such redundant checks
588 * themselves.) When Comparators are used,
589 * ComparableUsingComparators are created so that they act in the
590 * same way as natural orderings. This penalizes use of
591 * Comparators vs Comparables, which seems like the right
592 * tradeoff.
593 */
594 static final class ComparableUsingComparator<K> implements Comparable<K> {
595 final K actualKey;
596 final Comparator<? super K> cmp;
597 ComparableUsingComparator(K key, Comparator<? super K> cmp) {
598 this.actualKey = key;
599 this.cmp = cmp;
600 }
601 public int compareTo(K k2) {
602 return cmp.compare(actualKey, k2);
603 }
604 }
605
606 /**
607 * If using comparator, return a ComparableUsingComparator, else
608 * cast key as Comparator, which may cause ClassCastException,
609 * which is propagated back to caller.
610 */
611 private Comparable<? super K> comparable(Object key) throws ClassCastException {
612 if (key == null)
613 throw new NullPointerException();
614 return (comparator != null)
615 ? new ComparableUsingComparator(key, comparator)
616 : (Comparable<? super K>)key;
617 }
618
619 /**
620 * Compares using comparator or natural ordering. Used when the
621 * ComparableUsingComparator approach doesn't apply.
622 */
623 int compare(K k1, K k2) throws ClassCastException {
624 Comparator<? super K> cmp = comparator;
625 if (cmp != null)
626 return cmp.compare(k1, k2);
627 else
628 return ((Comparable<? super K>)k1).compareTo(k2);
629 }
630
631 /**
632 * Returns true if given key greater than or equal to least and
633 * strictly less than fence, bypassing either test if least or
634 * fence are null. Needed mainly in submap operations.
635 */
636 boolean inHalfOpenRange(K key, K least, K fence) {
637 if (key == null)
638 throw new NullPointerException();
639 return ((least == null || compare(key, least) >= 0) &&
640 (fence == null || compare(key, fence) < 0));
641 }
642
643 /**
644 * Returns true if given key greater than or equal to least and less
645 * or equal to fence. Needed mainly in submap operations.
646 */
647 boolean inOpenRange(K key, K least, K fence) {
648 if (key == null)
649 throw new NullPointerException();
650 return ((least == null || compare(key, least) >= 0) &&
651 (fence == null || compare(key, fence) <= 0));
652 }
653
654 /* ---------------- Traversal -------------- */
655
656 /**
657 * Returns a base-level node with key strictly less than given key,
658 * or the base-level header if there is no such node. Also
659 * unlinks indexes to deleted nodes found along the way. Callers
660 * rely on this side-effect of clearing indices to deleted nodes.
661 * @param key the key
662 * @return a predecessor of key
663 */
664 private Node<K,V> findPredecessor(Comparable<? super K> key) {
665 for (;;) {
666 Index<K,V> q = head;
667 for (;;) {
668 Index<K,V> d, r;
669 if ((r = q.right) != null) {
670 if (r.indexesDeletedNode()) {
671 if (q.unlink(r))
672 continue; // reread r
673 else
674 break; // restart
675 }
676 if (key.compareTo(r.key) > 0) {
677 q = r;
678 continue;
679 }
680 }
681 if ((d = q.down) != null)
682 q = d;
683 else
684 return q.node;
685 }
686 }
687 }
688
689 /**
690 * Returns node holding key or null if no such, clearing out any
691 * deleted nodes seen along the way. Repeatedly traverses at
692 * base-level looking for key starting at predecessor returned
693 * from findPredecessor, processing base-level deletions as
694 * encountered. Some callers rely on this side-effect of clearing
695 * deleted nodes.
696 *
697 * Restarts occur, at traversal step centered on node n, if:
698 *
699 * (1) After reading n's next field, n is no longer assumed
700 * predecessor b's current successor, which means that
701 * we don't have a consistent 3-node snapshot and so cannot
702 * unlink any subsequent deleted nodes encountered.
703 *
704 * (2) n's value field is null, indicating n is deleted, in
705 * which case we help out an ongoing structural deletion
706 * before retrying. Even though there are cases where such
707 * unlinking doesn't require restart, they aren't sorted out
708 * here because doing so would not usually outweigh cost of
709 * restarting.
710 *
711 * (3) n is a marker or n's predecessor's value field is null,
712 * indicating (among other possibilities) that
713 * findPredecessor returned a deleted node. We can't unlink
714 * the node because we don't know its predecessor, so rely
715 * on another call to findPredecessor to notice and return
716 * some earlier predecessor, which it will do. This check is
717 * only strictly needed at beginning of loop, (and the
718 * b.value check isn't strictly needed at all) but is done
719 * each iteration to help avoid contention with other
720 * threads by callers that will fail to be able to change
721 * links, and so will retry anyway.
722 *
723 * The traversal loops in doPut, doRemove, and findNear all
724 * include the same three kinds of checks. And specialized
725 * versions appear in doRemoveFirst, doRemoveLast, findFirst, and
726 * findLast. They can't easily share code because each uses the
727 * reads of fields held in locals occurring in the orders they
728 * were performed.
729 *
730 * @param key the key
731 * @return node holding key, or null if no such.
732 */
733 private Node<K,V> findNode(Comparable<? super K> key) {
734 for (;;) {
735 Node<K,V> b = findPredecessor(key);
736 Node<K,V> n = b.next;
737 for (;;) {
738 if (n == null)
739 return null;
740 Node<K,V> f = n.next;
741 if (n != b.next) // inconsistent read
742 break;
743 Object v = n.value;
744 if (v == null) { // n is deleted
745 n.helpDelete(b, f);
746 break;
747 }
748 if (v == n || b.value == null) // b is deleted
749 break;
750 int c = key.compareTo(n.key);
751 if (c < 0)
752 return null;
753 if (c == 0)
754 return n;
755 b = n;
756 n = f;
757 }
758 }
759 }
760
761 /**
762 * Specialized variant of findNode to perform Map.get. Does a weak
763 * traversal, not bothering to fix any deleted index nodes,
764 * returning early if it happens to see key in index, and passing
765 * over any deleted base nodes, falling back to getUsingFindNode
766 * only if it would otherwise return value from an ongoing
767 * deletion. Also uses "bound" to eliminate need for some
768 * comparisons (see Pugh Cookbook). Also folds uses of null checks
769 * and node-skipping because markers have null keys.
770 * @param okey the key
771 * @return the value, or null if absent
772 */
773 private V doGet(Object okey) {
774 Comparable<? super K> key = comparable(okey);
775 K bound = null;
776 Index<K,V> q = head;
777 for (;;) {
778 K rk;
779 Index<K,V> d, r;
780 if ((r = q.right) != null &&
781 (rk = r.key) != null && rk != bound) {
782 int c = key.compareTo(rk);
783 if (c > 0) {
784 q = r;
785 continue;
786 }
787 if (c == 0) {
788 Object v = r.node.value;
789 return (v != null)? (V)v : getUsingFindNode(key);
790 }
791 bound = rk;
792 }
793 if ((d = q.down) != null)
794 q = d;
795 else {
796 for (Node<K,V> n = q.node.next; n != null; n = n.next) {
797 K nk = n.key;
798 if (nk != null) {
799 int c = key.compareTo(nk);
800 if (c == 0) {
801 Object v = n.value;
802 return (v != null)? (V)v : getUsingFindNode(key);
803 }
804 if (c < 0)
805 return null;
806 }
807 }
808 return null;
809 }
810 }
811 }
812
813 /**
814 * Performs map.get via findNode. Used as a backup if doGet
815 * encounters an in-progress deletion.
816 * @param key the key
817 * @return the value, or null if absent
818 */
819 private V getUsingFindNode(Comparable<? super K> key) {
820 /*
821 * Loop needed here and elsewhere in case value field goes
822 * null just as it is about to be returned, in which case we
823 * lost a race with a deletion, so must retry.
824 */
825 for (;;) {
826 Node<K,V> n = findNode(key);
827 if (n == null)
828 return null;
829 Object v = n.value;
830 if (v != null)
831 return (V)v;
832 }
833 }
834
835 /* ---------------- Insertion -------------- */
836
837 /**
838 * Main insertion method. Adds element if not present, or
839 * replaces value if present and onlyIfAbsent is false.
840 * @param kkey the key
841 * @param value the value that must be associated with key
842 * @param onlyIfAbsent if should not insert if already present
843 * @return the old value, or null if newly inserted
844 */
845 private V doPut(K kkey, V value, boolean onlyIfAbsent) {
846 Comparable<? super K> key = comparable(kkey);
847 for (;;) {
848 Node<K,V> b = findPredecessor(key);
849 Node<K,V> n = b.next;
850 for (;;) {
851 if (n != null) {
852 Node<K,V> f = n.next;
853 if (n != b.next) // inconsistent read
854 break;;
855 Object v = n.value;
856 if (v == null) { // n is deleted
857 n.helpDelete(b, f);
858 break;
859 }
860 if (v == n || b.value == null) // b is deleted
861 break;
862 int c = key.compareTo(n.key);
863 if (c > 0) {
864 b = n;
865 n = f;
866 continue;
867 }
868 if (c == 0) {
869 if (onlyIfAbsent || n.casValue(v, value))
870 return (V)v;
871 else
872 break; // restart if lost race to replace value
873 }
874 // else c < 0; fall through
875 }
876
877 Node<K,V> z = new Node<K,V>(kkey, value, n);
878 if (!b.casNext(n, z))
879 break; // restart if lost race to append to b
880 int level = randomLevel();
881 if (level > 0)
882 insertIndex(z, level);
883 return null;
884 }
885 }
886 }
887
888 /**
889 * Returns a random level for inserting a new node.
890 * Hardwired to k=1, p=0.5, max 31.
891 *
892 * This uses a cheap pseudo-random function that according to
893 * http://home1.gte.net/deleyd/random/random4.html was used in
894 * Turbo Pascal. It seems the fastest usable one here. The low
895 * bits are apparently not very random (the original used only
896 * upper 16 bits) so we traverse from highest bit down (i.e., test
897 * sign), thus hardly ever use lower bits.
898 */
899 private int randomLevel() {
900 int level = 0;
901 int r = randomSeed;
902 randomSeed = r * 134775813 + 1;
903 if (r < 0) {
904 while ((r <<= 1) > 0)
905 ++level;
906 }
907 return level;
908 }
909
910 /**
911 * Creates and adds index nodes for the given node.
912 * @param z the node
913 * @param level the level of the index
914 */
915 private void insertIndex(Node<K,V> z, int level) {
916 HeadIndex<K,V> h = head;
917 int max = h.level;
918
919 if (level <= max) {
920 Index<K,V> idx = null;
921 for (int i = 1; i <= level; ++i)
922 idx = new Index<K,V>(z, idx, null);
923 addIndex(idx, h, level);
924
925 } else { // Add a new level
926 /*
927 * To reduce interference by other threads checking for
928 * empty levels in tryReduceLevel, new levels are added
929 * with initialized right pointers. Which in turn requires
930 * keeping levels in an array to access them while
931 * creating new head index nodes from the opposite
932 * direction.
933 */
934 level = max + 1;
935 Index<K,V>[] idxs = (Index<K,V>[])new Index[level+1];
936 Index<K,V> idx = null;
937 for (int i = 1; i <= level; ++i)
938 idxs[i] = idx = new Index<K,V>(z, idx, null);
939
940 HeadIndex<K,V> oldh;
941 int k;
942 for (;;) {
943 oldh = head;
944 int oldLevel = oldh.level;
945 if (level <= oldLevel) { // lost race to add level
946 k = level;
947 break;
948 }
949 HeadIndex<K,V> newh = oldh;
950 Node<K,V> oldbase = oldh.node;
951 for (int j = oldLevel+1; j <= level; ++j)
952 newh = new HeadIndex<K,V>(oldbase, newh, idxs[j], j);
953 if (casHead(oldh, newh)) {
954 k = oldLevel;
955 break;
956 }
957 }
958 addIndex(idxs[k], oldh, k);
959 }
960 }
961
962 /**
963 * Adds given index nodes from given level down to 1.
964 * @param idx the topmost index node being inserted
965 * @param h the value of head to use to insert. This must be
966 * snapshotted by callers to provide correct insertion level
967 * @param indexLevel the level of the index
968 */
969 private void addIndex(Index<K,V> idx, HeadIndex<K,V> h, int indexLevel) {
970 // Track next level to insert in case of retries
971 int insertionLevel = indexLevel;
972 Comparable<? super K> key = comparable(idx.key);
973
974 // Similar to findPredecessor, but adding index nodes along
975 // path to key.
976 for (;;) {
977 Index<K,V> q = h;
978 Index<K,V> t = idx;
979 int j = h.level;
980 for (;;) {
981 Index<K,V> r = q.right;
982 if (r != null) {
983 // compare before deletion check avoids needing recheck
984 int c = key.compareTo(r.key);
985 if (r.indexesDeletedNode()) {
986 if (q.unlink(r))
987 continue;
988 else
989 break;
990 }
991 if (c > 0) {
992 q = r;
993 continue;
994 }
995 }
996
997 if (j == insertionLevel) {
998 // Don't insert index if node already deleted
999 if (t.indexesDeletedNode()) {
1000 findNode(key); // cleans up
1001 return;
1002 }
1003 if (!q.link(r, t))
1004 break; // restart
1005 if (--insertionLevel == 0) {
1006 // need final deletion check before return
1007 if (t.indexesDeletedNode())
1008 findNode(key);
1009 return;
1010 }
1011 }
1012
1013 if (j > insertionLevel && j <= indexLevel)
1014 t = t.down;
1015 q = q.down;
1016 --j;
1017 }
1018 }
1019 }
1020
1021 /* ---------------- Deletion -------------- */
1022
1023 /**
1024 * Main deletion method. Locates node, nulls value, appends a
1025 * deletion marker, unlinks predecessor, removes associated index
1026 * nodes, and possibly reduces head index level.
1027 *
1028 * Index nodes are cleared out simply by calling findPredecessor.
1029 * which unlinks indexes to deleted nodes found along path to key,
1030 * which will include the indexes to this node. This is done
1031 * unconditionally. We can't check beforehand whether there are
1032 * index nodes because it might be the case that some or all
1033 * indexes hadn't been inserted yet for this node during initial
1034 * search for it, and we'd like to ensure lack of garbage
1035 * retention, so must call to be sure.
1036 *
1037 * @param okey the key
1038 * @param value if non-null, the value that must be
1039 * associated with key
1040 * @return the node, or null if not found
1041 */
1042 private V doRemove(Object okey, Object value) {
1043 Comparable<? super K> key = comparable(okey);
1044 for (;;) {
1045 Node<K,V> b = findPredecessor(key);
1046 Node<K,V> n = b.next;
1047 for (;;) {
1048 if (n == null)
1049 return null;
1050 Node<K,V> f = n.next;
1051 if (n != b.next) // inconsistent read
1052 break;
1053 Object v = n.value;
1054 if (v == null) { // n is deleted
1055 n.helpDelete(b, f);
1056 break;
1057 }
1058 if (v == n || b.value == null) // b is deleted
1059 break;
1060 int c = key.compareTo(n.key);
1061 if (c < 0)
1062 return null;
1063 if (c > 0) {
1064 b = n;
1065 n = f;
1066 continue;
1067 }
1068 if (value != null && !value.equals(v))
1069 return null;
1070 if (!n.casValue(v, null))
1071 break;
1072 if (!n.appendMarker(f) || !b.casNext(n, f))
1073 findNode(key); // Retry via findNode
1074 else {
1075 findPredecessor(key); // Clean index
1076 if (head.right == null)
1077 tryReduceLevel();
1078 }
1079 return (V)v;
1080 }
1081 }
1082 }
1083
1084 /**
1085 * Possibly reduce head level if it has no nodes. This method can
1086 * (rarely) make mistakes, in which case levels can disappear even
1087 * though they are about to contain index nodes. This impacts
1088 * performance, not correctness. To minimize mistakes as well as
1089 * to reduce hysteresis, the level is reduced by one only if the
1090 * topmost three levels look empty. Also, if the removed level
1091 * looks non-empty after CAS, we try to change it back quick
1092 * before anyone notices our mistake! (This trick works pretty
1093 * well because this method will practically never make mistakes
1094 * unless current thread stalls immediately before first CAS, in
1095 * which case it is very unlikely to stall again immediately
1096 * afterwards, so will recover.)
1097 *
1098 * We put up with all this rather than just let levels grow
1099 * because otherwise, even a small map that has undergone a large
1100 * number of insertions and removals will have a lot of levels,
1101 * slowing down access more than would an occasional unwanted
1102 * reduction.
1103 */
1104 private void tryReduceLevel() {
1105 HeadIndex<K,V> h = head;
1106 HeadIndex<K,V> d;
1107 HeadIndex<K,V> e;
1108 if (h.level > 3 &&
1109 (d = (HeadIndex<K,V>)h.down) != null &&
1110 (e = (HeadIndex<K,V>)d.down) != null &&
1111 e.right == null &&
1112 d.right == null &&
1113 h.right == null &&
1114 casHead(h, d) && // try to set
1115 h.right != null) // recheck
1116 casHead(d, h); // try to backout
1117 }
1118
1119 /**
1120 * Version of remove with boolean return. Needed by view classes
1121 */
1122 boolean removep(Object key) {
1123 return doRemove(key, null) != null;
1124 }
1125
1126 /* ---------------- Finding and removing first element -------------- */
1127
1128 /**
1129 * Specialized variant of findNode to get first valid node
1130 * @return first node or null if empty
1131 */
1132 Node<K,V> findFirst() {
1133 for (;;) {
1134 Node<K,V> b = head.node;
1135 Node<K,V> n = b.next;
1136 if (n == null)
1137 return null;
1138 if (n.value != null)
1139 return n;
1140 n.helpDelete(b, n.next);
1141 }
1142 }
1143
1144 /**
1145 * Removes first entry; returns either its key or a snapshot.
1146 * @param keyOnly if true return key, else return SimpleImmutableEntry
1147 * (This is a little ugly, but avoids code duplication.)
1148 * @return null if empty, first key if keyOnly true, else key,value entry
1149 */
1150 Object doRemoveFirst(boolean keyOnly) {
1151 for (;;) {
1152 Node<K,V> b = head.node;
1153 Node<K,V> n = b.next;
1154 if (n == null)
1155 return null;
1156 Node<K,V> f = n.next;
1157 if (n != b.next)
1158 continue;
1159 Object v = n.value;
1160 if (v == null) {
1161 n.helpDelete(b, f);
1162 continue;
1163 }
1164 if (!n.casValue(v, null))
1165 continue;
1166 if (!n.appendMarker(f) || !b.casNext(n, f))
1167 findFirst(); // retry
1168 clearIndexToFirst();
1169 K key = n.key;
1170 if (keyOnly)
1171 return key;
1172 else
1173 return new AbstractMap.SimpleImmutableEntry<K,V>(key, (V)v);
1174 }
1175 }
1176
1177 /**
1178 * Clears out index nodes associated with deleted first entry.
1179 * Needed by doRemoveFirst.
1180 */
1181 private void clearIndexToFirst() {
1182 for (;;) {
1183 Index<K,V> q = head;
1184 for (;;) {
1185 Index<K,V> r = q.right;
1186 if (r != null && r.indexesDeletedNode() && !q.unlink(r))
1187 break;
1188 if ((q = q.down) == null) {
1189 if (head.right == null)
1190 tryReduceLevel();
1191 return;
1192 }
1193 }
1194 }
1195 }
1196
1197 /**
1198 * Removes first entry; returns key or null if empty.
1199 */
1200 K pollFirstKey() {
1201 return (K)doRemoveFirst(true);
1202 }
1203
1204 /* ---------------- Finding and removing last element -------------- */
1205
1206 /**
1207 * Specialized version of find to get last valid node.
1208 * @return last node or null if empty
1209 */
1210 Node<K,V> findLast() {
1211 /*
1212 * findPredecessor can't be used to traverse index level
1213 * because this doesn't use comparisons. So traversals of
1214 * both levels are folded together.
1215 */
1216 Index<K,V> q = head;
1217 for (;;) {
1218 Index<K,V> d, r;
1219 if ((r = q.right) != null) {
1220 if (r.indexesDeletedNode()) {
1221 q.unlink(r);
1222 q = head; // restart
1223 }
1224 else
1225 q = r;
1226 } else if ((d = q.down) != null) {
1227 q = d;
1228 } else {
1229 Node<K,V> b = q.node;
1230 Node<K,V> n = b.next;
1231 for (;;) {
1232 if (n == null)
1233 return (b.isBaseHeader())? null : b;
1234 Node<K,V> f = n.next; // inconsistent read
1235 if (n != b.next)
1236 break;
1237 Object v = n.value;
1238 if (v == null) { // n is deleted
1239 n.helpDelete(b, f);
1240 break;
1241 }
1242 if (v == n || b.value == null) // b is deleted
1243 break;
1244 b = n;
1245 n = f;
1246 }
1247 q = head; // restart
1248 }
1249 }
1250 }
1251
1252
1253 /**
1254 * Specialized version of doRemove for last entry.
1255 * @param keyOnly if true return key, else return SimpleImmutableEntry
1256 * @return null if empty, last key if keyOnly true, else key,value entry
1257 */
1258 Object doRemoveLast(boolean keyOnly) {
1259 for (;;) {
1260 Node<K,V> b = findPredecessorOfLast();
1261 Node<K,V> n = b.next;
1262 if (n == null) {
1263 if (b.isBaseHeader()) // empty
1264 return null;
1265 else
1266 continue; // all b's successors are deleted; retry
1267 }
1268 for (;;) {
1269 Node<K,V> f = n.next;
1270 if (n != b.next) // inconsistent read
1271 break;
1272 Object v = n.value;
1273 if (v == null) { // n is deleted
1274 n.helpDelete(b, f);
1275 break;
1276 }
1277 if (v == n || b.value == null) // b is deleted
1278 break;
1279 if (f != null) {
1280 b = n;
1281 n = f;
1282 continue;
1283 }
1284 if (!n.casValue(v, null))
1285 break;
1286 K key = n.key;
1287 Comparable<? super K> ck = comparable(key);
1288 if (!n.appendMarker(f) || !b.casNext(n, f))
1289 findNode(ck); // Retry via findNode
1290 else {
1291 findPredecessor(ck); // Clean index
1292 if (head.right == null)
1293 tryReduceLevel();
1294 }
1295 if (keyOnly)
1296 return key;
1297 else
1298 return new AbstractMap.SimpleImmutableEntry<K,V>(key, (V)v);
1299 }
1300 }
1301 }
1302
1303 /**
1304 * Specialized variant of findPredecessor to get predecessor of
1305 * last valid node. Needed by doRemoveLast. It is possible that
1306 * all successors of returned node will have been deleted upon
1307 * return, in which case this method can be retried.
1308 * @return likely predecessor of last node.
1309 */
1310 private Node<K,V> findPredecessorOfLast() {
1311 for (;;) {
1312 Index<K,V> q = head;
1313 for (;;) {
1314 Index<K,V> d, r;
1315 if ((r = q.right) != null) {
1316 if (r.indexesDeletedNode()) {
1317 q.unlink(r);
1318 break; // must restart
1319 }
1320 // proceed as far across as possible without overshooting
1321 if (r.node.next != null) {
1322 q = r;
1323 continue;
1324 }
1325 }
1326 if ((d = q.down) != null)
1327 q = d;
1328 else
1329 return q.node;
1330 }
1331 }
1332 }
1333
1334 /**
1335 * Removes last entry; returns key or null if empty.
1336 */
1337 K pollLastKey() {
1338 return (K)doRemoveLast(true);
1339 }
1340
1341 /* ---------------- Relational operations -------------- */
1342
1343 // Control values OR'ed as arguments to findNear
1344
1345 private static final int EQ = 1;
1346 private static final int LT = 2;
1347 private static final int GT = 0; // Actually checked as !LT
1348
1349 /**
1350 * Utility for ceiling, floor, lower, higher methods.
1351 * @param kkey the key
1352 * @param rel the relation -- OR'ed combination of EQ, LT, GT
1353 * @return nearest node fitting relation, or null if no such
1354 */
1355 Node<K,V> findNear(K kkey, int rel) {
1356 Comparable<? super K> key = comparable(kkey);
1357 for (;;) {
1358 Node<K,V> b = findPredecessor(key);
1359 Node<K,V> n = b.next;
1360 for (;;) {
1361 if (n == null)
1362 return ((rel & LT) == 0 || b.isBaseHeader())? null : b;
1363 Node<K,V> f = n.next;
1364 if (n != b.next) // inconsistent read
1365 break;
1366 Object v = n.value;
1367 if (v == null) { // n is deleted
1368 n.helpDelete(b, f);
1369 break;
1370 }
1371 if (v == n || b.value == null) // b is deleted
1372 break;
1373 int c = key.compareTo(n.key);
1374 if ((c == 0 && (rel & EQ) != 0) ||
1375 (c < 0 && (rel & LT) == 0))
1376 return n;
1377 if ( c <= 0 && (rel & LT) != 0)
1378 return (b.isBaseHeader())? null : b;
1379 b = n;
1380 n = f;
1381 }
1382 }
1383 }
1384
1385 /**
1386 * Returns SimpleImmutableEntry for results of findNear.
1387 * @param kkey the key
1388 * @param rel the relation -- OR'ed combination of EQ, LT, GT
1389 * @return Entry fitting relation, or null if no such
1390 */
1391 AbstractMap.SimpleImmutableEntry<K,V> getNear(K kkey, int rel) {
1392 for (;;) {
1393 Node<K,V> n = findNear(kkey, rel);
1394 if (n == null)
1395 return null;
1396 AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot();
1397 if (e != null)
1398 return e;
1399 }
1400 }
1401
1402 /**
1403 * Returns ceiling, or first node if key is <tt>null</tt>.
1404 */
1405 Node<K,V> findCeiling(K key) {
1406 return (key == null)? findFirst() : findNear(key, GT|EQ);
1407 }
1408
1409 /**
1410 * Returns lower node, or last node if key is <tt>null</tt>.
1411 */
1412 Node<K,V> findLower(K key) {
1413 return (key == null)? findLast() : findNear(key, LT);
1414 }
1415
1416 /**
1417 * Returns SimpleImmutableEntry or key for results of findNear
1418 * after screening to ensure result is in given range. Needed by
1419 * submaps.
1420 * @param kkey the key
1421 * @param rel the relation -- OR'ed combination of EQ, LT, GT
1422 * @param least minimum allowed key value
1423 * @param fence key greater than maximum allowed key value
1424 * @param keyOnly if true return key, else return SimpleImmutableEntry
1425 * @return Key or Entry fitting relation, or <tt>null</tt> if no such
1426 */
1427 Object getNear(K kkey, int rel, K least, K fence, boolean keyOnly) {
1428 K key = kkey;
1429 // Don't return keys less than least
1430 if ((rel & LT) == 0) {
1431 if (compare(key, least) < 0) {
1432 key = least;
1433 rel = rel | EQ;
1434 }
1435 }
1436
1437 for (;;) {
1438 Node<K,V> n = findNear(key, rel);
1439 if (n == null || !inHalfOpenRange(n.key, least, fence))
1440 return null;
1441 K k = n.key;
1442 V v = n.getValidValue();
1443 if (v != null) {
1444 if (keyOnly)
1445 return k;
1446 else
1447 return new AbstractMap.SimpleImmutableEntry<K,V>(k, v);
1448 }
1449 }
1450 }
1451
1452 /**
1453 * Finds and removes least element of subrange.
1454 * @param least minimum allowed key value
1455 * @param fence key greater than maximum allowed key value
1456 * @param keyOnly if true return key, else return SimpleImmutableEntry
1457 * @return least Key or Entry, or <tt>null</tt> if no such
1458 */
1459 Object removeFirstEntryOfSubrange(K least, K fence, boolean keyOnly) {
1460 for (;;) {
1461 Node<K,V> n = findCeiling(least);
1462 if (n == null)
1463 return null;
1464 K k = n.key;
1465 if (fence != null && compare(k, fence) >= 0)
1466 return null;
1467 V v = doRemove(k, null);
1468 if (v != null) {
1469 if (keyOnly)
1470 return k;
1471 else
1472 return new AbstractMap.SimpleImmutableEntry<K,V>(k, v);
1473 }
1474 }
1475 }
1476
1477 /**
1478 * Finds and removes greatest element of subrange.
1479 * @param least minimum allowed key value
1480 * @param fence key greater than maximum allowed key value
1481 * @param keyOnly if true return key, else return SimpleImmutableEntry
1482 * @return least Key or Entry, or <tt>null</tt> if no such
1483 */
1484 Object removeLastEntryOfSubrange(K least, K fence, boolean keyOnly) {
1485 for (;;) {
1486 Node<K,V> n = findLower(fence);
1487 if (n == null)
1488 return null;
1489 K k = n.key;
1490 if (least != null && compare(k, least) < 0)
1491 return null;
1492 V v = doRemove(k, null);
1493 if (v != null) {
1494 if (keyOnly)
1495 return k;
1496 else
1497 return new AbstractMap.SimpleImmutableEntry<K,V>(k, v);
1498 }
1499 }
1500 }
1501
1502 /* ---------------- Constructors -------------- */
1503
1504 /**
1505 * Constructs a new empty map, sorted according to the keys' natural
1506 * order.
1507 */
1508 public ConcurrentSkipListMap() {
1509 this.comparator = null;
1510 initialize();
1511 }
1512
1513 /**
1514 * Constructs a new empty map, sorted according to the given comparator.
1515 *
1516 * @param c the comparator that will be used to sort this map. A
1517 * <tt>null</tt> value indicates that the keys' <i>natural
1518 * ordering</i> should be used.
1519 */
1520 public ConcurrentSkipListMap(Comparator<? super K> c) {
1521 this.comparator = c;
1522 initialize();
1523 }
1524
1525 /**
1526 * Constructs a new map containing the same mappings as the given map,
1527 * sorted according to the keys' <i>natural order</i>.
1528 *
1529 * @param m the map whose mappings are to be placed in this map.
1530 * @throws ClassCastException if the keys in m are not Comparable, or
1531 * are not mutually comparable.
1532 * @throws NullPointerException if the specified map is <tt>null</tt>.
1533 */
1534 public ConcurrentSkipListMap(Map<? extends K, ? extends V> m) {
1535 this.comparator = null;
1536 initialize();
1537 putAll(m);
1538 }
1539
1540 /**
1541 * Constructs a new map containing the same mappings as the given
1542 * <tt>SortedMap</tt>, sorted according to the same ordering.
1543 * @param m the sorted map whose mappings are to be placed in this
1544 * map, and whose comparator is to be used to sort this map.
1545 * @throws NullPointerException if the specified sorted map is
1546 * <tt>null</tt>.
1547 */
1548 public ConcurrentSkipListMap(SortedMap<K, ? extends V> m) {
1549 this.comparator = m.comparator();
1550 initialize();
1551 buildFromSorted(m);
1552 }
1553
1554 /**
1555 * Returns a shallow copy of this <tt>Map</tt> instance. (The keys and
1556 * values themselves are not cloned.)
1557 *
1558 * @return a shallow copy of this map.
1559 */
1560 public ConcurrentSkipListMap<K,V> clone() {
1561 ConcurrentSkipListMap<K,V> clone = null;
1562 try {
1563 clone = (ConcurrentSkipListMap<K,V>) super.clone();
1564 } catch (CloneNotSupportedException e) {
1565 throw new InternalError();
1566 }
1567
1568 clone.initialize();
1569 clone.buildFromSorted(this);
1570 return clone;
1571 }
1572
1573 /**
1574 * Streamlined bulk insertion to initialize from elements of
1575 * given sorted map. Call only from constructor or clone
1576 * method.
1577 */
1578 private void buildFromSorted(SortedMap<K, ? extends V> map) {
1579 if (map == null)
1580 throw new NullPointerException();
1581
1582 HeadIndex<K,V> h = head;
1583 Node<K,V> basepred = h.node;
1584
1585 // Track the current rightmost node at each level. Uses an
1586 // ArrayList to avoid committing to initial or maximum level.
1587 ArrayList<Index<K,V>> preds = new ArrayList<Index<K,V>>();
1588
1589 // initialize
1590 for (int i = 0; i <= h.level; ++i)
1591 preds.add(null);
1592 Index<K,V> q = h;
1593 for (int i = h.level; i > 0; --i) {
1594 preds.set(i, q);
1595 q = q.down;
1596 }
1597
1598 Iterator<? extends Map.Entry<? extends K, ? extends V>> it =
1599 map.entrySet().iterator();
1600 while (it.hasNext()) {
1601 Map.Entry<? extends K, ? extends V> e = it.next();
1602 int j = randomLevel();
1603 if (j > h.level) j = h.level + 1;
1604 K k = e.getKey();
1605 V v = e.getValue();
1606 if (k == null || v == null)
1607 throw new NullPointerException();
1608 Node<K,V> z = new Node<K,V>(k, v, null);
1609 basepred.next = z;
1610 basepred = z;
1611 if (j > 0) {
1612 Index<K,V> idx = null;
1613 for (int i = 1; i <= j; ++i) {
1614 idx = new Index<K,V>(z, idx, null);
1615 if (i > h.level)
1616 h = new HeadIndex<K,V>(h.node, h, idx, i);
1617
1618 if (i < preds.size()) {
1619 preds.get(i).right = idx;
1620 preds.set(i, idx);
1621 } else
1622 preds.add(idx);
1623 }
1624 }
1625 }
1626 head = h;
1627 }
1628
1629 /* ---------------- Serialization -------------- */
1630
1631 /**
1632 * Save the state of this map to a stream.
1633 *
1634 * @serialData The key (Object) and value (Object) for each
1635 * key-value mapping represented by the map, followed by
1636 * <tt>null</tt>. The key-value mappings are emitted in key-order
1637 * (as determined by the Comparator, or by the keys' natural
1638 * ordering if no Comparator).
1639 */
1640 private void writeObject(java.io.ObjectOutputStream s)
1641 throws java.io.IOException {
1642 // Write out the Comparator and any hidden stuff
1643 s.defaultWriteObject();
1644
1645 // Write out keys and values (alternating)
1646 for (Node<K,V> n = findFirst(); n != null; n = n.next) {
1647 V v = n.getValidValue();
1648 if (v != null) {
1649 s.writeObject(n.key);
1650 s.writeObject(v);
1651 }
1652 }
1653 s.writeObject(null);
1654 }
1655
1656 /**
1657 * Reconstitute the map from a stream.
1658 */
1659 private void readObject(final java.io.ObjectInputStream s)
1660 throws java.io.IOException, ClassNotFoundException {
1661 // Read in the Comparator and any hidden stuff
1662 s.defaultReadObject();
1663 // Reset transients
1664 initialize();
1665
1666 /*
1667 * This is nearly identical to buildFromSorted, but is
1668 * distinct because readObject calls can't be nicely adapted
1669 * as the kind of iterator needed by buildFromSorted. (They
1670 * can be, but doing so requires type cheats and/or creation
1671 * of adaptor classes.) It is simpler to just adapt the code.
1672 */
1673
1674 HeadIndex<K,V> h = head;
1675 Node<K,V> basepred = h.node;
1676 ArrayList<Index<K,V>> preds = new ArrayList<Index<K,V>>();
1677 for (int i = 0; i <= h.level; ++i)
1678 preds.add(null);
1679 Index<K,V> q = h;
1680 for (int i = h.level; i > 0; --i) {
1681 preds.set(i, q);
1682 q = q.down;
1683 }
1684
1685 for (;;) {
1686 Object k = s.readObject();
1687 if (k == null)
1688 break;
1689 Object v = s.readObject();
1690 if (v == null)
1691 throw new NullPointerException();
1692 K key = (K) k;
1693 V val = (V) v;
1694 int j = randomLevel();
1695 if (j > h.level) j = h.level + 1;
1696 Node<K,V> z = new Node<K,V>(key, val, null);
1697 basepred.next = z;
1698 basepred = z;
1699 if (j > 0) {
1700 Index<K,V> idx = null;
1701 for (int i = 1; i <= j; ++i) {
1702 idx = new Index<K,V>(z, idx, null);
1703 if (i > h.level)
1704 h = new HeadIndex<K,V>(h.node, h, idx, i);
1705
1706 if (i < preds.size()) {
1707 preds.get(i).right = idx;
1708 preds.set(i, idx);
1709 } else
1710 preds.add(idx);
1711 }
1712 }
1713 }
1714 head = h;
1715 }
1716
1717 /* ------ Map API methods ------ */
1718
1719 /**
1720 * Returns <tt>true</tt> if this map contains a mapping for the specified
1721 * key.
1722 * @param key key whose presence in this map is to be tested.
1723 * @return <tt>true</tt> if this map contains a mapping for the
1724 * specified key.
1725 * @throws ClassCastException if the key cannot be compared with the keys
1726 * currently in the map.
1727 * @throws NullPointerException if the key is <tt>null</tt>.
1728 */
1729 public boolean containsKey(Object key) {
1730 return doGet(key) != null;
1731 }
1732
1733 /**
1734 * Returns the value to which this map maps the specified key. Returns
1735 * <tt>null</tt> if the map contains no mapping for this key.
1736 *
1737 * @param key key whose associated value is to be returned.
1738 * @return the value to which this map maps the specified key, or
1739 * <tt>null</tt> if the map contains no mapping for the key.
1740 * @throws ClassCastException if the key cannot be compared with the keys
1741 * currently in the map.
1742 * @throws NullPointerException if the key is <tt>null</tt>.
1743 */
1744 public V get(Object key) {
1745 return doGet(key);
1746 }
1747
1748 /**
1749 * Associates the specified value with the specified key in this map.
1750 * If the map previously contained a mapping for this key, the old
1751 * value is replaced.
1752 *
1753 * @param key key with which the specified value is to be associated.
1754 * @param value value to be associated with the specified key.
1755 *
1756 * @return the previous value associated with specified key, or <tt>null</tt>
1757 * if there was no mapping for key.
1758 * @throws ClassCastException if the key cannot be compared with the keys
1759 * currently in the map.
1760 * @throws NullPointerException if the key or value are <tt>null</tt>.
1761 */
1762 public V put(K key, V value) {
1763 if (value == null)
1764 throw new NullPointerException();
1765 return doPut(key, value, false);
1766 }
1767
1768 /**
1769 * Removes the mapping for this key from this Map if present.
1770 *
1771 * @param key key for which mapping should be removed
1772 * @return the previous value associated with specified key, or <tt>null</tt>
1773 * if there was no mapping for key.
1774 *
1775 * @throws ClassCastException if the key cannot be compared with the keys
1776 * currently in the map.
1777 * @throws NullPointerException if the key is <tt>null</tt>.
1778 */
1779 public V remove(Object key) {
1780 return doRemove(key, null);
1781 }
1782
1783 /**
1784 * Returns <tt>true</tt> if this map maps one or more keys to the
1785 * specified value. This operation requires time linear in the
1786 * map size.
1787 *
1788 * @param value value whose presence in this map is to be tested.
1789 * @return <tt>true</tt> if a mapping to <tt>value</tt> exists;
1790 * <tt>false</tt> otherwise.
1791 * @throws NullPointerException if the value is <tt>null</tt>.
1792 */
1793 public boolean containsValue(Object value) {
1794 if (value == null)
1795 throw new NullPointerException();
1796 for (Node<K,V> n = findFirst(); n != null; n = n.next) {
1797 V v = n.getValidValue();
1798 if (v != null && value.equals(v))
1799 return true;
1800 }
1801 return false;
1802 }
1803
1804 /**
1805 * Returns the number of key-value mappings in this map. If this map
1806 * contains more than <tt>Integer.MAX_VALUE</tt> elements, it
1807 * returns <tt>Integer.MAX_VALUE</tt>.
1808 *
1809 * <p>Beware that, unlike in most collections, this method is
1810 * <em>NOT</em> a constant-time operation. Because of the
1811 * asynchronous nature of these maps, determining the current
1812 * number of elements requires traversing them all to count them.
1813 * Additionally, it is possible for the size to change during
1814 * execution of this method, in which case the returned result
1815 * will be inaccurate. Thus, this method is typically not very
1816 * useful in concurrent applications.
1817 *
1818 * @return the number of elements in this map.
1819 */
1820 public int size() {
1821 long count = 0;
1822 for (Node<K,V> n = findFirst(); n != null; n = n.next) {
1823 if (n.getValidValue() != null)
1824 ++count;
1825 }
1826 return (count >= Integer.MAX_VALUE)? Integer.MAX_VALUE : (int)count;
1827 }
1828
1829 /**
1830 * Returns <tt>true</tt> if this map contains no key-value mappings.
1831 * @return <tt>true</tt> if this map contains no key-value mappings.
1832 */
1833 public boolean isEmpty() {
1834 return findFirst() == null;
1835 }
1836
1837 /**
1838 * Removes all mappings from this map.
1839 */
1840 public void clear() {
1841 initialize();
1842 }
1843
1844 /**
1845 * Returns a set view of the keys contained in this map. The set is
1846 * backed by the map, so changes to the map are reflected in the set, and
1847 * vice-versa. The set supports element removal, which removes the
1848 * corresponding mapping from this map, via the <tt>Iterator.remove</tt>,
1849 * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and
1850 * <tt>clear</tt> operations. It does not support the <tt>add</tt> or
1851 * <tt>addAll</tt> operations.
1852 * The view's <tt>iterator</tt> is a "weakly consistent" iterator that
1853 * will never throw {@link java.util.ConcurrentModificationException},
1854 * and guarantees to traverse elements as they existed upon
1855 * construction of the iterator, and may (but is not guaranteed to)
1856 * reflect any modifications subsequent to construction.
1857 *
1858 * @return a set view of the keys contained in this map.
1859 */
1860 public Set<K> keySet() {
1861 /*
1862 * Note: Lazy initialization works here and for other views
1863 * because view classes are stateless/immutable so it doesn't
1864 * matter wrt correctness if more than one is created (which
1865 * will only rarely happen). Even so, the following idiom
1866 * conservatively ensures that the method returns the one it
1867 * created if it does so, not one created by another racing
1868 * thread.
1869 */
1870 KeySet ks = keySet;
1871 return (ks != null) ? ks : (keySet = new KeySet());
1872 }
1873
1874 /**
1875 * Returns a set view of the keys contained in this map in
1876 * descending order. The set is backed by the map, so changes to
1877 * the map are reflected in the set, and vice-versa. The set
1878 * supports element removal, which removes the corresponding
1879 * mapping from this map, via the <tt>Iterator.remove</tt>,
1880 * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>,
1881 * and <tt>clear</tt> operations. It does not support the
1882 * <tt>add</tt> or <tt>addAll</tt> operations. The view's
1883 * <tt>iterator</tt> is a "weakly consistent" iterator that will
1884 * never throw {@link java.util.ConcurrentModificationException},
1885 * and guarantees to traverse elements as they existed upon
1886 * construction of the iterator, and may (but is not guaranteed
1887 * to) reflect any modifications subsequent to construction.
1888 *
1889 * @return a set view of the keys contained in this map.
1890 */
1891 public Set<K> descendingKeySet() {
1892 /*
1893 * Note: Lazy initialization works here and for other views
1894 * because view classes are stateless/immutable so it doesn't
1895 * matter wrt correctness if more than one is created (which
1896 * will only rarely happen). Even so, the following idiom
1897 * conservatively ensures that the method returns the one it
1898 * created if it does so, not one created by another racing
1899 * thread.
1900 */
1901 DescendingKeySet ks = descendingKeySet;
1902 return (ks != null) ? ks : (descendingKeySet = new DescendingKeySet());
1903 }
1904
1905 /**
1906 * Returns a collection view of the values contained in this map.
1907 * The collection is backed by the map, so changes to the map are
1908 * reflected in the collection, and vice-versa. The collection
1909 * supports element removal, which removes the corresponding
1910 * mapping from this map, via the <tt>Iterator.remove</tt>,
1911 * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
1912 * <tt>retainAll</tt>, and <tt>clear</tt> operations. It does not
1913 * support the <tt>add</tt> or <tt>addAll</tt> operations. The
1914 * view's <tt>iterator</tt> is a "weakly consistent" iterator that
1915 * will never throw {@link
1916 * java.util.ConcurrentModificationException}, and guarantees to
1917 * traverse elements as they existed upon construction of the
1918 * iterator, and may (but is not guaranteed to) reflect any
1919 * modifications subsequent to construction.
1920 *
1921 * @return a collection view of the values contained in this map.
1922 */
1923 public Collection<V> values() {
1924 Values vs = values;
1925 return (vs != null) ? vs : (values = new Values());
1926 }
1927
1928 /**
1929 * Returns a collection view of the mappings contained in this
1930 * map. Each element in the returned collection is a
1931 * <tt>Map.Entry</tt>. The collection is backed by the map, so
1932 * changes to the map are reflected in the collection, and
1933 * vice-versa. The collection supports element removal, which
1934 * removes the corresponding mapping from the map, via the
1935 * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
1936 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
1937 * operations. It does not support the <tt>add</tt> or
1938 * <tt>addAll</tt> operations. The view's <tt>iterator</tt> is a
1939 * "weakly consistent" iterator that will never throw {@link
1940 * java.util.ConcurrentModificationException}, and guarantees to
1941 * traverse elements as they existed upon construction of the
1942 * iterator, and may (but is not guaranteed to) reflect any
1943 * modifications subsequent to construction. The
1944 * <tt>Map.Entry</tt> elements returned by
1945 * <tt>iterator.next()</tt> do <em>not</em> support the
1946 * <tt>setValue</tt> operation.
1947 *
1948 * @return a collection view of the mappings contained in this map.
1949 */
1950 public Set<Map.Entry<K,V>> entrySet() {
1951 EntrySet es = entrySet;
1952 return (es != null) ? es : (entrySet = new EntrySet());
1953 }
1954
1955 /**
1956 * Returns a collection view of the mappings contained in this
1957 * map, in descending order. Each element in the returned
1958 * collection is a <tt>Map.Entry</tt>. The collection is backed
1959 * by the map, so changes to the map are reflected in the
1960 * collection, and vice-versa. The collection supports element
1961 * removal, which removes the corresponding mapping from the map,
1962 * via the <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
1963 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
1964 * operations. It does not support the <tt>add</tt> or
1965 * <tt>addAll</tt> operations. The view's <tt>iterator</tt> is a
1966 * "weakly consistent" iterator that will never throw {@link
1967 * java.util.ConcurrentModificationException}, and guarantees to
1968 * traverse elements as they existed upon construction of the
1969 * iterator, and may (but is not guaranteed to) reflect any
1970 * modifications subsequent to construction. The
1971 * <tt>Map.Entry</tt> elements returned by
1972 * <tt>iterator.next()</tt> do <em>not</em> support the
1973 * <tt>setValue</tt> operation.
1974 *
1975 * @return a collection view of the mappings contained in this map.
1976 */
1977 public Set<Map.Entry<K,V>> descendingEntrySet() {
1978 DescendingEntrySet es = descendingEntrySet;
1979 return (es != null) ? es : (descendingEntrySet = new DescendingEntrySet());
1980 }
1981
1982 /* ---------------- AbstractMap Overrides -------------- */
1983
1984 /**
1985 * Compares the specified object with this map for equality.
1986 * Returns <tt>true</tt> if the given object is also a map and the
1987 * two maps represent the same mappings. More formally, two maps
1988 * <tt>t1</tt> and <tt>t2</tt> represent the same mappings if
1989 * <tt>t1.keySet().equals(t2.keySet())</tt> and for every key
1990 * <tt>k</tt> in <tt>t1.keySet()</tt>, <tt> (t1.get(k)==null ?
1991 * t2.get(k)==null : t1.get(k).equals(t2.get(k))) </tt>. This
1992 * operation may return misleading results if either map is
1993 * concurrently modified during execution of this method.
1994 *
1995 * @param o object to be compared for equality with this map.
1996 * @return <tt>true</tt> if the specified object is equal to this map.
1997 */
1998 public boolean equals(Object o) {
1999 if (o == this)
2000 return true;
2001 if (!(o instanceof Map))
2002 return false;
2003 Map<K,V> t = (Map<K,V>) o;
2004 try {
2005 return (containsAllMappings(this, t) &&
2006 containsAllMappings(t, this));
2007 } catch (ClassCastException unused) {
2008 return false;
2009 } catch (NullPointerException unused) {
2010 return false;
2011 }
2012 }
2013
2014 /**
2015 * Helper for equals -- check for containment, avoiding nulls.
2016 */
2017 static <K,V> boolean containsAllMappings(Map<K,V> a, Map<K,V> b) {
2018 Iterator<Entry<K,V>> it = b.entrySet().iterator();
2019 while (it.hasNext()) {
2020 Entry<K,V> e = it.next();
2021 Object k = e.getKey();
2022 Object v = e.getValue();
2023 if (k == null || v == null || !v.equals(a.get(k)))
2024 return false;
2025 }
2026 return true;
2027 }
2028
2029 /* ------ ConcurrentMap API methods ------ */
2030
2031 /**
2032 * If the specified key is not already associated
2033 * with a value, associate it with the given value.
2034 * This is equivalent to
2035 * <pre>
2036 * if (!map.containsKey(key))
2037 * return map.put(key, value);
2038 * else
2039 * return map.get(key);</pre>
2040 * except that the action is performed atomically.
2041 * @param key key with which the specified value is to be associated.
2042 * @param value value to be associated with the specified key.
2043 * @return the previous value associated with specified key, or <tt>null</tt>
2044 * if there was no mapping for key.
2045 *
2046 * @throws ClassCastException if the key cannot be compared with the keys
2047 * currently in the map.
2048 * @throws NullPointerException if the key or value are <tt>null</tt>.
2049 */
2050 public V putIfAbsent(K key, V value) {
2051 if (value == null)
2052 throw new NullPointerException();
2053 return doPut(key, value, true);
2054 }
2055
2056 /**
2057 * Remove entry for key only if currently mapped to given value.
2058 * This is equivalent to
2059 * <pre>
2060 * if (map.containsKey(key) && map.get(key).equals(value)) {
2061 * map.remove(key);
2062 * return true;
2063 * } else return false;</pre>
2064 * except that the action is performed atomically.
2065 * @param key key with which the specified value is associated.
2066 * @param value value associated with the specified key.
2067 * @return true if the value was removed, false otherwise
2068 * @throws ClassCastException if the key cannot be compared with the keys
2069 * currently in the map.
2070 * @throws NullPointerException if the key or value are <tt>null</tt>.
2071 */
2072 public boolean remove(Object key, Object value) {
2073 if (value == null)
2074 throw new NullPointerException();
2075 return doRemove(key, value) != null;
2076 }
2077
2078 /**
2079 * Replaces entry for key only if currently mapped to given value.
2080 * This is equivalent to
2081 * <pre>
2082 * if (map.containsKey(key) && map.get(key).equals(oldValue)) {
2083 * map.put(key, newValue);
2084 * return true;
2085 * } else return false;</pre>
2086 * except that the action is performed atomically.
2087 * @param key key with which the specified value is associated.
2088 * @param oldValue value expected to be associated with the specified key.
2089 * @param newValue value to be associated with the specified key.
2090 * @return true if the value was replaced
2091 * @throws ClassCastException if the key cannot be compared with the keys
2092 * currently in the map.
2093 * @throws NullPointerException if key, oldValue or newValue are
2094 * <tt>null</tt>.
2095 */
2096 public boolean replace(K key, V oldValue, V newValue) {
2097 if (oldValue == null || newValue == null)
2098 throw new NullPointerException();
2099 Comparable<? super K> k = comparable(key);
2100 for (;;) {
2101 Node<K,V> n = findNode(k);
2102 if (n == null)
2103 return false;
2104 Object v = n.value;
2105 if (v != null) {
2106 if (!oldValue.equals(v))
2107 return false;
2108 if (n.casValue(v, newValue))
2109 return true;
2110 }
2111 }
2112 }
2113
2114 /**
2115 * Replaces entry for key only if currently mapped to some value.
2116 * This is equivalent to
2117 * <pre>
2118 * if (map.containsKey(key)) {
2119 * return map.put(key, value);
2120 * } else return null;</pre>
2121 * except that the action is performed atomically.
2122 * @param key key with which the specified value is associated.
2123 * @param value value to be associated with the specified key.
2124 * @return the previous value associated with specified key, or <tt>null</tt>
2125 * if there was no mapping for key.
2126 * @throws ClassCastException if the key cannot be compared with the keys
2127 * currently in the map.
2128 * @throws NullPointerException if the key or value are <tt>null</tt>.
2129 */
2130 public V replace(K key, V value) {
2131 if (value == null)
2132 throw new NullPointerException();
2133 Comparable<? super K> k = comparable(key);
2134 for (;;) {
2135 Node<K,V> n = findNode(k);
2136 if (n == null)
2137 return null;
2138 Object v = n.value;
2139 if (v != null && n.casValue(v, value))
2140 return (V)v;
2141 }
2142 }
2143
2144 /* ------ SortedMap API methods ------ */
2145
2146 /**
2147 * Returns the comparator used to order this map, or <tt>null</tt>
2148 * if this map uses its keys' natural order.
2149 *
2150 * @return the comparator associated with this map, or
2151 * <tt>null</tt> if it uses its keys' natural sort method.
2152 */
2153 public Comparator<? super K> comparator() {
2154 return comparator;
2155 }
2156
2157 /**
2158 * Returns the first (lowest) key currently in this map.
2159 *
2160 * @return the first (lowest) key currently in this map.
2161 * @throws NoSuchElementException Map is empty.
2162 */
2163 public K firstKey() {
2164 Node<K,V> n = findFirst();
2165 if (n == null)
2166 throw new NoSuchElementException();
2167 return n.key;
2168 }
2169
2170 /**
2171 * Returns the last (highest) key currently in this map.
2172 *
2173 * @return the last (highest) key currently in this map.
2174 * @throws NoSuchElementException Map is empty.
2175 */
2176 public K lastKey() {
2177 Node<K,V> n = findLast();
2178 if (n == null)
2179 throw new NoSuchElementException();
2180 return n.key;
2181 }
2182
2183 /**
2184 * Returns a view of the portion of this map whose keys range from
2185 * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. (If
2186 * <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned navigable map
2187 * is empty.) The returned navigable map is backed by this map, so changes
2188 * in the returned navigable map are reflected in this map, and vice-versa.
2189
2190 * @param fromKey low endpoint (inclusive) of the subMap.
2191 * @param toKey high endpoint (exclusive) of the subMap.
2192 *
2193 * @return a view of the portion of this map whose keys range from
2194 * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive.
2195 *
2196 * @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt>
2197 * cannot be compared to one another using this map's comparator
2198 * (or, if the map has no comparator, using natural ordering).
2199 * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
2200 * <tt>toKey</tt>.
2201 * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is
2202 * <tt>null</tt>.
2203 */
2204 public ConcurrentNavigableMap<K,V> navigableSubMap(K fromKey, K toKey) {
2205 if (fromKey == null || toKey == null)
2206 throw new NullPointerException();
2207 return new ConcurrentSkipListSubMap(this, fromKey, toKey);
2208 }
2209
2210 /**
2211 * Returns a view of the portion of this map whose keys are
2212 * strictly less than <tt>toKey</tt>. The returned navigable map is
2213 * backed by this map, so changes in the returned navigable map are
2214 * reflected in this map, and vice-versa.
2215 * @param toKey high endpoint (exclusive) of the headMap.
2216 * @return a view of the portion of this map whose keys are
2217 * strictly less than <tt>toKey</tt>.
2218 *
2219 * @throws ClassCastException if <tt>toKey</tt> is not compatible
2220 * with this map's comparator (or, if the map has no comparator,
2221 * if <tt>toKey</tt> does not implement <tt>Comparable</tt>).
2222 * @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt>.
2223 */
2224 public ConcurrentNavigableMap<K,V> navigableHeadMap(K toKey) {
2225 if (toKey == null)
2226 throw new NullPointerException();
2227 return new ConcurrentSkipListSubMap(this, null, toKey);
2228 }
2229
2230 /**
2231 * Returns a view of the portion of this map whose keys are
2232 * greater than or equal to <tt>fromKey</tt>. The returned navigable
2233 * map is backed by this map, so changes in the returned navigable
2234 * map are reflected in this map, and vice-versa.
2235 * @param fromKey low endpoint (inclusive) of the tailMap.
2236 * @return a view of the portion of this map whose keys are
2237 * greater than or equal to <tt>fromKey</tt>.
2238 * @throws ClassCastException if <tt>fromKey</tt> is not
2239 * compatible with this map's comparator (or, if the map has no
2240 * comparator, if <tt>fromKey</tt> does not implement
2241 * <tt>Comparable</tt>).
2242 * @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt>.
2243 */
2244 public ConcurrentNavigableMap<K,V> navigableTailMap(K fromKey) {
2245 if (fromKey == null)
2246 throw new NullPointerException();
2247 return new ConcurrentSkipListSubMap(this, fromKey, null);
2248 }
2249
2250
2251 /**
2252 * Equivalent to <tt>navigableSubMap</tt> but with a return
2253 * type conforming to the <tt>SortedMap</tt> interface.
2254 * @param fromKey low endpoint (inclusive) of the subMap.
2255 * @param toKey high endpoint (exclusive) of the subMap.
2256 *
2257 * @return a view of the portion of this map whose keys range from
2258 * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive.
2259 *
2260 * @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt>
2261 * cannot be compared to one another using this map's comparator
2262 * (or, if the map has no comparator, using natural ordering).
2263 * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
2264 * <tt>toKey</tt>.
2265 * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is
2266 * <tt>null</tt>.
2267 */
2268 public SortedMap<K,V> subMap(K fromKey, K toKey) {
2269 return navigableSubMap(fromKey, toKey);
2270 }
2271
2272 /**
2273 * Equivalent to <tt>navigableHeadMap</tt> but with a return
2274 * type conforming to the <tt>SortedMap</tt> interface.
2275 * @param toKey high endpoint (exclusive) of the headMap.
2276 * @return a view of the portion of this map whose keys are
2277 * strictly less than <tt>toKey</tt>.
2278 *
2279 * @throws ClassCastException if <tt>toKey</tt> is not compatible
2280 * with this map's comparator (or, if the map has no comparator,
2281 * if <tt>toKey</tt> does not implement <tt>Comparable</tt>).
2282 * @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt>.
2283 */
2284 public SortedMap<K,V> headMap(K toKey) {
2285 return navigableHeadMap(toKey);
2286 }
2287
2288 /**
2289 * Equivalent to <tt>navigableTailMap</tt> but with a return
2290 * type conforming to the <tt>SortedMap</tt> interface.
2291 * @param fromKey low endpoint (inclusive) of the tailMap.
2292 * @return a view of the portion of this map whose keys are
2293 * greater than or equal to <tt>fromKey</tt>.
2294 * @throws ClassCastException if <tt>fromKey</tt> is not
2295 * compatible with this map's comparator (or, if the map has no
2296 * comparator, if <tt>fromKey</tt> does not implement
2297 * <tt>Comparable</tt>).
2298 * @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt>.
2299 */
2300 public SortedMap<K,V> tailMap(K fromKey) {
2301 return navigableTailMap(fromKey);
2302 }
2303
2304 /* ---------------- Relational operations -------------- */
2305
2306 /**
2307 * Returns a key-value mapping associated with the least key
2308 * greater than or equal to the given key, or <tt>null</tt> if
2309 * there is no such entry. The returned entry does <em>not</em>
2310 * support the <tt>Entry.setValue</tt> method.
2311 *
2312 * @param key the key.
2313 * @return an Entry associated with ceiling of given key, or
2314 * <tt>null</tt> if there is no such Entry.
2315 * @throws ClassCastException if key cannot be compared with the
2316 * keys currently in the map.
2317 * @throws NullPointerException if key is <tt>null</tt>.
2318 */
2319 public Map.Entry<K,V> ceilingEntry(K key) {
2320 return getNear(key, GT|EQ);
2321 }
2322
2323 /**
2324 * Returns least key greater than or equal to the given key, or
2325 * <tt>null</tt> if there is no such key.
2326 *
2327 * @param key the key.
2328 * @return the ceiling key, or <tt>null</tt>
2329 * if there is no such key.
2330 * @throws ClassCastException if key cannot be compared with the keys
2331 * currently in the map.
2332 * @throws NullPointerException if key is <tt>null</tt>.
2333 */
2334 public K ceilingKey(K key) {
2335 Node<K,V> n = findNear(key, GT|EQ);
2336 return (n == null)? null : n.key;
2337 }
2338
2339 /**
2340 * Returns a key-value mapping associated with the greatest
2341 * key strictly less than the given key, or <tt>null</tt> if there is no
2342 * such entry. The returned entry does <em>not</em> support
2343 * the <tt>Entry.setValue</tt> method.
2344 *
2345 * @param key the key.
2346 * @return an Entry with greatest key less than the given
2347 * key, or <tt>null</tt> if there is no such Entry.
2348 * @throws ClassCastException if key cannot be compared with the keys
2349 * currently in the map.
2350 * @throws NullPointerException if key is <tt>null</tt>.
2351 */
2352 public Map.Entry<K,V> lowerEntry(K key) {
2353 return getNear(key, LT);
2354 }
2355
2356 /**
2357 * Returns the greatest key strictly less than the given key, or
2358 * <tt>null</tt> if there is no such key.
2359 *
2360 * @param key the key.
2361 * @return the greatest key less than the given
2362 * key, or <tt>null</tt> if there is no such key.
2363 * @throws ClassCastException if key cannot be compared with the keys
2364 * currently in the map.
2365 * @throws NullPointerException if key is <tt>null</tt>.
2366 */
2367 public K lowerKey(K key) {
2368 Node<K,V> n = findNear(key, LT);
2369 return (n == null)? null : n.key;
2370 }
2371
2372 /**
2373 * Returns a key-value mapping associated with the greatest key
2374 * less than or equal to the given key, or <tt>null</tt> if there
2375 * is no such entry. The returned entry does <em>not</em> support
2376 * the <tt>Entry.setValue</tt> method.
2377 *
2378 * @param key the key.
2379 * @return an Entry associated with floor of given key, or <tt>null</tt>
2380 * if there is no such Entry.
2381 * @throws ClassCastException if key cannot be compared with the keys
2382 * currently in the map.
2383 * @throws NullPointerException if key is <tt>null</tt>.
2384 */
2385 public Map.Entry<K,V> floorEntry(K key) {
2386 return getNear(key, LT|EQ);
2387 }
2388
2389 /**
2390 * Returns the greatest key
2391 * less than or equal to the given key, or <tt>null</tt> if there
2392 * is no such key.
2393 *
2394 * @param key the key.
2395 * @return the floor of given key, or <tt>null</tt> if there is no
2396 * such key.
2397 * @throws ClassCastException if key cannot be compared with the keys
2398 * currently in the map.
2399 * @throws NullPointerException if key is <tt>null</tt>.
2400 */
2401 public K floorKey(K key) {
2402 Node<K,V> n = findNear(key, LT|EQ);
2403 return (n == null)? null : n.key;
2404 }
2405
2406 /**
2407 * Returns a key-value mapping associated with the least key
2408 * strictly greater than the given key, or <tt>null</tt> if there
2409 * is no such entry. The returned entry does <em>not</em> support
2410 * the <tt>Entry.setValue</tt> method.
2411 *
2412 * @param key the key.
2413 * @return an Entry with least key greater than the given key, or
2414 * <tt>null</tt> if there is no such Entry.
2415 * @throws ClassCastException if key cannot be compared with the keys
2416 * currently in the map.
2417 * @throws NullPointerException if key is <tt>null</tt>.
2418 */
2419 public Map.Entry<K,V> higherEntry(K key) {
2420 return getNear(key, GT);
2421 }
2422
2423 /**
2424 * Returns the least key strictly greater than the given key, or
2425 * <tt>null</tt> if there is no such key.
2426 *
2427 * @param key the key.
2428 * @return the least key greater than the given key, or
2429 * <tt>null</tt> if there is no such key.
2430 * @throws ClassCastException if key cannot be compared with the keys
2431 * currently in the map.
2432 * @throws NullPointerException if key is <tt>null</tt>.
2433 */
2434 public K higherKey(K key) {
2435 Node<K,V> n = findNear(key, GT);
2436 return (n == null)? null : n.key;
2437 }
2438
2439 /**
2440 * Returns a key-value mapping associated with the least
2441 * key in this map, or <tt>null</tt> if the map is empty.
2442 * The returned entry does <em>not</em> support
2443 * the <tt>Entry.setValue</tt> method.
2444 *
2445 * @return an Entry with least key, or <tt>null</tt>
2446 * if the map is empty.
2447 */
2448 public Map.Entry<K,V> firstEntry() {
2449 for (;;) {
2450 Node<K,V> n = findFirst();
2451 if (n == null)
2452 return null;
2453 AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot();
2454 if (e != null)
2455 return e;
2456 }
2457 }
2458
2459 /**
2460 * Returns a key-value mapping associated with the greatest
2461 * key in this map, or <tt>null</tt> if the map is empty.
2462 * The returned entry does <em>not</em> support
2463 * the <tt>Entry.setValue</tt> method.
2464 *
2465 * @return an Entry with greatest key, or <tt>null</tt>
2466 * if the map is empty.
2467 */
2468 public Map.Entry<K,V> lastEntry() {
2469 for (;;) {
2470 Node<K,V> n = findLast();
2471 if (n == null)
2472 return null;
2473 AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot();
2474 if (e != null)
2475 return e;
2476 }
2477 }
2478
2479 /**
2480 * Removes and returns a key-value mapping associated with
2481 * the least key in this map, or <tt>null</tt> if the map is empty.
2482 * The returned entry does <em>not</em> support
2483 * the <tt>Entry.setValue</tt> method.
2484 *
2485 * @return the removed first entry of this map, or <tt>null</tt>
2486 * if the map is empty.
2487 */
2488 public Map.Entry<K,V> pollFirstEntry() {
2489 return (AbstractMap.SimpleImmutableEntry<K,V>)doRemoveFirst(false);
2490 }
2491
2492 /**
2493 * Removes and returns a key-value mapping associated with
2494 * the greatest key in this map, or <tt>null</tt> if the map is empty.
2495 * The returned entry does <em>not</em> support
2496 * the <tt>Entry.setValue</tt> method.
2497 *
2498 * @return the removed last entry of this map, or <tt>null</tt>
2499 * if the map is empty.
2500 */
2501 public Map.Entry<K,V> pollLastEntry() {
2502 return (AbstractMap.SimpleImmutableEntry<K,V>)doRemoveLast(false);
2503 }
2504
2505
2506 /* ---------------- Iterators -------------- */
2507
2508 /**
2509 * Base of ten kinds of iterator classes:
2510 * ascending: {map, submap} X {key, value, entry}
2511 * descending: {map, submap} X {key, entry}
2512 */
2513 abstract class Iter {
2514 /** the last node returned by next() */
2515 Node<K,V> last;
2516 /** the next node to return from next(); */
2517 Node<K,V> next;
2518 /** Cache of next value field to maintain weak consistency */
2519 Object nextValue;
2520
2521 Iter() {}
2522
2523 public final boolean hasNext() {
2524 return next != null;
2525 }
2526
2527 /** Initializes ascending iterator for entire range. */
2528 final void initAscending() {
2529 for (;;) {
2530 next = findFirst();
2531 if (next == null)
2532 break;
2533 nextValue = next.value;
2534 if (nextValue != null && nextValue != next)
2535 break;
2536 }
2537 }
2538
2539 /**
2540 * Initializes ascending iterator starting at given least key,
2541 * or first node if least is <tt>null</tt>, but not greater or
2542 * equal to fence, or end if fence is <tt>null</tt>.
2543 */
2544 final void initAscending(K least, K fence) {
2545 for (;;) {
2546 next = findCeiling(least);
2547 if (next == null)
2548 break;
2549 nextValue = next.value;
2550 if (nextValue != null && nextValue != next) {
2551 if (fence != null && compare(fence, next.key) <= 0) {
2552 next = null;
2553 nextValue = null;
2554 }
2555 break;
2556 }
2557 }
2558 }
2559 /** Advances next to higher entry. */
2560 final void ascend() {
2561 if ((last = next) == null)
2562 throw new NoSuchElementException();
2563 for (;;) {
2564 next = next.next;
2565 if (next == null)
2566 break;
2567 nextValue = next.value;
2568 if (nextValue != null && nextValue != next)
2569 break;
2570 }
2571 }
2572
2573 /**
2574 * Version of ascend for submaps to stop at fence
2575 */
2576 final void ascend(K fence) {
2577 if ((last = next) == null)
2578 throw new NoSuchElementException();
2579 for (;;) {
2580 next = next.next;
2581 if (next == null)
2582 break;
2583 nextValue = next.value;
2584 if (nextValue != null && nextValue != next) {
2585 if (fence != null && compare(fence, next.key) <= 0) {
2586 next = null;
2587 nextValue = null;
2588 }
2589 break;
2590 }
2591 }
2592 }
2593
2594 /** Initializes descending iterator for entire range. */
2595 final void initDescending() {
2596 for (;;) {
2597 next = findLast();
2598 if (next == null)
2599 break;
2600 nextValue = next.value;
2601 if (nextValue != null && nextValue != next)
2602 break;
2603 }
2604 }
2605
2606 /**
2607 * Initializes descending iterator starting at key less
2608 * than or equal to given fence key, or
2609 * last node if fence is <tt>null</tt>, but not less than
2610 * least, or beginning if least is <tt>null</tt>.
2611 */
2612 final void initDescending(K least, K fence) {
2613 for (;;) {
2614 next = findLower(fence);
2615 if (next == null)
2616 break;
2617 nextValue = next.value;
2618 if (nextValue != null && nextValue != next) {
2619 if (least != null && compare(least, next.key) > 0) {
2620 next = null;
2621 nextValue = null;
2622 }
2623 break;
2624 }
2625 }
2626 }
2627
2628 /** Advances next to lower entry. */
2629 final void descend() {
2630 if ((last = next) == null)
2631 throw new NoSuchElementException();
2632 K k = last.key;
2633 for (;;) {
2634 next = findNear(k, LT);
2635 if (next == null)
2636 break;
2637 nextValue = next.value;
2638 if (nextValue != null && nextValue != next)
2639 break;
2640 }
2641 }
2642
2643 /**
2644 * Version of descend for submaps to stop at least
2645 */
2646 final void descend(K least) {
2647 if ((last = next) == null)
2648 throw new NoSuchElementException();
2649 K k = last.key;
2650 for (;;) {
2651 next = findNear(k, LT);
2652 if (next == null)
2653 break;
2654 nextValue = next.value;
2655 if (nextValue != null && nextValue != next) {
2656 if (least != null && compare(least, next.key) > 0) {
2657 next = null;
2658 nextValue = null;
2659 }
2660 break;
2661 }
2662 }
2663 }
2664
2665 public void remove() {
2666 Node<K,V> l = last;
2667 if (l == null)
2668 throw new IllegalStateException();
2669 // It would not be worth all of the overhead to directly
2670 // unlink from here. Using remove is fast enough.
2671 ConcurrentSkipListMap.this.remove(l.key);
2672 }
2673
2674 }
2675
2676 final class ValueIterator extends Iter implements Iterator<V> {
2677 ValueIterator() {
2678 initAscending();
2679 }
2680 public V next() {
2681 Object v = nextValue;
2682 ascend();
2683 return (V)v;
2684 }
2685 }
2686
2687 final class KeyIterator extends Iter implements Iterator<K> {
2688 KeyIterator() {
2689 initAscending();
2690 }
2691 public K next() {
2692 Node<K,V> n = next;
2693 ascend();
2694 return n.key;
2695 }
2696 }
2697
2698 class SubMapValueIterator extends Iter implements Iterator<V> {
2699 final K fence;
2700 SubMapValueIterator(K least, K fence) {
2701 initAscending(least, fence);
2702 this.fence = fence;
2703 }
2704
2705 public V next() {
2706 Object v = nextValue;
2707 ascend(fence);
2708 return (V)v;
2709 }
2710 }
2711
2712 final class SubMapKeyIterator extends Iter implements Iterator<K> {
2713 final K fence;
2714 SubMapKeyIterator(K least, K fence) {
2715 initAscending(least, fence);
2716 this.fence = fence;
2717 }
2718
2719 public K next() {
2720 Node<K,V> n = next;
2721 ascend(fence);
2722 return n.key;
2723 }
2724 }
2725
2726 final class DescendingKeyIterator extends Iter implements Iterator<K> {
2727 DescendingKeyIterator() {
2728 initDescending();
2729 }
2730 public K next() {
2731 Node<K,V> n = next;
2732 descend();
2733 return n.key;
2734 }
2735 }
2736
2737 final class DescendingSubMapKeyIterator extends Iter implements Iterator<K> {
2738 final K least;
2739 DescendingSubMapKeyIterator(K least, K fence) {
2740 initDescending(least, fence);
2741 this.least = least;
2742 }
2743
2744 public K next() {
2745 Node<K,V> n = next;
2746 descend(least);
2747 return n.key;
2748 }
2749 }
2750
2751 /**
2752 * Entry iterators use the same trick as in ConcurrentHashMap and
2753 * elsewhere of using the iterator itself to represent entries,
2754 * thus avoiding having to create entry objects in next().
2755 */
2756 abstract class EntryIter extends Iter implements Map.Entry<K,V> {
2757 /** Cache of last value returned */
2758 Object lastValue;
2759
2760 EntryIter() {
2761 }
2762
2763 public K getKey() {
2764 Node<K,V> l = last;
2765 if (l == null)
2766 throw new IllegalStateException();
2767 return l.key;
2768 }
2769
2770 public V getValue() {
2771 Object v = lastValue;
2772 if (last == null || v == null)
2773 throw new IllegalStateException();
2774 return (V)v;
2775 }
2776
2777 public V setValue(V value) {
2778 throw new UnsupportedOperationException();
2779 }
2780
2781 public boolean equals(Object o) {
2782 // If not acting as entry, just use default.
2783 if (last == null)
2784 return super.equals(o);
2785 if (!(o instanceof Map.Entry))
2786 return false;
2787 Map.Entry e = (Map.Entry)o;
2788 return (getKey().equals(e.getKey()) &&
2789 getValue().equals(e.getValue()));
2790 }
2791
2792 public int hashCode() {
2793 // If not acting as entry, just use default.
2794 if (last == null)
2795 return super.hashCode();
2796 return getKey().hashCode() ^ getValue().hashCode();
2797 }
2798
2799 public String toString() {
2800 // If not acting as entry, just use default.
2801 if (last == null)
2802 return super.toString();
2803 return getKey() + "=" + getValue();
2804 }
2805 }
2806
2807 final class EntryIterator extends EntryIter
2808 implements Iterator<Map.Entry<K,V>> {
2809 EntryIterator() {
2810 initAscending();
2811 }
2812 public Map.Entry<K,V> next() {
2813 lastValue = nextValue;
2814 ascend();
2815 return this;
2816 }
2817 }
2818
2819 final class SubMapEntryIterator extends EntryIter
2820 implements Iterator<Map.Entry<K,V>> {
2821 final K fence;
2822 SubMapEntryIterator(K least, K fence) {
2823 initAscending(least, fence);
2824 this.fence = fence;
2825 }
2826
2827 public Map.Entry<K,V> next() {
2828 lastValue = nextValue;
2829 ascend(fence);
2830 return this;
2831 }
2832 }
2833
2834 final class DescendingEntryIterator extends EntryIter
2835 implements Iterator<Map.Entry<K,V>> {
2836 DescendingEntryIterator() {
2837 initDescending();
2838 }
2839 public Map.Entry<K,V> next() {
2840 lastValue = nextValue;
2841 descend();
2842 return this;
2843 }
2844 }
2845
2846 final class DescendingSubMapEntryIterator extends EntryIter
2847 implements Iterator<Map.Entry<K,V>> {
2848 final K least;
2849 DescendingSubMapEntryIterator(K least, K fence) {
2850 initDescending(least, fence);
2851 this.least = least;
2852 }
2853
2854 public Map.Entry<K,V> next() {
2855 lastValue = nextValue;
2856 descend(least);
2857 return this;
2858 }
2859 }
2860
2861 // Factory methods for iterators needed by submaps and/or
2862 // ConcurrentSkipListSet
2863
2864 Iterator<K> keyIterator() {
2865 return new KeyIterator();
2866 }
2867
2868 Iterator<K> descendingKeyIterator() {
2869 return new DescendingKeyIterator();
2870 }
2871
2872 SubMapEntryIterator subMapEntryIterator(K least, K fence) {
2873 return new SubMapEntryIterator(least, fence);
2874 }
2875
2876 DescendingSubMapEntryIterator descendingSubMapEntryIterator(K least, K fence) {
2877 return new DescendingSubMapEntryIterator(least, fence);
2878 }
2879
2880 SubMapKeyIterator subMapKeyIterator(K least, K fence) {
2881 return new SubMapKeyIterator(least, fence);
2882 }
2883
2884 DescendingSubMapKeyIterator descendingSubMapKeyIterator(K least, K fence) {
2885 return new DescendingSubMapKeyIterator(least, fence);
2886 }
2887
2888 SubMapValueIterator subMapValueIterator(K least, K fence) {
2889 return new SubMapValueIterator(least, fence);
2890 }
2891
2892 /* ---------------- Views -------------- */
2893
2894 class KeySet extends AbstractSet<K> {
2895 public Iterator<K> iterator() {
2896 return new KeyIterator();
2897 }
2898 public boolean isEmpty() {
2899 return ConcurrentSkipListMap.this.isEmpty();
2900 }
2901 public int size() {
2902 return ConcurrentSkipListMap.this.size();
2903 }
2904 public boolean contains(Object o) {
2905 return ConcurrentSkipListMap.this.containsKey(o);
2906 }
2907 public boolean remove(Object o) {
2908 return ConcurrentSkipListMap.this.removep(o);
2909 }
2910 public void clear() {
2911 ConcurrentSkipListMap.this.clear();
2912 }
2913 public Object[] toArray() {
2914 Collection<K> c = new ArrayList<K>();
2915 for (Iterator<K> i = iterator(); i.hasNext(); )
2916 c.add(i.next());
2917 return c.toArray();
2918 }
2919 public <T> T[] toArray(T[] a) {
2920 Collection<K> c = new ArrayList<K>();
2921 for (Iterator<K> i = iterator(); i.hasNext(); )
2922 c.add(i.next());
2923 return c.toArray(a);
2924 }
2925 }
2926
2927 class DescendingKeySet extends KeySet {
2928 public Iterator<K> iterator() {
2929 return new DescendingKeyIterator();
2930 }
2931 }
2932
2933 final class Values extends AbstractCollection<V> {
2934 public Iterator<V> iterator() {
2935 return new ValueIterator();
2936 }
2937 public boolean isEmpty() {
2938 return ConcurrentSkipListMap.this.isEmpty();
2939 }
2940 public int size() {
2941 return ConcurrentSkipListMap.this.size();
2942 }
2943 public boolean contains(Object o) {
2944 return ConcurrentSkipListMap.this.containsValue(o);
2945 }
2946 public void clear() {
2947 ConcurrentSkipListMap.this.clear();
2948 }
2949 public Object[] toArray() {
2950 Collection<V> c = new ArrayList<V>();
2951 for (Iterator<V> i = iterator(); i.hasNext(); )
2952 c.add(i.next());
2953 return c.toArray();
2954 }
2955 public <T> T[] toArray(T[] a) {
2956 Collection<V> c = new ArrayList<V>();
2957 for (Iterator<V> i = iterator(); i.hasNext(); )
2958 c.add(i.next());
2959 return c.toArray(a);
2960 }
2961 }
2962
2963 class EntrySet extends AbstractSet<Map.Entry<K,V>> {
2964 public Iterator<Map.Entry<K,V>> iterator() {
2965 return new EntryIterator();
2966 }
2967 public boolean contains(Object o) {
2968 if (!(o instanceof Map.Entry))
2969 return false;
2970 Map.Entry<K,V> e = (Map.Entry<K,V>)o;
2971 V v = ConcurrentSkipListMap.this.get(e.getKey());
2972 return v != null && v.equals(e.getValue());
2973 }
2974 public boolean remove(Object o) {
2975 if (!(o instanceof Map.Entry))
2976 return false;
2977 Map.Entry<K,V> e = (Map.Entry<K,V>)o;
2978 return ConcurrentSkipListMap.this.remove(e.getKey(),
2979 e.getValue());
2980 }
2981 public boolean isEmpty() {
2982 return ConcurrentSkipListMap.this.isEmpty();
2983 }
2984 public int size() {
2985 return ConcurrentSkipListMap.this.size();
2986 }
2987 public void clear() {
2988 ConcurrentSkipListMap.this.clear();
2989 }
2990
2991 public Object[] toArray() {
2992 Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>();
2993 for (Map.Entry e : this)
2994 c.add(new AbstractMap.SimpleEntry(e.getKey(), e.getValue()));
2995 return c.toArray();
2996 }
2997 public <T> T[] toArray(T[] a) {
2998 Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>();
2999 for (Map.Entry e : this)
3000 c.add(new AbstractMap.SimpleEntry(e.getKey(), e.getValue()));
3001 return c.toArray(a);
3002 }
3003 }
3004
3005 class DescendingEntrySet extends EntrySet {
3006 public Iterator<Map.Entry<K,V>> iterator() {
3007 return new DescendingEntryIterator();
3008 }
3009 }
3010
3011 /**
3012 * Submaps returned by {@link ConcurrentSkipListMap} submap operations
3013 * represent a subrange of mappings of their underlying
3014 * maps. Instances of this class support all methods of their
3015 * underlying maps, differing in that mappings outside their range are
3016 * ignored, and attempts to add mappings outside their ranges result
3017 * in {@link IllegalArgumentException}. Instances of this class are
3018 * constructed only using the <tt>subMap</tt>, <tt>headMap</tt>, and
3019 * <tt>tailMap</tt> methods of their underlying maps.
3020 */
3021 static class ConcurrentSkipListSubMap<K,V> extends AbstractMap<K,V>
3022 implements ConcurrentNavigableMap<K,V>, java.io.Serializable {
3023
3024 private static final long serialVersionUID = -7647078645895051609L;
3025
3026 /** Underlying map */
3027 private final ConcurrentSkipListMap<K,V> m;
3028 /** lower bound key, or null if from start */
3029 private final K least;
3030 /** upper fence key, or null if to end */
3031 private final K fence;
3032 // Lazily initialized view holders
3033 private transient Set<K> keySetView;
3034 private transient Set<Map.Entry<K,V>> entrySetView;
3035 private transient Collection<V> valuesView;
3036 private transient Set<K> descendingKeySetView;
3037 private transient Set<Map.Entry<K,V>> descendingEntrySetView;
3038
3039 /**
3040 * Creates a new submap.
3041 * @param least inclusive least value, or <tt>null</tt> if from start
3042 * @param fence exclusive upper bound or <tt>null</tt> if to end
3043 * @throws IllegalArgumentException if least and fence nonnull
3044 * and least greater than fence
3045 */
3046 ConcurrentSkipListSubMap(ConcurrentSkipListMap<K,V> map,
3047 K least, K fence) {
3048 if (least != null &&
3049 fence != null &&
3050 map.compare(least, fence) > 0)
3051 throw new IllegalArgumentException("inconsistent range");
3052 this.m = map;
3053 this.least = least;
3054 this.fence = fence;
3055 }
3056
3057 /* ---------------- Utilities -------------- */
3058
3059 boolean inHalfOpenRange(K key) {
3060 return m.inHalfOpenRange(key, least, fence);
3061 }
3062
3063 boolean inOpenRange(K key) {
3064 return m.inOpenRange(key, least, fence);
3065 }
3066
3067 ConcurrentSkipListMap.Node<K,V> firstNode() {
3068 return m.findCeiling(least);
3069 }
3070
3071 ConcurrentSkipListMap.Node<K,V> lastNode() {
3072 return m.findLower(fence);
3073 }
3074
3075 boolean isBeforeEnd(ConcurrentSkipListMap.Node<K,V> n) {
3076 return (n != null &&
3077 (fence == null ||
3078 n.key == null || // pass by markers and headers
3079 m.compare(fence, n.key) > 0));
3080 }
3081
3082 void checkKey(K key) throws IllegalArgumentException {
3083 if (!inHalfOpenRange(key))
3084 throw new IllegalArgumentException("key out of range");
3085 }
3086
3087 /**
3088 * Returns underlying map. Needed by ConcurrentSkipListSet
3089 * @return the backing map
3090 */
3091 ConcurrentSkipListMap<K,V> getMap() {
3092 return m;
3093 }
3094
3095 /**
3096 * Returns least key. Needed by ConcurrentSkipListSet
3097 * @return least key or <tt>null</tt> if from start
3098 */
3099 K getLeast() {
3100 return least;
3101 }
3102
3103 /**
3104 * Returns fence key. Needed by ConcurrentSkipListSet
3105 * @return fence key or <tt>null</tt> if to end
3106 */
3107 K getFence() {
3108 return fence;
3109 }
3110
3111
3112 /* ---------------- Map API methods -------------- */
3113
3114 public boolean containsKey(Object key) {
3115 K k = (K)key;
3116 return inHalfOpenRange(k) && m.containsKey(k);
3117 }
3118
3119 public V get(Object key) {
3120 K k = (K)key;
3121 return ((!inHalfOpenRange(k)) ? null : m.get(k));
3122 }
3123
3124 public V put(K key, V value) {
3125 checkKey(key);
3126 return m.put(key, value);
3127 }
3128
3129 public V remove(Object key) {
3130 K k = (K)key;
3131 return (!inHalfOpenRange(k))? null : m.remove(k);
3132 }
3133
3134 public int size() {
3135 long count = 0;
3136 for (ConcurrentSkipListMap.Node<K,V> n = firstNode();
3137 isBeforeEnd(n);
3138 n = n.next) {
3139 if (n.getValidValue() != null)
3140 ++count;
3141 }
3142 return count >= Integer.MAX_VALUE? Integer.MAX_VALUE : (int)count;
3143 }
3144
3145 public boolean isEmpty() {
3146 return !isBeforeEnd(firstNode());
3147 }
3148
3149 public boolean containsValue(Object value) {
3150 if (value == null)
3151 throw new NullPointerException();
3152 for (ConcurrentSkipListMap.Node<K,V> n = firstNode();
3153 isBeforeEnd(n);
3154 n = n.next) {
3155 V v = n.getValidValue();
3156 if (v != null && value.equals(v))
3157 return true;
3158 }
3159 return false;
3160 }
3161
3162 public void clear() {
3163 for (ConcurrentSkipListMap.Node<K,V> n = firstNode();
3164 isBeforeEnd(n);
3165 n = n.next) {
3166 if (n.getValidValue() != null)
3167 m.remove(n.key);
3168 }
3169 }
3170
3171 /* ---------------- ConcurrentMap API methods -------------- */
3172
3173 public V putIfAbsent(K key, V value) {
3174 checkKey(key);
3175 return m.putIfAbsent(key, value);
3176 }
3177
3178 public boolean remove(Object key, Object value) {
3179 K k = (K)key;
3180 return inHalfOpenRange(k) && m.remove(k, value);
3181 }
3182
3183 public boolean replace(K key, V oldValue, V newValue) {
3184 checkKey(key);
3185 return m.replace(key, oldValue, newValue);
3186 }
3187
3188 public V replace(K key, V value) {
3189 checkKey(key);
3190 return m.replace(key, value);
3191 }
3192
3193 /* ---------------- SortedMap API methods -------------- */
3194
3195 public Comparator<? super K> comparator() {
3196 return m.comparator();
3197 }
3198
3199 public K firstKey() {
3200 ConcurrentSkipListMap.Node<K,V> n = firstNode();
3201 if (isBeforeEnd(n))
3202 return n.key;
3203 else
3204 throw new NoSuchElementException();
3205 }
3206
3207 public K lastKey() {
3208 ConcurrentSkipListMap.Node<K,V> n = lastNode();
3209 if (n != null) {
3210 K last = n.key;
3211 if (inHalfOpenRange(last))
3212 return last;
3213 }
3214 throw new NoSuchElementException();
3215 }
3216
3217 public ConcurrentNavigableMap<K,V> navigableSubMap(K fromKey, K toKey) {
3218 if (fromKey == null || toKey == null)
3219 throw new NullPointerException();
3220 if (!inOpenRange(fromKey) || !inOpenRange(toKey))
3221 throw new IllegalArgumentException("key out of range");
3222 return new ConcurrentSkipListSubMap(m, fromKey, toKey);
3223 }
3224
3225 public ConcurrentNavigableMap<K,V> navigableHeadMap(K toKey) {
3226 if (toKey == null)
3227 throw new NullPointerException();
3228 if (!inOpenRange(toKey))
3229 throw new IllegalArgumentException("key out of range");
3230 return new ConcurrentSkipListSubMap(m, least, toKey);
3231 }
3232
3233 public ConcurrentNavigableMap<K,V> navigableTailMap(K fromKey) {
3234 if (fromKey == null)
3235 throw new NullPointerException();
3236 if (!inOpenRange(fromKey))
3237 throw new IllegalArgumentException("key out of range");
3238 return new ConcurrentSkipListSubMap(m, fromKey, fence);
3239 }
3240
3241 public SortedMap<K,V> subMap(K fromKey, K toKey) {
3242 return navigableSubMap(fromKey, toKey);
3243 }
3244
3245 public SortedMap<K,V> headMap(K toKey) {
3246 return navigableHeadMap(toKey);
3247 }
3248
3249 public SortedMap<K,V> tailMap(K fromKey) {
3250 return navigableTailMap(fromKey);
3251 }
3252
3253 /* ---------------- Relational methods -------------- */
3254
3255 public Map.Entry<K,V> ceilingEntry(K key) {
3256 return (Map.Entry<K,V>)
3257 m.getNear(key, m.GT|m.EQ, least, fence, false);
3258 }
3259
3260 public K ceilingKey(K key) {
3261 return (K)
3262 m.getNear(key, m.GT|m.EQ, least, fence, true);
3263 }
3264
3265 public Map.Entry<K,V> lowerEntry(K key) {
3266 return (Map.Entry<K,V>)
3267 m.getNear(key, m.LT, least, fence, false);
3268 }
3269
3270 public K lowerKey(K key) {
3271 return (K)
3272 m.getNear(key, m.LT, least, fence, true);
3273 }
3274
3275 public Map.Entry<K,V> floorEntry(K key) {
3276 return (Map.Entry<K,V>)
3277 m.getNear(key, m.LT|m.EQ, least, fence, false);
3278 }
3279
3280 public K floorKey(K key) {
3281 return (K)
3282 m.getNear(key, m.LT|m.EQ, least, fence, true);
3283 }
3284
3285
3286 public Map.Entry<K,V> higherEntry(K key) {
3287 return (Map.Entry<K,V>)
3288 m.getNear(key, m.GT, least, fence, false);
3289 }
3290
3291 public K higherKey(K key) {
3292 return (K)
3293 m.getNear(key, m.GT, least, fence, true);
3294 }
3295
3296 public Map.Entry<K,V> firstEntry() {
3297 for (;;) {
3298 ConcurrentSkipListMap.Node<K,V> n = firstNode();
3299 if (!isBeforeEnd(n))
3300 return null;
3301 Map.Entry<K,V> e = n.createSnapshot();
3302 if (e != null)
3303 return e;
3304 }
3305 }
3306
3307 public Map.Entry<K,V> lastEntry() {
3308 for (;;) {
3309 ConcurrentSkipListMap.Node<K,V> n = lastNode();
3310 if (n == null || !inHalfOpenRange(n.key))
3311 return null;
3312 Map.Entry<K,V> e = n.createSnapshot();
3313 if (e != null)
3314 return e;
3315 }
3316 }
3317
3318 public Map.Entry<K,V> pollFirstEntry() {
3319 return (Map.Entry<K,V>)
3320 m.removeFirstEntryOfSubrange(least, fence, false);
3321 }
3322
3323 public Map.Entry<K,V> pollLastEntry() {
3324 return (Map.Entry<K,V>)
3325 m.removeLastEntryOfSubrange(least, fence, false);
3326 }
3327
3328 /* ---------------- Submap Views -------------- */
3329
3330 public Set<K> keySet() {
3331 Set<K> ks = keySetView;
3332 return (ks != null) ? ks : (keySetView = new KeySetView());
3333 }
3334
3335 class KeySetView extends AbstractSet<K> {
3336 public Iterator<K> iterator() {
3337 return m.subMapKeyIterator(least, fence);
3338 }
3339 public int size() {
3340 return ConcurrentSkipListSubMap.this.size();
3341 }
3342 public boolean isEmpty() {
3343 return ConcurrentSkipListSubMap.this.isEmpty();
3344 }
3345 public boolean contains(Object k) {
3346 return ConcurrentSkipListSubMap.this.containsKey(k);
3347 }
3348 public Object[] toArray() {
3349 Collection<K> c = new ArrayList<K>();
3350 for (Iterator<K> i = iterator(); i.hasNext(); )
3351 c.add(i.next());
3352 return c.toArray();
3353 }
3354 public <T> T[] toArray(T[] a) {
3355 Collection<K> c = new ArrayList<K>();
3356 for (Iterator<K> i = iterator(); i.hasNext(); )
3357 c.add(i.next());
3358 return c.toArray(a);
3359 }
3360 }
3361
3362 public Set<K> descendingKeySet() {
3363 Set<K> ks = descendingKeySetView;
3364 return (ks != null) ? ks : (descendingKeySetView = new DescendingKeySetView());
3365 }
3366
3367 class DescendingKeySetView extends KeySetView {
3368 public Iterator<K> iterator() {
3369 return m.descendingSubMapKeyIterator(least, fence);
3370 }
3371 }
3372
3373 public Collection<V> values() {
3374 Collection<V> vs = valuesView;
3375 return (vs != null) ? vs : (valuesView = new ValuesView());
3376 }
3377
3378 class ValuesView extends AbstractCollection<V> {
3379 public Iterator<V> iterator() {
3380 return m.subMapValueIterator(least, fence);
3381 }
3382 public int size() {
3383 return ConcurrentSkipListSubMap.this.size();
3384 }
3385 public boolean isEmpty() {
3386 return ConcurrentSkipListSubMap.this.isEmpty();
3387 }
3388 public boolean contains(Object v) {
3389 return ConcurrentSkipListSubMap.this.containsValue(v);
3390 }
3391 public Object[] toArray() {
3392 Collection<V> c = new ArrayList<V>();
3393 for (Iterator<V> i = iterator(); i.hasNext(); )
3394 c.add(i.next());
3395 return c.toArray();
3396 }
3397 public <T> T[] toArray(T[] a) {
3398 Collection<V> c = new ArrayList<V>();
3399 for (Iterator<V> i = iterator(); i.hasNext(); )
3400 c.add(i.next());
3401 return c.toArray(a);
3402 }
3403 }
3404
3405 public Set<Map.Entry<K,V>> entrySet() {
3406 Set<Map.Entry<K,V>> es = entrySetView;
3407 return (es != null) ? es : (entrySetView = new EntrySetView());
3408 }
3409
3410 class EntrySetView extends AbstractSet<Map.Entry<K,V>> {
3411 public Iterator<Map.Entry<K,V>> iterator() {
3412 return m.subMapEntryIterator(least, fence);
3413 }
3414 public int size() {
3415 return ConcurrentSkipListSubMap.this.size();
3416 }
3417 public boolean isEmpty() {
3418 return ConcurrentSkipListSubMap.this.isEmpty();
3419 }
3420 public boolean contains(Object o) {
3421 if (!(o instanceof Map.Entry))
3422 return false;
3423 Map.Entry<K,V> e = (Map.Entry<K,V>) o;
3424 K key = e.getKey();
3425 if (!inHalfOpenRange(key))
3426 return false;
3427 V v = m.get(key);
3428 return v != null && v.equals(e.getValue());
3429 }
3430 public boolean remove(Object o) {
3431 if (!(o instanceof Map.Entry))
3432 return false;
3433 Map.Entry<K,V> e = (Map.Entry<K,V>) o;
3434 K key = e.getKey();
3435 if (!inHalfOpenRange(key))
3436 return false;
3437 return m.remove(key, e.getValue());
3438 }
3439 public Object[] toArray() {
3440 Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>();
3441 for (Map.Entry e : this)
3442 c.add(new AbstractMap.SimpleEntry(e.getKey(), e.getValue()));
3443 return c.toArray();
3444 }
3445 public <T> T[] toArray(T[] a) {
3446 Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>();
3447 for (Map.Entry e : this)
3448 c.add(new AbstractMap.SimpleEntry(e.getKey(), e.getValue()));
3449 return c.toArray(a);
3450 }
3451 }
3452
3453 public Set<Map.Entry<K,V>> descendingEntrySet() {
3454 Set<Map.Entry<K,V>> es = descendingEntrySetView;
3455 return (es != null) ? es : (descendingEntrySetView = new DescendingEntrySetView());
3456 }
3457
3458 class DescendingEntrySetView extends EntrySetView {
3459 public Iterator<Map.Entry<K,V>> iterator() {
3460 return m.descendingSubMapEntryIterator(least, fence);
3461 }
3462 }
3463 }
3464 }