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.collect.Collections2;
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;
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.
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);
26 LazyContainerNode(final NormalizedNode<?, ?> data, final Version version,
27 final Map<PathArgument, TreeNode> children, final Version subtreeVersion) {
28 super(data, version, children, subtreeVersion);
32 public MutableTreeNode mutable() {
33 final Map<PathArgument, TreeNode> snapshot = snapshotChildren();
34 if (snapshot.size() == castData().size()) {
35 return new MaterializedMutableContainerNode(this, snapshot);
37 return new LazyMutableContainerNode(this, snapshot);
41 public TreeNode childByArg(final PathArgument arg) {
42 final TreeNode modified;
43 return (modified = getModifiedChild(arg)) == null ? getChildFromData(arg) : modified;
47 protected 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().getValue(),
50 input -> getModifiedChild(input.getIdentifier()) == null));