Duplicate :
|
A DESCRIPTION OF THE REQUEST : A null or Optional returning variant of valueOf can be handy for Enum transformation. JUSTIFICATION : Currently it is quiet incovenient to transform string to enum. We need to handle java.lang.IllegalArgumentException in case the string can't be converted to a specific enum. EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - Optional<AbcEnum> abc = Enum.optionalOf(AbcEnum.class, null) -> Optional.empty() Optional<AbcEnum> abc = Enum.optionalOf(AbcEnum.class, "1") -> Optional.empty() Optional<AbcEnum> abc = Enum.optionalOf(AbcEnum.class, "A") -> Optional<AbcEnum> ------------------- OR --------------------- Optional.of("1").map(AbcEnum::valueOfNull); -> Optional.empty() Optional.of("A").map(AbcEnum::valueOfNull); -> Optional<AbcEnum> ACTUAL - Optional<AbcEnum_1_0> abcEnum; try { abcEnum = Optional.of("1").map(AbcEnum::valueOf); }catch (IllegalArgumentException iae){ abcEnum = Optional.empty(); }