@@ -43,7 +43,8 @@ abstract class LinkedHashLruCache<K, V> implements LruCache<K, V> {
43
43
44
44
private final LinkedHashMap <K , SizedValue > delegate ;
45
45
private final Ticker ticker ;
46
- private final EvictionListener <K , SizedValue > evictionListener ;
46
+ @ Nullable
47
+ private final EvictionListener <K , V > evictionListener ;
47
48
private long estimatedSizeBytes ;
48
49
private long estimatedMaxSizeBytes ;
49
50
@@ -53,7 +54,7 @@ abstract class LinkedHashLruCache<K, V> implements LruCache<K, V> {
53
54
final Ticker ticker ) {
54
55
checkState (estimatedMaxSizeBytes > 0 , "max estimated cache size should be positive" );
55
56
this .estimatedMaxSizeBytes = estimatedMaxSizeBytes ;
56
- this .evictionListener = new SizeHandlingEvictionListener ( evictionListener ) ;
57
+ this .evictionListener = evictionListener ;
57
58
this .ticker = checkNotNull (ticker , "ticker" );
58
59
delegate = new LinkedHashMap <K , SizedValue >(
59
60
// rough estimate or minimum hashmap default
@@ -135,7 +136,7 @@ public final V cache(K key, V value) {
135
136
estimatedSizeBytes += size ;
136
137
existing = delegate .put (key , new SizedValue (size , value ));
137
138
if (existing != null ) {
138
- evictionListener . onEviction (key , existing , EvictionType .REPLACED );
139
+ fireOnEviction (key , existing , EvictionType .REPLACED );
139
140
}
140
141
return existing == null ? null : existing .value ;
141
142
}
@@ -174,7 +175,7 @@ private V invalidate(K key, EvictionType cause) {
174
175
checkNotNull (cause , "cause" );
175
176
SizedValue existing = delegate .remove (key );
176
177
if (existing != null ) {
177
- evictionListener . onEviction (key , existing , cause );
178
+ fireOnEviction (key , existing , cause );
178
179
}
179
180
return existing == null ? null : existing .value ;
180
181
}
@@ -185,7 +186,7 @@ public final void invalidateAll() {
185
186
while (iterator .hasNext ()) {
186
187
Map .Entry <K , SizedValue > entry = iterator .next ();
187
188
if (entry .getValue () != null ) {
188
- evictionListener . onEviction (entry .getKey (), entry .getValue (), EvictionType .EXPLICIT );
189
+ fireOnEviction (entry .getKey (), entry .getValue (), EvictionType .EXPLICIT );
189
190
}
190
191
iterator .remove ();
191
192
}
@@ -215,23 +216,22 @@ public final List<V> values() {
215
216
protected final boolean fitToLimit () {
216
217
boolean removedAnyUnexpired = false ;
217
218
if (estimatedSizeBytes <= estimatedMaxSizeBytes ) {
218
- // new size is larger no need to do cleanup
219
219
return false ;
220
220
}
221
221
// cleanup expired entries
222
222
long now = ticker .read ();
223
223
cleanupExpiredEntries (now );
224
224
225
- // cleanup eldest entry until new size limit
225
+ // cleanup eldest entry until the size of all entries fits within the limit
226
226
Iterator <Map .Entry <K , SizedValue >> lruIter = delegate .entrySet ().iterator ();
227
227
while (lruIter .hasNext () && estimatedMaxSizeBytes < this .estimatedSizeBytes ) {
228
228
Map .Entry <K , SizedValue > entry = lruIter .next ();
229
229
if (!shouldInvalidateEldestEntry (entry .getKey (), entry .getValue ().value , now )) {
230
230
break ; // Violates some constraint like minimum age so stop our cleanup
231
231
}
232
232
lruIter .remove ();
233
- // eviction listener will update the estimatedSizeBytes
234
- evictionListener . onEviction (entry .getKey (), entry .getValue (), EvictionType .SIZE );
233
+ // fireOnEviction will update the estimatedSizeBytes
234
+ fireOnEviction (entry .getKey (), entry .getValue (), EvictionType .SIZE );
235
235
removedAnyUnexpired = true ;
236
236
}
237
237
return removedAnyUnexpired ;
@@ -270,7 +270,7 @@ private boolean cleanupExpiredEntries(int maxExpiredEntries, long now) {
270
270
Map .Entry <K , SizedValue > entry = lruIter .next ();
271
271
if (isExpired (entry .getKey (), entry .getValue ().value , now )) {
272
272
lruIter .remove ();
273
- evictionListener . onEviction (entry .getKey (), entry .getValue (), EvictionType .EXPIRED );
273
+ fireOnEviction (entry .getKey (), entry .getValue (), EvictionType .EXPIRED );
274
274
removedAny = true ;
275
275
maxExpiredEntries --;
276
276
}
@@ -283,21 +283,10 @@ public final void close() {
283
283
invalidateAll ();
284
284
}
285
285
286
- /** A {@link EvictionListener} keeps track of size. */
287
- private final class SizeHandlingEvictionListener implements EvictionListener <K , SizedValue > {
288
-
289
- private final EvictionListener <K , V > delegate ;
290
-
291
- SizeHandlingEvictionListener (@ Nullable EvictionListener <K , V > delegate ) {
292
- this .delegate = delegate ;
293
- }
294
-
295
- @ Override
296
- public void onEviction (K key , SizedValue value , EvictionType cause ) {
297
- estimatedSizeBytes -= value .size ;
298
- if (delegate != null ) {
299
- delegate .onEviction (key , value .value , cause );
300
- }
286
+ private void fireOnEviction (K key , SizedValue value , EvictionType cause ) {
287
+ estimatedSizeBytes -= value .size ;
288
+ if (evictionListener != null ) {
289
+ evictionListener .onEviction (key , value .value , cause );
301
290
}
302
291
}
303
292
0 commit comments