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