Add a couple of toString() implementations
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractDataTreeCandidateNode.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.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Collections2;
14 import java.util.ArrayList;
15 import java.util.Collection;
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
22 abstract class AbstractDataTreeCandidateNode implements DataTreeCandidateNode {
23     private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_DELETED_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
24         @Override
25         public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
26             return AbstractRecursiveCandidateNode.deleteNode(input);
27         }
28     };
29     private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_WRITTEN_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
30         @Override
31         public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
32             return AbstractRecursiveCandidateNode.writeNode(input);
33         }
34     };
35
36     private static Optional<NormalizedNode<?, ?>> getChild(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> container, final PathArgument identifier) {
37         if (container != null) {
38             return container.getChild(identifier);
39         } else {
40             return Optional.absent();
41         }
42     }
43
44     static DataTreeCandidateNode deltaChild(
45             final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
46             final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData, final PathArgument identifier) {
47
48         final Optional<NormalizedNode<?, ?>> maybeNewChild = getChild(newData, identifier);
49         final Optional<NormalizedNode<?, ?>> maybeOldChild = getChild(oldData, identifier);
50         if (maybeOldChild.isPresent()) {
51             final NormalizedNode<?, ?> oldChild = maybeOldChild.get();
52             if (maybeNewChild.isPresent()) {
53                 return AbstractRecursiveCandidateNode.replaceNode(oldChild, maybeNewChild.get());
54             } else {
55                 return TO_DELETED_NODE.apply(oldChild);
56             }
57         } else {
58             if (maybeNewChild.isPresent()) {
59                 return TO_WRITTEN_NODE.apply(maybeNewChild.get());
60             } else {
61                 return null;
62             }
63         }
64     }
65
66     static Collection<DataTreeCandidateNode> deltaChildren(@Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
67             @Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData) {
68         if (newData == null) {
69             return Collections2.transform(oldData.getValue(), TO_DELETED_NODE);
70         }
71         if (oldData == null) {
72             return Collections2.transform(newData.getValue(), TO_WRITTEN_NODE);
73         }
74
75         /*
76          * This is slightly inefficient, as it requires N*F(M)+M*F(N) lookup operations, where
77          * F is dependent on the implementation of NormalizedNodeContainer.getChild().
78          *
79          * We build the return collection by iterating over new data and looking each child up
80          * in old data. Based on that we construct replaced/written nodes. We then proceed to
81          * iterate over old data and looking up each child in new data.
82          */
83         final Collection<DataTreeCandidateNode> result = new ArrayList<>();
84         for (NormalizedNode<?, ?> child : newData.getValue()) {
85             final DataTreeCandidateNode node;
86             final Optional<NormalizedNode<?, ?>> maybeOldChild = oldData.getChild(child.getIdentifier());
87
88             if (maybeOldChild.isPresent()) {
89                 // This does not find children which have not in fact been modified, as doing that
90                 // reliably would require us running a full equals() on the two nodes.
91                 node = AbstractRecursiveCandidateNode.replaceNode(maybeOldChild.get(), child);
92             } else {
93                 node = AbstractRecursiveCandidateNode.writeNode(child);
94             }
95
96             result.add(node);
97         }
98
99         // Process removals next, looking into new data to see if we processed it
100         for (NormalizedNode<?, ?> child : oldData.getValue()) {
101             if (!newData.getChild(child.getIdentifier()).isPresent()) {
102                 result.add(AbstractRecursiveCandidateNode.deleteNode(child));
103             }
104         }
105
106         return result;
107     }
108
109     private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?,?>> data;
110
111     protected AbstractDataTreeCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
112         this.data = Preconditions.checkNotNull(data);
113     }
114
115     protected final Optional<NormalizedNode<?, ?>> dataOptional() {
116         return Optional.<NormalizedNode<?, ?>>of(data);
117     }
118
119     @Override
120     public final PathArgument getIdentifier() {
121         return data.getIdentifier();
122     }
123
124     protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
125         return data;
126     }
127
128     @Override
129     public String toString() {
130         return this.getClass().getSimpleName() + "{data = " + this.data + "}";
131     }
132 }