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.tree.spi;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.ImmutableList;
14 import java.util.Collection;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
21 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
24 * Utility implementation of {@link DataTreeCandidateNode} which acts as if
25 * the {@link NormalizedNode} passed to it at creation time were freshly written.
27 final class NormalizedNodeDataTreeCandidateNode implements DataTreeCandidateNode {
28 private final NormalizedNode data;
31 * Create a new instance backed by supplied data.
33 * @param data Backing {@link NormalizedNode} data.
35 NormalizedNodeDataTreeCandidateNode(final @NonNull NormalizedNode data) {
36 this.data = requireNonNull(data);
40 public PathArgument getIdentifier() {
41 return data.getIdentifier();
45 public Collection<DataTreeCandidateNode> getChildNodes() {
46 if (data instanceof DistinctNodeContainer) {
47 return Collections2.transform(((DistinctNodeContainer<?, ?>) data).body(),
48 input -> input == null ? null : new NormalizedNodeDataTreeCandidateNode(input));
50 return ImmutableList.of();
54 @SuppressWarnings({ "rawtypes", "unchecked" })
55 public Optional<DataTreeCandidateNode> getModifiedChild(final PathArgument childIdentifier) {
56 if (data instanceof DistinctNodeContainer) {
57 return ((Optional<@NonNull NormalizedNode>) ((DistinctNodeContainer)data).findChildByArg(childIdentifier))
58 .map(NormalizedNodeDataTreeCandidateNode::new);
60 return Optional.empty();
64 public ModificationType getModificationType() {
65 return ModificationType.WRITE;
69 public Optional<NormalizedNode> getDataAfter() {
70 return Optional.of(data);
74 public Optional<NormalizedNode> getDataBefore() {
75 return Optional.empty();