InclusiveRange .java

net.minecraft.util.InclusiveRange (record): members: minInclusive, maxInclusive, isValueInRange, INT, map, contains, create, codec. imports: DataResult.

net.minecraft.utilrecord68 linescommonextends Comparable<T>>2.6 KB

API Surface (7)

<T extends Comparable<T>> Codec<InclusiveRange<T>> codec(final Codec<T> elementCodec)
<T extends Comparable<T>> Codec<InclusiveRange<T>> codec(final Codec<T> elementCodec, final T minAllowedInclusive, final T maxAllowedInclusive)
<T extends Comparable<T>> DataResult<InclusiveRange<T>> create(final T minInclusive, final T maxInclusive)
<S extends Comparable<S>> InclusiveRange<S> map(final Function<? super T, ? extends S> mapper)
boolean isValueInRange(final T value)
boolean contains(final InclusiveRange<T> subRange)
String toString()

Fields & Constants (1)

INT

String literals (4)

"min_inclusive"
"max_inclusive"
"Range limit too low, expected at least "
"Range limit too high, expected at most "

Code Graph: Dependencies (1)

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

Full Source

InclusiveRange.java68 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.DataResult;
5
import java.util.function.Function;
6
7
public record InclusiveRange<T extends Comparable<T>>(T minInclusive, T maxInclusive) {
8
	public static final Codec<InclusiveRange<Integer>> INT = codec(Codec.INT);
9
10
	public InclusiveRange {
11
		if (minInclusive.compareTo(maxInclusive) > 0) {
12
			throw new IllegalArgumentException("min_inclusive must be less than or equal to max_inclusive");
13
		}
14
	}
15
16
	public InclusiveRange(final T value) {
17
		this(value, value);
18
	}
19
20
	public static <T extends Comparable<T>> Codec<InclusiveRange<T>> codec(final Codec<T> elementCodec) {
21
		return ExtraCodecs.intervalCodec(
22
			elementCodec, "min_inclusive", "max_inclusive", InclusiveRange::create, InclusiveRange::minInclusive, InclusiveRange::maxInclusive
23
		);
24
	}
25
26
	public static <T extends Comparable<T>> Codec<InclusiveRange<T>> codec(final Codec<T> elementCodec, final T minAllowedInclusive, final T maxAllowedInclusive) {
27
		return codec(elementCodec)
28
			.validate(
29
				value -> {
30
					if (value.minInclusive().compareTo(minAllowedInclusive) < 0) {
31
						return DataResult.error(
32
							() -> "Range limit too low, expected at least " + minAllowedInclusive + " [" + value.minInclusive() + "-" + value.maxInclusive() + "]"
33
						);
34
					} else {
35
						return value.maxInclusive().compareTo(maxAllowedInclusive) > 0
36
							? DataResult.error(
37
								() -> "Range limit too high, expected at most " + maxAllowedInclusive + " [" + value.minInclusive() + "-" + value.maxInclusive() + "]"
38
							)
39
							: DataResult.success(value);
40
					}
41
				}
42
			);
43
	}
44
45
	public static <T extends Comparable<T>> DataResult<InclusiveRange<T>> create(final T minInclusive, final T maxInclusive) {
46
		return minInclusive.compareTo(maxInclusive) <= 0
47
			? DataResult.success(new InclusiveRange<>(minInclusive, maxInclusive))
48
			: DataResult.error(() -> "min_inclusive must be less than or equal to max_inclusive");
49
	}
50
51
	public <S extends Comparable<S>> InclusiveRange<S> map(final Function<? super T, ? extends S> mapper) {
52
		return new InclusiveRange<>((S)mapper.apply(this.minInclusive), (S)mapper.apply(this.maxInclusive));
53
	}
54
55
	public boolean isValueInRange(final T value) {
56
		return value.compareTo(this.minInclusive) >= 0 && value.compareTo(this.maxInclusive) <= 0;
57
	}
58
59
	public boolean contains(final InclusiveRange<T> subRange) {
60
		return subRange.minInclusive().compareTo(this.minInclusive) >= 0 && subRange.maxInclusive.compareTo(this.maxInclusive) <= 0;
61
	}
62
63
	@Override
64
	public String toString() {
65
		return "[" + this.minInclusive + ", " + this.maxInclusive + "]";
66
	}
67
}
68

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