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