What you need to do is use something else than NULL, and analyze this case differently:
public enum Type {
CSV, EXCEL, PDF, URL, NONE;
public static Type from(String text) {
if (text == null) {
return NONE;
} else {
return valueOf(text.toUpperCase());
}
}
}
Or better yet, use the options:
public enum Type {
CSV, EXCEL, PDF, URL;
public static Optional<Type> from(String text) {
return Optional.ofNullable(text)
.map(String::toUpperCase)
.map(Type::valueOf);
}
}
source
share