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.base.Optional;
11 import com.google.common.collect.Collections2;
12 import java.util.Collection;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
18 abstract class AbstractRecursiveCandidateNode extends AbstractDataTreeCandidateNode {
20 protected AbstractRecursiveCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
24 @SuppressWarnings("unchecked")
25 static DataTreeCandidateNode deleteNode(final NormalizedNode<?, ?> data) {
26 if (data instanceof NormalizedNodeContainer) {
27 return new RecursiveDeleteCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
29 return new DeleteLeafCandidateNode(data);
32 @SuppressWarnings("unchecked")
33 static DataTreeCandidateNode replaceNode(final NormalizedNode<?, ?> oldData, final NormalizedNode<?, ?> newData) {
34 if (isContainer(oldData)) {
35 return new RecursiveReplaceCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) oldData,
36 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) newData);
38 return new ReplaceLeafCandidateNode(oldData, newData);
41 @SuppressWarnings("unchecked")
42 static DataTreeCandidateNode unmodifiedNode(final NormalizedNode<?, ?> data) {
43 if (data instanceof NormalizedNodeContainer) {
44 return new RecursiveUnmodifiedCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
46 return new UnmodifiedLeafCandidateNode(data);
49 @SuppressWarnings("unchecked")
50 static DataTreeCandidateNode writeNode(final NormalizedNode<?, ?> data) {
51 if (data instanceof NormalizedNodeContainer) {
52 return new RecursiveWriteCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
54 return new WriteLeafCandidateNode(data);
57 protected static boolean isContainer(final NormalizedNode<?, ?> data) {
58 return data instanceof NormalizedNodeContainer;
62 public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
63 final Optional<NormalizedNode<?, ?>> potential = getData().getChild(identifier);
64 if (potential.isPresent()) {
65 return createChild(potential.get());
71 public final Collection<DataTreeCandidateNode> getChildNodes() {
72 return Collections2.transform(getData().getValue(), this::createChild);
75 @SuppressWarnings("unchecked")
76 private DataTreeCandidateNode createChild(final NormalizedNode<?, ?> childData) {
77 if (isContainer(childData)) {
78 return createContainer((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) childData);
80 return createLeaf(childData);
84 protected abstract DataTreeCandidateNode createContainer(NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> childData);
86 protected abstract DataTreeCandidateNode createLeaf(NormalizedNode<?,?> childData);