SingleKeyCache .java

net.minecraft.util.SingleKeyCache (class, 25 lines): members: computeValue, cacheKey, cachedValue, getValue. imports: Nullable.

net.minecraft.utilclass25 linescommon630 B

API Surface (1)

V getValue(final K cacheKey)

Fields & Constants (3)

computeValue
cacheKey
cachedValue

Full Source

SingleKeyCache.java25 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import java.util.Objects;
4
import java.util.function.Function;
5
import org.jspecify.annotations.Nullable;
6
7
public class SingleKeyCache<K, V> {
8
	private final Function<K, V> computeValue;
9
	private @Nullable K cacheKey = (K)null;
10
	private @Nullable V cachedValue;
11
12
	public SingleKeyCache(final Function<K, V> computeValue) {
13
		this.computeValue = computeValue;
14
	}
15
16
	public V getValue(final K cacheKey) {
17
		if (this.cachedValue == null || !Objects.equals(this.cacheKey, cacheKey)) {
18
			this.cachedValue = this.computeValue.apply(cacheKey);
19
			this.cacheKey = cacheKey;
20
		}
21
22
		return this.cachedValue;
23
	}
24
}
25

Decompiled Minecraft 26.3 source of SingleKeyCache (net/minecraft/util/SingleKeyCache.java) with Mojang official mappings, stored verbatim on the site.