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.impl.schema.tree;
10 import com.google.common.collect.Collections2;
11 import java.util.Collection;
12 import java.util.Optional;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
19 abstract class AbstractRecursiveCandidateNode extends AbstractDataTreeCandidateNode {
21 protected AbstractRecursiveCandidateNode(
22 final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
26 @SuppressWarnings("unchecked")
27 static DataTreeCandidateNode deleteNode(final NormalizedNode<?, ?> data) {
28 if (data instanceof NormalizedNodeContainer) {
29 return new RecursiveDeleteCandidateNode(
30 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
32 return new DeleteLeafCandidateNode(data);
35 @SuppressWarnings("unchecked")
36 static DataTreeCandidateNode replaceNode(final NormalizedNode<?, ?> oldData, final NormalizedNode<?, ?> newData) {
37 if (isContainer(oldData)) {
38 return new RecursiveReplaceCandidateNode(
39 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) oldData,
40 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) newData);
42 return new ReplaceLeafCandidateNode(oldData, newData);
45 @SuppressWarnings("unchecked")
46 static DataTreeCandidateNode unmodifiedNode(final NormalizedNode<?, ?> data) {
47 if (data instanceof NormalizedNodeContainer) {
48 return new RecursiveUnmodifiedCandidateNode(
49 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
51 return new UnmodifiedLeafCandidateNode(data);
54 @SuppressWarnings("unchecked")
55 static DataTreeCandidateNode writeNode(final NormalizedNode<?, ?> data) {
56 if (data instanceof NormalizedNodeContainer) {
57 return new RecursiveWriteCandidateNode(
58 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
60 return new WriteLeafCandidateNode(data);
63 protected static boolean isContainer(final NormalizedNode<?, ?> data) {
64 return data instanceof NormalizedNodeContainer;
68 public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
69 final Optional<NormalizedNode<?, ?>> potential = getData().getChild(identifier);
70 if (potential.isPresent()) {
71 return createChild(potential.get());
78 public final Collection<DataTreeCandidateNode> getChildNodes() {
79 return Collections2.transform(getData().getValue(), this::createChild);
82 @SuppressWarnings("unchecked")
83 private DataTreeCandidateNode createChild(final NormalizedNode<?, ?> childData) {
84 if (isContainer(childData)) {
85 return createContainer((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) childData);
87 return createLeaf(childData);
90 abstract DataTreeCandidateNode createContainer(
91 NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> childData);
93 abstract DataTreeCandidateNode createLeaf(NormalizedNode<?,?> childData);