Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8133234

Exception safe enum transformation.

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 9
    • core-libs
    • x86_64
    • windows_7

      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();
              }

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: