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