import static java.util.Objects.requireNonNull;
-import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.binding.dom.codec.api.BindingTypeObjectCodecTreeNode;
-import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
import org.opendaylight.yangtools.yang.binding.TypeObject;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
-class LeafNodeCodecContext extends ValueNodeCodecContext.WithCodec {
+sealed class LeafNodeCodecContext extends ValueNodeCodecContext.WithCodec {
static final class OfTypeObject<T extends TypeObject> extends LeafNodeCodecContext
implements BindingTypeObjectCodecTreeNode<T> {
private final @NonNull Class<T> bindingClass;
- OfTypeObject(final LeafSchemaNode schema, final IllegalArgumentCodec<Object, Object> codec,
- final String getterName, final EffectiveModelContext schemaContext, final Class<T> bindingClass) {
+ OfTypeObject(final LeafSchemaNode schema, final ValueCodec<Object, Object> codec, final String getterName,
+ final EffectiveModelContext schemaContext, final Class<T> bindingClass) {
super(schema, codec, getterName, schemaContext);
this.bindingClass = requireNonNull(bindingClass);
}
}
}
- LeafNodeCodecContext(final LeafSchemaNode schema, final IllegalArgumentCodec<Object, Object> codec,
- final String getterName, final EffectiveModelContext schemaContext) {
+ LeafNodeCodecContext(final LeafSchemaNode schema, final ValueCodec<Object, Object> codec, final String getterName,
+ final EffectiveModelContext schemaContext) {
super(schema, codec, getterName, createDefaultObject(schema, codec, schemaContext));
}
- static LeafNodeCodecContext of(final LeafSchemaNode schema, final IllegalArgumentCodec<Object, Object> codec,
+ static LeafNodeCodecContext of(final LeafSchemaNode schema, final ValueCodec<Object, Object> codec,
final String getterName, final Class<?> valueType, final EffectiveModelContext schemaContext) {
return TypeObject.class.isAssignableFrom(valueType)
? new OfTypeObject<>(schema, codec, getterName, schemaContext, valueType.asSubclass(TypeObject.class))
return normalizedNode != null ? getValueCodec().deserialize(normalizedNode.body()) : null;
}
- private static Object createDefaultObject(final LeafSchemaNode schema,
- final IllegalArgumentCodec<Object, Object> codec,
- final EffectiveModelContext schemaContext) {
- Optional<? extends Object> defaultValue = schema.getType().getDefaultValue();
+ private static Object createDefaultObject(final LeafSchemaNode schema, final ValueCodec<Object, Object> codec,
+ final EffectiveModelContext schemaContext) {
+ var optDefaultValue = schema.getType().getDefaultValue();
TypeDefinition<?> type = schema.getType();
- if (defaultValue.isPresent()) {
+ if (optDefaultValue.isPresent()) {
+ final var defaultValue = optDefaultValue.orElseThrow();
if (type instanceof IdentityrefTypeDefinition) {
- return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
+ return qnameDomValueFromString(codec, schema, (String) defaultValue, schemaContext);
}
- return domValueFromString(codec, type, defaultValue.get());
+ return domValueFromString(codec, type, defaultValue);
}
- while (type.getBaseType() != null && !type.getDefaultValue().isPresent()) {
+ while (type.getBaseType() != null && type.getDefaultValue().isEmpty()) {
type = type.getBaseType();
}
- defaultValue = type.getDefaultValue();
- if (defaultValue.isPresent()) {
+ optDefaultValue = type.getDefaultValue();
+ if (optDefaultValue.isPresent()) {
+ final var defaultValue = optDefaultValue.orElseThrow();
if (type instanceof IdentityrefTypeDefinition) {
- return qnameDomValueFromString(codec, schema, (String) defaultValue.get(), schemaContext);
+ return qnameDomValueFromString(codec, schema, (String) defaultValue, schemaContext);
}
return domValueFromString(codec, type, defaultValue);
}
return null;
}
- private static Object qnameDomValueFromString(final IllegalArgumentCodec<Object, Object> codec,
- final DataSchemaNode schema, final String defaultValue,
- final EffectiveModelContext schemaContext) {
+ private static Object qnameDomValueFromString(final ValueCodec<Object, Object> codec, final DataSchemaNode schema,
+ final String defaultValue, final EffectiveModelContext schemaContext) {
int prefixEndIndex = defaultValue.indexOf(':');
QName qname;
if (prefixEndIndex != -1) {
String defaultValuePrefix = defaultValue.substring(0, prefixEndIndex);
- Module module = schemaContext.findModule(schema.getQName().getModule()).get();
+ Module module = schemaContext.findModule(schema.getQName().getModule()).orElseThrow();
if (module.getPrefix().equals(defaultValuePrefix)) {
qname = QName.create(module.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
return codec.deserialize(qname);
for (ModuleImport moduleImport : module.getImports()) {
if (moduleImport.getPrefix().equals(defaultValuePrefix)) {
- Module importedModule = schemaContext.findModule(moduleImport.getModuleName(),
- moduleImport.getRevision()).get();
+ Module importedModule = schemaContext.findModule(moduleImport.getModuleName().getLocalName(),
+ moduleImport.getRevision()).orElseThrow();
qname = QName.create(importedModule.getQNameModule(), defaultValue.substring(prefixEndIndex + 1));
return codec.deserialize(qname);
}
return codec.deserialize(qname);
}
- private static Object domValueFromString(final IllegalArgumentCodec<Object, Object> codec,
- final TypeDefinition<?> type, final Object defaultValue) {
+ private static Object domValueFromString(final ValueCodec<Object, Object> codec, final TypeDefinition<?> type,
+ final Object defaultValue) {
TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
if (typeDefAwareCodec != null) {
Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue);