3106e369c8bbe04ce16e2b985551293c7011bb31
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / spi / AbstractMutableContainerNode.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
9
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Map;
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;
18
19 /**
20  * Abstract base for container-based {@link MutableTreeNode}s. It tracks modified nodes in a map and deals with
21  * correctly implementing {@link #seal()}.
22  */
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;
28
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);
35     }
36
37     protected final Version getVersion() {
38         return version;
39     }
40
41     protected final TreeNode getModifiedChild(final PathArgument child) {
42         return children.get(child);
43     }
44
45     @SuppressWarnings("unchecked")
46     protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
47         return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data;
48     }
49
50     @Override
51     public final void setSubtreeVersion(final Version subtreeVersion) {
52         this.subtreeVersion = requireNonNull(subtreeVersion);
53     }
54
55     @Override
56     public final void addChild(final TreeNode child) {
57         children.put(child.getIdentifier(), child);
58     }
59
60     @Override
61     public final void removeChild(final PathArgument id) {
62         children.remove(requireNonNull(id));
63     }
64
65     @Override
66     public final void setData(final NormalizedNode<?, ?> data) {
67         this.data = requireNonNull(data);
68     }
69
70     @Override
71     public final TreeNode seal() {
72         final TreeNode ret;
73
74         /*
75          * Decide which implementation:
76          *
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
81          */
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);
89             } else {
90                 ret = new MaterializedContainerNode(data, version, newChildren, subtreeVersion);
91             }
92         } else {
93             ret = new SimpleContainerNode(data, version);
94         }
95
96         // This forces a NPE if this class is accessed again. Better than corruption.
97         children = null;
98         return ret;
99     }
100 }