mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 09:44:24 +00:00
Mechanical: LazyBoundedCache comes from VolmLib (art.arcane.volmlib.util.cache); Iris's copy and its test moved there
This commit is contained in:
1 parent
08a4f64d9f
commit
c270f79827
3 files changed
+1
-192
No files matched your search
@@ -19,7 +19,7 @@
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.engine.data.cache.LazyBoundedCache;
|
||||
import art.arcane.volmlib.util.cache.LazyBoundedCache;
|
||||
import art.arcane.iris.engine.object.material.IrisBlockData;
|
||||
import art.arcane.iris.engine.object.material.IrisMaterialPalette;
|
||||
import art.arcane.iris.engine.object.prefab.TileData;
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
package art.arcane.iris.engine.data.cache;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
public final class LazyBoundedCache<K, V> {
|
||||
private final int maximumSize;
|
||||
private transient LinkedHashMap<K, V> entries;
|
||||
|
||||
public LazyBoundedCache(int maximumSize) {
|
||||
if (maximumSize <= 0) {
|
||||
throw new IllegalArgumentException("Maximum cache size must be positive");
|
||||
}
|
||||
this.maximumSize = maximumSize;
|
||||
}
|
||||
|
||||
public synchronized V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
||||
K requiredKey = Objects.requireNonNull(key, "Cache key");
|
||||
Function<? super K, ? extends V> requiredMapping = Objects.requireNonNull(mappingFunction, "Cache mapping function");
|
||||
if (entries != null) {
|
||||
V cached = entries.get(requiredKey);
|
||||
if (cached != null) {
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
V computed = requiredMapping.apply(requiredKey);
|
||||
if (computed == null) {
|
||||
return null;
|
||||
}
|
||||
if (entries == null) {
|
||||
entries = new LinkedHashMap<>(maximumSize, 1F, true);
|
||||
}
|
||||
entries.put(requiredKey, computed);
|
||||
if (entries.size() > maximumSize) {
|
||||
Iterator<K> iterator = entries.keySet().iterator();
|
||||
iterator.next();
|
||||
iterator.remove();
|
||||
}
|
||||
return computed;
|
||||
}
|
||||
|
||||
synchronized boolean isInitialized() {
|
||||
return entries != null;
|
||||
}
|
||||
|
||||
synchronized int size() {
|
||||
return entries == null ? 0 : entries.size();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user