Make MutableTreeNode @NonNullByDefault
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / AbstractModifiedNodeBasedCandidateNode.java
1 /*
2  * Copyright (c) 2015, 2016 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.tree.impl;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import java.util.Collection;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
22 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
23 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
24 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
25 import org.opendaylight.yangtools.yang.data.tree.spi.DataTreeCandidateNodes;
26
27 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
28     private final ModifiedNode mod;
29     private final TreeNode newMeta;
30     private final TreeNode oldMeta;
31
32     protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta,
33             final TreeNode newMeta) {
34         this.newMeta = newMeta;
35         this.oldMeta = oldMeta;
36         this.mod = requireNonNull(mod);
37     }
38
39     protected final ModifiedNode getMod() {
40         return mod;
41     }
42
43     protected final TreeNode getNewMeta() {
44         return newMeta;
45     }
46
47     protected final TreeNode getOldMeta() {
48         return oldMeta;
49     }
50
51     private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
52         return parent == null ? null : parent.childByArg(id);
53     }
54
55     private static boolean canHaveChildren(final @Nullable TreeNode oldMeta, final @Nullable TreeNode newMeta) {
56         return oldMeta != null && oldMeta.data() instanceof NormalizedNodeContainer
57             || newMeta != null && newMeta.data() instanceof NormalizedNodeContainer;
58     }
59
60     @SuppressWarnings("unchecked")
61     private static DistinctNodeContainer<PathArgument, NormalizedNode> getContainer(
62             final @Nullable TreeNode meta) {
63         return meta == null ? null : (DistinctNodeContainer<PathArgument, NormalizedNode>)meta.data();
64     }
65
66     private @NonNull ChildNode childNode(final ModifiedNode childMod) {
67         final var id = childMod.getIdentifier();
68         return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
69     }
70
71     @Override
72     public Collection<DataTreeCandidateNode> childNodes() {
73         return switch (mod.getModificationType()) {
74             case APPEARED, DISAPPEARED, SUBTREE_MODIFIED -> Collections2.transform(mod.getChildren(), this::childNode);
75             case UNMODIFIED -> {
76                 // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
77                 // false if both arguments are null.
78                 if (!canHaveChildren(oldMeta, newMeta)) {
79                     yield ImmutableList.of();
80                 }
81                 yield Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).body(),
82                     DataTreeCandidateNodes::unmodified);
83             }
84             case DELETE, WRITE -> {
85                 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
86                 // We need to fudge things based on before/after data to correctly fake the expectations.
87                 if (!canHaveChildren(oldMeta, newMeta)) {
88                     yield ImmutableList.of();
89                 }
90                 yield DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta));
91             }
92         };
93     }
94
95     @Override
96     public ModificationType modificationType() {
97         return verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
98     }
99
100     @Override
101     public final NormalizedNode dataBefore() {
102         return data(oldMeta);
103     }
104
105     @Override
106     public final NormalizedNode dataAfter() {
107         return data(newMeta);
108     }
109
110     private static @Nullable NormalizedNode data(final TreeNode meta) {
111         return meta == null ? null : meta.data();
112     }
113
114
115     @Override
116     public final DataTreeCandidateNode modifiedChild(final PathArgument childName) {
117         final var identifier = requireNonNull(childName);
118         return switch (mod.getModificationType()) {
119             case APPEARED, DISAPPEARED, SUBTREE_MODIFIED -> {
120                 final var child = mod.childByArg(identifier);
121                 yield child == null ? null : childNode(child);
122             }
123             case UNMODIFIED -> {
124                 if (!canHaveChildren(oldMeta, newMeta)) {
125                     yield null;
126                 }
127                 final var child = getContainer(newMeta != null ? newMeta : oldMeta).childByArg(identifier);
128                 yield child == null ? null : DataTreeCandidateNodes.unmodified(child);
129             }
130             case DELETE, WRITE -> {
131                 if (!canHaveChildren(oldMeta, newMeta)) {
132                     yield null;
133                 }
134                 yield DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta), identifier);
135             }
136         };
137     }
138
139     private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
140         ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
141             super(mod, oldMeta, newMeta);
142         }
143
144         @Override
145         public PathArgument name() {
146             return getMod().getIdentifier();
147         }
148     }
149
150     @Override
151     public String toString() {
152         return getClass().getSimpleName() + "{mod = " + mod + ", oldMeta = " + oldMeta + ", newMeta = " + newMeta + "}";
153     }
154 }