ContextMap.java

net.minecraft.util.context.ContextMap

信息

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

    TODO

字段/常量

  • params
    • 类型: Map<ContextKey<?>,Object>
    • 修饰符: private final
    • 源码定位: L12
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.util.context.ContextMap.Builder
    • 类型: class
    • 修饰符: public static
    • 源码定位: L40
    • 说明:

      TODO

构造器

private ContextMap(Map<ContextKey<?>,Object> params) @ L14

  • 构造器名:ContextMap
  • 源码定位:L14
  • 修饰符:private

参数:

  • params: Map<ContextKey<?>,Object>

说明:

TODO

方法

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

public boolean has(ContextKey<?> key) @ L18

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

参数:

  • key: ContextKey<?>

说明:

TODO

public <T> T getOrThrow(ContextKey<T> key) @ L22

  • 方法名:getOrThrow
  • 源码定位:L22
  • 返回类型: T
  • 修饰符:public

参数:

  • key: ContextKey

说明:

TODO

public <T> T getOptional(ContextKey<T> key) @ L31

  • 方法名:getOptional
  • 源码定位:L31
  • 返回类型: T
  • 修饰符:public

参数:

  • key: ContextKey

说明:

TODO

public <T> T getOrDefault(ContextKey<T> param, T _default) @ L35

  • 方法名:getOrDefault
  • 源码定位:L35
  • 返回类型: T
  • 修饰符:public

参数:

  • param: ContextKey
  • _default: T

说明:

TODO

代码

public class ContextMap {
    private final Map<ContextKey<?>, Object> params;
 
    private ContextMap(Map<ContextKey<?>, Object> params) {
        this.params = params;
    }
 
    public boolean has(ContextKey<?> key) {
        return this.params.containsKey(key);
    }
 
    public <T> T getOrThrow(ContextKey<T> key) {
        T value = (T)this.params.get(key);
        if (value == null) {
            throw new NoSuchElementException(key.name().toString());
        } else {
            return value;
        }
    }
 
    public <T> @Nullable T getOptional(ContextKey<T> key) {
        return (T)this.params.get(key);
    }
 
    @Contract("_,!null->!null; _,_->_")
    public <T> @Nullable T getOrDefault(ContextKey<T> param, @Nullable T _default) {
        return (T)this.params.getOrDefault(param, _default);
    }
 
    public static class Builder {
        private final Map<ContextKey<?>, Object> params = new IdentityHashMap<>();
 
        public <T> ContextMap.Builder withParameter(ContextKey<T> param, T value) {
            this.params.put(param, value);
            return this;
        }
 
        public <T> ContextMap.Builder withOptionalParameter(ContextKey<T> param, @Nullable T value) {
            if (value == null) {
                this.params.remove(param);
            } else {
                this.params.put(param, value);
            }
 
            return this;
        }
 
        public <T> T getParameter(ContextKey<T> param) {
            T value = (T)this.params.get(param);
            if (value == null) {
                throw new NoSuchElementException(param.name().toString());
            } else {
                return value;
            }
        }
 
        public <T> @Nullable T getOptionalParameter(ContextKey<T> param) {
            return (T)this.params.get(param);
        }
 
        public ContextMap create(ContextKeySet paramSet) {
            Set<ContextKey<?>> notAllowed = Sets.difference(this.params.keySet(), paramSet.allowed());
            if (!notAllowed.isEmpty()) {
                throw new IllegalArgumentException("Parameters not allowed in this parameter set: " + notAllowed);
            } else {
                Set<ContextKey<?>> missingRequired = Sets.difference(paramSet.required(), this.params.keySet());
                if (!missingRequired.isEmpty()) {
                    throw new IllegalArgumentException("Missing required parameters: " + missingRequired);
                } else {
                    return new ContextMap(this.params);
                }
            }
        }
    }
}

引用的其他类