be0eefb4e58c89d928bfd3575ef99aa12a4bdef0
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / node / LazyContainerNode.java
1 /*
2  * Copyright (c) 2015 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 com.google.common.base.MoreObjects.ToStringHelper;
11 import com.google.common.collect.Collections2;
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
17 /**
18  * Lazily-materialized container node. Any new/modified children are tracked in a map. This map is consulted before
19  * instantiating a child node from data node. Resulting node is not cached.
20  */
21 final class LazyContainerNode extends AbstractModifiedContainerNode {
22     LazyContainerNode(final NormalizedNode data, final Version version, final Version subtreeVersion) {
23         this(data, version, MapAdaptor.getDefaultInstance().initialSnapshot(1), subtreeVersion);
24     }
25
26     LazyContainerNode(final NormalizedNode data, final Version version,
27             final Map<PathArgument, TreeNode> children, final Version subtreeVersion) {
28         super(data, version, children, subtreeVersion);
29     }
30
31     @Override
32     public MutableTreeNode mutable() {
33         final Map<PathArgument, TreeNode> snapshot = snapshotChildren();
34         if (snapshot.size() == castData().body().size()) {
35             return new MaterializedMutableContainerNode(this, snapshot);
36         }
37         return new LazyMutableContainerNode(this, snapshot);
38     }
39
40     @Override
41     public TreeNode childByArg(final PathArgument arg) {
42         final TreeNode modified;
43         return (modified = getModifiedChild(arg)) == null ? getChildFromData(arg) : modified;
44     }
45
46     @Override
47     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
48         // Modified children add added by superclass. Here we filter the other children.
49         return super.addToStringAttributes(helper).add("untouched", Collections2.filter(castData().body(),
50             input -> getModifiedChild(input.getIdentifier()) == null));
51     }
52 }