View Javadoc

1   package com.atlassian.cache.ehcache;
2   
3   import com.atlassian.cache.Cache;
4   import com.atlassian.cache.CacheException;
5   import com.atlassian.cache.CacheSettings;
6   import net.sf.ehcache.Ehcache;
7   import net.sf.ehcache.Element;
8   import net.sf.ehcache.loader.CacheLoader;
9   
10  import java.util.Collection;
11  import javax.annotation.Nullable;
12  
13  /**
14   * A Cache that delegates to EhCache.
15   *
16   * @since 2.0
17   */
18  class DelegatingCache<K, V> extends ManagedCacheSupport implements Cache<K, V>
19  {
20      private final Ehcache delegate;
21      private final CacheLoader cacheLoader;
22  
23  
24      private DelegatingCache(final Ehcache delegate, final com.atlassian.cache.CacheLoader<K, V> loader, CacheSettings settings)
25      {
26          super(delegate, settings);
27          this.delegate = delegate;
28          if (loader != null)
29          {
30              cacheLoader = new DelegatingCacheLoader<K, V>(loader);
31          }
32          else
33          {
34              cacheLoader = null;
35          }
36      }
37  
38      static <K, V> DelegatingCache<K, V> create(final Ehcache delegate, final com.atlassian.cache.CacheLoader<K, V> loader, CacheSettings settings)
39      {
40          return new DelegatingCache<K, V>(delegate, loader, settings);
41      }
42  
43      @SuppressWarnings("unchecked")
44      @Override
45      public Collection<K> getKeys()
46      {
47          try
48          {
49              return delegate.getKeys();
50          }
51          catch (Exception e)
52          {
53              throw new CacheException(e);
54          }
55      }
56  
57      @Override
58      public void put(final K key, final V value)
59      {
60          try
61          {
62              delegate.put(new Element(key, value));
63          }
64          catch (Exception e)
65          {
66              throw new CacheException(e);
67          }
68      }
69  
70      @SuppressWarnings("unchecked")
71      @Override
72      public V get(final K key)
73      {
74          try
75          {
76              Element element;
77              if (cacheLoader != null)
78              {
79                  element = delegate.getWithLoader(key, cacheLoader, null);
80              }
81              else
82              {
83                  element = delegate.get(key);
84              }
85              return element == null ? null : (V) element.getObjectValue();
86          }
87          catch (net.sf.ehcache.CacheException e)
88          {
89              throw new CacheException(e.getCause());
90          }
91          catch (Exception e)
92          {
93              throw new CacheException(e);
94          }
95      }
96  
97      @Override
98      public void remove(final K key)
99      {
100         try
101         {
102             delegate.remove(key);
103         }
104         catch (Exception e)
105         {
106             throw new CacheException(e);
107         }
108     }
109 
110     @Override
111     public void removeAll()
112     {
113         try
114         {
115             delegate.removeAll();
116         }
117         catch (Exception e)
118         {
119             throw new CacheException(e);
120         }
121     }
122 
123     @Override
124     public void clear()
125     {
126         removeAll();
127     }
128 
129     @SuppressWarnings("unchecked")
130     @Override
131     public V putIfAbsent(K key, V value)
132     {
133         try
134         {
135             Element previous = delegate.putIfAbsent(new Element(key, value));
136             if (previous != null)
137             {
138                 return (V) previous.getObjectValue();
139             }
140             else
141             {
142                 return null;
143             }
144         }
145         catch (Exception e)
146         {
147             throw new CacheException(e);
148         }
149     }
150 
151     @Override
152     public boolean remove(K key, V value)
153     {
154         try
155         {
156             return delegate.removeElement(new Element(key, value));
157         }
158         catch (Exception e)
159         {
160             throw new CacheException(e);
161         }
162     }
163 
164     @Override
165     public boolean replace(K key, V oldValue, V newValue)
166     {
167         try
168         {
169             return delegate.replace(new Element(key, oldValue), new Element(key, newValue));
170         }
171         catch (Exception e)
172         {
173             throw new CacheException(e);
174         }
175     }
176 
177     @Override
178     public boolean equals(@Nullable final Object other)
179     {
180         if (other instanceof DelegatingCache)
181         {
182             DelegatingCache otherDelegatingCache = (DelegatingCache) other;
183             if (delegate.equals(otherDelegatingCache.delegate))
184             {
185                 return true;
186             }
187         }
188         return false;
189     }
190 
191     @Override
192     public int hashCode()
193     {
194         return 3 + delegate.hashCode();
195     }
196 
197 }