View Javadoc

1   package com.atlassian.cache.memory;
2   
3   import java.util.SortedMap;
4   
5   import com.atlassian.cache.CacheStatisticsKey;
6   import com.atlassian.util.concurrent.Supplier;
7   
8   import com.google.common.cache.Cache;
9   import com.google.common.collect.ImmutableSortedMap;
10  
11  import static com.atlassian.cache.CacheStatisticsKey.EVICTION_COUNT;
12  import static com.atlassian.cache.CacheStatisticsKey.HIT_COUNT;
13  import static com.atlassian.cache.CacheStatisticsKey.MISS_COUNT;
14  import static com.atlassian.cache.CacheStatisticsKey.SIZE;
15  
16  /**
17   * @since v2.4.0
18   */
19  public class DelegatingCacheStatistics
20  {
21      public static SortedMap<CacheStatisticsKey,Supplier<Long>> toStatistics(Cache<?, ?> internalCache)
22      {
23          return ImmutableSortedMap.<CacheStatisticsKey,Supplier<Long>>orderedBy(CacheStatisticsKey.SORT_BY_LABEL)
24                  .put(SIZE, memoize(internalCache.size()))
25                  .put(HIT_COUNT, memoize(internalCache.stats().hitCount()))
26                  .put(MISS_COUNT, memoize(internalCache.stats().missCount()))
27                  .put(EVICTION_COUNT, memoize(internalCache.stats().evictionCount()))
28                  .build();
29      }
30  
31      private static <V> Supplier<V> memoize(final V value)
32      {
33          return new ImmediateSupplier<V>(value);
34      }
35  
36      static class ImmediateSupplier<V> implements Supplier<V>
37      {
38          private final V value;
39  
40          ImmediateSupplier(final V value)
41          {
42              this.value = value;
43          }
44  
45          @Override
46          public V get()
47          {
48              return value;
49          }
50  
51          @Override
52          public String toString()
53          {
54              return "ImmediateSupplier[" + value + ']';
55          }
56      }
57  }