Cleanup use of Guava library
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / NormalizedNodeDataTreeCandidateNode.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.api.schema.tree;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.ImmutableList;
14 import java.util.Collection;
15 import java.util.Optional;
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;
20
21 /**
22  * Utility implementation of {@link DataTreeCandidateNode} which acts as if
23  * the {@link NormalizedNode} passed to it at creation time were freshly written.
24  */
25 final class NormalizedNodeDataTreeCandidateNode implements DataTreeCandidateNode {
26     private final NormalizedNode<?, ?> data;
27
28     /**
29      * Create a new instance backed by supplied data.
30      *
31      * @param data Backing {@link NormalizedNode} data.
32      */
33     NormalizedNodeDataTreeCandidateNode(@Nonnull final NormalizedNode<?, ?> data) {
34         this.data = requireNonNull(data);
35     }
36
37     @Nonnull
38     @Override
39     public PathArgument getIdentifier() {
40         return data.getIdentifier();
41     }
42
43     @Nonnull
44     @Override
45     public Collection<DataTreeCandidateNode> getChildNodes() {
46         if (data instanceof NormalizedNodeContainer) {
47             return Collections2.transform(((NormalizedNodeContainer<?, ?, ?>) data).getValue(),
48                 input -> input == null ? null : new NormalizedNodeDataTreeCandidateNode(input));
49         }
50         return ImmutableList.of();
51     }
52
53     @Override
54     public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
55         if (data instanceof NormalizedNodeContainer) {
56             @SuppressWarnings({ "rawtypes", "unchecked" })
57             final Optional<? extends NormalizedNode<?, ?>> child = ((NormalizedNodeContainer)data).getChild(identifier);
58             return child.map(input -> new NormalizedNodeDataTreeCandidateNode(input)).orElse(null);
59         }
60         return null;
61     }
62
63     @Nonnull
64     @Override
65     public ModificationType getModificationType() {
66         return ModificationType.WRITE;
67     }
68
69     @Nonnull
70     @Override
71     public Optional<NormalizedNode<?, ?>> getDataAfter() {
72         return Optional.of(data);
73     }
74
75     @Nonnull
76     @Override
77     public Optional<NormalizedNode<?, ?>> getDataBefore() {
78         return Optional.empty();
79     }
80 }