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.stmt.rfc6020.effective;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
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;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
21 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.opendaylight.yangtools.yang.model.api.Status;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.UsesNode;
32 final class CaseShorthandImpl implements ChoiceCaseNode, DerivableSchemaNode {
34 private final DataSchemaNode caseShorthandNode;
35 private final ChoiceCaseNode original;
36 private final SchemaPath path;
37 private final boolean augmenting;
39 CaseShorthandImpl(final DataSchemaNode caseShorthandNode) {
40 this.caseShorthandNode = Preconditions.checkNotNull(caseShorthandNode);
41 this.path = Preconditions.checkNotNull(caseShorthandNode.getPath().getParent());
42 this.original = getOriginalIfPresent(caseShorthandNode);
44 // We need to cache this, as it will be reset
45 this.augmenting = caseShorthandNode.isAugmenting();
49 public boolean isAugmenting() {
54 public boolean isAddedByUses() {
55 return caseShorthandNode.isAddedByUses();
59 public boolean isConfiguration() {
60 return caseShorthandNode.isConfiguration();
64 public ConstraintDefinition getConstraints() {
65 return caseShorthandNode.getConstraints();
69 public QName getQName() {
70 return caseShorthandNode.getQName();
74 public SchemaPath getPath() {
79 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
80 return ImmutableList.of();
84 public String getDescription() {
85 return caseShorthandNode.getDescription();
89 public String getReference() {
90 return caseShorthandNode.getReference();
94 public Status getStatus() {
95 return caseShorthandNode.getStatus();
99 public Set<TypeDefinition<?>> getTypeDefinitions() {
100 return ImmutableSet.of();
104 public Collection<DataSchemaNode> getChildNodes() {
105 return ImmutableList.of(caseShorthandNode);
109 public Set<GroupingDefinition> getGroupings() {
110 return ImmutableSet.of();
114 public DataSchemaNode getDataChildByName(final QName name) {
115 if (getQName().equals(name)) {
116 return caseShorthandNode;
123 public DataSchemaNode getDataChildByName(final String name) {
124 if (getQName().getLocalName().equals(name)) {
125 return caseShorthandNode;
132 public Set<UsesNode> getUses() {
133 return ImmutableSet.of();
137 public Set<AugmentationSchema> getAvailableAugmentations() {
138 return ImmutableSet.of();
142 public Optional<? extends SchemaNode> getOriginal() {
143 return Optional.fromNullable(original);
147 public int hashCode() {
148 final int prime = 31;
150 result = prime * result + Objects.hashCode(getQName());
151 result = prime * result + Objects.hashCode(path);
156 public boolean equals(final Object obj) {
163 if (getClass() != obj.getClass()) {
166 CaseShorthandImpl other = (CaseShorthandImpl) obj;
167 return Objects.equals(getQName(), other.getQName()) && Objects.equals(path, other.path);
171 public String toString() {
172 StringBuilder sb = new StringBuilder(CaseShorthandImpl.class.getSimpleName());
175 sb.append(getQName());
177 return sb.toString();
180 private static ChoiceCaseNode getOriginalIfPresent(final SchemaNode caseShorthandNode) {
181 if (caseShorthandNode instanceof DerivableSchemaNode) {
182 final Optional<? extends SchemaNode> original = ((DerivableSchemaNode) caseShorthandNode).getOriginal();
183 if (original.isPresent()) {
184 return new CaseShorthandImpl((DataSchemaNode) original.get());