Remove hashCode()/equals() from SchemaNode implementations
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / AbstractEffectiveOperationDefinition.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.rfc7950.stmt;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.HashSet;
14 import java.util.LinkedHashSet;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
19 import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27
28 @Deprecated(forRemoval = true)
29 public abstract class AbstractEffectiveOperationDefinition<D extends DeclaredStatement<QName>>
30         extends AbstractEffectiveSchemaNode<D> implements OperationDefinition {
31     private final ImmutableSet<TypeDefinition<?>> typeDefinitions;
32     private final ImmutableSet<GroupingDefinition> groupings;
33     private final ContainerSchemaNode input;
34     private final ContainerSchemaNode output;
35
36     protected AbstractEffectiveOperationDefinition(final StmtContext<QName, D, ?> ctx) {
37         super(ctx);
38         input = findAsContainer(this, InputEffectiveStatement.class);
39         output = findAsContainer(this, OutputEffectiveStatement.class);
40
41         // initSubstatements
42         final Set<GroupingDefinition> groupingsInit = new HashSet<>();
43         final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
44         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
45             if (effectiveStatement instanceof GroupingDefinition
46                     && !groupingsInit.add((GroupingDefinition) effectiveStatement)) {
47                 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
48             }
49             if (effectiveStatement instanceof TypedefEffectiveStatement) {
50                 final TypeDefinition<?> type = ((TypedefEffectiveStatement) effectiveStatement).getTypeDefinition();
51                 if (!mutableTypeDefinitions.add(type)) {
52                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
53                 }
54             }
55         }
56         this.groupings = ImmutableSet.copyOf(groupingsInit);
57         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
58     }
59
60     private static ContainerSchemaNode findAsContainer(final EffectiveStatement<?, ?> parent,
61             final Class<? extends EffectiveStatement<QName, ?>> statementType) {
62         final EffectiveStatement<?, ?> statement = parent.findFirstEffectiveSubstatement(statementType).get();
63         Verify.verify(statement instanceof ContainerSchemaNode, "Child statement %s is not a ContainerSchemaNode");
64         return (ContainerSchemaNode) statement;
65     }
66
67     @Override
68     public final ContainerSchemaNode getInput() {
69         return input;
70     }
71
72     @Override
73     public final ContainerSchemaNode getOutput() {
74         return output;
75     }
76
77     @Override
78     public final Set<TypeDefinition<?>> getTypeDefinitions() {
79         return typeDefinitions;
80     }
81
82     @Override
83     public final Set<GroupingDefinition> getGroupings() {
84         return groupings;
85     }
86
87     @Override
88     public final String toString() {
89         return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).add("input", input)
90                 .add("output", output).toString();
91     }
92 }