Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
64 additions
and
46 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,46 +1,46 @@ | |||
package com.lumpundform.utilerias; | package com.lumpundform.utilerias; | ||
|
|
||
import java.util.Collection; | import java.util.Collection; | ||
import java.util.LinkedHashMap; | import java.util.LinkedHashMap; | ||
import java.util.Map; | import java.util.Map; | ||
|
|
||
public class LRUCache<K, V> { | public class LRUCache<K, V> { | ||
public interface CacheEntryRemovedListener<K, V> { | public interface CacheEntryRemovedListener<K, V> { | ||
void notifyEntryRemoved(K key, V value); | void notifyEntryRemoved(K key, V value); | ||
} | } | ||
|
|
||
private Map<K, V> cache; | private Map<K, V> cache; | ||
private CacheEntryRemovedListener<K, V> entryRemovedListener; | private CacheEntryRemovedListener<K, V> entryRemovedListener; | ||
|
|
||
public LRUCache(final int maxEntries) { | public LRUCache(final int maxEntries) { | ||
cache = new LinkedHashMap<K, V>(maxEntries + 1, .75f, true) { | cache = new LinkedHashMap<K, V>(maxEntries + 1, .75f, true) { | ||
public boolean removeEldestEntry(Map.Entry<K, V> eldest) { | public boolean removeEldestEntry(Map.Entry<K, V> eldest) { | ||
if (size() > maxEntries) { | if (size() > maxEntries) { | ||
if (entryRemovedListener != null) { | if (entryRemovedListener != null) { | ||
entryRemovedListener.notifyEntryRemoved( | entryRemovedListener.notifyEntryRemoved( | ||
eldest.getKey(), eldest.getValue()); | eldest.getKey(), eldest.getValue()); | ||
} | } | ||
return true; | return true; | ||
} | } | ||
return false; | return false; | ||
} | } | ||
}; | }; | ||
} | } | ||
|
|
||
public void add(K key, V value) { | public void add(K key, V value) { | ||
cache.put(key, value); | cache.put(key, value); | ||
} | } | ||
|
|
||
public V get(K key) { | public V get(K key) { | ||
return cache.get(key); | return cache.get(key); | ||
} | } | ||
|
|
||
public Collection<V> retrieaveAll() { | public Collection<V> retrieaveAll() { | ||
return cache.values(); | return cache.values(); | ||
} | } | ||
|
|
||
public void setEntryRemovedListener( | public void setEntryRemovedListener( | ||
CacheEntryRemovedListener<K, V> entryRemovedListener) { | CacheEntryRemovedListener<K, V> entryRemovedListener) { | ||
this.entryRemovedListener = entryRemovedListener; | this.entryRemovedListener = entryRemovedListener; | ||
} | } | ||
} | } |