Cleanup effectiveStatements() access
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / RpcEffectiveStatementImpl.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.ImmutableSet;
11 import java.util.HashSet;
12 import java.util.LinkedHashSet;
13 import java.util.Objects;
14 import java.util.Set;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
18 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.RpcStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23
24 public class RpcEffectiveStatementImpl extends AbstractEffectiveSchemaNode<RpcStatement> implements RpcDefinition {
25     private final ContainerSchemaNode input;
26     private final ContainerSchemaNode output;
27     private final Set<TypeDefinition<?>> typeDefinitions;
28     private final Set<GroupingDefinition> groupings;
29
30     public RpcEffectiveStatementImpl(final StmtContext<QName, RpcStatement, EffectiveStatement<QName, RpcStatement>> ctx) {
31         super(ctx);
32         this.input = firstEffective(InputEffectiveStatementImpl.class);
33         this.output = firstEffective(OutputEffectiveStatementImpl.class);
34
35         // initSubstatements
36         Set<GroupingDefinition> groupingsInit = new HashSet<>();
37         Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
38         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
39             if (effectiveStatement instanceof GroupingDefinition) {
40                 GroupingDefinition groupingDefinition = (GroupingDefinition) effectiveStatement;
41                 groupingsInit.add(groupingDefinition);
42             }
43             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
44                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
45                 TypeDefinition<?> type = typeDef.getTypeDefinition();
46                 if (!mutableTypeDefinitions.contains(type)) {
47                     mutableTypeDefinitions.add(type);
48                 } else {
49                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
50                 }
51             }
52         }
53         this.groupings = ImmutableSet.copyOf(groupingsInit);
54         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
55     }
56
57     @Override
58     public ContainerSchemaNode getInput() {
59         return input;
60     }
61
62     @Override
63     public ContainerSchemaNode getOutput() {
64         return output;
65     }
66
67     @Override
68     public Set<TypeDefinition<?>> getTypeDefinitions() {
69         return typeDefinitions;
70     }
71
72     @Override
73     public Set<GroupingDefinition> getGroupings() {
74         return groupings;
75     }
76
77     @Override
78     public int hashCode() {
79         final int prime = 31;
80         int result = 1;
81         result = prime * result + Objects.hashCode(getQName());
82         result = prime * result + Objects.hashCode(getPath());
83         return result;
84     }
85
86     @Override
87     public boolean equals(final Object obj) {
88         if (this == obj) {
89             return true;
90         }
91         if (obj == null) {
92             return false;
93         }
94         if (getClass() != obj.getClass()) {
95             return false;
96         }
97         final RpcEffectiveStatementImpl other = (RpcEffectiveStatementImpl) obj;
98         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
99     }
100
101     @Override
102     public String toString() {
103         return RpcEffectiveStatementImpl.class.getSimpleName() + "[" +
104                 "qname=" +
105                 getQName() +
106                 ", path=" +
107                 getPath() +
108                 ", input=" +
109                 input +
110                 ", output=" +
111                 output +
112                 "]";
113     }
114 }