SoftCache zu Cache generalisiert
Ein Cache kann nun auch mit `WeakReference`n genutzt werden.
This commit is contained in:
parent
2c40e1ba31
commit
5232057b15
|
@ -1,19 +1,63 @@
|
|||
package schule.ngb.zm.util;
|
||||
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SoftCache<K, V> implements Map<K, V> {
|
||||
/**
|
||||
* Ein Cache ist ein {@link Map} Implementation, die Inhaltsobjekte in einer
|
||||
* {@link Reference} speichert und als Zwischenspeicher für Objekte dienen kann,
|
||||
* deren Erstellung aufwendig ist.
|
||||
* <p>
|
||||
* Für einen Cache ist nicht garantiert, dass ein eingefügtes Objekt beim
|
||||
* nächsten Aufruf noch vorhanden ist, da die Referenz inzwischen vom Garbage
|
||||
* Collector gelöscht worden sein kann.
|
||||
* <p>
|
||||
* Als interne Map wird einen {@link ConcurrentHashMap} verwendet.
|
||||
*
|
||||
* @param <K> Der Typ der Schlüssel.
|
||||
* @param <V> Der Typ der Objekte.
|
||||
*/
|
||||
public final class Cache<K, V> implements Map<K, V> {
|
||||
|
||||
private final Map<K, SoftReference<V>> cache = new ConcurrentHashMap<>();
|
||||
/**
|
||||
* Erstellt einen Cache mit {@link SoftReference} Referenzen.
|
||||
*
|
||||
* @param <K> Der Typ der Schlüssel.
|
||||
* @param <V> Der Typ der Objekte.
|
||||
* @return Ein Cache.
|
||||
*/
|
||||
public static <K, V> Cache<K, V> newSoftCache() {
|
||||
return new Cache<>(SoftReference::new);
|
||||
}
|
||||
|
||||
private final SoftReference<V> NOCACHE = new SoftReference<>(null);
|
||||
/**
|
||||
* Erstellt einen Cache mit {@link WeakReference} Referenzen.
|
||||
*
|
||||
* @param <K> Der Typ der Schlüssel.
|
||||
* @param <V> Der Typ der Objekte.
|
||||
* @return Ein Cache.
|
||||
*/
|
||||
public static <K, V> Cache<K, V> newWeakCache() {
|
||||
return new Cache<>(WeakReference::new);
|
||||
}
|
||||
|
||||
public SoftCache() {
|
||||
|
||||
private final Map<K, Reference<V>> cache = new ConcurrentHashMap<>();
|
||||
|
||||
private final Reference<V> NOCACHE;
|
||||
|
||||
private final Function<V, Reference<V>> refSupplier;
|
||||
|
||||
private Cache( Function<V, Reference<V>> refSupplier ) {
|
||||
this.refSupplier = refSupplier;
|
||||
NOCACHE = refSupplier.apply(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -26,6 +70,10 @@ public class SoftCache<K, V> implements Map<K, V> {
|
|||
return cache.isEmpty();
|
||||
}
|
||||
|
||||
private boolean containsRef( Object key ) {
|
||||
return cache.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey( Object key ) {
|
||||
return cache.containsKey(key) && cache.get(key).get() != null;
|
||||
|
@ -37,10 +85,6 @@ public class SoftCache<K, V> implements Map<K, V> {
|
|||
.anyMatch(( ref ) -> ref.get() != null && ref.get() == value);
|
||||
}
|
||||
|
||||
private boolean containsRef( Object key ) {
|
||||
return cache.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get( Object key ) {
|
||||
if( cache.containsKey(key) ) {
|
||||
|
@ -61,7 +105,7 @@ public class SoftCache<K, V> implements Map<K, V> {
|
|||
public V put( K key, V value ) {
|
||||
if( !isNocache(key) ) {
|
||||
V prev = remove(key);
|
||||
cache.put(key, new SoftReference<>(value));
|
||||
cache.put(key, refSupplier.apply(value));
|
||||
return prev;
|
||||
} else {
|
||||
return null;
|
||||
|
@ -70,7 +114,7 @@ public class SoftCache<K, V> implements Map<K, V> {
|
|||
|
||||
@Override
|
||||
public V remove( Object key ) {
|
||||
SoftReference<V> ref = cache.get(key);
|
||||
Reference<V> ref = cache.get(key);
|
||||
cache.remove(key);
|
||||
|
||||
V prev = null;
|
||||
|
@ -129,12 +173,12 @@ public class SoftCache<K, V> implements Map<K, V> {
|
|||
|
||||
@Override
|
||||
public V getValue() {
|
||||
return SoftCache.this.get(key);
|
||||
return Cache.this.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V setValue( V value ) {
|
||||
return SoftCache.this.put(key, value);
|
||||
return Cache.this.put(key, value);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package schule.ngb.zm.util.io;
|
||||
|
||||
import schule.ngb.zm.util.Cache;
|
||||
import schule.ngb.zm.util.Log;
|
||||
import schule.ngb.zm.util.Validator;
|
||||
|
||||
|
@ -22,7 +23,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
*/
|
||||
public class FontLoader {
|
||||
|
||||
private static final Map<String, Font> fontCache = new ConcurrentHashMap<>();
|
||||
private static final Cache<String, Font> fontCache = Cache.newSoftCache();
|
||||
|
||||
/**
|
||||
* Lädt eine Schrift aus einer Datei.
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package schule.ngb.zm.util.io;
|
||||
|
||||
import schule.ngb.zm.util.Log;
|
||||
import schule.ngb.zm.util.SoftCache;
|
||||
import schule.ngb.zm.util.Cache;
|
||||
import schule.ngb.zm.util.Validator;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
@ -17,7 +17,6 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Eine Helferklasse mit Klassenmethoden, um Bilder zu laden.
|
||||
|
@ -30,7 +29,7 @@ public final class ImageLoader {
|
|||
|
||||
public static boolean caching = true;
|
||||
|
||||
private static final SoftCache<String, BufferedImage> imageCache = new SoftCache<>();
|
||||
private static final Cache<String, BufferedImage> imageCache = Cache.newSoftCache();
|
||||
|
||||
/**
|
||||
* Lädt ein Bild von der angegebenen Quelle {@code source}.
|
||||
|
|
Loading…
Reference in New Issue