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, final Map<PathArgument, TreeNode> children) {
29 this.data = parent.getData();
30 this.version = parent.getVersion();
31 this.subtreeVersion = parent.getSubtreeVersion();
32 this.children = Preconditions.checkNotNull(children);
35 protected final Version getVersion() {
39 protected final TreeNode getModifiedChild(final PathArgument child) {
40 return children.get(child);
43 @SuppressWarnings("unchecked")
44 protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
45 return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data;
49 public final void setSubtreeVersion(final Version subtreeVersion) {
50 this.subtreeVersion = Preconditions.checkNotNull(subtreeVersion);
54 public final void addChild(final TreeNode child) {
55 children.put(child.getIdentifier(), child);
59 public final void removeChild(final PathArgument id) {
64 public final void setData(final NormalizedNode<?, ?> data) {
65 this.data = Preconditions.checkNotNull(data);
69 public final TreeNode seal() {
73 * Decide which implementation:
75 * => version equals subtree version, this node has not been updated since its creation
76 * => children.size() equals data child size, this node has been completely materialized and further lookups
77 * into data will not happen,
78 * => more materialization can happen
80 if (!version.equals(subtreeVersion)) {
81 final Map<PathArgument, TreeNode> newChildren = MapAdaptor.getDefaultInstance().optimize(children);
82 final int dataSize = getData().getValue().size();
83 if (dataSize != newChildren.size()) {
84 Verify.verify(dataSize > newChildren.size(), "Detected %s modified children, data has only %s",
85 newChildren.size(), dataSize);
86 ret = new LazyContainerNode(data, version, newChildren, subtreeVersion);
88 ret = new MaterializedContainerNode(data, version, newChildren, subtreeVersion);
91 ret = new SimpleContainerNode(data, version);
94 // This forces a NPE if this class is accessed again. Better than corruption.