MCPcopy
hub / github.com/google/guava / replace

Method replace

guava/src/com/google/common/cache/LocalCache.java:2938–2997  ·  view source on GitHub ↗
(K key, int hash, V oldValue, V newValue)

Source from the content-addressed store, hash-verified

2936 }
2937
2938 boolean replace(K key, int hash, V oldValue, V newValue) {
2939 lock();
2940 try {
2941 long now = map.ticker.read();
2942 preWriteCleanup(now);
2943
2944 AtomicReferenceArray<ReferenceEntry<K, V>> table = this.table;
2945 int index = hash & (table.length() - 1);
2946 ReferenceEntry<K, V> first = table.get(index);
2947
2948 for (ReferenceEntry<K, V> e = first; e != null; e = e.getNext()) {
2949 K entryKey = e.getKey();
2950 if (e.getHash() == hash
2951 && entryKey != null
2952 && map.keyEquivalence.equivalent(key, entryKey)) {
2953 ValueReference<K, V> valueReference = e.getValueReference();
2954 V entryValue = valueReference.get();
2955 if (entryValue == null) {
2956 if (valueReference.isActive()) {
2957 // If the value disappeared, this entry is partially collected.
2958 int newCount = this.count - 1;
2959 ++modCount;
2960 ReferenceEntry<K, V> newFirst =
2961 removeValueFromChain(
2962 first,
2963 e,
2964 entryKey,
2965 hash,
2966 entryValue,
2967 valueReference,
2968 RemovalCause.COLLECTED);
2969 newCount = this.count - 1;
2970 table.set(index, newFirst);
2971 this.count = newCount; // write-volatile
2972 }
2973 return false;
2974 }
2975
2976 if (map.valueEquivalence.equivalent(oldValue, entryValue)) {
2977 ++modCount;
2978 enqueueNotification(
2979 key, hash, entryValue, valueReference.getWeight(), RemovalCause.REPLACED);
2980 setValue(e, key, newValue, now);
2981 evictEntries(e);
2982 return true;
2983 } else {
2984 // Mimic
2985 // "if (map.containsKey(key) && map.get(key).equals(oldValue))..."
2986 recordLockedRead(e, now);
2987 return false;
2988 }
2989 }
2990 }
2991
2992 return false;
2993 } finally {
2994 unlock();
2995 postWriteCleanup();

Callers

nothing calls this directly

Calls 15

preWriteCleanupMethod · 0.95
removeValueFromChainMethod · 0.95
enqueueNotificationMethod · 0.95
setValueMethod · 0.95
evictEntriesMethod · 0.95
recordLockedReadMethod · 0.95
postWriteCleanupMethod · 0.95
getMethod · 0.65
getNextMethod · 0.65
getKeyMethod · 0.65
getHashMethod · 0.65
getValueReferenceMethod · 0.65

Tested by

no test coverage detected