Retain protype in DataContainerCodecContext
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / ChoiceCodecContext.java
index e4d15d497e2e49623ee84587716df832b76b44ff..8e0adc74b7e6bb314a16b24288cb9330a61f0e78 100644 (file)
@@ -40,7 +40,6 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.binding.contract.Naming;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -117,8 +116,8 @@ final class ChoiceCodecContext<D extends DataObject> extends CommonDataObjectCod
             .<Class<?>, CommonDataObjectCodecPrototype<?>>build();
 
         // Load case statements valid in this choice and keep track of their names
-        final var choiceType = prototype.getType();
-        final var factory = prototype.getFactory();
+        final var choiceType = prototype.runtimeType();
+        final var factory = prototype.contextFactory();
         final var localCases = new HashSet<JavaTypeName>();
         for (var caseType : choiceType.validCaseChildren()) {
             @SuppressWarnings("unchecked")
@@ -149,7 +148,7 @@ final class ChoiceCodecContext<D extends DataObject> extends CommonDataObjectCod
             if (cases.size() != 1) {
                 // Sort all possibilities by their FQCN to retain semi-predictable results
                 final var list = new ArrayList<>(entry.getValue());
-                list.sort(Comparator.comparing(proto -> proto.getBindingClass().getCanonicalName()));
+                list.sort(Comparator.comparing(proto -> proto.javaClass().getCanonicalName()));
                 ambiguousByCaseBuilder.putAll(entry.getKey(), list);
             } else {
                 unambiguousByCaseBuilder.put(entry.getKey(), cases.iterator().next());
@@ -205,14 +204,12 @@ final class ChoiceCodecContext<D extends DataObject> extends CommonDataObjectCod
     @Override
     public WithStatus getSchema() {
         // FIXME: Bad cast, we should be returning an EffectiveStatement perhaps?
-        return (WithStatus) type().statement();
+        return (WithStatus) prototype().runtimeType().statement();
     }
 
-    @SuppressWarnings("unchecked")
     @Override
-    public <C extends DataObject> CommonDataObjectCodecContext<C, ?> streamChild(final Class<C> childClass) {
-        final var child = byClass.get(childClass);
-        return child == null ? null : (CommonDataObjectCodecContext<C, ?>) child.get();
+    CommonDataObjectCodecPrototype<?> streamChildPrototype(final Class<?> childClass) {
+        return byClass.get(childClass);
     }
 
     Iterable<Class<?>> getCaseChildrenClasses() {
@@ -221,15 +218,12 @@ final class ChoiceCodecContext<D extends DataObject> extends CommonDataObjectCod
 
     @Override
     public CodecContext yangPathArgumentChild(final YangInstanceIdentifier.PathArgument arg) {
-        final CommonDataObjectCodecPrototype<?> cazeProto;
-        if (arg instanceof NodeIdentifierWithPredicates) {
-            cazeProto = byYangCaseChild.get(new NodeIdentifier(arg.getNodeType()));
-        } else {
-            cazeProto = byYangCaseChild.get(arg);
-        }
+        return ((CaseCodecContext<?>) super.yangPathArgumentChild(arg)).yangPathArgumentChild(arg);
+    }
 
-        return childNonNull(cazeProto, arg, "Argument %s is not valid child of %s", arg, getSchema()).get()
-                .yangPathArgumentChild(arg);
+    @Override
+    CaseCodecPrototype yangChildSupplier(final NodeIdentifier arg) {
+        return byYangCaseChild.get(arg);
     }
 
     @Override
@@ -244,7 +238,7 @@ final class ChoiceCodecContext<D extends DataObject> extends CommonDataObjectCod
             return null;
         }
         final var caze = byYangCaseChild.get(first.name());
-        return ((CaseCodecContext<D>) caze.get()).deserialize(data);
+        return ((CaseCodecContext<D>) caze.getCodecContext()).deserialize(data);
     }
 
     @Override
@@ -275,7 +269,7 @@ final class ChoiceCodecContext<D extends DataObject> extends CommonDataObjectCod
         return createCachingCodec(this, cacheSpecifier);
     }
 
-    DataContainerCodecContext<?, ?> getCaseByChildClass(final @NonNull Class<? extends DataObject> type) {
+    DataContainerCodecContext<?, ?, ?> getCaseByChildClass(final @NonNull Class<? extends DataObject> type) {
         var result = byCaseChildClass.get(type);
         if (result == null) {
             // We have not found an unambiguous result, try ambiguous ones
@@ -288,13 +282,14 @@ final class ChoiceCodecContext<D extends DataObject> extends CommonDataObjectCod
                         Ambiguous reference {} to child of {} resolved to {}, the first case in {} This mapping is \
                         not guaranteed to be stable and is subject to variations based on runtime circumstances. \
                         Please see the stack trace for hints about the source of ambiguity.""",
-                        type, bindingArg(), result.getBindingClass(),
-                        Lists.transform(inexact, CommonDataObjectCodecPrototype::getBindingClass), new Throwable());
+                        type, bindingArg(), result.javaClass(),
+                        Lists.transform(inexact, CommonDataObjectCodecPrototype::javaClass), new Throwable());
                 }
             }
         }
 
-        return childNonNull(result, type, "Class %s is not child of any cases for %s", type, bindingArg()).get();
+        return childNonNull(result, type, "Class %s is not child of any cases for %s", type, bindingArg())
+            .getCodecContext();
     }
 
     /**
@@ -311,7 +306,7 @@ final class ChoiceCodecContext<D extends DataObject> extends CommonDataObjectCod
         checkArgument(DataContainer.class.isAssignableFrom(type), "Supplied type must be derived from DataContainer");
         final var ret = new LinkedList<Class<? extends DataObject>>();
         for (var method : type.getMethods()) {
-            AbstractDataContainerAnalysis.getYangModeledReturnType(method, Naming.GETTER_PREFIX)
+            DataContainerAnalysis.getYangModeledReturnType(method, Naming.GETTER_PREFIX)
                 .ifPresent(entity -> ret.add((Class<? extends DataObject>) entity));
         }
         return ret;