HashOps .java

net.minecraft.util.HashOps (class): members: UNSUPPORTED_OPERATION_ERROR, TAG_BYTE_ARRAY_START, TAG_LONG_ARRAY_START, MAPLIKE_ENTRY_ORDER, CRC32C_INSTANCE.

net.minecraft.utilclass344 linescommon11.4 KB

Implements

API Surface (51)

HashCode empty()
HashCode emptyMap()
HashCode emptyList()
HashCode createNumeric(final Number value)
HashCode createByte(final byte value)
HashCode createShort(final short value)
HashCode createInt(final int value)
HashCode createLong(final long value)
HashCode createFloat(final float value)
HashCode createDouble(final double value)
HashCode createString(final String value)
HashCode createBoolean(final boolean value)
HashCode createMap(final Stream<Pair<HashCode, HashCode>> map)
HashCode createMap(final Map<HashCode, HashCode> map)
HashCode createList(final Stream<HashCode> input)
HashCode createByteList(final ByteBuffer input)
HashCode createIntList(final IntStream input)
HashCode createLongList(final LongStream input)
HashCode remove(final HashCode input, final String key)
RecordBuilder<HashCode> mapBuilder()
ListBuilder<HashCode> listBuilder()
String toString()
<U> U convertTo(final DynamicOps<U> outOps, final HashCode input)
Number getNumberValue(final HashCode input, final Number defaultValue)
HashCode set(final HashCode input, final String key, final HashCode value)
HashCode update(final HashCode input, final String key, final Function<HashCode, HashCode> function)
HashCode updateGeneric(final HashCode input, final HashCode key, final Function<HashCode, HashCode> function)
DataResult<HashCode> get(final HashCode input, final String key)
DataResult<HashCode> getGeneric(final HashCode input, final HashCode key)
DataResult<Number> getNumberValue(final HashCode input)
DataResult<Boolean> getBooleanValue(final HashCode input)
DataResult<String> getStringValue(final HashCode input)
DataResult<HashCode> mergeToList(final HashCode prefix, final HashCode value)
DataResult<HashCode> mergeToList(final HashCode prefix, final List<HashCode> values)
DataResult<HashCode> mergeToMap(final HashCode prefix, final HashCode key, final HashCode value)
DataResult<HashCode> mergeToMap(final HashCode prefix, final Map<HashCode, HashCode> values)
DataResult<HashCode> mergeToMap(final HashCode prefix, final MapLike<HashCode> values)
DataResult<Stream<Pair<HashCode, HashCode>>> getMapValues(final HashCode input)
DataResult<Consumer<BiConsumer<HashCode, HashCode>>> getMapEntries(final HashCode input)
DataResult<Stream<HashCode>> getStream(final HashCode input)
DataResult<Consumer<Consumer<HashCode>>> getList(final HashCode input)
DataResult<MapLike<HashCode>> getMap(final HashCode input)
DataResult<ByteBuffer> getByteBuffer(final HashCode input)
DataResult<IntStream> getIntStream(final HashCode input)
DataResult<LongStream> getLongStream(final HashCode input)
Hasher initBuilder()
Hasher append(final Hasher hasher, final HashCode value)
DataResult<HashCode> build(final Hasher hasher, final HashCode prefix)
List<Pair<HashCode, HashCode>> initBuilder()
List<Pair<HashCode, HashCode>> append(final HashCode key, final HashCode value, final List<Pair<HashCode, HashCode>> builder)
DataResult<HashCode> build(final List<Pair<HashCode, HashCode>> builder, final HashCode prefix)

Fields & Constants (35)

