From fce0851ab92495d99aac3c18ce2eba189e989904 Mon Sep 17 00:00:00 2001 From: iProdigy Date: Sun, 8 Sep 2024 19:39:08 -0700 Subject: [PATCH] chore: deprecate AbstractCache that relies upon synchronization --- .../xanthic/cache/core/AbstractCache.java | 4 + .../cache/core/LockedAbstractCache.java | 2 + .../provider/androidx/AbstractCache.java | 107 ++++++++++++++++++ .../androidx/ExpiringLruDelegate.java | 1 - .../cache/provider/androidx/LruDelegate.java | 1 - 5 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/AbstractCache.java diff --git a/core/src/main/java/io/github/xanthic/cache/core/AbstractCache.java b/core/src/main/java/io/github/xanthic/cache/core/AbstractCache.java index 67490cae..16f8caa5 100644 --- a/core/src/main/java/io/github/xanthic/cache/core/AbstractCache.java +++ b/core/src/main/java/io/github/xanthic/cache/core/AbstractCache.java @@ -1,6 +1,7 @@ package io.github.xanthic.cache.core; import io.github.xanthic.cache.api.Cache; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -24,7 +25,10 @@ * * @param The type of keys that form the cache * @param The type of values contained in the cache + * @deprecated no longer used by Xanthic; synchronization generally should be avoided */ +@Deprecated +@ApiStatus.ScheduledForRemoval(inVersion = "1.0.0") public abstract class AbstractCache implements Cache { @Override diff --git a/core/src/main/java/io/github/xanthic/cache/core/LockedAbstractCache.java b/core/src/main/java/io/github/xanthic/cache/core/LockedAbstractCache.java index b51c6d02..3eba6f36 100644 --- a/core/src/main/java/io/github/xanthic/cache/core/LockedAbstractCache.java +++ b/core/src/main/java/io/github/xanthic/cache/core/LockedAbstractCache.java @@ -1,6 +1,7 @@ package io.github.xanthic.cache.core; import io.github.xanthic.cache.api.Cache; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -30,6 +31,7 @@ * @deprecated no longer used by Xanthic to implement any canonical cache provider */ @Deprecated +@ApiStatus.ScheduledForRemoval(inVersion = "1.0.0") public abstract class LockedAbstractCache implements Cache { protected final ReadWriteLock lock = new ReentrantReadWriteLock(); diff --git a/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/AbstractCache.java b/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/AbstractCache.java new file mode 100644 index 00000000..b1bc6f53 --- /dev/null +++ b/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/AbstractCache.java @@ -0,0 +1,107 @@ +package io.github.xanthic.cache.provider.androidx; + +import io.github.xanthic.cache.api.Cache; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Objects; +import java.util.function.BiFunction; +import java.util.function.Function; + +abstract class AbstractCache implements Cache { + + @Override + public V computeIfAbsent(@NotNull K key, @NotNull Function computeFunc) { + synchronized (getLock()) { + V old = this.get(key); + if (old != null) return old; + V computed = computeFunc.apply(key); + this.put(key, computed); + return computed; + } + } + + @Nullable + @Override + public V compute(@NotNull K key, @NotNull BiFunction computeFunc) { + synchronized (getLock()) { + V oldValue = this.get(key); + V newValue = computeFunc.apply(key, oldValue); + if (newValue != null) { + this.put(key, newValue); + return newValue; + } else if (oldValue != null) { + this.remove(key); + } + } + return null; + } + + @Override + public V computeIfPresent(@NotNull K key, @NotNull BiFunction computeFunc) { + synchronized (getLock()) { + V oldValue = this.get(key); + if (oldValue != null) { + V newValue = computeFunc.apply(key, oldValue); + if (newValue != null) { + this.put(key, newValue); + return newValue; + } else { + this.remove(key); + } + } + } + return null; + } + + @Override + public V putIfAbsent(@NotNull K key, @NotNull V value) { + synchronized (getLock()) { + V old = this.get(key); + if (old == null) { + this.put(key, value); + } + return old; + } + } + + @Override + public V merge(@NotNull K key, @NotNull V value, @NotNull BiFunction mergeFunc) { + synchronized (getLock()) { + V old = putIfAbsent(key, value); + if (old == null) return value; + V merged = mergeFunc.apply(old, value); + this.put(key, merged); + return merged; + } + } + + @Override + public boolean replace(@NotNull K key, @NotNull V value) { + synchronized (getLock()) { + V old = this.get(key); + if (old == null) return false; + put(key, value); + return true; + } + } + + @Override + public boolean replace(@NotNull K key, @NotNull V oldValue, @NotNull V newValue) { + // noinspection ConstantConditions + if (oldValue == null) return false; + synchronized (getLock()) { + if (Objects.equals(oldValue, this.get(key))) { + this.put(key, newValue); + return true; + } + } + return false; + } + + @NotNull + protected Object getLock() { + return this; + } + +} diff --git a/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/ExpiringLruDelegate.java b/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/ExpiringLruDelegate.java index 420ff98c..91680f8c 100644 --- a/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/ExpiringLruDelegate.java +++ b/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/ExpiringLruDelegate.java @@ -4,7 +4,6 @@ import io.github.xanthic.cache.api.RemovalListener; import io.github.xanthic.cache.api.domain.ExpiryType; import io.github.xanthic.cache.api.domain.RemovalCause; -import io.github.xanthic.cache.core.AbstractCache; import lombok.AccessLevel; import lombok.EqualsAndHashCode; import lombok.Getter; diff --git a/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/LruDelegate.java b/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/LruDelegate.java index 003be1be..3051daef 100644 --- a/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/LruDelegate.java +++ b/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/LruDelegate.java @@ -1,7 +1,6 @@ package io.github.xanthic.cache.provider.androidx; import androidx.collection.LruCache; -import io.github.xanthic.cache.core.AbstractCache; import lombok.AccessLevel; import lombok.EqualsAndHashCode; import lombok.Getter;