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