@@ -43,7 +43,8 @@ abstract class LinkedHashLruCache<K, V> implements LruCache<K, V> {
4343
4444 private final LinkedHashMap <K , SizedValue > delegate ;
4545 private final Ticker ticker ;
46- private final EvictionListener <K , SizedValue > evictionListener ;
46+ @ Nullable
47+ private final EvictionListener <K , V > evictionListener ;
4748 private long estimatedSizeBytes ;
4849 private long estimatedMaxSizeBytes ;
4950
@@ -53,7 +54,7 @@ abstract class LinkedHashLruCache<K, V> implements LruCache<K, V> {
5354 final Ticker ticker ) {
5455 checkState (estimatedMaxSizeBytes > 0 , "max estimated cache size should be positive" );
5556 this .estimatedMaxSizeBytes = estimatedMaxSizeBytes ;
56- this .evictionListener = new SizeHandlingEvictionListener ( evictionListener ) ;
57+ this .evictionListener = evictionListener ;
5758 this .ticker = checkNotNull (ticker , "ticker" );
5859 delegate = new LinkedHashMap <K , SizedValue >(
5960 // rough estimate or minimum hashmap default
@@ -135,7 +136,7 @@ public final V cache(K key, V value) {
135136 estimatedSizeBytes += size ;
136137 existing = delegate .put (key , new SizedValue (size , value ));
137138 if (existing != null ) {
138- evictionListener . onEviction (key , existing , EvictionType .REPLACED );
139+ fireOnEviction (key , existing , EvictionType .REPLACED );
139140 }
140141 return existing == null ? null : existing .value ;
141142 }
@@ -174,7 +175,7 @@ private V invalidate(K key, EvictionType cause) {
174175 checkNotNull (cause , "cause" );
175176 SizedValue existing = delegate .remove (key );
176177 if (existing != null ) {
177- evictionListener . onEviction (key , existing , cause );
178+ fireOnEviction (key , existing , cause );
178179 }
179180 return existing == null ? null : existing .value ;
180181 }
@@ -185,7 +186,7 @@ public final void invalidateAll() {
185186 while (iterator .hasNext ()) {
186187 Map .Entry <K , SizedValue > entry = iterator .next ();
187188 if (entry .getValue () != null ) {
188- evictionListener . onEviction (entry .getKey (), entry .getValue (), EvictionType .EXPLICIT );
189+ fireOnEviction (entry .getKey (), entry .getValue (), EvictionType .EXPLICIT );
189190 }
190191 iterator .remove ();
191192 }
@@ -215,23 +216,22 @@ public final List<V> values() {
215216 protected final boolean fitToLimit () {
216217 boolean removedAnyUnexpired = false ;
217218 if (estimatedSizeBytes <= estimatedMaxSizeBytes ) {
218- // new size is larger no need to do cleanup
219219 return false ;
220220 }
221221 // cleanup expired entries
222222 long now = ticker .read ();
223223 cleanupExpiredEntries (now );
224224
225- // cleanup eldest entry until new size limit
225+ // cleanup eldest entry until the size of all entries fits within the limit
226226 Iterator <Map .Entry <K , SizedValue >> lruIter = delegate .entrySet ().iterator ();
227227 while (lruIter .hasNext () && estimatedMaxSizeBytes < this .estimatedSizeBytes ) {
228228 Map .Entry <K , SizedValue > entry = lruIter .next ();
229229 if (!shouldInvalidateEldestEntry (entry .getKey (), entry .getValue ().value , now )) {
230230 break ; // Violates some constraint like minimum age so stop our cleanup
231231 }
232232 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 );
235235 removedAnyUnexpired = true ;
236236 }
237237 return removedAnyUnexpired ;
@@ -270,7 +270,7 @@ private boolean cleanupExpiredEntries(int maxExpiredEntries, long now) {
270270 Map .Entry <K , SizedValue > entry = lruIter .next ();
271271 if (isExpired (entry .getKey (), entry .getValue ().value , now )) {
272272 lruIter .remove ();
273- evictionListener . onEviction (entry .getKey (), entry .getValue (), EvictionType .EXPIRED );
273+ fireOnEviction (entry .getKey (), entry .getValue (), EvictionType .EXPIRED );
274274 removedAny = true ;
275275 maxExpiredEntries --;
276276 }
@@ -283,21 +283,10 @@ public final void close() {
283283 invalidateAll ();
284284 }
285285
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 );
301290 }
302291 }
303292
0 commit comments