2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.tree.impl.node;
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
14 import org.opendaylight.yangtools.util.MapAdaptor;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 * Abstract base for container-based {@link MutableTreeNode}s. It tracks modified nodes in a map and deals with
21 * correctly implementing {@link #seal()}.
23 abstract class AbstractMutableContainerNode extends MutableTreeNode {
24 private final Version version;
25 private Map<PathArgument, TreeNode> children;
26 private NormalizedNode data;
27 private Version subtreeVersion;
29 AbstractMutableContainerNode(final AbstractContainerNode parent, final Map<PathArgument, TreeNode> children) {
30 data = parent.getData();
31 version = parent.getVersion();
32 subtreeVersion = parent.getSubtreeVersion();
33 this.children = requireNonNull(children);
36 final Version getVersion() {
40 final TreeNode getModifiedChild(final PathArgument child) {
41 return children.get(child);
44 @SuppressWarnings("unchecked")
45 final DistinctNodeContainer<PathArgument, NormalizedNode> getData() {
46 return (DistinctNodeContainer<PathArgument, NormalizedNode>) data;
50 public final void setSubtreeVersion(final Version subtreeVersion) {
51 this.subtreeVersion = requireNonNull(subtreeVersion);
55 public final TreeNode putChild(final TreeNode child) {
56 return children.put(child.getIdentifier(), child);
60 public final TreeNode removeChild(final PathArgument id) {
61 return children.remove(requireNonNull(id));
65 public final void setData(final NormalizedNode data) {
66 this.data = requireNonNull(data);
70 public final TreeNode seal() {
74 * Decide which implementation:
76 * => version equals subtree version, this node has not been updated since its creation
77 * => children.size() equals data child size, this node has been completely materialized and further lookups
78 * into data will not happen,
79 * => more materialization can happen
81 if (!version.equals(subtreeVersion)) {
82 final Map<PathArgument, TreeNode> newChildren = MapAdaptor.getDefaultInstance().optimize(children);
83 final int dataSize = getData().body().size();
84 final int childrenSize = newChildren.size();
85 if (dataSize != childrenSize) {
86 verify(dataSize > childrenSize, "Detected %s modified children, data has only %s",
87 childrenSize, dataSize);
88 ret = new LazyContainerNode(data, version, newChildren, subtreeVersion);
90 ret = new MaterializedContainerNode(data, version, newChildren, subtreeVersion);
93 ret = new SimpleContainerNode(data, version);
96 // This forces a NPE if this class is accessed again. Better than corruption.