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 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.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
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 implements MutableTreeNode {
24 private final Version version;
25 private Map<PathArgument, TreeNode> children;
26 private NormalizedNode<?, ?> data;
27 private Version subtreeVersion;
29 protected AbstractMutableContainerNode(final AbstractContainerNode parent,
30 final Map<PathArgument, TreeNode> children) {
31 this.data = parent.getData();
32 this.version = parent.getVersion();
33 this.subtreeVersion = parent.getSubtreeVersion();
34 this.children = requireNonNull(children);
37 protected final Version getVersion() {
41 protected final TreeNode getModifiedChild(final PathArgument child) {
42 return children.get(child);
45 @SuppressWarnings("unchecked")
46 protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
47 return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data;
51 public final void setSubtreeVersion(final Version subtreeVersion) {
52 this.subtreeVersion = requireNonNull(subtreeVersion);
56 public final void addChild(final TreeNode child) {
57 children.put(child.getIdentifier(), child);
61 public final void removeChild(final PathArgument id) {
62 children.remove(requireNonNull(id));
66 public final void setData(final NormalizedNode<?, ?> data) {
67 this.data = requireNonNull(data);
71 public final TreeNode seal() {
75 * Decide which implementation:
77 * => version equals subtree version, this node has not been updated since its creation
78 * => children.size() equals data child size, this node has been completely materialized and further lookups
79 * into data will not happen,
80 * => more materialization can happen
82 if (!version.equals(subtreeVersion)) {
83 final Map<PathArgument, TreeNode> newChildren = MapAdaptor.getDefaultInstance().optimize(children);
84 final int dataSize = getData().getValue().size();
85 if (dataSize != newChildren.size()) {
86 verify(dataSize > newChildren.size(), "Detected %s modified children, data has only %s",
87 newChildren.size(), 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.