minecraft-src/net/minecraft/util/SingleKeyCache.java
2025-07-04 01:41:11 +03:00

26 lines
624 B
Java

package net.minecraft.util;
import java.util.Objects;
import java.util.function.Function;
import org.jetbrains.annotations.Nullable;
public class SingleKeyCache<K, V> {
private final Function<K, V> computeValue;
@Nullable
private K cacheKey = (K)null;
@Nullable
private V cachedValue;
public SingleKeyCache(Function<K, V> computeValue) {
this.computeValue = computeValue;
}
public V getValue(K cacheKey) {
if (this.cachedValue == null || !Objects.equals(this.cacheKey, cacheKey)) {
this.cachedValue = (V)this.computeValue.apply(cacheKey);
this.cacheKey = cacheKey;
}
return this.cachedValue;
}
}