YANGTOOLS-706: Retrofit EffectiveStatement interfaces into parser
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / UsesEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
9
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;
17 import java.util.Map;
18 import java.util.Objects;
19 import java.util.Optional;
20 import java.util.Set;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
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.UsesEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
34 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
37
38 public final class UsesEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, UsesStatement>
39         implements UsesEffectiveStatement, UsesNode {
40     private final SchemaPath groupingPath;
41     private final boolean addedByUses;
42     private final Map<SchemaPath, SchemaNode> refines;
43     private final Set<AugmentationSchemaNode> augmentations;
44     private final List<UnknownSchemaNode> unknownNodes;
45     private final RevisionAwareXPath whenCondition;
46
47     public UsesEffectiveStatementImpl(
48             final StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
49         super(ctx);
50
51         // initGroupingPath
52         final StmtContext<?, GroupingStatement, EffectiveStatement<QName, GroupingStatement>> grpCtx =
53                 ctx.getFromNamespace(GroupingNamespace.class, ctx.getStatementArgument());
54         this.groupingPath = grpCtx.getSchemaPath().get();
55
56         // initCopyType
57         addedByUses = ctx.getCopyHistory().contains(CopyType.ADDED_BY_USES);
58
59         // initSubstatementCollections
60         final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
61         final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
62         final Map<SchemaPath, SchemaNode> refinesInit = new HashMap<>();
63         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
64             if (effectiveStatement instanceof UnknownSchemaNode) {
65                 final UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
66                 unknownNodesInit.add(unknownNode);
67             }
68             if (effectiveStatement instanceof AugmentationSchemaNode) {
69                 final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
70                 augmentationsInit.add(augmentationSchema);
71             }
72             if (effectiveStatement instanceof RefineEffectiveStatementImpl) {
73                 final RefineEffectiveStatementImpl refineStmt = (RefineEffectiveStatementImpl) effectiveStatement;
74                 final SchemaNodeIdentifier identifier = refineStmt.argument();
75                 refinesInit.put(identifier.asSchemaPath(), refineStmt.getRefineTargetNode());
76             }
77         }
78         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
79         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
80         this.refines = ImmutableMap.copyOf(refinesInit);
81
82         final WhenEffectiveStatementImpl whenStmt = firstEffective(WhenEffectiveStatementImpl.class);
83         this.whenCondition = whenStmt == null ? null : whenStmt.argument();
84     }
85
86     @Nonnull
87     @Override
88     public SchemaPath getGroupingPath() {
89         return groupingPath;
90     }
91
92     @Nonnull
93     @Override
94     public Set<AugmentationSchemaNode> getAugmentations() {
95         return augmentations;
96     }
97
98     @Override
99     public boolean isAugmenting() {
100         return false;
101     }
102
103     @Override
104     public boolean isAddedByUses() {
105         return addedByUses;
106     }
107
108     @Nonnull
109     @Override
110     public Map<SchemaPath, SchemaNode> getRefines() {
111         return refines;
112     }
113
114     @Nonnull
115     @Override
116     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
117         return unknownNodes;
118     }
119
120     @Nonnull
121     @Override
122     public Optional<RevisionAwareXPath> getWhenCondition() {
123         return Optional.ofNullable(whenCondition);
124     }
125
126     @Override
127     public int hashCode() {
128         final int prime = 31;
129         int result = 1;
130         result = prime * result + Objects.hashCode(groupingPath);
131         result = prime * result + Objects.hashCode(augmentations);
132         return result;
133     }
134
135     @Override
136     public boolean equals(final Object obj) {
137         if (this == obj) {
138             return true;
139         }
140         if (obj == null) {
141             return false;
142         }
143         if (getClass() != obj.getClass()) {
144             return false;
145         }
146         final UsesEffectiveStatementImpl other = (UsesEffectiveStatementImpl) obj;
147         return Objects.equals(groupingPath, other.groupingPath) && Objects.equals(augmentations, other.augmentations);
148     }
149
150     @Override
151     public String toString() {
152         return UsesEffectiveStatementImpl.class.getSimpleName() + "[groupingPath=" + groupingPath + "]";
153     }
154 }