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> params) { this.params = params; } public boolean has(ContextKey key) { return this.params.containsKey(key); } public T getOrThrow(ContextKey key) { T object = (T)this.params.get(key); if (object == null) { throw new NoSuchElementException(key.name().toString()); } else { return object; } } @Nullable public T getOptional(ContextKey key) { return (T)this.params.get(key); } @Nullable @Contract("_,!null->!null; _,_->_") public T getOrDefault(ContextKey key, @Nullable T defaultValue) { return (T)this.params.getOrDefault(key, defaultValue); } public static class Builder { private final Map, Object> params = new IdentityHashMap(); public ContextMap.Builder withParameter(ContextKey key, T value) { this.params.put(key, value); return this; } public ContextMap.Builder withOptionalParameter(ContextKey key, @Nullable T value) { if (value == null) { this.params.remove(key); } else { this.params.put(key, value); } return this; } public T getParameter(ContextKey key) { T object = (T)this.params.get(key); if (object == null) { throw new NoSuchElementException(key.name().toString()); } else { return object; } } @Nullable public T getOptionalParameter(ContextKey key) { return (T)this.params.get(key); } 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); } } } } }