From cbd3cec3fc2d393cef9f251d05b4e3afb40bc945 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Tue, 21 Jul 2020 21:42:21 -0400 Subject: [PATCH] touch values when accessed --- .../cobbzilla/util/collection/ExpirationMap.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/cobbzilla/util/collection/ExpirationMap.java b/src/main/java/org/cobbzilla/util/collection/ExpirationMap.java index 6f4ebdd..5fb6f30 100644 --- a/src/main/java/org/cobbzilla/util/collection/ExpirationMap.java +++ b/src/main/java/org/cobbzilla/util/collection/ExpirationMap.java @@ -121,7 +121,7 @@ public class ExpirationMap implements Map { @Override public V put(K key, V value) { if (isTimestampInPast(nextCleaningTime)) cleanExpired(); final ExpirationMapEntry previous = map.put(key, new ExpirationMapEntry<>(value)); - return previous == null ? null : previous.value; + return previous == null ? null : previous.touch().value; } @Override public V remove(Object key) { @@ -144,36 +144,36 @@ public class ExpirationMap implements Map { @Override public Collection values() { if (isTimestampInPast(nextCleaningTime)) cleanExpired(); - return map.values().stream().map(v -> v.value).collect(Collectors.toList()); + return map.values().stream().map(v -> v.touch().value).collect(Collectors.toList()); } @Override public V putIfAbsent(K key, V value) { if (isTimestampInPast(nextCleaningTime)) cleanExpired(); final ExpirationMapEntry val = map.putIfAbsent(key, new ExpirationMapEntry<>(value)); - return val == null ? null : val.value; + return val == null ? null : val.touch().value; } @Override public V computeIfAbsent(K key, Function mappingFunction) { if (isTimestampInPast(nextCleaningTime)) cleanExpired(); - return map.computeIfAbsent(key, k -> new ExpirationMapEntry<>(mappingFunction.apply(k))).value; + return map.computeIfAbsent(key, k -> new ExpirationMapEntry<>(mappingFunction.apply(k))).touch().value; } @Override public V computeIfPresent(K key, BiFunction remappingFunction) { if (isTimestampInPast(nextCleaningTime)) cleanExpired(); final ExpirationMapEntry found = map.computeIfPresent(key, (k, vExpirationMapEntry) -> new ExpirationMapEntry<>(remappingFunction.apply(k, vExpirationMapEntry.value))); - return found == null ? null : found.value; + return found == null ? null : found.touch().value; } @AllArgsConstructor private static class EMEntry implements Entry { - @Getter private K key; - @Getter private V value; + @Getter private final K key; + @Getter private final V value; @Override public V setValue(V value) { return notSupported("setValue"); } } @Override public Set> entrySet() { if (isTimestampInPast(nextCleaningTime)) cleanExpired(); - return map.entrySet().stream().map(e -> new EMEntry<>(e.getKey(), e.getValue().value)).collect(Collectors.toSet()); + return map.entrySet().stream().map(e -> new EMEntry<>(e.getKey(), e.getValue().touch().value)).collect(Collectors.toSet()); } private synchronized void cleanExpired () {