diff --git a/maven-resolver-api/src/main/java/org/eclipse/aether/DefaultRepositoryCache.java b/maven-resolver-api/src/main/java/org/eclipse/aether/DefaultRepositoryCache.java index de8c4dc9a..e0f6d5052 100644 --- a/maven-resolver-api/src/main/java/org/eclipse/aether/DefaultRepositoryCache.java +++ b/maven-resolver-api/src/main/java/org/eclipse/aether/DefaultRepositoryCache.java @@ -18,8 +18,8 @@ */ package org.eclipse.aether; -import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; /** * A simplistic repository cache backed by a thread-safe map. The simplistic nature of this cache makes it only suitable @@ -27,7 +27,7 @@ */ public final class DefaultRepositoryCache implements RepositoryCache { - private final Map cache = new ConcurrentHashMap<>(256); + private final ConcurrentHashMap cache = new ConcurrentHashMap<>(256); public Object get(RepositorySystemSession session, Object key) { return cache.get(key); @@ -40,4 +40,9 @@ public void put(RepositorySystemSession session, Object key, Object data) { cache.remove(key); } } + + @Override + public Object computeIfAbsent(RepositorySystemSession session, Object key, Supplier supplier) { + return cache.computeIfAbsent(key, k -> supplier.get()); + } } diff --git a/maven-resolver-api/src/main/java/org/eclipse/aether/RepositoryCache.java b/maven-resolver-api/src/main/java/org/eclipse/aether/RepositoryCache.java index 9bf6268f0..565048c8e 100644 --- a/maven-resolver-api/src/main/java/org/eclipse/aether/RepositoryCache.java +++ b/maven-resolver-api/src/main/java/org/eclipse/aether/RepositoryCache.java @@ -18,12 +18,16 @@ */ package org.eclipse.aether; +import java.util.function.Supplier; + /** * Caches auxiliary data used during repository access like already processed metadata. The data in the cache is meant * for exclusive consumption by the repository system and is opaque to the cache implementation. Note: * Actual cache implementations must be thread-safe. * * @see RepositorySystemSession#getCache() + * @noimplement This interface is not intended to be implemented by clients. + * @noextend This interface is not intended to be extended by clients. */ public interface RepositoryCache { @@ -53,4 +57,14 @@ public interface RepositoryCache { * @return The requested data or {@code null} if none was present in the cache. */ Object get(RepositorySystemSession session, Object key); + + /** + * Retrieve or compute the data associated with the specified key. + * + * @param key The key for which to retrieve the session data, must not be {@code null}. + * @param supplier The supplier will compute the new value, must not be {@code null}. + * @return The cache data associated with the key. + * @since 2.0.0 + */ + Object computeIfAbsent(RepositorySystemSession session, Object key, Supplier supplier); } diff --git a/pom.xml b/pom.xml index 178a31526..602423fc2 100644 --- a/pom.xml +++ b/pom.xml @@ -299,6 +299,7 @@ + org.eclipse.aether.RepositoryCache org.eclipse.aether.RepositoryListener org.eclipse.aether.RepositorySystem org.eclipse.aether.RepositorySystemSession