View Javadoc

1   package com.atlassian.vcache.internal.core;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   import java.util.Optional;
6   import java.util.Set;
7   import java.util.function.Function;
8   import java.util.function.Supplier;
9   import javax.annotation.Nonnull;
10  
11  import com.atlassian.vcache.RequestCache;
12  import com.atlassian.vcache.internal.RequestContext;
13  
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  import static com.atlassian.vcache.internal.NameValidator.requireValidCacheName;
18  import static java.util.Objects.requireNonNull;
19  
20  /**
21   * Implementation of {@link RequestCache} that uses a delegate {@link HashMap}
22   *
23   * @param <K> the key type
24   * @param <V> the value type
25   * @since 1.0
26   */
27  public class DefaultRequestCache<K, V> implements RequestCache<K, V>
28  {
29      private static final Logger log = LoggerFactory.getLogger(DefaultRequestCache.class);
30  
31      private final String name;
32      private final Supplier<RequestContext> contextSupplier;
33  
34      public DefaultRequestCache(String name, Supplier<RequestContext> contextSupplier)
35      {
36          this.name = requireValidCacheName(name);
37          this.contextSupplier = requireNonNull(contextSupplier);
38      }
39  
40      @Nonnull
41      @Override
42      public Optional<V> get(K key)
43      {
44          return Optional.ofNullable(ensureDelegate().get(key));
45      }
46  
47      @Nonnull
48      @Override
49      public V get(K key, Supplier<? extends V> supplier)
50      {
51          return ensureDelegate().computeIfAbsent(requireNonNull(key), k -> requireNonNull(supplier.get()));
52      }
53  
54      @Override
55      public void put(K key, V value)
56      {
57          ensureDelegate().put(requireNonNull(key), requireNonNull(value));
58      }
59  
60      @Nonnull
61      @Override
62      public Optional<V> putIfAbsent(K key, V value)
63      {
64          return Optional.ofNullable(
65                  ensureDelegate().putIfAbsent(requireNonNull(key), requireNonNull(value)));
66      }
67  
68      @Override
69      public boolean replaceIf(K key, V currentValue, V newValue)
70      {
71          return ensureDelegate().replace(requireNonNull(key), requireNonNull(currentValue), requireNonNull(newValue));
72      }
73  
74      @Override
75      public boolean removeIf(K key, V value)
76      {
77          return ensureDelegate().remove(requireNonNull(key), requireNonNull(value));
78      }
79  
80      @Override
81      public void remove(Iterable<K> keys)
82      {
83          final HashMap<K, V> delegate = ensureDelegate();
84          keys.forEach(k -> delegate.remove(requireNonNull(k)));
85      }
86  
87      @Override
88      public void removeAll()
89      {
90          ensureDelegate().clear();
91      }
92  
93      @Nonnull
94      @Override
95      public Map<K, Optional<V>> getBulk(Iterable<K> keys)
96      {
97          final Map<K, Optional<V>> result = new HashMap<>();
98          keys.forEach(key -> result.put(requireNonNull(key), this.get(key)));
99          return result;
100     }
101 
102     @Nonnull
103     @Override
104     public Map<K, V> getBulk(Function<Set<K>, Map<K, V>> factory, Iterable<K> keys)
105     {
106         return VCacheUtils.getBulkOn(this, factory, keys);
107     }
108 
109     @Nonnull
110     @Override
111     public String getName()
112     {
113         return name;
114     }
115 
116     private HashMap<K, V> ensureDelegate()
117     {
118         final RequestContext requestContext = contextSupplier.get();
119         return requestContext.computeIfAbsent(this, HashMap::new);
120     }
121 }