Add EnumObject::getOrThrow

Saves calls to Object::requireNonNull. If nullable is allowed, use get as before
This commit is contained in:
KnightMiner committed 2020-07-28 02:34:36 -05:00
1 parent 5c26722425
commit aee55e8dff
1 file changed
+15
@@ -10,6 +10,7 @@ import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@@ -53,6 +54,20 @@ public class EnumObject<T extends Enum<T>, I extends IForgeRegistryEntry<? super
return supplier.get();
}
/**
* Gets the value for the given enum, or throws an exception if missing
* @param value Key to get
* @return Value, or null if missing
* @throws NoSuchElementException If the key is not defined
*/
public I getOrThrow(T value) {
Supplier<? extends I> supplier = map.get(value);
if (supplier == null) {
throw new NoSuchElementException("Missing key " + value);
}
return supplier.get();
}
/**
* Checks if this enum object contains the given value
* @param value Value to check for