Merge "Bug 1446: Changed QNM to toString listener for debug output"
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / spi / LazyContainerNode.java
1 /*
2  * Copyright (c) 2014 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.Optional;
11
12 import java.util.HashMap;
13 import java.util.Map;
14
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;
18
19 final class LazyContainerNode extends ContainerNode {
20     protected LazyContainerNode(final NormalizedNode<?, ?> data, final Version version) {
21         super(data, version, version);
22     }
23
24     @Override
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()));
30         }
31
32         return Optional.absent();
33     }
34
35     @Override
36     public MutableTreeNode mutable() {
37         /*
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
40          * children retain it.
41          *
42          * The simplest thing to do is to just flush the amortized work and be done
43          * with it.
44          */
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()));
49         }
50
51         return new Mutable(this, children);
52     }
53
54     @SuppressWarnings("unchecked")
55     private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> castData() {
56         return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) getData();
57     }
58 }