Address trivial eclipse warnings
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / 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.impl.schema.tree;
9 import com.google.common.base.Optional;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.ImmutableList;
14 import java.util.Collection;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
23
24 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
25     private final ModifiedNode mod;
26     private final TreeNode newMeta;
27     private final TreeNode oldMeta;
28
29     protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta,
30             final TreeNode newMeta) {
31         this.newMeta = newMeta;
32         this.oldMeta = oldMeta;
33         this.mod = Preconditions.checkNotNull(mod);
34     }
35
36     protected final ModifiedNode getMod() {
37         return mod;
38     }
39
40     protected final TreeNode getNewMeta() {
41         return newMeta;
42     }
43
44     protected final TreeNode getOldMeta() {
45         return oldMeta;
46     }
47
48     private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
49         return parent == null ? null : parent.getChild(id).orNull();
50     }
51
52     private static boolean canHaveChildren(@Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) {
53         if (oldMeta != null) {
54             return oldMeta.getData() instanceof NormalizedNodeContainer;
55         }
56         if (newMeta != null) {
57             return newMeta.getData() instanceof NormalizedNodeContainer;
58         }
59         return false;
60     }
61
62     @SuppressWarnings("unchecked")
63     private static NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getContainer(@Nullable final TreeNode meta) {
64         return (meta == null ? null : (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>)meta.getData());
65     }
66
67     private ChildNode childNode(final ModifiedNode childMod) {
68         final PathArgument id = childMod.getIdentifier();
69         return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
70     }
71
72     @Override
73     @Nonnull
74     public Collection<DataTreeCandidateNode> getChildNodes() {
75         switch (mod.getModificationType()) {
76         case APPEARED:
77         case DISAPPEARED:
78         case SUBTREE_MODIFIED:
79             return Collections2.transform(mod.getChildren(), this::childNode);
80         case UNMODIFIED:
81             // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
82             // false if both arguments are null.
83             if (!canHaveChildren(oldMeta, newMeta)) {
84                 return ImmutableList.of();
85             }
86
87             return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(),
88                 AbstractRecursiveCandidateNode::unmodifiedNode);
89         case DELETE:
90         case WRITE:
91             // This is unusual, the user is requesting we follow into an otherwise-terminal node.
92             // We need to fudge things based on before/after data to correctly fake the expectations.
93             if (!canHaveChildren(oldMeta, newMeta)) {
94                 return ImmutableList.of();
95             }
96             return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
97         default:
98             throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
99         }
100     }
101
102     @Override
103     @Nonnull
104     public ModificationType getModificationType() {
105         return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
106     }
107
108     private static Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
109         return meta == null ? Optional.absent() : Optional.of(meta.getData());
110     }
111
112     @Override
113     @Nonnull
114     public final Optional<NormalizedNode<?, ?>> getDataAfter() {
115         return optionalData(newMeta);
116     }
117
118     @Override
119     @Nonnull
120     public final Optional<NormalizedNode<?, ?>> getDataBefore() {
121         return optionalData(oldMeta);
122     }
123
124     @Override
125     public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
126         switch (mod.getModificationType()) {
127         case APPEARED:
128         case DISAPPEARED:
129         case SUBTREE_MODIFIED:
130             final Optional<ModifiedNode> childMod = mod.getChild(identifier);
131             if (childMod.isPresent()) {
132                 return childNode(childMod.get());
133             }
134             return null;
135         case UNMODIFIED:
136             if (!canHaveChildren(oldMeta, newMeta)) {
137                 return null;
138             }
139             final Optional<NormalizedNode<?, ?>> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta)
140                     .getChild(identifier);
141             return maybeChild.isPresent() ? AbstractRecursiveCandidateNode.unmodifiedNode(maybeChild.get()) : null;
142         case DELETE:
143         case WRITE:
144             if (!canHaveChildren(oldMeta, newMeta)) {
145                 return null;
146             }
147             return AbstractDataTreeCandidateNode.deltaChild(getContainer(oldMeta), getContainer(newMeta), identifier);
148         default:
149             throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
150         }
151     }
152
153     private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
154         ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
155             super(mod, oldMeta, newMeta);
156         }
157
158         @Override
159         @Nonnull
160         public PathArgument getIdentifier() {
161             return getMod().getIdentifier();
162         }
163     }
164
165     @Override
166     public String toString() {
167         return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = " +
168                 this.newMeta + "}";
169     }
170 }