mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-10-12 01:45:18 +02:00
Compare commits
4 Commits
f0a3c65552
...
836571ca95
| Author | SHA1 | Date | |
|---|---|---|---|
| 836571ca95 | |||
| 5232057b15 | |||
| 2c40e1ba31 | |||
| 26e3593f2c |
186
src/main/java/schule/ngb/zm/util/Cache.java
Normal file
186
src/main/java/schule/ngb/zm/util/Cache.java
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
public int size() {
|
||||||
|
return cache.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsValue( Object value ) {
|
||||||
|
return cache.values().stream()
|
||||||
|
.anyMatch(( ref ) -> ref.get() != null && ref.get() == value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V get( Object key ) {
|
||||||
|
if( cache.containsKey(key) ) {
|
||||||
|
return cache.get(key).get();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void nocache( K key ) {
|
||||||
|
cache.put(key, NOCACHE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isNocache( K key ) {
|
||||||
|
return cache.get(key) == NOCACHE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V put( K key, V value ) {
|
||||||
|
if( !isNocache(key) ) {
|
||||||
|
V prev = remove(key);
|
||||||
|
cache.put(key, refSupplier.apply(value));
|
||||||
|
return prev;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V remove( Object key ) {
|
||||||
|
Reference<V> ref = cache.get(key);
|
||||||
|
cache.remove(key);
|
||||||
|
|
||||||
|
V prev = null;
|
||||||
|
if( ref != null ) {
|
||||||
|
prev = ref.get();
|
||||||
|
ref.clear();
|
||||||
|
}
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putAll( Map<? extends K, ? extends V> m ) {
|
||||||
|
for( Entry<? extends K, ? extends V> e : m.entrySet() ) {
|
||||||
|
put(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
cache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<K> keySet() {
|
||||||
|
return cache.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<V> values() {
|
||||||
|
return cache.values().stream()
|
||||||
|
.filter(( ref ) -> ref.get() != null)
|
||||||
|
.map(( ref ) -> ref.get())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Entry<K, V>> entrySet() {
|
||||||
|
return cache.entrySet().stream()
|
||||||
|
.filter(( e ) -> e.getValue() != null && e.getValue().get() != null)
|
||||||
|
.map(( e ) -> new SoftCacheEntryView(e.getKey()))
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
private final class SoftCacheEntryView implements Map.Entry<K, V> {
|
||||||
|
|
||||||
|
private K key;
|
||||||
|
|
||||||
|
public SoftCacheEntryView( K key ) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public K getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V getValue() {
|
||||||
|
return Cache.this.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V setValue( V value ) {
|
||||||
|
return Cache.this.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package schule.ngb.zm.util.io;
|
package schule.ngb.zm.util.io;
|
||||||
|
|
||||||
|
import schule.ngb.zm.util.Cache;
|
||||||
import schule.ngb.zm.util.Log;
|
import schule.ngb.zm.util.Log;
|
||||||
import schule.ngb.zm.util.Validator;
|
import schule.ngb.zm.util.Validator;
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
*/
|
*/
|
||||||
public class FontLoader {
|
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.
|
* Lädt eine Schrift aus einer Datei.
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package schule.ngb.zm.util.io;
|
package schule.ngb.zm.util.io;
|
||||||
|
|
||||||
import schule.ngb.zm.util.Log;
|
import schule.ngb.zm.util.Log;
|
||||||
|
import schule.ngb.zm.util.Cache;
|
||||||
import schule.ngb.zm.util.Validator;
|
import schule.ngb.zm.util.Validator;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
@@ -16,7 +17,6 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.ref.SoftReference;
|
import java.lang.ref.SoftReference;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Eine Helferklasse mit Klassenmethoden, um Bilder zu laden.
|
* Eine Helferklasse mit Klassenmethoden, um Bilder zu laden.
|
||||||
@@ -29,9 +29,7 @@ public final class ImageLoader {
|
|||||||
|
|
||||||
public static boolean caching = true;
|
public static boolean caching = true;
|
||||||
|
|
||||||
private static final Map<String, SoftReference<BufferedImage>> imageCache = new ConcurrentHashMap<>();
|
private static final Cache<String, BufferedImage> imageCache = Cache.newSoftCache();
|
||||||
|
|
||||||
private static final SoftReference<BufferedImage> NOCACHE = new SoftReference<>(null);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lädt ein Bild von der angegebenen Quelle {@code source}.
|
* Lädt ein Bild von der angegebenen Quelle {@code source}.
|
||||||
@@ -145,8 +143,7 @@ public final class ImageLoader {
|
|||||||
* {@code false}.
|
* {@code false}.
|
||||||
*/
|
*/
|
||||||
public static boolean isCached( String name ) {
|
public static boolean isCached( String name ) {
|
||||||
SoftReference<BufferedImage> imgRef = imageCache.get(name);
|
return imageCache.containsKey(name);
|
||||||
return imgRef != null && imgRef != NOCACHE && imgRef.get() != null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -166,7 +163,7 @@ public final class ImageLoader {
|
|||||||
* @param img Das zu speichernde Bild.
|
* @param img Das zu speichernde Bild.
|
||||||
*/
|
*/
|
||||||
private static void putCache( final String name, final BufferedImage img ) {
|
private static void putCache( final String name, final BufferedImage img ) {
|
||||||
imageCache.put(name, new SoftReference<>(img));
|
imageCache.put(name, img);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -180,7 +177,7 @@ public final class ImageLoader {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private static BufferedImage getCache( final String name ) {
|
private static BufferedImage getCache( final String name ) {
|
||||||
return imageCache.get(name).get();
|
return imageCache.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -193,7 +190,7 @@ public final class ImageLoader {
|
|||||||
* @param name Die Quelle des Bildes.
|
* @param name Die Quelle des Bildes.
|
||||||
*/
|
*/
|
||||||
public static void preventCache( final String name ) {
|
public static void preventCache( final String name ) {
|
||||||
imageCache.put(name, NOCACHE);
|
imageCache.nocache(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user