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.ChoiceCaseNode;
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 ImplicitChoiceCaseNode implements ChoiceCaseNode, DerivableSchemaNode {
37 private final DataSchemaNode caseShorthandNode;
38 private final ChoiceCaseNode original;
39 private final SchemaPath path;
40 private final boolean augmenting;
42 public ImplicitChoiceCaseNode(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();
52 public boolean isAugmenting() {
57 public boolean isAddedByUses() {
58 return caseShorthandNode.isAddedByUses();
62 public boolean isConfiguration() {
63 return caseShorthandNode.isConfiguration();
68 public QName getQName() {
69 return caseShorthandNode.getQName();
74 public SchemaPath getPath() {
80 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
81 return ImmutableList.of();
85 public Optional<String> getDescription() {
86 return caseShorthandNode.getDescription();
90 public Optional<String> getReference() {
91 return caseShorthandNode.getReference();
96 public Status getStatus() {
97 return caseShorthandNode.getStatus();
101 public Set<TypeDefinition<?>> getTypeDefinitions() {
102 return ImmutableSet.of();
106 public Collection<DataSchemaNode> getChildNodes() {
107 return ImmutableList.of(caseShorthandNode);
111 public Set<GroupingDefinition> getGroupings() {
112 return ImmutableSet.of();
116 public Optional<DataSchemaNode> findDataChildByName(final QName name) {
117 return name.equals(getQName()) ? Optional.of(caseShorthandNode) : Optional.empty();
121 public Set<UsesNode> getUses() {
122 return ImmutableSet.of();
126 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
127 return ImmutableSet.of();
131 public Optional<? extends SchemaNode> getOriginal() {
132 return Optional.ofNullable(original);
136 public Optional<RevisionAwareXPath> getWhenCondition() {
137 return caseShorthandNode.getWhenCondition();
141 public int hashCode() {
142 final int prime = 31;
144 result = prime * result + Objects.hashCode(getQName());
145 result = prime * result + Objects.hashCode(path);
150 public boolean equals(final Object obj) {
157 if (getClass() != obj.getClass()) {
160 ImplicitChoiceCaseNode other = (ImplicitChoiceCaseNode) obj;
161 return Objects.equals(getQName(), other.getQName()) && Objects.equals(path, other.path);
165 public String toString() {
166 return ImplicitChoiceCaseNode.class.getSimpleName() + "[" + "qname=" + getQName() + "]";
169 private static ChoiceCaseNode getOriginalIfPresent(final SchemaNode caseShorthandNode) {
170 if (caseShorthandNode instanceof DerivableSchemaNode) {
171 final Optional<? extends SchemaNode> original = ((DerivableSchemaNode) caseShorthandNode).getOriginal();
172 if (original.isPresent()) {
173 return new ImplicitChoiceCaseNode((DataSchemaNode) original.get());