aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java
diff options
context:
space:
mode:
authorRoman / Linnea Gräf <roman.graef@gmail.com>2022-11-26 09:48:51 +0100
committerGitHub <noreply@github.com>2022-11-26 19:48:51 +1100
commitd0617ef3799fe7c91756c1e01a2710572ddb84ec (patch)
tree075bcfa04c5e42b1bd47ea93b95f72fb74d42618 /src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java
parent7db5251c1cf9558b6d310b39a596d94850738c82 (diff)
downloadnotenoughupdates-d0617ef3799fe7c91756c1e01a2710572ddb84ec.tar.gz
notenoughupdates-d0617ef3799fe7c91756c1e01a2710572ddb84ec.tar.bz2
notenoughupdates-d0617ef3799fe7c91756c1e01a2710572ddb84ec.zip
EnchantStyleCustomizer rewrite (#406)
Co-authored-by: nea <romangraef@gmail.com> Co-authored-by: nea <roman.graef@gmail.com> Co-authored-by: nea <nea@nea.moe> Co-authored-by: nea <romangraef@loves.dicksinhisan.us> Co-authored-by: nea <roman.graef@grb-online.net> Co-authored-by: nea <hello@nea89.moe> Co-authored-by: nea <roman.graef@stud.tu-darmstadt.de>
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java b/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java
new file mode 100644
index 00000000..f107d522
--- /dev/null
+++ b/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2022 NotEnoughUpdates contributors
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.util;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.function.IntSupplier;
+
+public interface LRUCache<K, V> extends Function<K, V> {
+
+ static <K, V> LRUCache<K, V> memoize(Function<K, V> mapper, int maxCacheSize) {
+ return memoize(mapper, () -> maxCacheSize);
+ }
+
+ static <K, V> LRUCache<K, V> memoize(Function<K, V> mapper, IntSupplier maxCacheSize) {
+ Map<K, V> cache = new LinkedHashMap<K, V>(10, 0.75F, true) {
+ @Override
+ protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
+ return this.size() > maxCacheSize.getAsInt();
+ }
+ };
+ Map<K, V> synchronizedCache = Collections.synchronizedMap(cache);
+ return new LRUCache<K, V>() {
+ @Override
+ public void clearCache() {
+ synchronizedCache.clear();
+ }
+
+ @Override
+ public int size() {
+ return synchronizedCache.size();
+ }
+
+ @Override
+ public V apply(K k) {
+ return synchronizedCache.computeIfAbsent(k, mapper);
+ }
+ };
+ }
+
+ int size();
+
+ void clearCache();
+}