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