blob: 1834a5053ad90d83a7dafec41e970262b938e39f [file] [log] [blame]
tom0872a172014-09-23 11:24:26 -07001package org.onlab.onos.store.impl;
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -07002
3import java.util.concurrent.Callable;
4import java.util.concurrent.ExecutionException;
5
6import com.google.common.base.Optional;
7import com.google.common.cache.ForwardingLoadingCache.SimpleForwardingLoadingCache;
8import com.google.common.cache.LoadingCache;
9
10public 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}