Do not access size indirect through body()
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / node / 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.tree.impl.node;
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.DistinctNodeContainer;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
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 extends MutableTreeNode {
24     private final Version version;
25     private Map<PathArgument, TreeNode> children;
26     private NormalizedNode data;
27     private Version subtreeVersion;
28
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);
34     }
35
36     final Version getVersion() {
37         return version;
38     }
39
40     final TreeNode getModifiedChild(final PathArgument child) {
41         return children.get(child);
42     }
43
44     @SuppressWarnings("unchecked")
45     final DistinctNodeContainer<PathArgument, NormalizedNode> getData() {
46         return (DistinctNodeContainer<PathArgument, NormalizedNode>) data;
47     }
48
49     @Override
50     public final void setSubtreeVersion(final Version subtreeVersion) {
51         this.subtreeVersion = requireNonNull(subtreeVersion);
52     }
53
54     @Override
55     public final TreeNode putChild(final TreeNode child) {
56         return children.put(child.getIdentifier(), child);
57     }
58
59     @Override
60     public final TreeNode removeChild(final PathArgument id) {
61         return children.remove(requireNonNull(id));
62     }
63
64     @Override
65     public final void setData(final NormalizedNode data) {
66         this.data = requireNonNull(data);
67     }
68
69     @Override
70     public final TreeNode seal() {
71         final TreeNode ret;
72
73         /*
74          * Decide which implementation:
75          *
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
80          */
81         if (!version.equals(subtreeVersion)) {
82             final Map<PathArgument, TreeNode> newChildren = MapAdaptor.getDefaultInstance().optimize(children);
83             final int dataSize = getData().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);
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 }