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.api.schema.tree.spi;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
13 import org.opendaylight.yangtools.util.MapAdaptor;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
19 * Abstract base for container-based {@link MutableTreeNode}s. It tracks modified nodes in a map and deals with
20 * correctly implementing {@link #seal()}.
22 abstract class AbstractMutableContainerNode implements MutableTreeNode {
23 private final Version version;
24 private Map<PathArgument, TreeNode> children;
25 private NormalizedNode<?, ?> data;
26 private Version subtreeVersion;
28 protected AbstractMutableContainerNode(final AbstractContainerNode parent,
29 final Map<PathArgument, TreeNode> children) {
30 this.data = parent.getData();
31 this.version = parent.getVersion();
32 this.subtreeVersion = parent.getSubtreeVersion();
33 this.children = Preconditions.checkNotNull(children);
36 protected final Version getVersion() {
40 protected final TreeNode getModifiedChild(final PathArgument child) {
41 return children.get(child);
44 @SuppressWarnings("unchecked")
45 protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
46 return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data;
50 public final void setSubtreeVersion(final Version subtreeVersion) {
51 this.subtreeVersion = Preconditions.checkNotNull(subtreeVersion);
55 public final void addChild(final TreeNode child) {
56 children.put(child.getIdentifier(), child);
60 public final void removeChild(final PathArgument id) {
65 public final void setData(final NormalizedNode<?, ?> data) {
66 this.data = Preconditions.checkNotNull(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().getValue().size();
84 if (dataSize != newChildren.size()) {
85 Verify.verify(dataSize > newChildren.size(), "Detected %s modified children, data has only %s",
86 newChildren.size(), dataSize);
87 ret = new LazyContainerNode(data, version, newChildren, subtreeVersion);
89 ret = new MaterializedContainerNode(data, version, newChildren, subtreeVersion);
92 ret = new SimpleContainerNode(data, version);
95 // This forces a NPE if this class is accessed again. Better than corruption.