mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-10-12 09:56:33 +02:00
Compare commits
4 Commits
26e3593f2c
...
836571ca95
| Author | SHA1 | Date | |
|---|---|---|---|
| 836571ca95 | |||
| 5232057b15 | |||
| f0a3c65552 | |||
| 2c40e1ba31 |
@@ -59,3 +59,10 @@ tasks.register('jarMP3SPI', Jar) {
|
|||||||
}
|
}
|
||||||
with jar
|
with jar
|
||||||
}
|
}
|
||||||
|
|
||||||
|
task buildAll {
|
||||||
|
dependsOn 'jar'
|
||||||
|
dependsOn 'jarMP3SPI'
|
||||||
|
dependsOn 'sourcesJar'
|
||||||
|
dependsOn 'javadocJar'
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,19 +1,63 @@
|
|||||||
package schule.ngb.zm.util;
|
package schule.ngb.zm.util;
|
||||||
|
|
||||||
|
import java.lang.ref.Reference;
|
||||||
import java.lang.ref.SoftReference;
|
import java.lang.ref.SoftReference;
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
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
|
@Override
|
||||||
@@ -26,6 +70,10 @@ public class SoftCache<K, V> implements Map<K, V> {
|
|||||||
return cache.isEmpty();
|
return cache.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean containsRef( Object key ) {
|
||||||
|
return cache.containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsKey( Object key ) {
|
public boolean containsKey( Object key ) {
|
||||||
return cache.containsKey(key) && cache.get(key).get() != null;
|
return cache.containsKey(key) && cache.get(key).get() != null;
|
||||||
@@ -33,11 +81,8 @@ public class SoftCache<K, V> implements Map<K, V> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsValue( Object value ) {
|
public boolean containsValue( Object value ) {
|
||||||
return false;
|
return cache.values().stream()
|
||||||
}
|
.anyMatch(( ref ) -> ref.get() != null && ref.get() == value);
|
||||||
|
|
||||||
private boolean containsRef( Object key ) {
|
|
||||||
return cache.containsKey(key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -60,7 +105,7 @@ public class SoftCache<K, V> implements Map<K, V> {
|
|||||||
public V put( K key, V value ) {
|
public V put( K key, V value ) {
|
||||||
if( !isNocache(key) ) {
|
if( !isNocache(key) ) {
|
||||||
V prev = remove(key);
|
V prev = remove(key);
|
||||||
cache.put(key, new SoftReference<>(value));
|
cache.put(key, refSupplier.apply(value));
|
||||||
return prev;
|
return prev;
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
@@ -69,7 +114,7 @@ public class SoftCache<K, V> implements Map<K, V> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V remove( Object key ) {
|
public V remove( Object key ) {
|
||||||
SoftReference<V> ref = cache.get(key);
|
Reference<V> ref = cache.get(key);
|
||||||
cache.remove(key);
|
cache.remove(key);
|
||||||
|
|
||||||
V prev = null;
|
V prev = null;
|
||||||
@@ -128,12 +173,12 @@ public class SoftCache<K, V> implements Map<K, V> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V getValue() {
|
public V getValue() {
|
||||||
return SoftCache.this.get(key);
|
return Cache.this.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V setValue( V value ) {
|
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;
|
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