1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.common.lru;
21
22 import java.util.concurrent.Callable;
23
24
25
26
27
28
29 public abstract class AbstractLruCache<K, V>
30 implements InterfaceLruCache<K, V> {
31 private long ttl;
32
33
34
35
36
37
38
39
40 protected AbstractLruCache(final long ttl) {
41 if (ttl <= 0) {
42 throw new IllegalArgumentException("ttl must be positive");
43 }
44
45 this.ttl = ttl;
46 }
47
48 @Override
49 public final boolean contains(final K key) {
50
51 final V value = get(key);
52
53 return value != null;
54 }
55
56
57
58
59
60
61
62
63
64
65
66 protected InterfaceLruCacheEntry<V> createEntry(final V value,
67 final long ttl) {
68 return new StrongReferenceCacheEntry<V>(value, ttl);
69 }
70
71 @Override
72 public V get(final K key) {
73 return getValue(key);
74 }
75
76 @Override
77 public final V get(final K key, final Callable<V> callback) throws Exception {
78 return get(key, callback, ttl);
79 }
80
81 @Override
82 public final V get(final K key, final Callable<V> callback, final long ttl)
83 throws Exception {
84 V value = get(key);
85
86
87 if (value == null) {
88 value = callback.call();
89 put(key, value, ttl);
90 }
91
92 return value;
93 }
94
95 @Override
96 public final long getTtl() {
97 return ttl;
98 }
99
100 @Override
101 public final void setNewTtl(final long ttl) {
102 if (ttl <= 0) {
103 throw new IllegalArgumentException("ttl must be positive");
104 }
105 this.ttl = ttl;
106 }
107
108
109
110
111
112
113
114
115 protected abstract InterfaceLruCacheEntry<V> getEntry(K key);
116
117 @Override
118 public final void updateTtl(final K key) {
119 final InterfaceLruCacheEntry<V> cacheEntry = getEntry(key);
120 if (cacheEntry != null) {
121 cacheEntry.resetTime(ttl);
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134 protected final V getValue(final K key) {
135 V value = null;
136
137 final InterfaceLruCacheEntry<V> cacheEntry = getEntry(key);
138
139 if (cacheEntry != null) {
140 value = cacheEntry.getValue();
141
142
143 if (value == null) {
144 remove(key);
145 }
146 }
147
148 return value;
149 }
150
151 @Override
152 public final boolean isEmpty() {
153 return size() == 0;
154 }
155
156 @Override
157 public final void put(final K key, final V value) {
158 put(key, value, ttl);
159 }
160
161 @Override
162 public void put(final K key, final V value, final long ttl) {
163 if (value != null) {
164 putEntry(key, createEntry(value, ttl));
165 }
166 }
167
168
169
170
171
172
173
174 protected abstract void putEntry(K key, InterfaceLruCacheEntry<V> entry);
175 }