tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.impl; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 2 | |
| 3 | import java.util.concurrent.Callable; |
| 4 | import java.util.concurrent.ExecutionException; |
| 5 | |
| 6 | import com.google.common.base.Optional; |
| 7 | import com.google.common.cache.ForwardingLoadingCache.SimpleForwardingLoadingCache; |
| 8 | import com.google.common.cache.LoadingCache; |
| 9 | |
| 10 | public class AbsentInvalidatingLoadingCache<K, V> extends |
| 11 | SimpleForwardingLoadingCache<K, Optional<V>> { |
| 12 | |
| 13 | public AbsentInvalidatingLoadingCache(LoadingCache<K, Optional<V>> delegate) { |
| 14 | super(delegate); |
| 15 | } |
| 16 | |
| 17 | @Override |
| 18 | public Optional<V> get(K key) throws ExecutionException { |
| 19 | Optional<V> v = super.get(key); |
| 20 | if (!v.isPresent()) { |
| 21 | invalidate(key); |
| 22 | } |
| 23 | return v; |
| 24 | } |
| 25 | |
| 26 | @Override |
| 27 | public Optional<V> getUnchecked(K key) { |
| 28 | Optional<V> v = super.getUnchecked(key); |
| 29 | if (!v.isPresent()) { |
| 30 | invalidate(key); |
| 31 | } |
| 32 | return v; |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public Optional<V> apply(K key) { |
| 37 | return getUnchecked(key); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public Optional<V> getIfPresent(Object key) { |
| 42 | Optional<V> v = super.getIfPresent(key); |
| 43 | if (!v.isPresent()) { |
| 44 | invalidate(key); |
| 45 | } |
| 46 | return v; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public Optional<V> get(K key, Callable<? extends Optional<V>> valueLoader) |
| 51 | throws ExecutionException { |
| 52 | |
| 53 | Optional<V> v = super.get(key, valueLoader); |
| 54 | if (!v.isPresent()) { |
| 55 | invalidate(key); |
| 56 | } |
| 57 | return v; |
| 58 | } |
| 59 | |
| 60 | // TODO should we be also checking getAll, etc. |
| 61 | } |