minecraft-src/net/minecraft/util/context/ContextMap.java
2025-07-04 02:49:36 +03:00

88 lines
2.3 KiB
Java

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<ContextKey<?>, Object> params;
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 object = (T)this.params.get(key);
if (object == null) {
throw new NoSuchElementException(key.name().toString());
} else {
return object;
}
}
@Nullable
public <T> T getOptional(ContextKey<T> key) {
return (T)this.params.get(key);
}
@Nullable
@Contract("_,!null->!null; _,_->_")
public <T> T getOrDefault(ContextKey<T> key, @Nullable T defaultValue) {
return (T)this.params.getOrDefault(key, defaultValue);
}
public static class Builder {
private final Map<ContextKey<?>, Object> params = new IdentityHashMap();
public <T> ContextMap.Builder withParameter(ContextKey<T> key, T value) {
this.params.put(key, value);
return this;
}
public <T> ContextMap.Builder withOptionalParameter(ContextKey<T> key, @Nullable T value) {
if (value == null) {
this.params.remove(key);
} else {
this.params.put(key, value);
}
return this;
}
public <T> T getParameter(ContextKey<T> key) {
T object = (T)this.params.get(key);
if (object == null) {
throw new NoSuchElementException(key.name().toString());
} else {
return object;
}
}
@Nullable
public <T> T getOptionalParameter(ContextKey<T> key) {
return (T)this.params.get(key);
}
public ContextMap create(ContextKeySet contextKeySet) {
Set<ContextKey<?>> set = Sets.<ContextKey<?>>difference(this.params.keySet(), contextKeySet.allowed());
if (!set.isEmpty()) {
throw new IllegalArgumentException("Parameters not allowed in this parameter set: " + set);
} else {
Set<ContextKey<?>> set2 = Sets.<ContextKey<?>>difference(contextKeySet.required(), this.params.keySet());
if (!set2.isEmpty()) {
throw new IllegalArgumentException("Missing required parameters: " + set2);
} else {
return new ContextMap(this.params);
}
}
}
}
}