2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
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;
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;
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.
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);
28 LazyContainerNode(final NormalizedNode<?, ?> data, final Version version, final Map<PathArgument, TreeNode> children,
29 final Version subtreeVersion) {
30 super(data, version, children, subtreeVersion);
34 public MutableTreeNode mutable() {
35 final Map<PathArgument, TreeNode> snapshot = snapshotChildren();
36 if (snapshot.size() == castData().getValue().size()) {
37 return new MaterializedMutableContainerNode(this, snapshot);
40 return new LazyMutableContainerNode(this, snapshot);
44 public Optional<TreeNode> getChild(final PathArgument childId) {
45 final TreeNode modified = getModifiedChild(childId);
46 return modified == null ? getChildFromData(childId) : Optional.of(modified);
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<?, ?>>() {
55 public boolean apply(final NormalizedNode<?, ?> input) {
56 return getModifiedChild(input.getIdentifier()) == null;