BUG-4300: Make Lazy*ContainerNode really lazy
[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 com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import java.util.Map;
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;
17
18 /**
19  * Abstract base for container-based {@link MutableTreeNode}s. It tracks modified nodes in a map and deals with
20  * correctly implementing {@link #seal()}.
21  */
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;
27
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);
33     }
34
35     protected final Version getVersion() {
36         return version;
37     }
38
39     protected final TreeNode getModifiedChild(final PathArgument child) {
40         return children.get(child);
41     }
42
43     @SuppressWarnings("unchecked")
44     protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
45         return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data;
46     }
47
48     @Override
49     public final void setSubtreeVersion(final Version subtreeVersion) {
50         this.subtreeVersion = Preconditions.checkNotNull(subtreeVersion);
51     }
52
53     @Override
54     public final void addChild(final TreeNode child) {
55         children.put(child.getIdentifier(), child);
56     }
57
58     @Override
59     public final void removeChild(final PathArgument id) {
60         children.remove(id);
61     }
62
63     @Override
64     public final void setData(final NormalizedNode<?, ?> data) {
65         this.data = Preconditions.checkNotNull(data);
66     }
67
68     @Override
69     public final TreeNode seal() {
70         final TreeNode ret;
71
72         /*
73          * Decide which implementation:
74          *
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
79          */
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);
87             } else {
88                 ret = new MaterializedContainerNode(data, version, newChildren, subtreeVersion);
89             }
90         } else {
91             ret = new SimpleContainerNode(data, version);
92         }
93
94         // This forces a NPE if this class is accessed again. Better than corruption.
95         children = null;
96         return ret;
97     }
98 }