3ca205c67ec25bd535109a14c87d15d8adcb55d6
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.Optional;
16 import javax.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.api.schema.tree.DataTreeCandidateNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
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         @Nonnull
33         public ModificationType getModificationType() {
34             return ModificationType.UNMODIFIED;
35         }
36
37         @Nonnull
38         @Override
39         public Collection<DataTreeCandidateNode> getChildNodes() {
40             return Collections.emptyList();
41         }
42
43         @Override
44         @Nonnull
45         public PathArgument getIdentifier() {
46             throw new IllegalStateException("Attempted to read identifier of the no-operation change");
47         }
48
49         @Override
50         @Nonnull
51         public Optional<NormalizedNode<?, ?>> getDataAfter() {
52             return Optional.empty();
53         }
54
55         @Override
56         @Nonnull
57         public Optional<NormalizedNode<?, ?>> getDataBefore() {
58             return Optional.empty();
59         }
60
61         @Override
62         public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
63             return null;
64         }
65     };
66     private final TreeNode afterRoot;
67
68     protected NoopDataTreeCandidate(final YangInstanceIdentifier rootPath, final ModifiedNode modificationRoot,
69             final TreeNode afterRoot) {
70         super(rootPath);
71         checkArgument(modificationRoot.getOperation() == LogicalOperation.NONE);
72         this.afterRoot = requireNonNull(afterRoot);
73     }
74
75     @Override
76     public DataTreeCandidateNode getRootNode() {
77         return ROOT;
78     }
79
80     @Override
81     @Nonnull
82     protected TreeNode getTipRoot() {
83         return afterRoot;
84     }
85 }