Cleanup use of Guava library
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractRecursiveCandidateNode.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
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;
18
19 abstract class AbstractRecursiveCandidateNode extends AbstractDataTreeCandidateNode {
20
21     protected AbstractRecursiveCandidateNode(
22             final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
23         super(data);
24     }
25
26     @SuppressWarnings("unchecked")
27     static DataTreeCandidateNode deleteNode(final NormalizedNode<?, ?> data) {
28         if (data instanceof NormalizedNodeContainer) {
29             return new RecursiveDeleteCandidateNode(
30                 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
31         }
32         return new DeleteLeafCandidateNode(data);
33     }
34
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);
41         }
42         return new ReplaceLeafCandidateNode(oldData, newData);
43     }
44
45     @SuppressWarnings("unchecked")
46     static DataTreeCandidateNode unmodifiedNode(final NormalizedNode<?, ?> data) {
47         if (data instanceof NormalizedNodeContainer) {
48             return new RecursiveUnmodifiedCandidateNode(
49                 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
50         }
51         return new UnmodifiedLeafCandidateNode(data);
52     }
53
54     @SuppressWarnings("unchecked")
55     static DataTreeCandidateNode writeNode(final NormalizedNode<?, ?> data) {
56         if (data instanceof NormalizedNodeContainer) {
57             return new RecursiveWriteCandidateNode(
58                 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
59         }
60         return new WriteLeafCandidateNode(data);
61     }
62
63     protected static boolean isContainer(final NormalizedNode<?, ?> data) {
64         return data instanceof NormalizedNodeContainer;
65     }
66
67     @Override
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());
72         }
73         return null;
74     }
75
76     @Nonnull
77     @Override
78     public final Collection<DataTreeCandidateNode> getChildNodes() {
79         return Collections2.transform(getData().getValue(), this::createChild);
80     }
81
82     @SuppressWarnings("unchecked")
83     private DataTreeCandidateNode createChild(final NormalizedNode<?, ?> childData) {
84         if (isContainer(childData)) {
85             return createContainer((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) childData);
86         }
87         return createLeaf(childData);
88     }
89
90     abstract DataTreeCandidateNode createContainer(
91             NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> childData);
92
93     abstract DataTreeCandidateNode createLeaf(NormalizedNode<?,?> childData);
94 }