df179d09e910ef84d2d365b36dd3453e79f9f31d
[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         @Nonnull
36         @Override
37         public Collection<DataTreeCandidateNode> getChildNodes() {
38             return Collections.emptyList();
39         }
40
41         @Override
42         @Nonnull
43         public PathArgument getIdentifier() {
44             throw new IllegalStateException("Attempted to read identifier of the no-operation change");
45         }
46
47         @Override
48         @Nonnull
49         public Optional<NormalizedNode<?, ?>> getDataAfter() {
50             return Optional.absent();
51         }
52
53         @Override
54         @Nonnull
55         public Optional<NormalizedNode<?, ?>> getDataBefore() {
56             return Optional.absent();
57         }
58
59         @Override
60         public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
61             return null;
62         }
63     };
64     private final TreeNode afterRoot;
65
66     protected NoopDataTreeCandidate(final YangInstanceIdentifier rootPath, final ModifiedNode modificationRoot,
67             final TreeNode afterRoot) {
68         super(rootPath);
69         Preconditions.checkArgument(modificationRoot.getOperation() == LogicalOperation.NONE);
70         this.afterRoot = Preconditions.checkNotNull(afterRoot);
71     }
72
73     @Override
74     public DataTreeCandidateNode getRootNode() {
75         return ROOT;
76     }
77
78     @Override
79     @Nonnull
80     protected TreeNode getTipRoot() {
81         return afterRoot;
82     }
83 }