TAG_EMPTY
TAG_MAP_START
TAG_MAP_END
TAG_LIST_START
TAG_LIST_END
TAG_BYTE
TAG_SHORT
TAG_INT
TAG_LONG
TAG_FLOAT
TAG_DOUBLE
TAG_STRING
TAG_BOOLEAN
TAG_BYTE_ARRAY_START
TAG_BYTE_ARRAY_END
TAG_INT_ARRAY_START
TAG_INT_ARRAY_END
TAG_LONG_ARRAY_START
TAG_LONG_ARRAY_END
EMPTY_PAYLOAD
FALSE_PAYLOAD
TRUE_PAYLOAD
EMPTY_MAP_PAYLOAD
EMPTY_LIST_PAYLOAD
UNSUPPORTED_OPERATION_ERROR
HASH_COMPARATOR
MAP_ENTRY_ORDER
MAPLIKE_ENTRY_ORDER
CRC32C_INSTANCE
hashFunction
empty
emptyMap
emptyList
trueHash
falseHash

String literals (3)

"Unsupported operation"
"Hash "
"Can't convert from this type"

Code Graph: Dependencies (1)

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

Full Source

HashOps.java344 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import com.google.common.hash.HashCode;
4
import com.google.common.hash.HashFunction;
5
import com.google.common.hash.Hasher;
6
import com.google.common.hash.Hashing;
7
import com.mojang.datafixers.util.Pair;
8
import com.mojang.serialization.DataResult;
9
import com.mojang.serialization.DynamicOps;
10
import com.mojang.serialization.ListBuilder;
11
import com.mojang.serialization.MapLike;
12
import com.mojang.serialization.RecordBuilder;
13
import com.mojang.serialization.RecordBuilder.AbstractUniversalBuilder;
14
import java.nio.ByteBuffer;
15
import java.util.ArrayList;
16
import java.util.Comparator;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.Map.Entry;
20
import java.util.function.BiConsumer;
21
import java.util.function.Consumer;
22
import java.util.function.Function;
23
import java.util.stream.IntStream;
24
import java.util.stream.LongStream;
25
import java.util.stream.Stream;
26
27
public class HashOps implements DynamicOps<HashCode> {
28
	private static final byte TAG_EMPTY = 1;
29
	private static final byte TAG_MAP_START = 2;
30
	private static final byte TAG_MAP_END = 3;
31
	private static final byte TAG_LIST_START = 4;
32
	private static final byte TAG_LIST_END = 5;
33
	private static final byte TAG_BYTE = 6;
34
	private static final byte TAG_SHORT = 7;
35
	private static final byte TAG_INT = 8;
36
	private static final byte TAG_LONG = 9;
37
	private static final byte TAG_FLOAT = 10;
38
	private static final byte TAG_DOUBLE = 11;
39
	private static final byte TAG_STRING = 12;
40
	private static final byte TAG_BOOLEAN = 13;
41
	private static final byte TAG_BYTE_ARRAY_START = 14;
42
	private static final byte TAG_BYTE_ARRAY_END = 15;
43
	private static final byte TAG_INT_ARRAY_START = 16;
44
	private static final byte TAG_INT_ARRAY_END = 17;
45
	private static final byte TAG_LONG_ARRAY_START = 18;
46
	private static final byte TAG_LONG_ARRAY_END = 19;
47
	private static final byte[] EMPTY_PAYLOAD = new byte[]{1};
48
	private static final byte[] FALSE_PAYLOAD = new byte[]{13, 0};
49
	private static final byte[] TRUE_PAYLOAD = new byte[]{13, 1};
50
	public static final byte[] EMPTY_MAP_PAYLOAD = new byte[]{2, 3};
51
	public static final byte[] EMPTY_LIST_PAYLOAD = new byte[]{4, 5};
52
	private static final DataResult<Object> UNSUPPORTED_OPERATION_ERROR = DataResult.error(() -> "Unsupported operation");
53
	private static final Comparator<HashCode> HASH_COMPARATOR = Comparator.comparingLong(HashCode::padToLong);
54
	private static final Comparator<Entry<HashCode, HashCode>> MAP_ENTRY_ORDER = Entry.<HashCode, HashCode>comparingByKey(HASH_COMPARATOR)
55
		.thenComparing(Entry.comparingByValue(HASH_COMPARATOR));
56
	private static final Comparator<Pair<HashCode, HashCode>> MAPLIKE_ENTRY_ORDER = Comparator.comparing(Pair::getFirst, HASH_COMPARATOR)
57
		.thenComparing(Pair::getSecond, HASH_COMPARATOR);
58
	public static final HashOps CRC32C_INSTANCE = new HashOps(Hashing.crc32c());
59
	private final HashFunction hashFunction;
60
	private final HashCode empty;
61
	private final HashCode emptyMap;
62
	private final HashCode emptyList;
63
	private final HashCode trueHash;
64
	private final HashCode falseHash;
65
66
	public HashOps(final HashFunction hashFunction) {
67
		this.hashFunction = hashFunction;
68
		this.empty = hashFunction.hashBytes(EMPTY_PAYLOAD);
69
		this.emptyMap = hashFunction.hashBytes(EMPTY_MAP_PAYLOAD);
70
		this.emptyList = hashFunction.hashBytes(EMPTY_LIST_PAYLOAD);
71
		this.falseHash = hashFunction.hashBytes(FALSE_PAYLOAD);
72
		this.trueHash = hashFunction.hashBytes(TRUE_PAYLOAD);
73
	}
74
75
	public HashCode empty() {
76
		return this.empty;
77
	}
78
79
	public HashCode emptyMap() {
80
		return this.emptyMap;
81
	}
82
83
	public HashCode emptyList() {
84
		return this.emptyList;
85
	}
86
87
	public HashCode createNumeric(final Number value) {
88
		return switch (value) {
89
			case Byte v -> this.createByte(v);
90
			case Short v -> this.createShort(v);
91
			case Integer v -> this.createInt(v);
92
			case Long v -> this.createLong(v);
93
			case Double v -> this.createDouble(v);
94
			case Float v -> this.createFloat(v);
95
			default -> this.createDouble(value.doubleValue());
96
		};
97
	}
98
99
	public HashCode createByte(final byte value) {
100
		return this.hashFunction.newHasher(2).putByte((byte)6).putByte(value).hash();
101
	}
102
103
	public HashCode createShort(final short value) {
104
		return this.hashFunction.newHasher(3).putByte((byte)7).putShort(value).hash();
105
	}
106
107
	public HashCode createInt(final int value) {
108
		return this.hashFunction.newHasher(5).putByte((byte)8).putInt(value).hash();
109
	}
110
111
	public HashCode createLong(final long value) {
112
		return this.hashFunction.newHasher(9).putByte((byte)9).putLong(value).hash();
113
	}
114
115
	public HashCode createFloat(final float value) {
116
		return this.hashFunction.newHasher(5).putByte((byte)10).putFloat(value).hash();
117
	}
118
119
	public HashCode createDouble(final double value) {
120
		return this.hashFunction.newHasher(9).putByte((byte)11).putDouble(value).hash();
121
	}
122
123
	public HashCode createString(final String value) {
124
		return this.hashFunction.newHasher().putByte((byte)12).putInt(value.length()).putUnencodedChars(value).hash();
125
	}
126
127
	public HashCode createBoolean(final boolean value) {
128
		return value ? this.trueHash : this.falseHash;
129
	}
130
131
	private static Hasher hashMap(final Hasher hasher, final Map<HashCode, HashCode> map) {
132
		hasher.putByte((byte)2);
133
		map.entrySet().stream().sorted(MAP_ENTRY_ORDER).forEach(e -> hasher.putBytes(e.getKey().asBytes()).putBytes(e.getValue().asBytes()));
134
		hasher.putByte((byte)3);
135
		return hasher;
136
	}
137
138
	private static Hasher hashMap(final Hasher hasher, final Stream<Pair<HashCode, HashCode>> map) {
139
		hasher.putByte((byte)2);
140
		map.sorted(MAPLIKE_ENTRY_ORDER).forEach(e -> hasher.putBytes(e.getFirst().asBytes()).putBytes(e.getSecond().asBytes()));
141
		hasher.putByte((byte)3);
142
		return hasher;
143
	}
144
145
	public HashCode createMap(final Stream<Pair<HashCode, HashCode>> map) {
146
		return hashMap(this.hashFunction.newHasher(), map).hash();
147
	}
148
149
	public HashCode createMap(final Map<HashCode, HashCode> map) {
150
		return hashMap(this.hashFunction.newHasher(), map).hash();
151
	}
152
153
	public HashCode createList(final Stream<HashCode> input) {
154
		Hasher hasher = this.hashFunction.newHasher();
155
		hasher.putByte((byte)4);
156
		input.forEach(value -> hasher.putBytes(value.asBytes()));
157
		hasher.putByte((byte)5);
158
		return hasher.hash();
159
	}
160
161
	public HashCode createByteList(final ByteBuffer input) {
162
		Hasher hasher = this.hashFunction.newHasher();
163
		hasher.putByte((byte)14);
164
		hasher.putBytes(input);
165
		hasher.putByte((byte)15);
166
		return hasher.hash();
167
	}
168
169
	public HashCode createIntList(final IntStream input) {
170
		Hasher hasher = this.hashFunction.newHasher();
171
		hasher.putByte((byte)16);
172
		input.forEach(hasher::putInt);
173
		hasher.putByte((byte)17);
174
		return hasher.hash();
175
	}
176
177
	public HashCode createLongList(final LongStream input) {
178
		Hasher hasher = this.hashFunction.newHasher();
179
		hasher.putByte((byte)18);
180
		input.forEach(hasher::putLong);
181
		hasher.putByte((byte)19);
182
		return hasher.hash();
183
	}
184
185
	public HashCode remove(final HashCode input, final String key) {
186
		return input;
187
	}
188
189
	@Override
190
	public RecordBuilder<HashCode> mapBuilder() {
191
		return new HashOps.MapHashBuilder();
192
	}
193
194
	@Override
195
	public ListBuilder<HashCode> listBuilder() {
196
		return new HashOps.ListHashBuilder();
197
	}
198
199
	@Override
200
	public String toString() {
201
		return "Hash " + this.hashFunction;
202
	}
203
204
	public <U> U convertTo(final DynamicOps<U> outOps, final HashCode input) {
205
		throw new UnsupportedOperationException("Can't convert from this type");
206
	}
207
208
	public Number getNumberValue(final HashCode input, final Number defaultValue) {
209
		return defaultValue;
210
	}
211
212
	public HashCode set(final HashCode input, final String key, final HashCode value) {
213
		return input;
214
	}
215
216
	public HashCode update(final HashCode input, final String key, final Function<HashCode, HashCode> function) {
217
		return input;
218
	}
219
220
	public HashCode updateGeneric(final HashCode input, final HashCode key, final Function<HashCode, HashCode> function) {
221
		return input;
222
	}
223
224
	private static <T> DataResult<T> unsupported() {
225
		return (DataResult<T>)UNSUPPORTED_OPERATION_ERROR;
226
	}
227
228
	public DataResult<HashCode> get(final HashCode input, final String key) {
229
		return unsupported();
230
	}
231
232
	public DataResult<HashCode> getGeneric(final HashCode input, final HashCode key) {
233
		return unsupported();
234
	}
235
236
	public DataResult<Number> getNumberValue(final HashCode input) {
237
		return unsupported();
238
	}
239
240
	public DataResult<Boolean> getBooleanValue(final HashCode input) {
241
		return unsupported();
242
	}
243
244
	public DataResult<String> getStringValue(final HashCode input) {
245
		return unsupported();
246
	}
247
248
	private boolean isEmpty(final HashCode value) {
249
		return value.equals(this.empty);
250
	}
251
252
	public DataResult<HashCode> mergeToList(final HashCode prefix, final HashCode value) {
253
		return this.isEmpty(prefix) ? DataResult.success(this.createList(Stream.of(value))) : unsupported();
254
	}
255
256
	public DataResult<HashCode> mergeToList(final HashCode prefix, final List<HashCode> values) {
257
		return this.isEmpty(prefix) ? DataResult.success(this.createList(values.stream())) : unsupported();
258
	}
259
260
	public DataResult<HashCode> mergeToMap(final HashCode prefix, final HashCode key, final HashCode value) {
261
		return this.isEmpty(prefix) ? DataResult.success(this.createMap(Map.of(key, value))) : unsupported();
262
	}
263
264
	public DataResult<HashCode> mergeToMap(final HashCode prefix, final Map<HashCode, HashCode> values) {
265
		return this.isEmpty(prefix) ? DataResult.success(this.createMap(values)) : unsupported();
266
	}
267
268
	public DataResult<HashCode> mergeToMap(final HashCode prefix, final MapLike<HashCode> values) {
269
		return this.isEmpty(prefix) ? DataResult.success(this.createMap(values.entries())) : unsupported();
270
	}
271
272
	public DataResult<Stream<Pair<HashCode, HashCode>>> getMapValues(final HashCode input) {
273
		return unsupported();
274
	}
275
276
	public DataResult<Consumer<BiConsumer<HashCode, HashCode>>> getMapEntries(final HashCode input) {
277
		return unsupported();
278
	}
279
280
	public DataResult<Stream<HashCode>> getStream(final HashCode input) {
281
		return unsupported();
282
	}
283
284
	public DataResult<Consumer<Consumer<HashCode>>> getList(final HashCode input) {
285
		return unsupported();
286
	}
287
288
	public DataResult<MapLike<HashCode>> getMap(final HashCode input) {
289
		return unsupported();
290
	}
291
292
	public DataResult<ByteBuffer> getByteBuffer(final HashCode input) {
293
		return unsupported();
294
	}
295
296
	public DataResult<IntStream> getIntStream(final HashCode input) {
297
		return unsupported();
298
	}
299
300
	public DataResult<LongStream> getLongStream(final HashCode input) {
301
		return unsupported();
302
	}
303
304
	private class ListHashBuilder extends AbstractListBuilder<HashCode, Hasher> {
305
		public ListHashBuilder() {
306
			super(HashOps.this);
307
		}
308
309
		protected Hasher initBuilder() {
310
			return HashOps.this.hashFunction.newHasher().putByte((byte)4);
311
		}
312
313
		protected Hasher append(final Hasher hasher, final HashCode value) {
314
			return hasher.putBytes(value.asBytes());
315
		}
316
317
		protected DataResult<HashCode> build(final Hasher hasher, final HashCode prefix) {
318
			assert prefix.equals(HashOps.this.empty);
319
			hasher.putByte((byte)5);
320
			return DataResult.success(hasher.hash());
321
		}
322
	}
323
324
	private final class MapHashBuilder extends AbstractUniversalBuilder<HashCode, List<Pair<HashCode, HashCode>>> {
325
		public MapHashBuilder() {
326
			super(HashOps.this);
327
		}
328
329
		protected List<Pair<HashCode, HashCode>> initBuilder() {
330
			return new ArrayList<>();
331
		}
332
333
		protected List<Pair<HashCode, HashCode>> append(final HashCode key, final HashCode value, final List<Pair<HashCode, HashCode>> builder) {
334
			builder.add(Pair.of(key, value));
335
			return builder;
336
		}
337
338
		protected DataResult<HashCode> build(final List<Pair<HashCode, HashCode>> builder, final HashCode prefix) {
339
			assert HashOps.this.isEmpty(prefix);
340
			return DataResult.success(HashOps.hashMap(HashOps.this.hashFunction.newHasher(), builder.stream()).hash());
341
		}
342
	}
343
}
344

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