package net.minecraft.util.context; import com.google.common.collect.Sets; import java.util.IdentityHashMap; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Nullable; public class ContextMap { private final Map, Object> params; ContextMap(Map, Object> map) { this.params = map; } public boolean has(ContextKey contextKey) { return this.params.containsKey(contextKey); } public T getOrThrow(ContextKey contextKey) { T object = (T)this.params.get(contextKey); if (object == null) { throw new NoSuchElementException(contextKey.name().toString()); } else { return object; } } @Nullable public T getOptional(ContextKey contextKey) { return (T)this.params.get(contextKey); } @Nullable @Contract("_,!null->!null; _,_->_") public T getOrDefault(ContextKey contextKey, @Nullable T object) { return (T)this.params.getOrDefault(contextKey, object); } public static class Builder { private final Map, Object> params = new IdentityHashMap(); public ContextMap.Builder withParameter(ContextKey contextKey, T object) { this.params.put(contextKey, object); return this; } public ContextMap.Builder withOptionalParameter(ContextKey contextKey, @Nullable T object) { if (object == null) { this.params.remove(contextKey); } else { this.params.put(contextKey, object); } return this; } public T getParameter(ContextKey contextKey) { T object = (T)this.params.get(contextKey); if (object == null) { throw new NoSuchElementException(contextKey.name().toString()); } else { return object; } } @Nullable public T getOptionalParameter(ContextKey contextKey) { return (T)this.params.get(contextKey); } public ContextMap create(ContextKeySet contextKeySet) { Set> set = Sets.>difference(this.params.keySet(), contextKeySet.allowed()); if (!set.isEmpty()) { throw new IllegalArgumentException("Parameters not allowed in this parameter set: " + set); } else { Set> set2 = Sets.>difference(contextKeySet.required(), this.params.keySet()); if (!set2.isEmpty()) { throw new IllegalArgumentException("Missing required parameters: " + set2); } else { return new ContextMap(this.params); } } } } }