Propagate type safety a bit and reduce casts a bit.
JIRA: MDSAL-743
Change-Id: I05a727f9eb21f55f41c84b3dd5bb264aefd71a73
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
import java.util.concurrent.ExecutionException;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
+import org.opendaylight.yangtools.yang.binding.BitsTypeObject;
import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
// FIXME: 'SchemaUnawareCodec' is not correct: we use BitsTypeDefinition in construction
-// FIXME: require the base class to be a TypeObject
-// FIXME: MDSAL-743: require BitsTypeObject base class
final class BitsCodec extends SchemaUnawareCodec {
/*
* Use identity comparison for keys and allow classes to be GCd themselves.
* "Virtual machine implementations are, however, encouraged to bias against clearing recently-created or
* recently-used soft references."
*/
- private static final Cache<Class<?>, @NonNull BitsCodec> CACHE = CacheBuilder.newBuilder().weakKeys().softValues()
- .build();
+ private static final Cache<Class<? extends BitsTypeObject>, @NonNull BitsCodec> CACHE =
+ CacheBuilder.newBuilder().weakKeys().softValues().build();
private static final MethodType CONSTRUCTOR_INVOKE_TYPE = MethodType.methodType(Object.class, Boolean[].class);
// Ordered by position
static @NonNull BitsCodec of(final Class<?> returnType, final BitsTypeDefinition rootType)
throws ExecutionException {
- return CACHE.get(returnType, () -> {
+ return CACHE.get(returnType.asSubclass(BitsTypeObject.class), () -> {
final Map<String, Method> getters = new LinkedHashMap<>();
final Set<String> ctorArgs = new TreeSet<>();
package org.opendaylight.mdsal.binding.dom.codec.impl;
import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Verify.verifyNotNull;
import static java.util.Objects.requireNonNull;
import com.google.common.cache.Cache;
* "Virtual machine implementations are, however, encouraged to bias against clearing recently-created or
* recently-used soft references."
*/
- private static final Cache<Class<?>, @NonNull EnumerationCodec> CACHE = CacheBuilder.newBuilder().weakKeys()
- .softValues().build();
+ private static final Cache<Class<? extends EnumTypeObject>, @NonNull EnumerationCodec> CACHE =
+ CacheBuilder.newBuilder().weakKeys().softValues().build();
private final ImmutableBiMap<String, Enum<?>> nameToEnum;
private final Class<? extends Enum<?>> enumClass;
static @NonNull EnumerationCodec of(final Class<?> returnType, final EnumTypeDefinition def)
throws ExecutionException {
- return CACHE.get(returnType, () -> {
+ return CACHE.get(returnType.asSubclass(EnumTypeObject.class), () -> {
final Class<? extends Enum<?>> enumType = castType(returnType);
final Map<String, Enum<?>> mapping = Maps.uniqueIndex(Arrays.asList(enumType.getEnumConstants()),
@SuppressWarnings("unchecked")
private static Class<? extends Enum<?>> castType(final Class<?> returnType) {
checkArgument(Enum.class.isAssignableFrom(returnType));
- return (Class<? extends Enum<?>>) returnType;
+ return (Class<? extends Enum<?>>) returnType.asSubclass(Enum.class);
}
@Override
@Override
protected String serializeImpl(final Object input) {
checkArgument(enumClass.isInstance(input), "Input %s is not a instance of %s", input, enumClass);
- // FIXME: verifyNotNull here
- return requireNonNull(nameToEnum.inverse().get(input));
+ return verifyNotNull(nameToEnum.inverse().get(input));
}
}
\ No newline at end of file
}
try {
- if (rootType instanceof EnumTypeDefinition) {
- return EnumerationCodec.of(typeClz, (EnumTypeDefinition) rootType);
- } else if (rootType instanceof BitsTypeDefinition) {
- return BitsCodec.of(typeClz, (BitsTypeDefinition) rootType);
+ if (rootType instanceof EnumTypeDefinition enumType) {
+ return EnumerationCodec.of(typeClz, enumType);
+ } else if (rootType instanceof BitsTypeDefinition bitsType) {
+ return BitsCodec.of(typeClz, bitsType);
} else {
return EncapsulatedValueCodec.of(typeClz);
}