import static com.google.common.base.Preconditions.checkArgument;
+import com.google.common.annotations.Beta;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.io.IOException;
import java.util.Deque;
import java.util.List;
import java.util.Map.Entry;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.odlext.model.api.YangModeledAnyxmlSchemaNode;
import org.opendaylight.yangtools.rfc7952.data.api.StreamWriterMetadataExtension;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
* Represents a node which is composed of multiple simpler nodes.
*/
public class CompositeNodeDataWithSchema<T extends DataSchemaNode> extends AbstractNodeDataWithSchema<T> {
+ /**
+ * Policy on how child nodes should be treated when an attempt is made to add them multiple times.
+ */
+ @Beta
+ public enum ChildReusePolicy {
+ /**
+ * Do not consider any existing nodes at all, just perform a straight append. Multiple occurrences of a child
+ * will result in multiple children being emitted. This is almost certainly the wrong policy unless the caller
+ * prevents such a situation from arising via some different mechanism.
+ */
+ NOOP,
+ /**
+ * Do not allow duplicate definition of a child node. This would typically be used when a child cannot be
+ * encountered multiple times, but the caller does not make any provision to detect such a conflict. If a child
+ * node would end up being defined a second time, {@link DuplicateChildNodeRejectedException} is reported.
+ */
+ REJECT {
+ @Override
+ AbstractNodeDataWithSchema<?> appendChild(final Collection<AbstractNodeDataWithSchema<?>> view,
+ final AbstractNodeDataWithSchema<?> newChild) {
+ final DataSchemaNode childSchema = newChild.getSchema();
+ final AbstractNodeDataWithSchema<?> existing = findExistingChild(view, childSchema);
+ if (existing != null) {
+ throw new DuplicateChildNodeRejectedException("Duplicate child " + childSchema.getQName());
+ }
+ return super.appendChild(view, newChild);
+ }
+ },
+ /**
+ * Reuse previously-defined child node. This is most appropriate when a child may be visited multiple times
+ * and the intent is to append content of each visit. A typical usage is list elements with RFC7950 XML
+ * encoding, where there is no encapsulating element and hence list entries may be interleaved with other
+ * children.
+ */
+ REUSE {
+ @Override
+ AbstractNodeDataWithSchema<?> appendChild(final Collection<AbstractNodeDataWithSchema<?>> view,
+ final AbstractNodeDataWithSchema<?> newChild) {
+ final AbstractNodeDataWithSchema<?> existing = findExistingChild(view, newChild.getSchema());
+ return existing != null ? existing : super.appendChild(view, newChild);
+ }
+ };
+
+ AbstractNodeDataWithSchema<?> appendChild(final Collection<AbstractNodeDataWithSchema<?>> view,
+ final AbstractNodeDataWithSchema<?> newChild) {
+ view.add(newChild);
+ return newChild;
+ }
+
+ static @Nullable AbstractNodeDataWithSchema<?> findExistingChild(
+ final Collection<AbstractNodeDataWithSchema<?>> view, final DataSchemaNode childSchema) {
+ for (AbstractNodeDataWithSchema<?> existing : view) {
+ if (childSchema.equals(existing.getSchema())) {
+ return existing;
+ }
+ }
+ return null;
+ }
+ }
+
/**
* nodes which were added to schema via augmentation and are present in data input.
*/
super(schema);
}
- private AbstractNodeDataWithSchema<?> addChild(final DataSchemaNode schema) {
- AbstractNodeDataWithSchema<?> newChild = addSimpleChild(schema);
- return newChild == null ? addCompositeChild(schema) : newChild;
- }
-
@Deprecated
public void addChild(final AbstractNodeDataWithSchema<?> newChild) {
children.add(newChild);
}
+ @Deprecated
public AbstractNodeDataWithSchema<?> addChild(final Deque<DataSchemaNode> schemas) {
+ return addChild(schemas, ChildReusePolicy.NOOP);
+ }
+
+ public AbstractNodeDataWithSchema<?> addChild(final Deque<DataSchemaNode> schemas, final ChildReusePolicy policy) {
checkArgument(!schemas.isEmpty(), "Expecting at least one schema");
// Pop the first node...
final DataSchemaNode schema = schemas.pop();
if (schemas.isEmpty()) {
// Simple, direct node
- return addChild(schema);
+ return addChild(schema, policy);
}
// The choice/case mess, reuse what we already popped
if (caseNodeDataWithSchema == null) {
ChoiceNodeDataWithSchema choiceNodeDataWithSchema = new ChoiceNodeDataWithSchema(choiceNode);
childNodes.add(choiceNodeDataWithSchema);
- caseNodeDataWithSchema = choiceNodeDataWithSchema.addCompositeChild(caseNode);
+ caseNodeDataWithSchema = choiceNodeDataWithSchema.addCompositeChild(caseNode, ChildReusePolicy.NOOP);
}
- return caseNodeDataWithSchema.addChild(schemas);
+ return caseNodeDataWithSchema.addChild(schemas, policy);
}
- private AbstractNodeDataWithSchema<?> addSimpleChild(final DataSchemaNode schema) {
+ private AbstractNodeDataWithSchema<?> addChild(final DataSchemaNode schema, final ChildReusePolicy policy) {
+ AbstractNodeDataWithSchema<?> newChild = addSimpleChild(schema, policy);
+ return newChild == null ? addCompositeChild(schema, policy) : newChild;
+ }
+
+ private AbstractNodeDataWithSchema<?> addSimpleChild(final DataSchemaNode schema, final ChildReusePolicy policy) {
final SimpleNodeDataWithSchema<?> newChild;
if (schema instanceof LeafSchemaNode) {
newChild = new LeafNodeDataWithSchema((LeafSchemaNode) schema);
} else {
augSchema = null;
}
+
+ // FIXME: 6.0.0: use policy once we have removed addChild() visibility
+
if (augSchema != null) {
augmentationsToChild.put(augSchema, newChild);
} else {
return null;
}
- AbstractNodeDataWithSchema<?> addCompositeChild(final DataSchemaNode schema) {
+ AbstractNodeDataWithSchema<?> addCompositeChild(final DataSchemaNode schema, final ChildReusePolicy policy) {
final CompositeNodeDataWithSchema<?> newChild;
if (schema instanceof ListSchemaNode) {
newChild = new CompositeNodeDataWithSchema<>(schema);
}
- addCompositeChild(newChild);
- return newChild;
+ return addCompositeChild(newChild, policy);
}
- final void addCompositeChild(final CompositeNodeDataWithSchema<?> newChild) {
+ final AbstractNodeDataWithSchema<?> addCompositeChild(final CompositeNodeDataWithSchema<?> newChild,
+ final ChildReusePolicy policy) {
final AugmentationSchemaNode augSchema = findCorrespondingAugment(getSchema(), newChild.getSchema());
- if (augSchema != null) {
- augmentationsToChild.put(augSchema, newChild);
- } else {
- addChild(newChild);
- }
+ final Collection<AbstractNodeDataWithSchema<?>> view = augSchema == null ? children
+ : augmentationsToChild.get(augSchema);
+
+ return policy.appendChild(view, newChild);
}
/**