Drop unneeded generic type specifiers
[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 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 java.util.Collection;
15 import java.util.Collections;
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     /**
27      * Convenience function for functional transformation of {@link NormalizedNode} into
28      * a {@link DataTreeCandidateNode}.
29      */
30     private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> FACTORY_FUNCTION = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
31         @Override
32         public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
33             return input == null ? null : new NormalizedNodeDataTreeCandidateNode(input);
34         }
35     };
36     private final NormalizedNode<?, ?> data;
37
38     /**
39      * Create a new instance backed by supplied data.
40      *
41      * @param data Backing {@link NormalizedNode} data.
42      */
43     NormalizedNodeDataTreeCandidateNode(@Nonnull final NormalizedNode<?, ?> data) {
44         this.data = Preconditions.checkNotNull(data);
45     }
46
47     @Override
48     public PathArgument getIdentifier() {
49         return data.getIdentifier();
50     }
51
52     @Override
53     public Collection<DataTreeCandidateNode> getChildNodes() {
54         if (data instanceof NormalizedNodeContainer) {
55             return Collections2.transform(((NormalizedNodeContainer<?, ?, ?>) data).getValue(), FACTORY_FUNCTION);
56         } else {
57             return Collections.emptyList();
58         }
59     }
60
61     @Override
62     public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
63         if (data instanceof NormalizedNodeContainer) {
64             @SuppressWarnings({ "rawtypes", "unchecked" })
65             final Optional<? extends NormalizedNode<?, ?>> child = ((NormalizedNodeContainer)data).getChild(identifier);
66             return FACTORY_FUNCTION.apply(child.orNull());
67         } else {
68             return null;
69         }
70     }
71
72     @Override
73     public ModificationType getModificationType() {
74         return ModificationType.WRITE;
75     }
76
77     @Override
78     public Optional<NormalizedNode<?, ?>> getDataAfter() {
79         return Optional.of(data);
80     }
81
82     @Override
83     public Optional<NormalizedNode<?, ?>> getDataBefore() {
84         return Optional.absent();
85     }
86 }