Exception safe enum transformation.

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Duplicate
    • Priority: P4
    • None
    • Affects Version/s: 9
    • Component/s: 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();
              }

            Assignee:
            Stuart Marks
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: