From c64af7849c86fb17df6ab7857028cc609f091c0e Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Wed, 30 Jan 2019 19:10:49 +0100 Subject: [PATCH] Revert "ArC - remove LazyValue from ComputingCache" This reverts commit 8329b6000634119bb4026c1feb0e12935e654c7a. --- .../protean/arc/AbstractSharedContext.java | 2 +- .../org/jboss/protean/arc/ComputingCache.java | 50 ++++++++++++++----- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/independent-projects/arc/runtime/src/main/java/org/jboss/protean/arc/AbstractSharedContext.java b/independent-projects/arc/runtime/src/main/java/org/jboss/protean/arc/AbstractSharedContext.java index 8e2167ef368bf..7f75f45335360 100644 --- a/independent-projects/arc/runtime/src/main/java/org/jboss/protean/arc/AbstractSharedContext.java +++ b/independent-projects/arc/runtime/src/main/java/org/jboss/protean/arc/AbstractSharedContext.java @@ -67,7 +67,7 @@ public void destroy(Contextual contextual) { @Override public synchronized void destroy() { - instances.forEachValue(InstanceHandleImpl::destroyInternal); + instances.forEachExistingValue(InstanceHandleImpl::destroyInternal); instances.clear(); } diff --git a/independent-projects/arc/runtime/src/main/java/org/jboss/protean/arc/ComputingCache.java b/independent-projects/arc/runtime/src/main/java/org/jboss/protean/arc/ComputingCache.java index 02cd2d0879168..6fdedbd3a9035 100644 --- a/independent-projects/arc/runtime/src/main/java/org/jboss/protean/arc/ComputingCache.java +++ b/independent-projects/arc/runtime/src/main/java/org/jboss/protean/arc/ComputingCache.java @@ -33,33 +33,35 @@ */ public class ComputingCache { - private final ConcurrentMap map; + private final ConcurrentMap> map; - private final Function function; + private final Function> function; public ComputingCache(Function computingFunction) { this.map = new ConcurrentHashMap<>(); - this.function = computingFunction; + this.function = new CacheFunction(computingFunction); } public V getValue(K key) { - V value = map.get(key); + LazyValue value = map.get(key); if (value == null) { value = function.apply(key); - V previous = map.putIfAbsent(key, value); + LazyValue previous = map.putIfAbsent(key, value); if (previous != null) { value = previous; } } - return value; + return value.get(); } public V getValueIfPresent(K key) { - return map.get(key); + LazyValue value = map.get(key); + return value != null ? value.getIfPresent() :null; } public V remove(K key) { - return map.remove(key); + LazyValue previous = map.remove(key); + return previous != null ? previous.get() : null; } public void clear() { @@ -68,15 +70,24 @@ public void clear() { public void forEachValue(Consumer action) { Objects.requireNonNull(action); - for (V value : map.values()) { - action.accept(value); + for (LazyValue value : map.values()) { + action.accept(value.get()); + } + } + + public void forEachExistingValue(Consumer action) { + Objects.requireNonNull(action); + for (LazyValue value : map.values()) { + if(value.isSet()) { + action.accept(value.get()); + } } } public void forEachEntry(BiConsumer action) { Objects.requireNonNull(action); - for (Map.Entry entry : map.entrySet()) { - action.accept(entry.getKey(), entry.getValue()); + for (Map.Entry> entry : map.entrySet()) { + action.accept(entry.getKey(), entry.getValue().get()); } } @@ -84,4 +95,19 @@ public boolean isEmpty() { return map.isEmpty(); } + class CacheFunction implements Function> { + + private final Function computingFunction; + + public CacheFunction(Function computingFunction) { + this.computingFunction = computingFunction; + } + + @Override + public LazyValue apply(K key) { + return new LazyValue(() -> computingFunction.apply(key)); + } + + } + }