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.uses;
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 org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
26 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.UsesNode;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDocumentedNode;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.refine.RefineEffectiveStatementImpl;
38 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
42 final class UsesEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, UsesStatement>
43 implements UsesEffectiveStatement, UsesNode {
44 private final @NonNull SchemaPath groupingPath;
45 private final boolean addedByUses;
46 private final @NonNull ImmutableMap<SchemaPath, SchemaNode> refines;
47 private final @NonNull ImmutableSet<AugmentationSchemaNode> augmentations;
48 private final @NonNull ImmutableList<UnknownSchemaNode> unknownNodes;
49 private final @Nullable RevisionAwareXPath whenCondition;
51 UsesEffectiveStatementImpl(final StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
55 final StmtContext<?, GroupingStatement, EffectiveStatement<QName, GroupingStatement>> grpCtx =
56 ctx.getFromNamespace(GroupingNamespace.class, ctx.coerceStatementArgument());
57 this.groupingPath = grpCtx.getSchemaPath().get();
60 addedByUses = ctx.getCopyHistory().contains(CopyType.ADDED_BY_USES);
62 // initSubstatementCollections
63 final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
64 final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
65 final Map<SchemaPath, SchemaNode> refinesInit = new HashMap<>();
66 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
67 if (effectiveStatement instanceof UnknownSchemaNode) {
68 final UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
69 unknownNodesInit.add(unknownNode);
71 if (effectiveStatement instanceof AugmentationSchemaNode) {
72 final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
73 augmentationsInit.add(augmentationSchema);
75 if (effectiveStatement instanceof RefineEffectiveStatementImpl) {
76 final RefineEffectiveStatementImpl refineStmt = (RefineEffectiveStatementImpl) effectiveStatement;
77 final SchemaNodeIdentifier identifier = refineStmt.argument();
78 refinesInit.put(identifier.asSchemaPath(), refineStmt.getRefineTargetNode());
81 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
82 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
83 this.refines = ImmutableMap.copyOf(refinesInit);
85 whenCondition = findFirstEffectiveSubstatementArgument(WhenEffectiveStatement.class).orElse(null);
89 public SchemaPath getGroupingPath() {
94 public Set<AugmentationSchemaNode> getAugmentations() {
100 public boolean isAugmenting() {
106 public boolean isAddedByUses() {
111 public Map<SchemaPath, SchemaNode> getRefines() {
116 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 + "]";