mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-04-14 06:33:34 +02:00
SoftCache in ImageLoader benutzt
This commit is contained in:
@@ -33,7 +33,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 ) {
|
private boolean containsRef( Object key ) {
|
||||||
|
|||||||
@@ -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.SoftCache;
|
||||||
import schule.ngb.zm.util.Validator;
|
import schule.ngb.zm.util.Validator;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
@@ -29,9 +30,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 SoftCache<String, BufferedImage> imageCache = new SoftCache<>();
|
||||||
|
|
||||||
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 +144,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 +164,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 +178,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 +191,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