Details
-
Improvement
-
Status: Resolved
-
Medium
-
Resolution: Done
-
None
-
None
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);
}
Attachments
Issue Links
- relates to
-
MDSAL-753 Generate a switch expression for enum's forName()/forValue() methods
-
- Resolved
-