JDK-8133234 : Exception safe enum transformation.
  • Type: Enhancement
  • Component: core-libs
  • Affected Version: 9
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2015-08-07
  • Updated: 2017-05-11
  • Resolved: 2017-05-11
Related Reports
Duplicate :  
Description
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();
        }