Simplify image caching
This commit is contained in:
parent
3c7f2da752
commit
cbc4d1f534
@ -22,6 +22,7 @@ import envoy.util.EnvoyLog;
|
|||||||
public final class IconUtil {
|
public final class IconUtil {
|
||||||
|
|
||||||
private static final HashMap<String, Image> cache = new HashMap<>();
|
private static final HashMap<String, Image> cache = new HashMap<>();
|
||||||
|
private static final HashMap<String, Image> scaledCache = new HashMap<>();
|
||||||
private static final HashMap<String, BufferedImage> awtCache = new HashMap<>();
|
private static final HashMap<String, BufferedImage> awtCache = new HashMap<>();
|
||||||
|
|
||||||
private IconUtil() {}
|
private IconUtil() {}
|
||||||
@ -33,14 +34,7 @@ public final class IconUtil {
|
|||||||
* @return the loaded image
|
* @return the loaded image
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
*/
|
*/
|
||||||
public static Image load(String path) {
|
public static Image load(String path) { return cache.computeIfAbsent(path, p -> new Image(IconUtil.class.getResource(p).toExternalForm())); }
|
||||||
if (cache.containsKey(path)) return cache.get(path);
|
|
||||||
else {
|
|
||||||
final var image = new Image(IconUtil.class.getResource(path).toExternalForm());
|
|
||||||
cache.put(path, image);
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads an image from the resource folder and scales it to the given size.
|
* Loads an image from the resource folder and scales it to the given size.
|
||||||
@ -49,26 +43,15 @@ public final class IconUtil {
|
|||||||
* @param size the size to scale the icon to
|
* @param size the size to scale the icon to
|
||||||
* @return the scaled image
|
* @return the scaled image
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
* @apiNote There is an extraordinarily low chance that there is a size and name
|
|
||||||
* conflict. To achieve that however, you basically have to name your
|
|
||||||
* image intentionally like that. For normal human readable names or
|
|
||||||
* even automatically generated ones it will never occur.
|
|
||||||
*/
|
*/
|
||||||
public static Image load(String path, int size) {
|
public static Image load(String path, int size) {
|
||||||
|
return scaledCache.computeIfAbsent(path + size, p -> new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true));
|
||||||
// Minimizing the risk of size and name conflict
|
|
||||||
final var sizeSpecificString = path + size + "+a* ";
|
|
||||||
if (cache.containsKey(sizeSpecificString)) return cache.get(sizeSpecificString);
|
|
||||||
else {
|
|
||||||
final var image = new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true);
|
|
||||||
cache.put(sizeSpecificString, image);
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a {@code .png} image from the sub-folder {@code /icons/} of the
|
* Loads a {@code .png} image from the sub-folder {@code /icons/} of the
|
||||||
* resource folder.<br>
|
* resource folder.
|
||||||
|
* <p>
|
||||||
* The suffix {@code .png} is automatically appended.
|
* The suffix {@code .png} is automatically appended.
|
||||||
*
|
*
|
||||||
* @param name the image name without the .png suffix
|
* @param name the image name without the .png suffix
|
||||||
@ -160,19 +143,14 @@ public final class IconUtil {
|
|||||||
* @since Envoy Client v0.2-beta
|
* @since Envoy Client v0.2-beta
|
||||||
*/
|
*/
|
||||||
public static BufferedImage loadAWTCompatible(String path) {
|
public static BufferedImage loadAWTCompatible(String path) {
|
||||||
if (awtCache.containsKey(path)) return awtCache.get(path);
|
return awtCache.computeIfAbsent(path, p -> {
|
||||||
else {
|
|
||||||
BufferedImage image = null;
|
|
||||||
try {
|
try {
|
||||||
image = ImageIO.read(IconUtil.class.getResource(path));
|
return ImageIO.read(IconUtil.class.getResource(path));
|
||||||
} catch (final IOException e) {
|
} catch (IOException e) {
|
||||||
EnvoyLog.getLogger(IconUtil.class).log(Level.WARNING, String.format("Could not load image at path %s: ", path), e);
|
EnvoyLog.getLogger(IconUtil.class).log(Level.WARNING, String.format("Could not load image at path %s: ", path), e);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
// If an IOException occurs we don't assume it'll be fixed on the next call
|
|
||||||
awtCache.put(path, image);
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user