Split out yang-data-tree-impl
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / NoopDataTreeCandidate.java
1 /*
2  * Copyright (c) 2014 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.tree.impl;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.ImmutableList;
14 import java.util.Collection;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
22 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
23
24 /**
25  * Internal utility class for an empty candidate. We instantiate this class
26  * for empty modifications, saving memory and processing speed. Instances
27  * of this class are explicitly recognized and processing of them is skipped.
28  */
29 final class NoopDataTreeCandidate extends AbstractDataTreeCandidate {
30     private static final DataTreeCandidateNode ROOT = new DataTreeCandidateNode() {
31         @Override
32         public ModificationType getModificationType() {
33             return ModificationType.UNMODIFIED;
34         }
35
36         @Override
37         public Collection<DataTreeCandidateNode> getChildNodes() {
38             return ImmutableList.of();
39         }
40
41         @Override
42         public PathArgument getIdentifier() {
43             throw new IllegalStateException("Attempted to read identifier of the no-operation change");
44         }
45
46         @Override
47         public Optional<NormalizedNode> getDataAfter() {
48             return Optional.empty();
49         }
50
51         @Override
52         public Optional<NormalizedNode> getDataBefore() {
53             return Optional.empty();
54         }
55
56         @Override
57         public Optional<DataTreeCandidateNode> getModifiedChild(final PathArgument identifier) {
58             return Optional.empty();
59         }
60     };
61
62     private final @NonNull TreeNode afterRoot;
63
64     protected NoopDataTreeCandidate(final YangInstanceIdentifier rootPath, final ModifiedNode modificationRoot,
65             final TreeNode afterRoot) {
66         super(rootPath);
67         checkArgument(modificationRoot.getOperation() == LogicalOperation.NONE);
68         this.afterRoot = requireNonNull(afterRoot);
69     }
70
71     @Override
72     public DataTreeCandidateNode getRootNode() {
73         return ROOT;
74     }
75
76     @Override
77     protected TreeNode getTipRoot() {
78         return afterRoot;
79     }
80 }