Merge "Cleanup some major sonar warning in yang/*"
[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 com.google.common.collect.Maps;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Map;
18 import javax.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
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.api.schema.tree.DataTreeCandidateNode;
23
24 abstract class AbstractDataTreeCandidateNode implements DataTreeCandidateNode {
25     private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_DELETED_NODES = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
26         @Override
27         public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
28             return AbstractRecursiveCandidateNode.deleteNode(input);
29         }
30     };    private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_WRITTEN_NODES = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
31         @Override
32         public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
33             return AbstractRecursiveCandidateNode.writeNode(input);
34         }
35     };
36
37     static Collection<DataTreeCandidateNode> deltaChildren(@Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
38             @Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData) {
39         if (newData == null) {
40             return Collections2.transform(oldData.getValue(), TO_DELETED_NODES);
41         }
42         if (oldData == null) {
43             return Collections2.transform(newData.getValue(), TO_WRITTEN_NODES);
44         }
45
46         // Create index for fast cross-references
47         // FIXME: speed this up by exposing maps inside ImmutableMapNode and similar.
48         final Map<PathArgument, NormalizedNode<?, ?>> oldChildren = Maps.newHashMapWithExpectedSize(oldData.getValue().size());
49         for (NormalizedNode<?, ?> child : oldData.getValue()) {
50             oldChildren.put(child.getIdentifier(), child);
51         }
52
53         final Collection<DataTreeCandidateNode> ret = new ArrayList<>(Math.max(oldChildren.size(), newData.getValue().size()));
54         for (NormalizedNode<?, ?> child : newData.getValue()) {
55             // Slight optimization iterator length
56             final NormalizedNode<?, ?> oldChild = oldChildren.remove(child.getIdentifier());
57             final DataTreeCandidateNode node;
58             if (oldChild == null) {
59                 node = AbstractRecursiveCandidateNode.writeNode(child);
60             } else {
61                 node = AbstractRecursiveCandidateNode.replaceNode(oldChild, child);
62             }
63
64             ret.add(node);
65         }
66
67         for (NormalizedNode<?, ?> child : oldChildren.values()) {
68             ret.add(AbstractRecursiveCandidateNode.deleteNode(child));
69         }
70
71         return ret;
72     }
73
74     private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?,?>> data;
75
76     protected AbstractDataTreeCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
77         this.data = Preconditions.checkNotNull(data);
78     }
79
80     protected final Optional<NormalizedNode<?, ?>> dataOptional() {
81         return Optional.<NormalizedNode<?, ?>>of(data);
82     }
83
84     @Override
85     public final PathArgument getIdentifier() {
86         return data.getIdentifier();
87     }
88
89     protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
90         return data;
91     }
92 }