Uploaded image for project: 'mdsal'
  1. mdsal
  2. MDSAL-754

Generate ofName() and ofValue() for enumerations

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Done
    • Icon: Medium Medium
    • 10.0.0
    • None
    • Binding codegen
    • None

      For each enumeration we currently generate following methods:

              /**
               * Return the enumeration member whose {@link #getName()} matches specified value.
               *
               * @param name YANG assigned name
               * @return corresponding Simple item, if present
               * @throws NullPointerException if name is null
               */
              public static Optional<Simple> forName(String name) {
                  return Optional.ofNullable(NAME_MAP.get(Objects.requireNonNull(name)));
              }
      
              /**
               * Return the enumeration member whose {@link #getIntValue()} matches specified value.
               *
               * @param intValue integer value
               * @return corresponding Simple item, or null if no such item exists
               */
              public static Simple forValue(int intValue) {
                  return VALUE_MAP.get(intValue);
              }
      

      This is quite inconsistent: we use Optional for the former and a nullable (unmarked) return for the latter. Fix this up by using properly-marked nullable return in forName().
      Also introduce non-null-returning counterparts, ofName() and ofValue(), which throw an IllegalArgumentException:

              public static @NonNull Simple ofName(String name) {
                  return CodeHelpers.checkEnum(forName(name), name);
              }
      
              public static @NonNull Simple ofValue(int intValue) {
                  return CodeHelpers.checkEnum(forValue(intValue), intValue);
              }
      

            rovarga Robert Varga
            rovarga Robert Varga
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: