29378674bb21ef3e4d5d553a5dee0db422bd2422
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / spi / LazyMutableContainerNode.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.Optional;
11 import java.util.Map;
12 import org.opendaylight.yangtools.util.MapAdaptor;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14
15 /**
16  * A container node which may need further materialization. Materialized nodes are tracked in a map, which is consulted
17  * before creating a new child node from backing data. Since the child is immutable, we do not have to track it.
18  */
19 final class LazyMutableContainerNode extends AbstractMutableContainerNode {
20     LazyMutableContainerNode(final AbstractContainerNode parent) {
21         this(parent, MapAdaptor.getDefaultInstance().<PathArgument, TreeNode>initialSnapshot(1));
22     }
23
24     LazyMutableContainerNode(final AbstractContainerNode parent, final Map<PathArgument, TreeNode> children) {
25         super(parent, children);
26     }
27
28     @Override
29     public Optional<TreeNode> getChild(final PathArgument childId) {
30         final TreeNode modified = getModifiedChild(childId);
31         if (modified != null) {
32             return Optional.of(modified);
33         }
34
35         return Optional.fromNullable(AbstractContainerNode.getChildFromData(getData(), childId, getVersion()));
36     }
37 }