View Javadoc

1   package com.atlassian.vcache.internal.core.service;
2   
3   import java.util.Map;
4   import java.util.Optional;
5   import java.util.stream.Collectors;
6   import java.util.stream.StreamSupport;
7   import javax.annotation.Nonnull;
8   
9   import com.atlassian.vcache.StableReadExternalCache;
10  
11  /**
12   * Provides operations common to {@link com.atlassian.vcache.StableReadExternalCache} instances.
13   *
14   * @param <V> the value type
15   */
16  public abstract class AbstractStableReadExternalCache<V>
17          extends AbstractNonDirectExternalCache<V>
18          implements StableReadExternalCache<V>
19  {
20      protected AbstractStableReadExternalCache(String name)
21      {
22          super(name);
23      }
24  
25      @Nonnull
26      protected final Optional<Optional<V>> checkValueRecorded(String internalKey)
27      {
28          return ensureCacheContext().getValueRecorded(internalKey);
29      }
30  
31      @Nonnull
32      protected final Map<String, Optional<V>> checkValuesRecorded(Iterable<String> internalKeys)
33      {
34          final AbstractExternalCacheRequestContext<V> cacheContext = ensureCacheContext();
35          return StreamSupport.stream(internalKeys.spliterator(), false)
36                  .filter(k -> cacheContext.getValueRecorded(k).isPresent())
37                  .distinct()
38                  .collect(Collectors.toMap(
39                                  k -> k,
40                                  k -> cacheContext.getValueRecorded(k).get())
41                  );
42      }
43  }