MCPcopy Create free account
hub / github.com/antlr/codebuff / replace

Method replace

corpus/java/training/guava/cache/LocalCache.java:3007–3066  ·  view source on GitHub ↗
(K key, int hash, V oldValue, V newValue)

Source from the content-addressed store, hash-verified

3005 }
3006
3007 boolean replace(K key, int hash, V oldValue, V newValue) {
3008 lock();
3009 try {
3010 long now = map.ticker.read();
3011 preWriteCleanup(now);
3012
3013 AtomicReferenceArray<ReferenceEntry<K, V>> table = this.table;
3014 int index = hash & (table.length() - 1);
3015 ReferenceEntry<K, V> first = table.get(index);
3016
3017 for (ReferenceEntry<K, V> e = first; e != null; e = e.getNext()) {
3018 K entryKey = e.getKey();
3019 if (e.getHash() == hash
3020 && entryKey != null
3021 && map.keyEquivalence.equivalent(key, entryKey)) {
3022 ValueReference<K, V> valueReference = e.getValueReference();
3023 V entryValue = valueReference.get();
3024 if (entryValue == null) {
3025 if (valueReference.isActive()) {
3026 // If the value disappeared, this entry is partially collected.
3027 int newCount = this.count - 1;
3028 ++modCount;
3029 ReferenceEntry<K, V> newFirst =
3030 removeValueFromChain(
3031 first,
3032 e,
3033 entryKey,
3034 hash,
3035 entryValue,
3036 valueReference,
3037 RemovalCause.COLLECTED);
3038 newCount = this.count - 1;
3039 table.set(index, newFirst);
3040 this.count = newCount; // write-volatile
3041 }
3042 return false;
3043 }
3044
3045 if (map.valueEquivalence.equivalent(oldValue, entryValue)) {
3046 ++modCount;
3047 enqueueNotification(
3048 key, hash, entryValue, valueReference.getWeight(), RemovalCause.REPLACED);
3049 setValue(e, key, newValue, now);
3050 evictEntries(e);
3051 return true;
3052 } else {
3053 // Mimic
3054 // "if (map.containsKey(key) && map.get(key).equals(oldValue))..."
3055 recordLockedRead(e, now);
3056 return false;
3057 }
3058 }
3059 }
3060
3061 return false;
3062 } finally {
3063 unlock();
3064 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