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 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(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
25 @SuppressWarnings("unchecked")
26 static DataTreeCandidateNode deleteNode(final NormalizedNode<?, ?> data) {
27 if (data instanceof NormalizedNodeContainer) {
28 return new RecursiveDeleteCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
30 return new DeleteLeafCandidateNode(data);
33 @SuppressWarnings("unchecked")
34 static DataTreeCandidateNode replaceNode(final NormalizedNode<?, ?> oldData, final NormalizedNode<?, ?> newData) {
35 if (isContainer(oldData)) {
36 return new RecursiveReplaceCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) oldData,
37 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) newData);
39 return new ReplaceLeafCandidateNode(oldData, newData);
42 @SuppressWarnings("unchecked")
43 static DataTreeCandidateNode unmodifiedNode(final NormalizedNode<?, ?> data) {
44 if (data instanceof NormalizedNodeContainer) {
45 return new RecursiveUnmodifiedCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
47 return new UnmodifiedLeafCandidateNode(data);
50 @SuppressWarnings("unchecked")
51 static DataTreeCandidateNode writeNode(final NormalizedNode<?, ?> data) {
52 if (data instanceof NormalizedNodeContainer) {
53 return new RecursiveWriteCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
55 return new WriteLeafCandidateNode(data);
58 protected static boolean isContainer(final NormalizedNode<?, ?> data) {
59 return data instanceof NormalizedNodeContainer;
63 public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
64 final Optional<NormalizedNode<?, ?>> potential = getData().getChild(identifier);
65 if (potential.isPresent()) {
66 return createChild(potential.get());
73 public final Collection<DataTreeCandidateNode> getChildNodes() {
74 return Collections2.transform(getData().getValue(), this::createChild);
77 @SuppressWarnings("unchecked")
78 private DataTreeCandidateNode createChild(final NormalizedNode<?, ?> childData) {
79 if (isContainer(childData)) {
80 return createContainer((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) childData);
82 return createLeaf(childData);
85 protected abstract DataTreeCandidateNode createContainer(NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> childData);
87 protected abstract DataTreeCandidateNode createLeaf(NormalizedNode<?,?> childData);