import com.google.common.collect.ImmutableMap
import com.google.common.collect.ImmutableMap.Builder
-import java.util.Optional
import org.opendaylight.mdsal.binding.model.api.Enumeration
import org.opendaylight.mdsal.binding.model.api.GeneratedType
VALUE_MAP = vb.build();
}
- private final «STRING.importedName» name;
+ private final «STRING.importedNonNull» name;
private final int value;
- private «enums.name»(int value, «STRING.importedName» name) {
+ private «enums.name»(int value, «STRING.importedNonNull» name) {
this.value = value;
this.name = name;
}
@«OVERRIDE.importedName»
- public «STRING.importedName» getName() {
+ public «STRING.importedNonNull» getName() {
return name;
}
}
/**
- * Return the enumeration member whose {@link #getName()} matches specified value.
+ * Return the enumeration member whose {@link #getName()} matches specified assigned name.
*
* @param name YANG assigned name
- * @return corresponding «enums.name» item, if present
- * @throws NullPointerException if name is null
+ * @return corresponding «enums.name» item, or {@code null} if no such item exists
+ * @throws NullPointerException if {@code name} is null
*/
- public static «Optional.importedName»<«enums.name»> forName(«STRING.importedName» name) {
- return «Optional.importedName».ofNullable(NAME_MAP.get(«JU_OBJECTS.importedName».requireNonNull(name)));
+ public static «enums.importedNullable» forName(«STRING.importedName» name) {
+ return NAME_MAP.get(«JU_OBJECTS.importedName».requireNonNull(name));
}
/**
* Return the enumeration member whose {@link #getIntValue()} matches specified value.
*
* @param intValue integer value
- * @return corresponding «enums.name» item, or null if no such item exists
+ * @return corresponding «enums.name» item, or {@code null} if no such item exists
*/
- public static «enums.name» forValue(int intValue) {
+ public static «enums.importedNullable» forValue(int intValue) {
return VALUE_MAP.get(intValue);
}
+
+ /**
+ * Return the enumeration member whose {@link #getName()} matches specified assigned name.
+ *
+ * @param name YANG assigned name
+ * @return corresponding «enums.name» item
+ * @throws NullPointerException if {@code name} is null
+ * @throws IllegalArgumentException if {@code name} does not match any item
+ */
+ public static «enums.importedNonNull» ofName(«STRING.importedName» name) {
+ return «CODEHELPERS.importedName».checkEnum(forName(name), name);
+ }
+
+ /**
+ * Return the enumeration member whose {@link #getIntValue()} matches specified value.
+ *
+ * @param intValue integer value
+ * @return corresponding «enums.name» item
+ * @throws IllegalArgumentException if {@code intValue} does not match any item
+ */
+ public static «enums.importedNonNull» ofValue(int intValue) {
+ return «CODEHELPERS.importedName».checkEnum(forValue(intValue), intValue);
+ }
}
'''
return hash == 0 ? 31 : hash;
}
+ /**
+ * Check that the specified {@link Enumeration} object is not {@code null}. This method is meant to be used with
+ * {@code ofName(String)} and {@code ofValue(int)} static factory methods.
+ *
+ * @param obj enumeration object, possibly null
+ * @param name User-supplied enumeration name
+ * @return Enumeration object
+ * @throws IllegalArgumentException if {@code obj} is null
+ */
+ public static <T extends Enumeration> @NonNull T checkEnum(final @Nullable T obj, final String name) {
+ if (obj == null) {
+ throw new IllegalArgumentException("\"" + name + "\" is not a valid name");
+ }
+ return obj;
+ }
+
+ /**
+ * Check that the specified {@link Enumeration} object is not {@code null}. This method is meant to be used with
+ * {@code ofName(String)} and {@code ofValue(int)} static factory methods.
+ *
+ * @param obj enumeration object, possibly null
+ * @param value User-supplied enumeration value
+ * @return Enumeration object
+ * @throws IllegalArgumentException if {@code obj} is null
+ */
+ public static <T extends Enumeration> @NonNull T checkEnum(final @Nullable T obj, final int value) {
+ if (obj == null) {
+ throw new IllegalArgumentException(value + " is not a valid value");
+ }
+ return obj;
+ }
+
/**
* Utility method for checking whether a target object is a compatible DataObject.
*
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
+import static org.mockito.Mockito.mock;
import java.util.Collections;
import java.util.List;
() -> CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Set.of(new Object())));
assertThat(iae.getCause(), instanceOf(ClassCastException.class));
}
+
+ @Test
+ public void testCheckEnumName() {
+ final var ex = assertThrows(IllegalArgumentException.class, () -> CodeHelpers.checkEnum(null, "xyzzy"));
+ assertEquals("\"xyzzy\" is not a valid name", ex.getMessage());
+
+ final var obj = mock(Enumeration.class);
+ assertSame(obj, CodeHelpers.checkEnum(obj, "xyzzy"));
+ }
+
+ @Test
+ public void testCheckEnumValue() {
+ final var ex = assertThrows(IllegalArgumentException.class, () -> CodeHelpers.checkEnum(null, 1234));
+ assertEquals("1234 is not a valid value", ex.getMessage());
+
+ final var obj = mock(Enumeration.class);
+ assertSame(obj, CodeHelpers.checkEnum(obj, 1234));
+ }
}
}
public static Ipv4MulticastSourceAddress ipv4MulticastSourceAddressFor(final String str) {
- return Ipv4MulticastSourceAddress.Enumeration.forName(str)
- .map(ENUMERATED_IPV4_MCAST_SRC::get)
- .orElse(new Ipv4MulticastSourceAddress(new Ipv4Address(str)));
+ final var enumeration = Ipv4MulticastSourceAddress.Enumeration.forName(str);
+ return enumeration != null ? ipv4MulticastSourceAddressFor(enumeration)
+ : new Ipv4MulticastSourceAddress(new Ipv4Address(str));
}
public static Ipv4MulticastSourceAddress ipv4MulticastSourceAddressFor(
}
public static Ipv6MulticastSourceAddress ipv6MulticastSourceAddressFor(final String str) {
- return Ipv6MulticastSourceAddress.Enumeration.forName(str)
- .map(ENUMERATED_IPV6_MCAST_SRC::get)
- .orElse(new Ipv6MulticastSourceAddress(new Ipv6Address(str)));
+ final var enumeration = Ipv6MulticastSourceAddress.Enumeration.forName(str);
+ return enumeration != null ? ipv6MulticastSourceAddressFor(enumeration)
+ : new Ipv6MulticastSourceAddress(new Ipv6Address(str));
}
public static Ipv6MulticastSourceAddress ipv6MulticastSourceAddressFor(
}
public static TimerValueMilliseconds timerValueMillisecondsFor(final String str) {
- return TimerValueMilliseconds.Enumeration.forName(str)
- .map(ENUMERATED_TIMERVAR_MS::get)
- .orElse(new TimerValueMilliseconds(Uint32.valueOf(str)));
+ final var enumeration = TimerValueMilliseconds.Enumeration.forName(str);
+ return enumeration != null ? timerValueMillisecondsFor(enumeration)
+ : new TimerValueMilliseconds(Uint32.valueOf(str));
}
public static TimerValueMilliseconds timerValueMillisecondsFor(
return verifyNotNull(ENUMERATED_TIMERVAR_MS.get(requireNonNull(enumeration)));
}
-
public static TimerValueSeconds16 timerValueSeconds16For(final String str) {
- return TimerValueSeconds16.Enumeration.forName(str).map(ENUMERATED_TIMERVAL_16::get)
- .orElse(new TimerValueSeconds16(Uint16.valueOf(str)));
+ final var enumeration = TimerValueSeconds16.Enumeration.forName(str);
+ return enumeration != null ? timerValueSeconds16For(enumeration) : new TimerValueSeconds16(Uint16.valueOf(str));
}
public static TimerValueSeconds16 timerValueSeconds16For(
return verifyNotNull(ENUMERATED_TIMERVAL_16.get(requireNonNull(enumeration)));
}
- public static TimerValueSeconds32 timerValueSeconds32For(final String defaultValue) {
- return TimerValueSeconds32.Enumeration.forName(defaultValue)
- .map(ENUMERATED_TIMERVAL_32::get)
- .orElse(new TimerValueSeconds32(Uint32.valueOf(defaultValue)));
+ public static TimerValueSeconds32 timerValueSeconds32For(final String str) {
+ final var enumeration = TimerValueSeconds32.Enumeration.forName(str);
+ return enumeration != null ? timerValueSeconds32For(enumeration) : new TimerValueSeconds32(Uint32.valueOf(str));
}
public static TimerValueSeconds32 timerValueSeconds32For(final TimerValueSeconds32.Enumeration enumeration) {
import java.util.Comparator;
import java.util.EnumMap;
import java.util.Objects;
-import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.Uint16;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ethertypes.rev190304.Ethertype.Enumeration;
// Fall through and interpret as a string
}
- final Optional<Enumeration> known = Enumeration.forName(defaultValue);
- checkArgument(known.isPresent(), "Unknown ethertype %s", defaultValue);
- return verifyNotNull(ENUM_ETHERTYPES.get(known.get()));
+ final var known = Enumeration.forName(defaultValue);
+ checkArgument(known != null, "Unknown ethertype %s", defaultValue);
+ return verifyNotNull(ENUM_ETHERTYPES.get(known));
}
/**