IdMapper .java

net.minecraft.core.IdMapper (class, 68 lines): members: idToT, tToId, addMapping, nextId, iterator, byId, contains, getId, add, size. imports: Iterators.

net.minecraft.coreclass68 linescommon1.5 KB

Implements

API Surface (6)

void addMapping(final T thing, final int id)
void add(final T thing)
int getId(final T thing)
Iterator<T> iterator()
boolean contains(final int id)
int size()

Fields & Constants (3)

nextId
tToId
idToT

Code Graph: Dependencies (1)

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

Full Source

IdMapper.java68 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.core;
2
3
import com.google.common.collect.Iterators;
4
import com.google.common.collect.Lists;
5
import it.unimi.dsi.fastutil.objects.Reference2IntMap;
6
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
7
import java.util.Iterator;
8
import java.util.List;
9
import java.util.Objects;
10
import org.jspecify.annotations.Nullable;
11
12
public class IdMapper<T> implements IdMap<T> {
13
	private int nextId;
14
	private final Reference2IntMap<T> tToId;
15
	private final List<T> idToT;
16
17
	public IdMapper() {
18
		this(512);
19
	}
20
21
	public IdMapper(final int expectedSize) {
22
		this.idToT = Lists.newArrayListWithExpectedSize(expectedSize);
23
		this.tToId = new Reference2IntOpenHashMap<>(expectedSize);
24
		this.tToId.defaultReturnValue(-1);
25
	}
26
27
	public void addMapping(final T thing, final int id) {
28
		this.tToId.put(thing, id);
29
30
		while (this.idToT.size() <= id) {
31
			this.idToT.add(null);
32
		}
33
34
		this.idToT.set(id, thing);
35
		if (this.nextId <= id) {
36
			this.nextId = id + 1;
37
		}
38
	}
39
40
	public void add(final T thing) {
41
		this.addMapping(thing, this.nextId);
42
	}
43
44
	@Override
45
	public int getId(final T thing) {
46
		return this.tToId.getInt(thing);
47
	}
48
49
	@Override
50
	public final @Nullable T byId(final int id) {
51
		return id >= 0 && id < this.idToT.size() ? this.idToT.get(id) : null;
52
	}
53
54
	@Override
55
	public Iterator<T> iterator() {
56
		return Iterators.filter(this.idToT.iterator(), Objects::nonNull);
57
	}
58
59
	public boolean contains(final int id) {
60
		return this.byId(id) != null;
61
	}
62
63
	@Override
64
	public int size() {
65
		return this.tToId.size();
66
	}
67
}
68

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