ByIdMap.java
net.minecraft.util.ByIdMap
信息
- 全限定名:net.minecraft.util.ByIdMap
- 类型:public class
- 包:net.minecraft.util
- 源码路径:src/main/java/net/minecraft/util/ByIdMap.java
- 起始行号:L10
- 职责:
TODO
字段/常量
- 无
内部类/嵌套类型
net.minecraft.util.ByIdMap.OutOfBoundsStrategy- 类型:
enum - 修饰符:
public static - 源码定位:
L80 - 说明:
TODO
- 类型:
构造器
- 无
方法
下面的方法块按源码顺序生成。
private static <T> IntFunction<T> createMap(ToIntFunction<T> idGetter, T[] values) @ L11
- 方法名:createMap
- 源码定位:L11
- 返回类型:
IntFunction - 修饰符:private static
参数:
- idGetter: ToIntFunction
- values: T[]
说明:
TODO
public static <T> IntFunction<T> sparse(ToIntFunction<T> idGetter, T[] values, T _default) @ L29
- 方法名:sparse
- 源码定位:L29
- 返回类型:
IntFunction - 修饰符:public static
参数:
- idGetter: ToIntFunction
- values: T[]
- _default: T
说明:
TODO
private static <T> T[] createSortedArray(ToIntFunction<T> idGetter, T[] values) @ L34
- 方法名:createSortedArray
- 源码定位:L34
- 返回类型:
T[] - 修饰符:private static
参数:
- idGetter: ToIntFunction
- values: T[]
说明:
TODO
public static <T> IntFunction<T> continuous(ToIntFunction<T> idGetter, T[] values, ByIdMap.OutOfBoundsStrategy strategy) @ L66
- 方法名:continuous
- 源码定位:L66
- 返回类型:
IntFunction - 修饰符:public static
参数:
- idGetter: ToIntFunction
- values: T[]
- strategy: ByIdMap.OutOfBoundsStrategy
说明:
TODO
代码
public class ByIdMap {
private static <T> IntFunction<T> createMap(ToIntFunction<T> idGetter, T[] values) {
if (values.length == 0) {
throw new IllegalArgumentException("Empty value list");
} else {
Int2ObjectMap<T> result = new Int2ObjectOpenHashMap<>();
for (T value : values) {
int id = idGetter.applyAsInt(value);
T previous = result.put(id, value);
if (previous != null) {
throw new IllegalArgumentException("Duplicate entry on id " + id + ": current=" + value + ", previous=" + previous);
}
}
return result;
}
}
public static <T> IntFunction<T> sparse(ToIntFunction<T> idGetter, T[] values, T _default) {
IntFunction<T> idToObject = createMap(idGetter, values);
return id -> Objects.requireNonNullElse(idToObject.apply(id), _default);
}
private static <T> T[] createSortedArray(ToIntFunction<T> idGetter, T[] values) {
int length = values.length;
if (length == 0) {
throw new IllegalArgumentException("Empty value list");
} else {
T[] result = (T[])values.clone();
Arrays.fill(result, null);
for (T value : values) {
int id = idGetter.applyAsInt(value);
if (id < 0 || id >= length) {
throw new IllegalArgumentException("Values are not continous, found index " + id + " for value " + value);
}
T previous = result[id];
if (previous != null) {
throw new IllegalArgumentException("Duplicate entry on id " + id + ": current=" + value + ", previous=" + previous);
}
result[id] = value;
}
for (int i = 0; i < length; i++) {
if (result[i] == null) {
throw new IllegalArgumentException("Missing value at index: " + i);
}
}
return result;
}
}
public static <T> IntFunction<T> continuous(ToIntFunction<T> idGetter, T[] values, ByIdMap.OutOfBoundsStrategy strategy) {
T[] sortedValues = createSortedArray(idGetter, values);
int length = sortedValues.length;
return switch (strategy) {
case ZERO -> {
T zeroValue = sortedValues[0];
yield id -> id >= 0 && id < length ? sortedValues[id] : zeroValue;
}
case WRAP -> id -> sortedValues[Mth.positiveModulo(id, length)];
case CLAMP -> id -> sortedValues[Mth.clamp(id, 0, length - 1)];
};
}
public static enum OutOfBoundsStrategy {
ZERO,
WRAP,
CLAMP;
}
}引用的其他类
- Mth
- 引用位置:
方法调用 - 关联成员:
Mth.clamp(), Mth.positiveModulo()
- 引用位置: