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.api.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.ImmutableList;
15 import java.util.Collection;
16 import javax.annotation.Nonnull;
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;
22 * Utility implementation of {@link DataTreeCandidateNode} which acts as if
23 * the {@link NormalizedNode} passed to it at creation time were freshly written.
25 final class NormalizedNodeDataTreeCandidateNode implements DataTreeCandidateNode {
27 * Convenience function for functional transformation of {@link NormalizedNode} into
28 * a {@link DataTreeCandidateNode}.
30 private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> FACTORY_FUNCTION =
31 input -> input == null ? null : new NormalizedNodeDataTreeCandidateNode(input);
32 private final NormalizedNode<?, ?> data;
35 * Create a new instance backed by supplied data.
37 * @param data Backing {@link NormalizedNode} data.
39 NormalizedNodeDataTreeCandidateNode(@Nonnull final NormalizedNode<?, ?> data) {
40 this.data = Preconditions.checkNotNull(data);
44 public PathArgument getIdentifier() {
45 return data.getIdentifier();
49 public Collection<DataTreeCandidateNode> getChildNodes() {
50 if (data instanceof NormalizedNodeContainer) {
51 return Collections2.transform(((NormalizedNodeContainer<?, ?, ?>) data).getValue(), FACTORY_FUNCTION);
53 return ImmutableList.of();
57 public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
58 if (data instanceof NormalizedNodeContainer) {
59 @SuppressWarnings({ "rawtypes", "unchecked" })
60 final Optional<? extends NormalizedNode<?, ?>> child = ((NormalizedNodeContainer)data).getChild(identifier);
61 return FACTORY_FUNCTION.apply(child.orNull());
67 public ModificationType getModificationType() {
68 return ModificationType.WRITE;
72 public Optional<NormalizedNode<?, ?>> getDataAfter() {
73 return Optional.of(data);
77 public Optional<NormalizedNode<?, ?>> getDataBefore() {
78 return Optional.absent();