From fee9b95befb1e1206c973eb0f3afcfbd28e3c8f2 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Thu, 22 Jul 2021 11:42:54 +0800 Subject: [PATCH] Safer null checking with parsing enum value. --- .../onarandombox/MultiverseCore/utils/EnumMapping.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/EnumMapping.java b/src/main/java/com/onarandombox/MultiverseCore/utils/EnumMapping.java index 21b29339..eeccd5dd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/EnumMapping.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/EnumMapping.java @@ -1,6 +1,7 @@ package com.onarandombox.MultiverseCore.utils; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Collections; @@ -29,11 +30,16 @@ public class EnumMapping> { } @NotNull - public Optional parseValue(String value) { + public Optional parseValue(@Nullable String value) { + if (value == null) { + return Optional.empty(); + } + T enumType = this.enumsMap.get(caseSensitive ? value : value.toLowerCase()); if (enumType != null) { return Optional.of(enumType); } + try { return Optional.of(Enum.valueOf(enumClass, value)); } catch (IllegalArgumentException e) {