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;
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.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
22 * Utility implementation of {@link DataTreeCandidateNode} which acts as if
23 * the {@link NormalizedNode} passed to it at creation time were freshly written.
25 final class NormalizedNodeDataTreeCandidateNode implements DataTreeCandidateNode {
26 private final NormalizedNode<?, ?> data;
29 * Create a new instance backed by supplied data.
31 * @param data Backing {@link NormalizedNode} data.
33 NormalizedNodeDataTreeCandidateNode(final @NonNull NormalizedNode<?, ?> data) {
34 this.data = requireNonNull(data);
38 public PathArgument getIdentifier() {
39 return data.getIdentifier();
43 public Collection<DataTreeCandidateNode> getChildNodes() {
44 if (data instanceof NormalizedNodeContainer) {
45 return Collections2.transform(((NormalizedNodeContainer<?, ?, ?>) data).getValue(),
46 input -> input == null ? null : new NormalizedNodeDataTreeCandidateNode(input));
48 return ImmutableList.of();
52 public Optional<DataTreeCandidateNode> getModifiedChild(final PathArgument childIdentifier) {
53 if (data instanceof NormalizedNodeContainer) {
54 @SuppressWarnings({ "rawtypes", "unchecked" })
55 final Optional<? extends NormalizedNode<?, ?>> child =
56 ((NormalizedNodeContainer)data).getChild(childIdentifier);
57 return child.map(NormalizedNodeDataTreeCandidateNode::new);
59 return Optional.empty();
63 public ModificationType getModificationType() {
64 return ModificationType.WRITE;
68 public Optional<NormalizedNode<?, ?>> getDataAfter() {
69 return Optional.of(data);
73 public Optional<NormalizedNode<?, ?>> getDataBefore() {
74 return Optional.empty();