[MDSAL-754] Generate ofName() and ofValue() for enumerations Created: 23/Apr/22  Updated: 26/Apr/22  Resolved: 26/Apr/22

Status: Resolved
Project: mdsal
Component/s: Binding codegen
Affects Version/s: None
Fix Version/s: 10.0.0

Type: Improvement Priority: Medium
Reporter: Robert Varga Assignee: Robert Varga
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Relates
relates to MDSAL-753 Generate a switch expression for enum... Resolved

 Description   

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

Generated at Wed Feb 07 20:10:53 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.