/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015, 2016 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
*/
package org.opendaylight.yangtools.yang.data.impl.schema.tree;
-import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import java.util.ArrayList;
import java.util.Collection;
+import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
abstract class AbstractDataTreeCandidateNode implements DataTreeCandidateNode {
- private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_DELETED_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
- @Override
- public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
- return AbstractRecursiveCandidateNode.deleteNode(input);
- }
- };
- private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_WRITTEN_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
- @Override
- public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
- return AbstractRecursiveCandidateNode.writeNode(input);
- }
- };
-
- private static Optional<NormalizedNode<?, ?>> getChild(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> container, final PathArgument identifier) {
- if (container != null) {
- return container.getChild(identifier);
- } else {
- return Optional.absent();
- }
+ private static Optional<NormalizedNode<?, ?>> getChild(
+ final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> container,
+ final PathArgument identifier) {
+ return container == null ? Optional.absent() : container.getChild(identifier);
}
static DataTreeCandidateNode deltaChild(
final NormalizedNode<?, ?> oldChild = maybeOldChild.get();
if (maybeNewChild.isPresent()) {
return AbstractRecursiveCandidateNode.replaceNode(oldChild, maybeNewChild.get());
- } else {
- return TO_DELETED_NODE.apply(oldChild);
- }
- } else {
- if (maybeNewChild.isPresent()) {
- return TO_WRITTEN_NODE.apply(maybeNewChild.get());
- } else {
- return null;
}
+ return AbstractRecursiveCandidateNode.deleteNode(oldChild);
}
+
+ return maybeNewChild.isPresent() ? AbstractRecursiveCandidateNode.writeNode(maybeNewChild.get()) : null;
}
- static Collection<DataTreeCandidateNode> deltaChildren(@Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
+ static Collection<DataTreeCandidateNode> deltaChildren(
+ @Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
@Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData) {
+ Preconditions.checkArgument(newData != null || oldData != null,
+ "No old or new data, modification type should be NONE and deltaChildren() mustn't be called.");
if (newData == null) {
- return Collections2.transform(oldData.getValue(), TO_DELETED_NODE);
+ return Collections2.transform(oldData.getValue(), AbstractRecursiveCandidateNode::deleteNode);
}
if (oldData == null) {
- return Collections2.transform(newData.getValue(), TO_WRITTEN_NODE);
+ return Collections2.transform(newData.getValue(), AbstractRecursiveCandidateNode::writeNode);
}
/*
}
protected final Optional<NormalizedNode<?, ?>> dataOptional() {
- return Optional.<NormalizedNode<?, ?>>of(data);
+ return Optional.of(data);
}
@Override
+ @Nonnull
public final PathArgument getIdentifier() {
return data.getIdentifier();
}
protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
return data;
}
+
+ @Override
+ public String toString() {
+ return this.getClass().getSimpleName() + "{data = " + this.data + "}";
+ }
}