2 * Copyright (c) 2014 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.Optional;
12 import java.util.HashMap;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
19 final class LazyContainerNode extends ContainerNode {
20 protected LazyContainerNode(final NormalizedNode<?, ?> data, final Version version) {
21 super(data, version, version);
25 public Optional<TreeNode> getChild(final PathArgument key) {
26 // We do not cache the instantiated node as it is dirt cheap
27 final Optional<NormalizedNode<?, ?>> child = castData().getChild(key);
28 if (child.isPresent()) {
29 return Optional.of(TreeNodeFactory.createTreeNode(child.get(), getVersion()));
32 return Optional.absent();
36 public MutableTreeNode mutable() {
38 * We are creating a mutable view of the data, which means that the version
39 * is going to probably change -- and we need to make sure any unmodified
42 * The simplest thing to do is to just flush the amortized work and be done
45 final Map<PathArgument, TreeNode> children = new HashMap<>();
46 for (NormalizedNode<?, ?> child : castData().getValue()) {
47 PathArgument id = child.getIdentifier();
48 children.put(id, TreeNodeFactory.createTreeNode(child, getVersion()));
51 return new Mutable(this, children);
54 @SuppressWarnings("unchecked")
55 private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> castData() {
56 return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) getData();