2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
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;
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;
24 abstract class AbstractDataTreeCandidateNode implements DataTreeCandidateNode {
25 private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_DELETED_NODES = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
27 public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
28 return AbstractRecursiveCandidateNode.deleteNode(input);
30 }; private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_WRITTEN_NODES = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
32 public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
33 return AbstractRecursiveCandidateNode.writeNode(input);
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);
42 if (oldData == null) {
43 return Collections2.transform(newData.getValue(), TO_WRITTEN_NODES);
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);
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);
61 node = AbstractRecursiveCandidateNode.replaceNode(oldChild, child);
67 for (NormalizedNode<?, ?> child : oldChildren.values()) {
68 ret.add(AbstractRecursiveCandidateNode.deleteNode(child));
74 private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?,?>> data;
76 protected AbstractDataTreeCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
77 this.data = Preconditions.checkNotNull(data);
80 protected final Optional<NormalizedNode<?, ?>> dataOptional() {
81 return Optional.<NormalizedNode<?, ?>>of(data);
85 public final PathArgument getIdentifier() {
86 return data.getIdentifier();
89 protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {