0a737f2e6698d5d4a686ab1489d8a53397cd2e8e
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / spi / 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.api.schema.tree.spi;
9
10 import com.google.common.base.MoreObjects.ToStringHelper;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Predicate;
13 import com.google.common.collect.Collections2;
14 import java.util.Map;
15 import org.opendaylight.yangtools.util.MapAdaptor;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 /**
20  * Lazily-materialized container node. Any new/modified children are tracked in a map. This map is consulted before
21  * instantiating a child node from data node. Resulting node is not cached.
22  */
23 final class LazyContainerNode extends AbstractModifiedContainerNode {
24     LazyContainerNode(final NormalizedNode<?, ?> data, final Version version, final Version subtreeVersion) {
25         this(data, version, MapAdaptor.getDefaultInstance().<PathArgument, TreeNode>initialSnapshot(1), subtreeVersion);
26     }
27
28     LazyContainerNode(final NormalizedNode<?, ?> data, final Version version, final Map<PathArgument, TreeNode> children,
29             final Version subtreeVersion) {
30         super(data, version, children, subtreeVersion);
31     }
32
33     @Override
34     public MutableTreeNode mutable() {
35         final Map<PathArgument, TreeNode> snapshot = snapshotChildren();
36         if (snapshot.size() == castData().getValue().size()) {
37             return new MaterializedMutableContainerNode(this, snapshot);
38         }
39
40         return new LazyMutableContainerNode(this, snapshot);
41     }
42
43     @Override
44     public Optional<TreeNode> getChild(final PathArgument childId) {
45         final TreeNode modified = getModifiedChild(childId);
46         return modified == null ? getChildFromData(childId) : Optional.of(modified);
47     }
48
49     @Override
50     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
51         // Modified children add added by superclass. Here we filter the other children.
52         return super.addToStringAttributes(helper).add("untouched", Collections2.filter(castData().getValue(),
53             new Predicate<NormalizedNode<?, ?>>() {
54                 @Override
55                 public boolean apply(final NormalizedNode<?, ?> input) {
56                     return getModifiedChild(input.getIdentifier()) == null;
57                 }
58         }));
59     }
60 }