mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-22 02:25:41 +01:00
Safer null checking with parsing enum value.
This commit is contained in:
parent
b940063104
commit
fee9b95bef
@ -1,6 +1,7 @@
|
|||||||
package com.onarandombox.MultiverseCore.utils;
|
package com.onarandombox.MultiverseCore.utils;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -29,11 +30,16 @@ public class EnumMapping<T extends Enum<T>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Optional<T> parseValue(String value) {
|
public Optional<T> parseValue(@Nullable String value) {
|
||||||
|
if (value == null) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
T enumType = this.enumsMap.get(caseSensitive ? value : value.toLowerCase());
|
T enumType = this.enumsMap.get(caseSensitive ? value : value.toLowerCase());
|
||||||
if (enumType != null) {
|
if (enumType != null) {
|
||||||
return Optional.of(enumType);
|
return Optional.of(enumType);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return Optional.of(Enum.valueOf(enumClass, value));
|
return Optional.of(Enum.valueOf(enumClass, value));
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user