View Javadoc

1   package com.atlassian.cache.memory;
2   
3   import com.atlassian.cache.CacheException;
4   import com.atlassian.cache.CacheSettings;
5   import com.atlassian.cache.CachedReference;
6   import com.atlassian.cache.impl.ReferenceKey;
7   import com.google.common.cache.LoadingCache;
8   import com.google.common.util.concurrent.UncheckedExecutionException;
9   
10  import javax.annotation.Nullable;
11  
12  /**
13   * A Lazy Reference that delegates Concurrent Map.
14   *
15   * @since 2.0
16   */
17  class DelegatingCachedReference<V> extends ManagedCacheSupport implements CachedReference<V>
18  {
19      private final LoadingCache<ReferenceKey, V> internalCache;
20  
21      private DelegatingCachedReference(final LoadingCache<ReferenceKey, V> internalCache,
22              String name, CacheSettings settings)
23      {
24          super(name, settings);
25          this.internalCache = internalCache;
26      }
27  
28      static <V> DelegatingCachedReference<V> create(final LoadingCache<ReferenceKey, V> internalCache,
29              String name, CacheSettings settings)
30      {
31          return new DelegatingCachedReference<V>(internalCache, name, settings);
32      }
33  
34      @Override
35      public V get()
36      {
37          try
38          {
39              return internalCache.get(ReferenceKey.KEY);
40          }
41          catch (UncheckedExecutionException e)
42          {
43              throw new CacheException(e.getCause());
44          }
45          catch (Exception e)
46          {
47              throw new CacheException(e);
48          }
49      }
50  
51      @Override
52      public void reset()
53      {
54          try
55          {
56              internalCache.invalidate(ReferenceKey.KEY);
57          }
58          catch (Exception e)
59          {
60              throw new CacheException(e);
61          }
62      }
63  
64      @Override
65      public void clear()
66      {
67          reset();
68      }
69  
70      @Override
71      public boolean equals(@Nullable final Object other)
72      {
73          if (other instanceof DelegatingCachedReference)
74          {
75              DelegatingCachedReference otherDelegatingReference = (DelegatingCachedReference) other;
76              if (internalCache.equals(otherDelegatingReference.internalCache))
77              {
78                  return true;
79              }
80          }
81          return false;
82      }
83  
84      @Override
85      public int hashCode()
86      {
87          return 3 + internalCache.hashCode();
88      }
89  }