1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package extra166y; |
25 |
|
* <li> {@linkplain SoftReference Soft}, {@linkplain |
26 |
|
* WeakReference weak} or strong (regular) keys and values. |
27 |
|
* |
28 |
< |
* <li> User-definable <code>MappingFunctions</code> that may be |
28 |
> |
* <li> User-definable {@code MappingFunctions} that may be |
29 |
|
* used in method {@link |
30 |
|
* CustomConcurrentHashMap#computeIfAbsent} to atomically |
31 |
|
* establish a computed value, along with |
32 |
< |
* <code>RemappingFunctions</code> that can be used in method |
32 |
> |
* {@code RemappingFunctions} that can be used in method |
33 |
|
* {@link CustomConcurrentHashMap#compute} to atomically |
34 |
|
* replace values. |
35 |
|
* |
36 |
< |
* <li>Factory methods returning specialized forms for <tt>int</tt> |
36 |
> |
* <li>Factory methods returning specialized forms for {@code int} |
37 |
|
* keys and/or values, that may be more space-efficient |
38 |
|
* |
39 |
|
* </ul> |
53 |
|
* (STRONG, |
54 |
|
* new Equivalence<Person>() { |
55 |
|
* public boolean equal(Person k, Object x) { |
56 |
< |
* return x instanceOf Person && k.name.equals(((Person)x).name); |
56 |
> |
* return x instanceof Person && k.name.equals(((Person)x).name); |
57 |
|
* } |
58 |
|
* public int hash(Object x) { |
59 |
< |
* return (x instanceOf Person)? ((Person)x).name.hashCode() : 0; |
59 |
> |
* return (x instanceof Person) ? ((Person)x).name.hashCode() : 0; |
60 |
|
* } |
61 |
|
* }, |
62 |
|
* STRONG, EQUALS, 0); |
72 |
|
* |
73 |
|
* <p>This class also includes nested class {@link KeySet} |
74 |
|
* that provides space-efficient Set views of maps, also supporting |
75 |
< |
* method <code>intern</code>, which may be of use in canonicalizing |
75 |
> |
* method {@code intern}, which may be of use in canonicalizing |
76 |
|
* elements. |
77 |
|
* |
78 |
|
* <p>When used with (Weak or Soft) Reference keys and/or values, |
79 |
< |
* elements that have asynchronously become <code>null</code> are |
79 |
> |
* elements that have asynchronously become {@code null} are |
80 |
|
* treated as absent from the map and (eventually) removed from maps |
81 |
|
* via a background thread common across all maps. Because of the |
82 |
|
* potential for asynchronous clearing of References, methods such as |
83 |
< |
* <code>containsValue</code> have weaker guarantees than you might |
84 |
< |
* expect even in the absence of other explicity concurrent |
85 |
< |
* operations. For example <code>containsValue(value)</code> may |
86 |
< |
* return true even if <code>value</code> is no longer available upon |
83 |
> |
* {@code containsValue} have weaker guarantees than you might |
84 |
> |
* expect even in the absence of other explicitly concurrent |
85 |
> |
* operations. For example {@code containsValue(value)} may |
86 |
> |
* return true even if {@code value} is no longer available upon |
87 |
|
* return from the method. |
88 |
|
* |
89 |
|
* <p>When Equivalences other than equality are used, the returned |
90 |
< |
* collections may violate the specifications of <tt>Map</tt> and/or |
91 |
< |
* <tt>Set</tt> interfaces, which mandate the use of the |
92 |
< |
* <tt>equals</tt> method when comparing objects. The methods of this |
90 |
> |
* collections may violate the specifications of {@code Map} and/or |
91 |
> |
* {@code Set} interfaces, which mandate the use of the |
92 |
> |
* {@code equals} method when comparing objects. The methods of this |
93 |
|
* class otherwise have properties similar to those of {@link |
94 |
|
* java.util.ConcurrentHashMap} under its default settings. To |
95 |
|
* adaptively maintain semantics and performance under varying |
165 |
|
* An object performing equality comparisons, along with a hash |
166 |
|
* function consistent with this comparison. The type signatures |
167 |
|
* of the methods of this interface reflect those of {@link |
168 |
< |
* java.util.Map}: While only elements of <code>K</code> may be |
169 |
< |
* entered into a Map, any <code>Object</code> may be tested for |
168 |
> |
* java.util.Map}: While only elements of {@code K} may be |
169 |
> |
* entered into a Map, any {@code Object} may be tested for |
170 |
|
* membership. Note that the performance of hash maps is heavily |
171 |
|
* dependent on the quality of hash functions. |
172 |
|
*/ |
224 |
|
|
225 |
|
/** |
226 |
|
* A function computing a mapping from the given key to a value, |
227 |
< |
* or <code>null</code> if there is no mapping. |
227 |
> |
* or {@code null} if there is no mapping. |
228 |
|
*/ |
229 |
|
public static interface MappingFunction<K, V> { |
230 |
|
/** |
236 |
|
* simple. The most common usage is to construct a new object |
237 |
|
* serving as an initial mapped value. |
238 |
|
* |
239 |
< |
* @param key the (nonnull) key |
239 |
> |
* @param key the (non-null) key |
240 |
|
* @return a value, or null if none |
241 |
|
*/ |
242 |
|
V map(K key); |
244 |
|
|
245 |
|
/** |
246 |
|
* A function computing a new mapping from the given key and its |
247 |
< |
* current value to a new value, or <code>null</code> if there is |
247 |
> |
* current value to a new value, or {@code null} if there is |
248 |
|
* no mapping |
249 |
|
*/ |
250 |
|
public static interface RemappingFunction<K, V> { |
275 |
|
*/ |
276 |
|
static interface NodeFactory extends Serializable { |
277 |
|
/** |
278 |
< |
* Creates and returns a Node using the given parameters |
278 |
> |
* Creates and returns a Node using the given parameters. |
279 |
> |
* |
280 |
|
* @param locator an opaque immutable locator for this node |
281 |
< |
* @parem key the (nonnull) immutable key |
282 |
< |
* @parem value the (nonnull) volatile value; |
283 |
< |
* @param cchm the table creating this node. |
281 |
> |
* @param key the (non-null) immutable key |
282 |
> |
* @param value the (non-null) volatile value |
283 |
> |
* @param cchm the table creating this node |
284 |
|
* @param linkage an opaque volatile linkage for maintaining this node |
285 |
|
*/ |
286 |
|
Node newNode(int locator, Object key, Object value, |
304 |
|
static interface Node extends Reclaimable { |
305 |
|
/** |
306 |
|
* Returns the key established during the creation of this node. |
307 |
< |
* Note: This method is named "get" rether than "getKey" |
307 |
> |
* Note: This method is named "get" rather than "getKey" |
308 |
|
* to simplify usage of Reference keys. |
309 |
|
* @return the key |
310 |
|
*/ |
320 |
|
* Returns the value established during the creation of this |
321 |
|
* node or, if since updated, the value set by the most |
322 |
|
* recent call to setValue, or throws an exception if |
323 |
< |
* value could not be computed |
323 |
> |
* value could not be computed. |
324 |
|
* @return the value |
325 |
|
* @throws RuntimeException or Error if computeValue failed |
326 |
|
*/ |
333 |
|
void setValue(Object value); |
334 |
|
|
335 |
|
/** |
336 |
< |
* Returns the lonkage established during the creation of this |
336 |
> |
* Returns the linkage established during the creation of this |
337 |
|
* node or, if since updated, the linkage set by the most |
338 |
|
* recent call to setLinkage. |
339 |
|
* @return the linkage |
533 |
|
int sc = (int)((1L + (4L * es) / 3) >>> SEGMENT_BITS); |
534 |
|
if (sc < MIN_SEGMENT_CAPACITY) |
535 |
|
sc = MIN_SEGMENT_CAPACITY; |
536 |
< |
else if (sc > MAX_SEGMENT_CAPACITY) |
537 |
< |
sc = MAX_SEGMENT_CAPACITY; |
538 |
< |
this.initialSegmentCapacity = sc; |
536 |
> |
int capacity = MIN_SEGMENT_CAPACITY; // ensure power of two |
537 |
> |
while (capacity < sc) |
538 |
> |
capacity <<= 1; |
539 |
> |
if (capacity > MAX_SEGMENT_CAPACITY) |
540 |
> |
capacity = MAX_SEGMENT_CAPACITY; |
541 |
> |
this.initialSegmentCapacity = capacity; |
542 |
|
} |
543 |
|
this.segments = (Segment[])new Segment[NSEGMENTS]; |
544 |
|
} |
545 |
|
|
546 |
|
/** |
547 |
< |
* Creates a new CustomConcurrentHashMap with the given parameters |
547 |
> |
* Creates a new CustomConcurrentHashMap with the given parameters. |
548 |
|
* @param keyStrength the strength for keys |
549 |
|
* @param keyEquivalence the Equivalence to use for keys |
550 |
|
* @param valueStrength the strength for values |
573 |
|
|
574 |
|
/** |
575 |
|
* Returns a new map using Integer keys and the given value |
576 |
< |
* parameters |
576 |
> |
* parameters. |
577 |
|
* @param valueStrength the strength for values |
578 |
|
* @param valueEquivalence the Equivalence to use for values |
579 |
|
* @param expectedSize an estimate of the number of elements |
591 |
|
} |
592 |
|
|
593 |
|
/** |
594 |
< |
* Returns a new map using the given key parameters and Integer values |
594 |
> |
* Returns a new map using the given key parameters and Integer values. |
595 |
|
* @param keyStrength the strength for keys |
596 |
|
* @param keyEquivalence the Equivalence to use for keys |
597 |
|
* @param expectedSize an estimate of the number of elements |
609 |
|
} |
610 |
|
|
611 |
|
/** |
612 |
< |
* Returns a new map using Integer keys and values |
612 |
> |
* Returns a new map using Integer keys and values. |
613 |
|
* @param expectedSize an estimate of the number of elements |
614 |
|
* that will be held in the map. If no estimate is known, |
615 |
|
* zero is an acceptable value. |
623 |
|
} |
624 |
|
|
625 |
|
/** |
626 |
< |
* Returns the segment for traversing table for key with given hash |
626 |
> |
* Returns the segment for traversing table for key with given hash. |
627 |
|
* @param hash the hash code for the key |
628 |
|
* @return the segment, or null if not yet initialized |
629 |
|
*/ |
633 |
|
|
634 |
|
/** |
635 |
|
* Returns the segment for possibly inserting into the table |
636 |
< |
* associated with given hash, constructing it if necesary. |
636 |
> |
* associated with given hash, constructing it if necessary. |
637 |
|
* @param hash the hash code for the key |
638 |
|
* @return the segment |
639 |
|
*/ |
642 |
|
int index = (hash >>> SEGMENT_SHIFT) & SEGMENT_MASK; |
643 |
|
Segment seg = segs[index]; |
644 |
|
if (seg == null) { |
645 |
< |
synchronized(segs) { |
645 |
> |
synchronized (segs) { |
646 |
|
seg = segs[index]; |
647 |
|
if (seg == null) { |
648 |
|
seg = new Segment(); |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
< |
* Returns node for key, or null if none |
659 |
> |
* Returns node for key, or null if none. |
660 |
|
*/ |
661 |
|
final Node findNode(Object key, int hash, Segment seg) { |
662 |
|
if (seg != null) { |
678 |
|
} |
679 |
|
|
680 |
|
/** |
681 |
< |
* Returns <tt>true</tt> if this map contains a key equivalent to |
681 |
> |
* Returns {@code true} if this map contains a key equivalent to |
682 |
|
* the given key with respect to this map's key Equivalence. |
683 |
|
* |
684 |
|
* @param key possible key |
685 |
< |
* @return <tt>true</tt> if this map contains the specified key |
685 |
> |
* @return {@code true} if this map contains the specified key |
686 |
|
* @throws NullPointerException if the specified key is null |
687 |
|
*/ |
688 |
|
public boolean containsKey(Object key) { |
697 |
|
/** |
698 |
|
* Returns the value associated with a key equivalent to the given |
699 |
|
* key with respect to this map's key Equivalence, or {@code null} |
700 |
< |
* if no such mapping exists |
700 |
> |
* if no such mapping exists. |
701 |
|
* |
702 |
|
* @param key possible key |
703 |
< |
* @return the value associated with the kew or <tt>null</tt> if |
703 |
> |
* @return the value associated with the key or {@code null} if |
704 |
|
* there is no mapping. |
705 |
|
* @throws NullPointerException if the specified key is null |
706 |
|
*/ |
716 |
|
} |
717 |
|
|
718 |
|
/** |
719 |
< |
* Share dimplementation for put, putIfAbsent |
719 |
> |
* Shared implementation for put, putIfAbsent |
720 |
|
*/ |
721 |
|
final V doPut(K key, V value, boolean onlyIfNull) { |
722 |
|
if (key == null || value == null) |
752 |
|
* |
753 |
|
* @param key key with which the specified value is to be associated |
754 |
|
* @param value value to be associated with the specified key |
755 |
< |
* @return the previous value associated with <tt>key</tt>, or |
756 |
< |
* <tt>null</tt> if there was no mapping for <tt>key</tt> |
755 |
> |
* @return the previous value associated with {@code key}, or |
756 |
> |
* {@code null} if there was no mapping for {@code key} |
757 |
|
* @throws NullPointerException if the specified key or value is null |
758 |
|
*/ |
759 |
|
public V put(K key, V value) { |
764 |
|
* {@inheritDoc} |
765 |
|
* |
766 |
|
* @return the previous value associated with the specified key, |
767 |
< |
* or <tt>null</tt> if there was no mapping for the key |
767 |
> |
* or {@code null} if there was no mapping for the key |
768 |
|
* @throws NullPointerException if the specified key or value is null |
769 |
|
*/ |
770 |
|
public V putIfAbsent(K key, V value) { |
813 |
|
* {@inheritDoc} |
814 |
|
* |
815 |
|
* @return the previous value associated with the specified key, |
816 |
< |
* or <tt>null</tt> if there was no mapping for the key |
816 |
> |
* or {@code null} if there was no mapping for the key |
817 |
|
* @throws NullPointerException if the specified key or value is null |
818 |
|
*/ |
819 |
|
public boolean replace(K key, V oldValue, V newValue) { |
845 |
|
* Removes the mapping for the specified key. |
846 |
|
* |
847 |
|
* @param key the key to remove |
848 |
< |
* @return the previous value associated with <tt>key</tt>, or |
849 |
< |
* <tt>null</tt> if there was no mapping for <tt>key</tt> |
848 |
> |
* @return the previous value associated with {@code key}, or |
849 |
> |
* {@code null} if there was no mapping for {@code key} |
850 |
|
* @throws NullPointerException if the specified key is null |
851 |
|
*/ |
852 |
|
public V remove(Object key) { |
942 |
|
} |
943 |
|
|
944 |
|
/** |
945 |
< |
* Remove node if its key or value are null |
945 |
> |
* Removes node if its key or value are null. |
946 |
|
*/ |
947 |
|
final void removeIfReclaimed(Node r) { |
948 |
|
int hash = r.getLocator(); |
959 |
|
while (p != null) { |
960 |
|
Node n = p.getLinkage(); |
961 |
|
if (p.get() != null && p.getValue() != null) { |
962 |
+ |
pred = p; |
963 |
+ |
p = n; |
964 |
+ |
} |
965 |
+ |
else { |
966 |
|
if (pred == null) |
967 |
|
tab[i] = n; |
968 |
|
else |
970 |
|
seg.decrementCount(); |
971 |
|
p = n; |
972 |
|
} |
965 |
– |
else { |
966 |
– |
pred = p; |
967 |
– |
p = n; |
968 |
– |
} |
973 |
|
} |
974 |
|
} |
975 |
|
} finally { |
979 |
|
} |
980 |
|
|
981 |
|
/** |
982 |
< |
* Returns <tt>true</tt> if this map contains no key-value mappings. |
982 |
> |
* Returns {@code true} if this map contains no key-value mappings. |
983 |
|
* |
984 |
< |
* @return <tt>true</tt> if this map contains no key-value mappings |
984 |
> |
* @return {@code true} if this map contains no key-value mappings |
985 |
|
*/ |
986 |
|
public final boolean isEmpty() { |
987 |
|
final Segment[] segs = this.segments; |
997 |
|
|
998 |
|
/** |
999 |
|
* Returns the number of key-value mappings in this map. If the |
1000 |
< |
* map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
1001 |
< |
* <tt>Integer.MAX_VALUE</tt>. |
1000 |
> |
* map contains more than {@code Integer.MAX_VALUE} elements, returns |
1001 |
> |
* {@code Integer.MAX_VALUE}. |
1002 |
|
* |
1003 |
|
* @return the number of key-value mappings in this map |
1004 |
|
*/ |
1010 |
|
if (seg != null && seg.getTableForTraversal() != null) |
1011 |
|
sum += seg.count; |
1012 |
|
} |
1013 |
< |
return sum >= Integer.MAX_VALUE? Integer.MAX_VALUE : (int)sum; |
1013 |
> |
return (sum >= Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) sum; |
1014 |
|
} |
1015 |
|
|
1016 |
|
/** |
1017 |
< |
* Returns <tt>true</tt> if this map maps one or more keys to a |
1017 |
> |
* Returns {@code true} if this map maps one or more keys to a |
1018 |
|
* value equivalent to the given value with respect to this map's |
1019 |
|
* value Equivalence. Note: This method requires a full internal |
1020 |
|
* traversal of the hash table, and so is much slower than method |
1021 |
< |
* <tt>containsKey</tt>. |
1021 |
> |
* {@code containsKey}. |
1022 |
|
* |
1023 |
|
* @param value value whose presence in this map is to be tested |
1024 |
< |
* @return <tt>true</tt> if this map maps one or more keys to the |
1024 |
> |
* @return {@code true} if this map maps one or more keys to the |
1025 |
|
* specified value |
1026 |
|
* @throws NullPointerException if the specified value is null |
1027 |
|
*/ |
1069 |
|
/** |
1070 |
|
* If the specified key is not already associated with a value, |
1071 |
|
* computes its value using the given mappingFunction, and if |
1072 |
< |
* nonnull, enters it into the map. This is equivalent to |
1072 |
> |
* non-null, enters it into the map. This is equivalent to |
1073 |
|
* |
1074 |
|
* <pre> |
1075 |
|
* if (map.containsKey(key)) |
1082 |
|
* </pre> |
1083 |
|
* |
1084 |
|
* except that the action is performed atomically. Some |
1085 |
< |
* attemmpted operations on this map by other threads may be |
1085 |
> |
* attempted operations on this map by other threads may be |
1086 |
|
* blocked while computation is in progress. Because this function |
1087 |
|
* is invoked within atomicity control, the computation should be |
1088 |
|
* short and simple. The most common usage is to construct a new |
1091 |
|
* @param key key with which the specified value is to be associated |
1092 |
|
* @param mappingFunction the function to compute a value |
1093 |
|
* @return the current (existing or computed) value associated with |
1094 |
< |
* the specified key, or <tt>null</tt> if the computation |
1095 |
< |
* returned <tt>null</tt>. |
1094 |
> |
* the specified key, or {@code null} if the computation |
1095 |
> |
* returned {@code null}. |
1096 |
|
* @throws NullPointerException if the specified key or mappingFunction |
1097 |
|
* is null, |
1098 |
|
* @throws RuntimeException or Error if the mappingFunction does so, |
1149 |
|
* return remove(key); |
1150 |
|
* </pre> |
1151 |
|
* |
1152 |
< |
* except that the action is performed atomically. Some attemmpted |
1152 |
> |
* except that the action is performed atomically. Some attempted |
1153 |
|
* operations on this map by other threads may be blocked while |
1154 |
|
* computation is in progress. |
1155 |
|
* |
1158 |
|
* <pre> |
1159 |
|
* map.compute(word, new RemappingFunction<String,Integer>() { |
1160 |
|
* public Integer remap(String k, Integer v) { |
1161 |
< |
* return v == null? 1 : v + 1; |
1161 |
> |
* return (v == null) ? 1 : v + 1; |
1162 |
|
* }}); |
1163 |
|
* </pre> |
1164 |
|
* |
1165 |
|
* @param key key with which the specified value is to be associated |
1166 |
|
* @param remappingFunction the function to compute a value |
1167 |
|
* @return the updated value or |
1168 |
< |
* <tt>null</tt> if the computation returned <tt>null</tt> |
1168 |
> |
* {@code null} if the computation returned {@code null} |
1169 |
|
* @throws NullPointerException if the specified key or remappingFunction |
1170 |
|
* is null, |
1171 |
|
* @throws RuntimeException or Error if the remappingFunction does so, |
1414 |
|
if (!(o instanceof Map.Entry)) |
1415 |
|
return false; |
1416 |
|
Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
1417 |
< |
return CustomConcurrentHashMap.this.remove(e.getKey(), e.getValue()); |
1417 |
> |
return CustomConcurrentHashMap.this.remove(e.getKey(), |
1418 |
> |
e.getValue()); |
1419 |
|
} |
1420 |
|
public int size() { |
1421 |
|
return CustomConcurrentHashMap.this.size(); |
1433 |
|
* The set is backed by the map, so changes to the map are |
1434 |
|
* reflected in the set, and vice-versa. The set supports element |
1435 |
|
* removal, which removes the corresponding mapping from this map, |
1436 |
< |
* via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
1437 |
< |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> |
1438 |
< |
* operations. It does not support the <tt>add</tt> or |
1439 |
< |
* <tt>addAll</tt> operations. |
1436 |
> |
* via the {@code Iterator.remove}, {@code Set.remove}, |
1437 |
> |
* {@code removeAll}, {@code retainAll}, and {@code clear} |
1438 |
> |
* operations. It does not support the {@code add} or |
1439 |
> |
* {@code addAll} operations. |
1440 |
|
* |
1441 |
< |
* <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator |
1441 |
> |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
1442 |
|
* that will never throw {@link ConcurrentModificationException}, |
1443 |
|
* and guarantees to traverse elements as they existed upon |
1444 |
|
* construction of the iterator, and may (but is not guaranteed to) |
1454 |
|
* The collection is backed by the map, so changes to the map are |
1455 |
|
* reflected in the collection, and vice-versa. The collection |
1456 |
|
* supports element removal, which removes the corresponding |
1457 |
< |
* mapping from this map, via the <tt>Iterator.remove</tt>, |
1458 |
< |
* <tt>Collection.remove</tt>, <tt>removeAll</tt>, |
1459 |
< |
* <tt>retainAll</tt>, and <tt>clear</tt> operations. It does not |
1460 |
< |
* support the <tt>add</tt> or <tt>addAll</tt> operations. |
1457 |
> |
* mapping from this map, via the {@code Iterator.remove}, |
1458 |
> |
* {@code Collection.remove}, {@code removeAll}, |
1459 |
> |
* {@code retainAll}, and {@code clear} operations. It does not |
1460 |
> |
* support the {@code add} or {@code addAll} operations. |
1461 |
|
* |
1462 |
< |
* <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator |
1462 |
> |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
1463 |
|
* that will never throw {@link ConcurrentModificationException}, |
1464 |
|
* and guarantees to traverse elements as they existed upon |
1465 |
|
* construction of the iterator, and may (but is not guaranteed to) |
1475 |
|
* The set is backed by the map, so changes to the map are |
1476 |
|
* reflected in the set, and vice-versa. The set supports element |
1477 |
|
* removal, which removes the corresponding mapping from the map, |
1478 |
< |
* via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
1479 |
< |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> |
1480 |
< |
* operations. It does not support the <tt>add</tt> or |
1481 |
< |
* <tt>addAll</tt> operations. |
1478 |
> |
* via the {@code Iterator.remove}, {@code Set.remove}, |
1479 |
> |
* {@code removeAll}, {@code retainAll}, and {@code clear} |
1480 |
> |
* operations. It does not support the {@code add} or |
1481 |
> |
* {@code addAll} operations. |
1482 |
|
* |
1483 |
< |
* <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator |
1483 |
> |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
1484 |
|
* that will never throw {@link ConcurrentModificationException}, |
1485 |
|
* and guarantees to traverse elements as they existed upon |
1486 |
|
* construction of the iterator, and may (but is not guaranteed to) |
1495 |
|
|
1496 |
|
/** |
1497 |
|
* Compares the specified object with this map for equality. |
1498 |
< |
* Returns <tt>true</tt> if the given object is also a map of the |
1498 |
> |
* Returns {@code true} if the given object is also a map of the |
1499 |
|
* same size, holding keys that are equal using this Map's key |
1500 |
|
* Equivalence, and which map to values that are equal according |
1501 |
|
* to this Map's value equivalence. |
1502 |
|
* |
1503 |
|
* @param o object to be compared for equality with this map |
1504 |
< |
* @return <tt>true</tt> if the specified object is equal to this map |
1504 |
> |
* @return {@code true} if the specified object is equal to this map |
1505 |
|
*/ |
1506 |
|
public boolean equals(Object o) { |
1507 |
|
if (o == this) |
1538 |
|
|
1539 |
|
/** |
1540 |
|
* Returns the sum of the hash codes of each entry in this map's |
1541 |
< |
* <tt>entrySet()</tt> view, which in turn are the hash codes |
1541 |
> |
* {@code entrySet()} view, which in turn are the hash codes |
1542 |
|
* computed using key and value Equivalences for this Map. |
1543 |
|
* @return the hash code |
1544 |
|
*/ |
1551 |
|
} |
1552 |
|
|
1553 |
|
/** |
1554 |
< |
* Save the state of the instance to a stream (i.e., serialize |
1555 |
< |
* it). |
1554 |
> |
* Saves the state of the instance to a stream (i.e., serializes it). |
1555 |
> |
* |
1556 |
|
* @param s the stream |
1557 |
|
* @serialData |
1558 |
|
* the key (Object) and value (Object) |
1559 |
|
* for each key-value mapping, followed by a null pair. |
1560 |
|
* The key-value mappings are emitted in no particular order. |
1561 |
|
*/ |
1562 |
< |
private void writeObject(java.io.ObjectOutputStream s) throws IOException { |
1562 |
> |
private void writeObject(java.io.ObjectOutputStream s) throws IOException { |
1563 |
|
s.defaultWriteObject(); |
1564 |
|
for (Map.Entry<K,V> e : entrySet()) { |
1565 |
|
s.writeObject(e.getKey()); |
1570 |
|
} |
1571 |
|
|
1572 |
|
/** |
1573 |
< |
* Reconstitute the instance from a stream (i.e., deserialize it). |
1573 |
> |
* Reconstitutes the instance from a stream (that is, deserializes it). |
1574 |
|
* @param s the stream |
1575 |
|
*/ |
1576 |
|
private void readObject(java.io.ObjectInputStream s) |
1577 |
< |
throws IOException, ClassNotFoundException { |
1577 |
> |
throws IOException, ClassNotFoundException { |
1578 |
|
s.defaultReadObject(); |
1579 |
|
this.segments = (Segment[])(new Segment[NSEGMENTS]); |
1580 |
|
for (;;) { |
1588 |
|
|
1589 |
|
/** |
1590 |
|
* A hash-based set with properties identical to those of |
1591 |
< |
* <code>Collections.newSetFromMap</code> applied to a |
1592 |
< |
* <code>CustomConcurrentHashMap</code>, but possibly more |
1591 |
> |
* {@code Collections.newSetFromMap} applied to a |
1592 |
> |
* {@code CustomConcurrentHashMap}, but possibly more |
1593 |
|
* space-efficient. The set does not permit null elements. The |
1594 |
|
* set is serializable; however, serializing a set that uses soft |
1595 |
|
* or weak references can give unpredictable results. |
1600 |
|
final CustomConcurrentHashMap<K,K> cchm; |
1601 |
|
|
1602 |
|
/** |
1603 |
< |
* Creates a set with the given parameters |
1603 |
> |
* Creates a set with the given parameters. |
1604 |
|
* @param strength the strength of elements |
1605 |
|
* @param equivalence the Equivalence to use |
1606 |
|
* @param expectedSize an estimate of the number of elements |
1621 |
|
* exists, else adds and returns the given element. |
1622 |
|
* |
1623 |
|
* @param e the element |
1624 |
< |
* @return e, or an element equivalent to e. |
1624 |
> |
* @return e, or an element equivalent to e |
1625 |
|
*/ |
1626 |
|
public K intern(K e) { |
1627 |
|
K oldElement = cchm.doPut(e, e, true); |
1629 |
|
} |
1630 |
|
|
1631 |
|
/** |
1632 |
< |
* Returns <tt>true</tt> if this set contains an |
1632 |
> |
* Returns {@code true} if this set contains an |
1633 |
|
* element equivalent to the given element with respect |
1634 |
|
* to this set's Equivalence. |
1635 |
|
* @param o element whose presence in this set is to be tested |
1636 |
< |
* @return <tt>true</tt> if this set contains the specified element |
1636 |
> |
* @return {@code true} if this set contains the specified element |
1637 |
|
*/ |
1638 |
|
public boolean contains(Object o) { |
1639 |
|
return cchm.containsKey(o); |
1656 |
|
* respect to this set's Equivalence. |
1657 |
|
* |
1658 |
|
* @param e element to be added to this set |
1659 |
< |
* @return <tt>true</tt> if this set did not already contain |
1659 |
> |
* @return {@code true} if this set did not already contain |
1660 |
|
* the specified element |
1661 |
|
*/ |
1662 |
|
public boolean add(K e) { |
1668 |
|
* respect to this set's Equivalence, if one is present. |
1669 |
|
* |
1670 |
|
* @param o object to be removed from this set, if present |
1671 |
< |
* @return <tt>true</tt> if the set contained the specified element |
1671 |
> |
* @return {@code true} if the set contained the specified element |
1672 |
|
*/ |
1673 |
|
public boolean remove(Object o) { |
1674 |
|
return cchm.remove(o) != null; |
1675 |
|
} |
1676 |
|
|
1677 |
|
/** |
1678 |
< |
* Returns <tt>true</tt> if this set contains no elements. |
1678 |
> |
* Returns {@code true} if this set contains no elements. |
1679 |
|
* |
1680 |
< |
* @return <tt>true</tt> if this set contains no elements |
1680 |
> |
* @return {@code true} if this set contains no elements |
1681 |
|
*/ |
1682 |
|
public boolean isEmpty() { |
1683 |
|
return cchm.isEmpty(); |
1793 |
|
|
1794 |
|
// Strong Keys |
1795 |
|
|
1796 |
< |
static abstract class StrongKeyNode implements Node { |
1796 |
> |
abstract static class StrongKeyNode implements Node { |
1797 |
|
final Object key; |
1798 |
|
final int locator; |
1799 |
|
StrongKeyNode(int locator, Object key) { |
1805 |
|
} |
1806 |
|
|
1807 |
|
|
1808 |
< |
static abstract class StrongKeySelfValueNode |
1808 |
> |
abstract static class StrongKeySelfValueNode |
1809 |
|
extends StrongKeyNode { |
1810 |
|
StrongKeySelfValueNode(int locator, Object key) { |
1811 |
|
super(locator, key); |
1852 |
|
} |
1853 |
|
} |
1854 |
|
|
1855 |
< |
static abstract class StrongKeyStrongValueNode |
1855 |
> |
abstract static class StrongKeyStrongValueNode |
1856 |
|
extends StrongKeyNode { |
1857 |
|
volatile Object value; |
1858 |
|
StrongKeyStrongValueNode(int locator, Object key, Object value) { |
1905 |
|
|
1906 |
|
// ... |
1907 |
|
|
1908 |
< |
static abstract class StrongKeyIntValueNode |
1908 |
> |
abstract static class StrongKeyIntValueNode |
1909 |
|
extends StrongKeyNode { |
1910 |
|
volatile int value; |
1911 |
|
StrongKeyIntValueNode(int locator, Object key, Object value) { |
1960 |
|
|
1961 |
|
// ... |
1962 |
|
|
1963 |
< |
static abstract class StrongKeyWeakValueNode |
1963 |
> |
abstract static class StrongKeyWeakValueNode |
1964 |
|
extends StrongKeyNode { |
1965 |
|
volatile EmbeddedWeakReference valueRef; |
1966 |
|
final CustomConcurrentHashMap cchm; |
1976 |
|
} |
1977 |
|
public final Object getValue() { |
1978 |
|
EmbeddedWeakReference vr = valueRef; |
1979 |
< |
return vr == null? null : vr.get(); |
1979 |
> |
return (vr == null) ? null : vr.get(); |
1980 |
|
} |
1981 |
|
public final void setValue(Object value) { |
1982 |
|
if (value == null) |
2028 |
|
} |
2029 |
|
|
2030 |
|
|
2031 |
< |
static abstract class StrongKeySoftValueNode |
2031 |
> |
abstract static class StrongKeySoftValueNode |
2032 |
|
extends StrongKeyNode { |
2033 |
|
volatile EmbeddedSoftReference valueRef; |
2034 |
|
final CustomConcurrentHashMap cchm; |
2044 |
|
} |
2045 |
|
public final Object getValue() { |
2046 |
|
EmbeddedSoftReference vr = valueRef; |
2047 |
< |
return vr == null? null : vr.get(); |
2047 |
> |
return (vr == null) ? null : vr.get(); |
2048 |
|
} |
2049 |
|
public final void setValue(Object value) { |
2050 |
|
if (value == null) |
2097 |
|
|
2098 |
|
// Weak keys |
2099 |
|
|
2100 |
< |
static abstract class WeakKeyNode extends WeakReference |
2100 |
> |
abstract static class WeakKeyNode extends WeakReference |
2101 |
|
implements Node { |
2102 |
|
final int locator; |
2103 |
|
final CustomConcurrentHashMap cchm; |
2104 |
|
WeakKeyNode(int locator, Object key, CustomConcurrentHashMap cchm) { |
2105 |
< |
super(key); |
2105 |
> |
super(key, getReclamationQueue()); |
2106 |
|
this.locator = locator; |
2107 |
|
this.cchm = cchm; |
2108 |
|
} |
2113 |
|
} |
2114 |
|
} |
2115 |
|
|
2116 |
< |
static abstract class WeakKeySelfValueNode |
2116 |
> |
abstract static class WeakKeySelfValueNode |
2117 |
|
extends WeakKeyNode { |
2118 |
< |
WeakKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm) { |
2118 |
> |
WeakKeySelfValueNode(int locator, Object key, |
2119 |
> |
CustomConcurrentHashMap cchm) { |
2120 |
|
super(locator, key, cchm); |
2121 |
|
} |
2122 |
|
public final Object getValue() { return get(); } |
2125 |
|
|
2126 |
|
static final class TerminalWeakKeySelfValueNode |
2127 |
|
extends WeakKeySelfValueNode { |
2128 |
< |
TerminalWeakKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm) { |
2128 |
> |
TerminalWeakKeySelfValueNode(int locator, Object key, |
2129 |
> |
CustomConcurrentHashMap cchm) { |
2130 |
|
super(locator, key, cchm); |
2131 |
|
} |
2132 |
|
public final Node getLinkage() { return null; } |
2136 |
|
static final class LinkedWeakKeySelfValueNode |
2137 |
|
extends WeakKeySelfValueNode { |
2138 |
|
volatile Node linkage; |
2139 |
< |
LinkedWeakKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm, |
2139 |
> |
LinkedWeakKeySelfValueNode(int locator, Object key, |
2140 |
> |
CustomConcurrentHashMap cchm, |
2141 |
|
Node linkage) { |
2142 |
|
super(locator, key, cchm); |
2143 |
|
this.linkage = linkage; |
2163 |
|
} |
2164 |
|
|
2165 |
|
|
2166 |
< |
static abstract class WeakKeyStrongValueNode |
2166 |
> |
abstract static class WeakKeyStrongValueNode |
2167 |
|
extends WeakKeyNode { |
2168 |
|
volatile Object value; |
2169 |
< |
WeakKeyStrongValueNode(int locator, Object key, Object value, CustomConcurrentHashMap cchm) { |
2169 |
> |
WeakKeyStrongValueNode(int locator, Object key, Object value, |
2170 |
> |
CustomConcurrentHashMap cchm) { |
2171 |
|
super(locator, key, cchm); |
2172 |
|
this.value = value; |
2173 |
|
} |
2178 |
|
static final class TerminalWeakKeyStrongValueNode |
2179 |
|
extends WeakKeyStrongValueNode { |
2180 |
|
TerminalWeakKeyStrongValueNode(int locator, |
2181 |
< |
Object key, Object value, CustomConcurrentHashMap cchm) { |
2181 |
> |
Object key, Object value, |
2182 |
> |
CustomConcurrentHashMap cchm) { |
2183 |
|
super(locator, key, value, cchm); |
2184 |
|
} |
2185 |
|
public final Node getLinkage() { return null; } |
2190 |
|
extends WeakKeyStrongValueNode { |
2191 |
|
volatile Node linkage; |
2192 |
|
LinkedWeakKeyStrongValueNode(int locator, |
2193 |
< |
Object key, Object value, CustomConcurrentHashMap cchm, |
2193 |
> |
Object key, Object value, |
2194 |
> |
CustomConcurrentHashMap cchm, |
2195 |
|
Node linkage) { |
2196 |
|
super(locator, key, value, cchm); |
2197 |
|
this.linkage = linkage; |
2216 |
|
} |
2217 |
|
} |
2218 |
|
|
2219 |
< |
static abstract class WeakKeyIntValueNode |
2219 |
> |
abstract static class WeakKeyIntValueNode |
2220 |
|
extends WeakKeyNode { |
2221 |
|
volatile int value; |
2222 |
|
WeakKeyIntValueNode(int locator, Object key, Object value, |
2271 |
|
} |
2272 |
|
} |
2273 |
|
|
2274 |
< |
static abstract class WeakKeyWeakValueNode |
2274 |
> |
abstract static class WeakKeyWeakValueNode |
2275 |
|
extends WeakKeyNode { |
2276 |
|
volatile EmbeddedWeakReference valueRef; |
2277 |
|
WeakKeyWeakValueNode(int locator, Object key, Object value, |
2282 |
|
} |
2283 |
|
public final Object getValue() { |
2284 |
|
EmbeddedWeakReference vr = valueRef; |
2285 |
< |
return vr == null? null : vr.get(); |
2285 |
> |
return (vr == null) ? null : vr.get(); |
2286 |
|
} |
2287 |
|
public final void setValue(Object value) { |
2288 |
|
if (value == null) |
2334 |
|
} |
2335 |
|
|
2336 |
|
|
2337 |
< |
static abstract class WeakKeySoftValueNode |
2337 |
> |
abstract static class WeakKeySoftValueNode |
2338 |
|
extends WeakKeyNode { |
2339 |
|
volatile EmbeddedSoftReference valueRef; |
2340 |
|
WeakKeySoftValueNode(int locator, Object key, Object value, |
2345 |
|
} |
2346 |
|
public final Object getValue() { |
2347 |
|
EmbeddedSoftReference vr = valueRef; |
2348 |
< |
return vr == null? null : vr.get(); |
2348 |
> |
return (vr == null) ? null : vr.get(); |
2349 |
|
} |
2350 |
|
public final void setValue(Object value) { |
2351 |
|
if (value == null) |
2398 |
|
|
2399 |
|
// Soft keys |
2400 |
|
|
2401 |
< |
static abstract class SoftKeyNode extends SoftReference |
2401 |
> |
abstract static class SoftKeyNode extends SoftReference |
2402 |
|
implements Node { |
2403 |
|
final int locator; |
2404 |
|
final CustomConcurrentHashMap cchm; |
2405 |
|
SoftKeyNode(int locator, Object key, CustomConcurrentHashMap cchm) { |
2406 |
< |
super(key); |
2406 |
> |
super(key, getReclamationQueue()); |
2407 |
|
this.locator = locator; |
2408 |
|
this.cchm = cchm; |
2409 |
|
} |
2414 |
|
} |
2415 |
|
} |
2416 |
|
|
2417 |
< |
static abstract class SoftKeySelfValueNode |
2417 |
> |
abstract static class SoftKeySelfValueNode |
2418 |
|
extends SoftKeyNode { |
2419 |
< |
SoftKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm) { |
2419 |
> |
SoftKeySelfValueNode(int locator, Object key, |
2420 |
> |
CustomConcurrentHashMap cchm) { |
2421 |
|
super(locator, key, cchm); |
2422 |
|
} |
2423 |
|
public final Object getValue() { return get(); } |
2426 |
|
|
2427 |
|
static final class TerminalSoftKeySelfValueNode |
2428 |
|
extends SoftKeySelfValueNode { |
2429 |
< |
TerminalSoftKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm) { |
2429 |
> |
TerminalSoftKeySelfValueNode(int locator, Object key, |
2430 |
> |
CustomConcurrentHashMap cchm) { |
2431 |
|
super(locator, key, cchm); |
2432 |
|
} |
2433 |
|
public final Node getLinkage() { return null; } |
2437 |
|
static final class LinkedSoftKeySelfValueNode |
2438 |
|
extends SoftKeySelfValueNode { |
2439 |
|
volatile Node linkage; |
2440 |
< |
LinkedSoftKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm, |
2440 |
> |
LinkedSoftKeySelfValueNode(int locator, Object key, |
2441 |
> |
CustomConcurrentHashMap cchm, |
2442 |
|
Node linkage) { |
2443 |
|
super(locator, key, cchm); |
2444 |
|
this.linkage = linkage; |
2464 |
|
} |
2465 |
|
|
2466 |
|
|
2467 |
< |
static abstract class SoftKeyStrongValueNode |
2467 |
> |
abstract static class SoftKeyStrongValueNode |
2468 |
|
extends SoftKeyNode { |
2469 |
|
volatile Object value; |
2470 |
< |
SoftKeyStrongValueNode(int locator, Object key, Object value, CustomConcurrentHashMap cchm) { |
2470 |
> |
SoftKeyStrongValueNode(int locator, Object key, Object value, |
2471 |
> |
CustomConcurrentHashMap cchm) { |
2472 |
|
super(locator, key, cchm); |
2473 |
|
this.value = value; |
2474 |
|
} |
2479 |
|
static final class TerminalSoftKeyStrongValueNode |
2480 |
|
extends SoftKeyStrongValueNode { |
2481 |
|
TerminalSoftKeyStrongValueNode(int locator, |
2482 |
< |
Object key, Object value, CustomConcurrentHashMap cchm) { |
2482 |
> |
Object key, Object value, |
2483 |
> |
CustomConcurrentHashMap cchm) { |
2484 |
|
super(locator, key, value, cchm); |
2485 |
|
} |
2486 |
|
public final Node getLinkage() { return null; } |
2491 |
|
extends SoftKeyStrongValueNode { |
2492 |
|
volatile Node linkage; |
2493 |
|
LinkedSoftKeyStrongValueNode(int locator, |
2494 |
< |
Object key, Object value, CustomConcurrentHashMap cchm, |
2494 |
> |
Object key, Object value, |
2495 |
> |
CustomConcurrentHashMap cchm, |
2496 |
|
Node linkage) { |
2497 |
|
super(locator, key, value, cchm); |
2498 |
|
this.linkage = linkage; |
2517 |
|
} |
2518 |
|
} |
2519 |
|
|
2520 |
< |
static abstract class SoftKeyIntValueNode |
2520 |
> |
abstract static class SoftKeyIntValueNode |
2521 |
|
extends SoftKeyNode { |
2522 |
|
volatile int value; |
2523 |
|
SoftKeyIntValueNode(int locator, Object key, Object value, |
2572 |
|
} |
2573 |
|
} |
2574 |
|
|
2575 |
< |
static abstract class SoftKeyWeakValueNode |
2575 |
> |
abstract static class SoftKeyWeakValueNode |
2576 |
|
extends SoftKeyNode { |
2577 |
|
volatile EmbeddedWeakReference valueRef; |
2578 |
|
SoftKeyWeakValueNode(int locator, Object key, Object value, |
2583 |
|
} |
2584 |
|
public final Object getValue() { |
2585 |
|
EmbeddedWeakReference vr = valueRef; |
2586 |
< |
return vr == null? null : vr.get(); |
2586 |
> |
return (vr == null) ? null : vr.get(); |
2587 |
|
} |
2588 |
|
public final void setValue(Object value) { |
2589 |
|
if (value == null) |
2635 |
|
} |
2636 |
|
|
2637 |
|
|
2638 |
< |
static abstract class SoftKeySoftValueNode |
2638 |
> |
abstract static class SoftKeySoftValueNode |
2639 |
|
extends SoftKeyNode { |
2640 |
|
volatile EmbeddedSoftReference valueRef; |
2641 |
|
SoftKeySoftValueNode(int locator, Object key, Object value, |
2646 |
|
} |
2647 |
|
public final Object getValue() { |
2648 |
|
EmbeddedSoftReference vr = valueRef; |
2649 |
< |
return vr == null? null : vr.get(); |
2649 |
> |
return (vr == null) ? null : vr.get(); |
2650 |
|
} |
2651 |
|
public final void setValue(Object value) { |
2652 |
|
if (value == null) |
2697 |
|
} |
2698 |
|
} |
2699 |
|
|
2700 |
< |
static abstract class IntKeyNode implements Node { |
2700 |
> |
abstract static class IntKeyNode implements Node { |
2701 |
|
final int key; |
2702 |
|
IntKeyNode(int locator, Object key) { |
2703 |
|
this.key = ((Integer)key).intValue(); |
2707 |
|
} |
2708 |
|
|
2709 |
|
|
2710 |
< |
static abstract class IntKeySelfValueNode |
2710 |
> |
abstract static class IntKeySelfValueNode |
2711 |
|
extends IntKeyNode { |
2712 |
|
IntKeySelfValueNode(int locator, Object key) { |
2713 |
|
super(locator, key); |
2754 |
|
} |
2755 |
|
} |
2756 |
|
|
2757 |
< |
static abstract class IntKeyStrongValueNode |
2757 |
> |
abstract static class IntKeyStrongValueNode |
2758 |
|
extends IntKeyNode { |
2759 |
|
volatile Object value; |
2760 |
|
IntKeyStrongValueNode(int locator, Object key, Object value) { |
2805 |
|
} |
2806 |
|
} |
2807 |
|
|
2808 |
< |
static abstract class IntKeyIntValueNode |
2808 |
> |
abstract static class IntKeyIntValueNode |
2809 |
|
extends IntKeyNode { |
2810 |
|
volatile int value; |
2811 |
|
IntKeyIntValueNode(int locator, Object key, Object value) { |
2858 |
|
} |
2859 |
|
} |
2860 |
|
|
2861 |
< |
static abstract class IntKeyWeakValueNode |
2861 |
> |
abstract static class IntKeyWeakValueNode |
2862 |
|
extends IntKeyNode { |
2863 |
|
volatile EmbeddedWeakReference valueRef; |
2864 |
|
final CustomConcurrentHashMap cchm; |
2874 |
|
} |
2875 |
|
public final Object getValue() { |
2876 |
|
EmbeddedWeakReference vr = valueRef; |
2877 |
< |
return vr == null? null : vr.get(); |
2877 |
> |
return (vr == null) ? null : vr.get(); |
2878 |
|
} |
2879 |
|
public final void setValue(Object value) { |
2880 |
|
if (value == null) |
2926 |
|
} |
2927 |
|
|
2928 |
|
|
2929 |
< |
static abstract class IntKeySoftValueNode |
2929 |
> |
abstract static class IntKeySoftValueNode |
2930 |
|
extends IntKeyNode { |
2931 |
|
volatile EmbeddedSoftReference valueRef; |
2932 |
|
final CustomConcurrentHashMap cchm; |
2933 |
|
IntKeySoftValueNode(int locator, Object key, Object value, |
2934 |
< |
CustomConcurrentHashMap cchm) { |
2934 |
> |
CustomConcurrentHashMap cchm) { |
2935 |
|
super(locator, key); |
2936 |
|
this.cchm = cchm; |
2937 |
|
if (value != null) |
2942 |
|
} |
2943 |
|
public final Object getValue() { |
2944 |
|
EmbeddedSoftReference vr = valueRef; |
2945 |
< |
return vr == null? null : vr.get(); |
2945 |
> |
return (vr == null) ? null : vr.get(); |
2946 |
|
} |
2947 |
|
public final void setValue(Object value) { |
2948 |
|
if (value == null) |
2997 |
|
|
2998 |
|
// Temporary Unsafe mechanics for preliminary release |
2999 |
|
|
3000 |
< |
static final Unsafe _unsafe; |
3000 |
> |
static final Unsafe UNSAFE; |
3001 |
|
static final long tableBase; |
3002 |
|
static final int tableShift; |
3003 |
|
static final long segmentsBase; |
3004 |
|
static final int segmentsShift; |
3005 |
|
|
2989 |
– |
private static Unsafe getUnsafe() throws Throwable { |
2990 |
– |
try { |
2991 |
– |
return Unsafe.getUnsafe(); |
2992 |
– |
} catch (SecurityException se) { |
2993 |
– |
try { |
2994 |
– |
return java.security.AccessController.doPrivileged |
2995 |
– |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
2996 |
– |
public Unsafe run() throws Exception { |
2997 |
– |
return getUnsafePrivileged(); |
2998 |
– |
}}); |
2999 |
– |
} catch (java.security.PrivilegedActionException e) { |
3000 |
– |
throw e.getCause(); |
3001 |
– |
} |
3002 |
– |
} |
3003 |
– |
} |
3004 |
– |
|
3005 |
– |
private static Unsafe getUnsafePrivileged() |
3006 |
– |
throws NoSuchFieldException, IllegalAccessException { |
3007 |
– |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
3008 |
– |
f.setAccessible(true); |
3009 |
– |
return (Unsafe) f.get(null); |
3010 |
– |
} |
3011 |
– |
|
3006 |
|
static { |
3007 |
|
try { |
3008 |
< |
_unsafe = getUnsafe(); |
3009 |
< |
tableBase = _unsafe.arrayBaseOffset(Node[].class); |
3010 |
< |
int s = _unsafe.arrayIndexScale(Node[].class); |
3011 |
< |
if ((s & (s-1)) != 0) |
3008 |
> |
UNSAFE = getUnsafe(); |
3009 |
> |
tableBase = UNSAFE.arrayBaseOffset(Node[].class); |
3010 |
> |
int scale = UNSAFE.arrayIndexScale(Node[].class); |
3011 |
> |
if ((scale & (scale - 1)) != 0) |
3012 |
|
throw new Error("data type scale not a power of two"); |
3013 |
< |
tableShift = 31 - Integer.numberOfLeadingZeros(s); |
3014 |
< |
segmentsBase = _unsafe.arrayBaseOffset(Segment[].class); |
3015 |
< |
s = _unsafe.arrayIndexScale(Segment[].class); |
3016 |
< |
if ((s & (s-1)) != 0) |
3013 |
> |
tableShift = 31 - Integer.numberOfLeadingZeros(scale); |
3014 |
> |
segmentsBase = UNSAFE.arrayBaseOffset(Segment[].class); |
3015 |
> |
scale = UNSAFE.arrayIndexScale(Segment[].class); |
3016 |
> |
if ((scale & (scale - 1)) != 0) |
3017 |
|
throw new Error("data type scale not a power of two"); |
3018 |
< |
segmentsShift = 31 - Integer.numberOfLeadingZeros(s); |
3018 |
> |
segmentsShift = 31 - Integer.numberOfLeadingZeros(scale); |
3019 |
|
} catch (Throwable e) { |
3020 |
|
throw new RuntimeException("Could not initialize intrinsics", e); |
3021 |
|
} |
3022 |
|
} |
3023 |
|
|
3024 |
|
// Fenced store into segment table array. Unneeded when we have Fences |
3025 |
< |
static final void storeNode(Node[] table, |
3026 |
< |
int i, Node r) { |
3027 |
< |
_unsafe.putOrderedObject(table, (i << tableShift) + tableBase, r); |
3025 |
> |
static final void storeNode(Node[] table, |
3026 |
> |
int i, Node r) { |
3027 |
> |
long nodeOffset = ((long) i << tableShift) + tableBase; |
3028 |
> |
UNSAFE.putOrderedObject(table, nodeOffset, r); |
3029 |
|
} |
3030 |
|
|
3031 |
< |
static final void storeSegment(Segment[] segs, |
3032 |
< |
int i, Segment s) { |
3033 |
< |
_unsafe.putOrderedObject(segs, (i << segmentsShift) + segmentsBase, s); |
3031 |
> |
static final void storeSegment(Segment[] segs, |
3032 |
> |
int i, Segment s) { |
3033 |
> |
long segmentOffset = ((long) i << segmentsShift) + segmentsBase; |
3034 |
> |
UNSAFE.putOrderedObject(segs, segmentOffset, s); |
3035 |
|
} |
3036 |
|
|
3037 |
< |
|
3037 |
> |
/** |
3038 |
> |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
3039 |
> |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
3040 |
> |
* into a jdk. |
3041 |
> |
* |
3042 |
> |
* @return a sun.misc.Unsafe |
3043 |
> |
*/ |
3044 |
> |
private static sun.misc.Unsafe getUnsafe() { |
3045 |
> |
try { |
3046 |
> |
return sun.misc.Unsafe.getUnsafe(); |
3047 |
> |
} catch (SecurityException tryReflectionInstead) {} |
3048 |
> |
try { |
3049 |
> |
return java.security.AccessController.doPrivileged |
3050 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
3051 |
> |
public sun.misc.Unsafe run() throws Exception { |
3052 |
> |
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; |
3053 |
> |
for (java.lang.reflect.Field f : k.getDeclaredFields()) { |
3054 |
> |
f.setAccessible(true); |
3055 |
> |
Object x = f.get(null); |
3056 |
> |
if (k.isInstance(x)) |
3057 |
> |
return k.cast(x); |
3058 |
> |
} |
3059 |
> |
throw new NoSuchFieldError("the Unsafe"); |
3060 |
> |
}}); |
3061 |
> |
} catch (java.security.PrivilegedActionException e) { |
3062 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
3063 |
> |
e.getCause()); |
3064 |
> |
} |
3065 |
> |
} |
3066 |
|
} |