Fix eclipse/checkstyle warnings
[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,
29             final Map<PathArgument, TreeNode> children) {
30         this.data = parent.getData();
31         this.version = parent.getVersion();
32         this.subtreeVersion = parent.getSubtreeVersion();
33         this.children = Preconditions.checkNotNull(children);
34     }
35
36     protected final Version getVersion() {
37         return version;
38     }
39
40     protected final TreeNode getModifiedChild(final PathArgument child) {
41         return children.get(child);
42     }
43
44     @SuppressWarnings("unchecked")
45     protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
46         return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data;
47     }
48
49     @Override
50     public final void setSubtreeVersion(final Version subtreeVersion) {
51         this.subtreeVersion = Preconditions.checkNotNull(subtreeVersion);
52     }
53
54     @Override
55     public final void addChild(final TreeNode child) {
56         children.put(child.getIdentifier(), child);
57     }
58
59     @Override
60     public final void removeChild(final PathArgument id) {
61         children.remove(id);
62     }
63
64     @Override
65     public final void setData(final NormalizedNode<?, ?> data) {
66         this.data = Preconditions.checkNotNull(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().getValue().size();
84             if (dataSize != newChildren.size()) {
85                 Verify.verify(dataSize > newChildren.size(), "Detected %s modified children, data has only %s",
86                     newChildren.size(), dataSize);
87                 ret = new LazyContainerNode(data, version, newChildren, subtreeVersion);
88             } else {
89                 ret = new MaterializedContainerNode(data, version, newChildren, subtreeVersion);
90             }
91         } else {
92             ret = new SimpleContainerNode(data, version);
93         }
94
95         // This forces a NPE if this class is accessed again. Better than corruption.
96         children = null;
97         return ret;
98     }
99 }