Skip to content

Package: DefaultCacheManager$DefaultCache

DefaultCacheManager$DefaultCache

nameinstructionbranchcomplexitylinemethod
DefaultCacheManager.DefaultCache(String)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
computeObject(Object, Supplier)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getCachedObject(Object, Supplier)
M: 16 C: 16
50%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 4
67%
M: 0 C: 1
100%
lambda$getCachedObject$0(Supplier, Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.model.impl;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Map;
31: import java.util.concurrent.ConcurrentHashMap;
32: import java.util.function.Supplier;
33: import java.lang.ref.SoftReference;
34: import it.tidalwave.messagebus.annotation.ListensTo;
35: import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
36: import it.tidalwave.bluemarine2.model.spi.CacheManager;
37: import it.tidalwave.bluemarine2.message.PersistenceInitializedNotification;
38: import lombok.RequiredArgsConstructor;
39: import lombok.extern.slf4j.Slf4j;
40: import static java.util.Objects.requireNonNull;
41:
42: /***********************************************************************************************************************
43: *
44: * @author Fabrizio Giudici
45: *
46: **********************************************************************************************************************/
47: @Slf4j @SimpleMessageSubscriber
48: public class DefaultCacheManager implements CacheManager
49: {
50: private final Map<Object, Cache> cacheMap = new ConcurrentHashMap<>();
51:
52: @RequiredArgsConstructor
53: static class DefaultCache implements Cache
54: {
55: @Nonnull
56: private final String name;
57: // TODO: use Spring ConcurrentReferenceHashMap?
58: private final Map<Object, SoftReference<?>> objectMap = new ConcurrentHashMap<>();
59:
60: @Override @Nonnull
61: public <T> T getCachedObject (@Nonnull final Object key, @Nonnull final Supplier<T> supplier)
62: {
63: SoftReference<T> ref = (SoftReference<T>)(objectMap.computeIfAbsent(key, k -> computeObject(k, supplier)));
64: T object = ref.get();
65:
66:• if (object == null)
67: {
68: objectMap.put(key, ref = computeObject(key, supplier));
69: object = requireNonNull(ref.get(), "Cache returned null");
70: }
71:
72: return object;
73: }
74:
75: @Nonnull
76: private <T> SoftReference<T> computeObject (@Nonnull final Object key, @Nonnull final Supplier<T> supplier)
77: {
78: log.info(">>>> cache {} miss for {}", name, key);
79: return new SoftReference<>(requireNonNull(supplier.get(), "Supplier returned null"));
80: }
81: }
82:
83: @Override @Nonnull
84: public Cache getCache (@Nonnull final Object cacheKey)
85: {
86: return cacheMap.computeIfAbsent(cacheKey, key -> new DefaultCache(cacheKey.toString()));
87: }
88:
89: /* visible for testing FIXME */ public void onPersistenceUpdated (@ListensTo final PersistenceInitializedNotification notification)
90: {
91: log.debug("onPersistenceUpdated({})", notification);
92: cacheMap.clear();
93: }
94: }