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.collect.ImmutableList;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.LinkedHashSet;
16 import java.util.List;
18 import java.util.Objects;
19 import java.util.Optional;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
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.UnknownSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.UsesNode;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
33 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
37 public final class UsesEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, UsesStatement>
39 private final SchemaPath groupingPath;
40 private final boolean addedByUses;
41 private final Map<SchemaPath, SchemaNode> refines;
42 private final Set<AugmentationSchema> augmentations;
43 private final List<UnknownSchemaNode> unknownNodes;
44 private final RevisionAwareXPath whenCondition;
46 public UsesEffectiveStatementImpl(
47 final StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
51 final StmtContext<?, GroupingStatement, EffectiveStatement<QName, GroupingStatement>> grpCtx =
52 ctx.getFromNamespace(GroupingNamespace.class, ctx.getStatementArgument());
53 this.groupingPath = grpCtx.getSchemaPath().get();
56 addedByUses = ctx.getCopyHistory().contains(CopyType.ADDED_BY_USES);
58 // initSubstatementCollections
59 final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
60 final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
61 final Map<SchemaPath, SchemaNode> refinesInit = new HashMap<>();
62 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
63 if (effectiveStatement instanceof UnknownSchemaNode) {
64 final UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
65 unknownNodesInit.add(unknownNode);
67 if (effectiveStatement instanceof AugmentationSchema) {
68 final AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
69 augmentationsInit.add(augmentationSchema);
71 if (effectiveStatement instanceof RefineEffectiveStatementImpl) {
72 final RefineEffectiveStatementImpl refineStmt = (RefineEffectiveStatementImpl) effectiveStatement;
73 final SchemaNodeIdentifier identifier = refineStmt.argument();
74 refinesInit.put(identifier.asSchemaPath(), refineStmt.getRefineTargetNode());
77 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
78 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
79 this.refines = ImmutableMap.copyOf(refinesInit);
81 final WhenEffectiveStatementImpl whenStmt = firstEffective(WhenEffectiveStatementImpl.class);
82 this.whenCondition = whenStmt == null ? null : whenStmt.argument();
87 public SchemaPath getGroupingPath() {
93 public Set<AugmentationSchema> getAugmentations() {
98 public boolean isAugmenting() {
103 public boolean isAddedByUses() {
109 public Map<SchemaPath, SchemaNode> getRefines() {
115 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
121 public Optional<RevisionAwareXPath> getWhenCondition() {
122 return Optional.ofNullable(whenCondition);
126 public int hashCode() {
127 final int prime = 31;
129 result = prime * result + Objects.hashCode(groupingPath);
130 result = prime * result + Objects.hashCode(augmentations);
135 public boolean equals(final Object obj) {
142 if (getClass() != obj.getClass()) {
145 final UsesEffectiveStatementImpl other = (UsesEffectiveStatementImpl) obj;
146 return Objects.equals(groupingPath, other.groupingPath) && Objects.equals(augmentations, other.augmentations);
150 public String toString() {
151 return UsesEffectiveStatementImpl.class.getSimpleName() + "[groupingPath=" + groupingPath + "]";