ByIdMap .java

net.minecraft.util.ByIdMap (class): members: OutOfBoundsStrategy, continuous, sparse. strings: "Values are not continous, found index ", " for value ".

net.minecraft.utilclass86 linescommon2.5 KB

API Surface (2)

<T> IntFunction<T> sparse(final ToIntFunction<T> idGetter, final T[] values, final T _default)
<T> IntFunction<T> continuous(final ToIntFunction<T> idGetter, final T[] values, final ByIdMap.OutOfBoundsStrategy strategy)

String literals (7)

"Empty value list"
"Duplicate entry on id "
": current="
", previous="
"Values are not continous, found index "
" for value "
"Missing value at index: "

Code Graph: Dependencies (1)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

ByIdMap.java86 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
4
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
5
import java.util.Arrays;
6
import java.util.Objects;
7
import java.util.function.IntFunction;
8
import java.util.function.ToIntFunction;
9
10
public class ByIdMap {
11
	private static <T> IntFunction<T> createMap(final ToIntFunction<T> idGetter, final T[] values) {
12
		if (values.length == 0) {
13
			throw new IllegalArgumentException("Empty value list");
14
		}
15
16
		Int2ObjectMap<T> result = new Int2ObjectOpenHashMap<>();
17
18
		for (T value : values) {
19
			int id = idGetter.applyAsInt(value);
20
			T previous = result.put(id, value);
21
			if (previous != null) {
22
				throw new IllegalArgumentException("Duplicate entry on id " + id + ": current=" + value + ", previous=" + previous);
23
			}
24
		}
25
26
		return result;
27
	}
28
29
	public static <T> IntFunction<T> sparse(final ToIntFunction<T> idGetter, final T[] values, final T _default) {
30
		IntFunction<T> idToObject = createMap(idGetter, values);
31
		return id -> Objects.requireNonNullElse(idToObject.apply(id), _default);
32
	}
33
34
	private static <T> T[] createSortedArray(final ToIntFunction<T> idGetter, final T[] values) {
35
		int length = values.length;
36
		if (length == 0) {
37
			throw new IllegalArgumentException("Empty value list");
38
		}
39
40
		T[] result = (T[])values.clone();
41
		Arrays.fill(result, null);
42
43
		for (T value : values) {
44
			int id = idGetter.applyAsInt(value);
45
			if (id < 0 || id >= length) {
46
				throw new IllegalArgumentException("Values are not continous, found index " + id + " for value " + value);
47
			}
48
49
			T previous = result[id];
50
			if (previous != null) {
51
				throw new IllegalArgumentException("Duplicate entry on id " + id + ": current=" + value + ", previous=" + previous);
52
			}
53
54
			result[id] = value;
55
		}
56
57
		for (int i = 0; i < length; i++) {
58
			if (result[i] == null) {
59
				throw new IllegalArgumentException("Missing value at index: " + i);
60
			}
61
		}
62
63
		return result;
64
	}
65
66
	public static <T> IntFunction<T> continuous(final ToIntFunction<T> idGetter, final T[] values, final ByIdMap.OutOfBoundsStrategy strategy) {
67
		T[] sortedValues = createSortedArray(idGetter, values);
68
		int length = sortedValues.length;
69
70
		return switch (strategy) {
71
			case ZERO -> {
72
				T zeroValue = sortedValues[0];
73
				yield id -> id >= 0 && id < length ? sortedValues[id] : zeroValue;
74
			}
75
			case WRAP -> id -> sortedValues[Mth.positiveModulo(id, length)];
76
			case CLAMP -> id -> sortedValues[Mth.clamp(id, 0, length - 1)];
77
		};
78
	}
79
80
	public enum OutOfBoundsStrategy {
81
		ZERO,
82
		WRAP,
83
		CLAMP;
84
	}
85
}
86

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