Adds a new constructor which allows the user to set a maximum size for the cache.
Updates the default cache to have a maximum size of 10_000.
@@ -36,7 +36,7 @@ public final class ImageCache {
* If the font is null.
*/
public ImageCache(final @NonNull Font font) {
- this(font, 3);
+ this(font, 3, 10_000);
}
/**
@@ -46,8 +46,8 @@ public final class ImageCache {
* The font.
*
* @param duration
- * The number of minutes, after the most recent access, that a cached
- * image will be removed from the cache.
+ * The number of minutes, after the most recent access, that a cached image will be removed from
+ * the cache.
*
* @throws NullPointerException
* If the font is null.
@@ -67,6 +67,42 @@ public final class ImageCache {
.build();
}
+ /**
+ * Constructs a new ImageCache.
+ *
+ * @param font
+ * The font.
+ *
+ * @param duration
+ * The number of minutes, after the most recent access, that a cached image will be removed from
+ * the cache.
+ *
+ * @param maximumCapacity
+ * The maximum number of images that can be contained within the cache.
+ *
+ * @throws NullPointerException
+ * If the font is null.
+ *
+ * @throws IllegalArgumentException
+ * If the duration is below 1.
+ */
+ public ImageCache(final @NonNull Font font, final int duration, int maximumCapacity) {
+ if (duration < 1) {
+ throw new IllegalArgumentException("The duration cannot be below 1.");
+ }
+
+ if (maximumCapacity < 500) {
+ maximumCapacity = 500;
+ }
+
+ this.font = font;
+ cachedImages = Caffeine.newBuilder()
+ .initialCapacity(500)
+ .maximumSize(maximumCapacity)
+ .expireAfterAccess(duration, TimeUnit.MINUTES)
+ .build();
+ }
+
/**
* Retrieves a tile image from the cache.
*
