2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.choice;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Objects;
17 import java.util.Optional;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
26 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
27 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.opendaylight.yangtools.yang.model.api.Status;
30 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.UsesNode;
34 // FIXME: hide this somewhere
35 public final class ImplicitCaseSchemaNode implements CaseSchemaNode, DerivableSchemaNode {
37 private final DataSchemaNode caseShorthandNode;
38 private final CaseSchemaNode original;
39 private final SchemaPath path;
40 private final boolean augmenting;
42 public ImplicitCaseSchemaNode(final DataSchemaNode caseShorthandNode) {
43 this.caseShorthandNode = requireNonNull(caseShorthandNode);
44 this.path = requireNonNull(caseShorthandNode.getPath().getParent());
45 this.original = getOriginalIfPresent(caseShorthandNode);
47 // We need to cache this, as it will be reset
48 this.augmenting = caseShorthandNode.isAugmenting();
53 public boolean isAugmenting() {
59 public boolean isAddedByUses() {
60 return caseShorthandNode.isAddedByUses();
64 public boolean isConfiguration() {
65 return caseShorthandNode.isConfiguration();
70 public QName getQName() {
71 return caseShorthandNode.getQName();
76 public SchemaPath getPath() {
82 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
83 return ImmutableList.of();
87 public Optional<String> getDescription() {
88 return caseShorthandNode.getDescription();
92 public Optional<String> getReference() {
93 return caseShorthandNode.getReference();
98 public Status getStatus() {
99 return caseShorthandNode.getStatus();
103 public Set<TypeDefinition<?>> getTypeDefinitions() {
104 return ImmutableSet.of();
108 public Collection<DataSchemaNode> getChildNodes() {
109 return ImmutableList.of(caseShorthandNode);
113 public Set<GroupingDefinition> getGroupings() {
114 return ImmutableSet.of();
118 public Optional<DataSchemaNode> findDataChildByName(final QName name) {
119 return name.equals(getQName()) ? Optional.of(caseShorthandNode) : Optional.empty();
123 public Set<UsesNode> getUses() {
124 return ImmutableSet.of();
128 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
129 return ImmutableSet.of();
133 public Optional<? extends SchemaNode> getOriginal() {
134 return Optional.ofNullable(original);
138 public Optional<RevisionAwareXPath> getWhenCondition() {
139 return caseShorthandNode.getWhenCondition();
143 public int hashCode() {
144 final int prime = 31;
146 result = prime * result + Objects.hashCode(getQName());
147 result = prime * result + Objects.hashCode(path);
152 public boolean equals(final Object obj) {
159 if (getClass() != obj.getClass()) {
162 ImplicitCaseSchemaNode other = (ImplicitCaseSchemaNode) obj;
163 return Objects.equals(getQName(), other.getQName()) && Objects.equals(path, other.path);
167 public String toString() {
168 return ImplicitCaseSchemaNode.class.getSimpleName() + "[" + "qname=" + getQName() + "]";
171 private static CaseSchemaNode getOriginalIfPresent(final SchemaNode caseShorthandNode) {
172 if (caseShorthandNode instanceof DerivableSchemaNode) {
173 final Optional<? extends SchemaNode> original = ((DerivableSchemaNode) caseShorthandNode).getOriginal();
174 if (original.isPresent()) {
175 return new ImplicitCaseSchemaNode((DataSchemaNode) original.get());