InclusiveRange.java

net.minecraft.util.InclusiveRange

信息

  • 全限定名:net.minecraft.util.InclusiveRange
  • 类型:public record
  • 包:net.minecraft.util
  • 源码路径:src/main/java/net/minecraft/util/InclusiveRange.java
  • 起始行号:L7
  • 职责:

    TODO

字段/常量

  • INT
    • 类型: Codec<InclusiveRange<Integer>>
    • 修饰符: public static final
    • 源码定位: L8
    • 说明:

      TODO

内部类/嵌套类型

构造器

public InclusiveRange(T minInclusive, T maxInclusive) @ L10

  • 构造器名:InclusiveRange
  • 源码定位:L10
  • 修饰符:public

参数:

  • minInclusive: T
  • maxInclusive: T

说明:

TODO

public InclusiveRange(T value) @ L19

  • 构造器名:InclusiveRange
  • 源码定位:L19
  • 修饰符:public

参数:

  • value: T

说明:

TODO

方法

下面的方法块按源码顺序生成。

public static <T extends Comparable<T>> Codec<InclusiveRange<T>> codec(Codec<T> elementCodec) @ L23

  • 方法名:codec
  • 源码定位:L23
  • 返回类型:<T extends Comparable> Codec<InclusiveRange>
  • 修饰符:public static

参数:

  • elementCodec: Codec

说明:

TODO

public static <T extends Comparable<T>> Codec<InclusiveRange<T>> codec(Codec<T> elementCodec, T minAllowedInclusive, T maxAllowedInclusive) @ L29

  • 方法名:codec
  • 源码定位:L29
  • 返回类型:<T extends Comparable> Codec<InclusiveRange>
  • 修饰符:public static

参数:

  • elementCodec: Codec
  • minAllowedInclusive: T
  • maxAllowedInclusive: T

说明:

TODO

public static <T extends Comparable<T>> DataResult<InclusiveRange<T>> create(T minInclusive, T maxInclusive) @ L60

  • 方法名:create
  • 源码定位:L60
  • 返回类型:<T extends Comparable> DataResult<InclusiveRange>
  • 修饰符:public static

参数:

  • minInclusive: T
  • maxInclusive: T

说明:

TODO

public <S extends Comparable<S>> InclusiveRange<S> map(Function<?super T,?extends S> mapper) @ L66

  • 方法名:map
  • 源码定位:L66
  • 返回类型:<S extends Comparable> InclusiveRange
  • 修饰符:public

参数:

  • mapper: Function<?super T,?extends S>

说明:

TODO

public boolean isValueInRange(T value) @ L70

  • 方法名:isValueInRange
  • 源码定位:L70
  • 返回类型:boolean
  • 修饰符:public

参数:

  • value: T

说明:

TODO

public boolean contains(InclusiveRange<T> subRange) @ L74

  • 方法名:contains
  • 源码定位:L74
  • 返回类型:boolean
  • 修饰符:public

参数:

  • subRange: InclusiveRange

说明:

TODO

public String toString() @ L78

  • 方法名:toString
  • 源码定位:L78
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

代码

public record InclusiveRange<T extends Comparable<T>>(T minInclusive, T maxInclusive) {
    public static final Codec<InclusiveRange<Integer>> INT = codec(Codec.INT);
 
    public InclusiveRange(T minInclusive, T maxInclusive) {
        if (minInclusive.compareTo(maxInclusive) > 0) {
            throw new IllegalArgumentException("min_inclusive must be less than or equal to max_inclusive");
        } else {
            this.minInclusive = minInclusive;
            this.maxInclusive = maxInclusive;
        }
    }
 
    public InclusiveRange(T value) {
        this(value, value);
    }
 
    public static <T extends Comparable<T>> Codec<InclusiveRange<T>> codec(Codec<T> elementCodec) {
        return ExtraCodecs.intervalCodec(
            elementCodec, "min_inclusive", "max_inclusive", InclusiveRange::create, InclusiveRange::minInclusive, InclusiveRange::maxInclusive
        );
    }
 
    public static <T extends Comparable<T>> Codec<InclusiveRange<T>> codec(Codec<T> elementCodec, T minAllowedInclusive, T maxAllowedInclusive) {
        return codec(elementCodec)
            .validate(
                value -> {
                    if (value.minInclusive().compareTo(minAllowedInclusive) < 0) {
                        return DataResult.error(
                            () -> "Range limit too low, expected at least "
                                + minAllowedInclusive
                                + " ["
                                + value.minInclusive()
                                + "-"
                                + value.maxInclusive()
                                + "]"
                        );
                    } else {
                        return value.maxInclusive().compareTo(maxAllowedInclusive) > 0
                            ? DataResult.error(
                                () -> "Range limit too high, expected at most "
                                    + maxAllowedInclusive
                                    + " ["
                                    + value.minInclusive()
                                    + "-"
                                    + value.maxInclusive()
                                    + "]"
                            )
                            : DataResult.success(value);
                    }
                }
            );
    }
 
    public static <T extends Comparable<T>> DataResult<InclusiveRange<T>> create(T minInclusive, T maxInclusive) {
        return minInclusive.compareTo(maxInclusive) <= 0
            ? DataResult.success(new InclusiveRange<>(minInclusive, maxInclusive))
            : DataResult.error(() -> "min_inclusive must be less than or equal to max_inclusive");
    }
 
    public <S extends Comparable<S>> InclusiveRange<S> map(Function<? super T, ? extends S> mapper) {
        return new InclusiveRange<>((S)mapper.apply(this.minInclusive), (S)mapper.apply(this.maxInclusive));
    }
 
    public boolean isValueInRange(T value) {
        return value.compareTo(this.minInclusive) >= 0 && value.compareTo(this.maxInclusive) <= 0;
    }
 
    public boolean contains(InclusiveRange<T> subRange) {
        return subRange.minInclusive().compareTo(this.minInclusive) >= 0 && subRange.maxInclusive.compareTo(this.maxInclusive) <= 0;
    }
 
    @Override
    public String toString() {
        return "[" + this.minInclusive + ", " + this.maxInclusive + "]";
    }
}

引用的其他类

  • ExtraCodecs
    • 引用位置: 方法调用
    • 关联成员: ExtraCodecs.intervalCodec()