YANGTOOLS-706: Split up base utility classes into rfc6020.util
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / action / ActionEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2016 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.rfc7950.stmt.action;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.HashSet;
13 import java.util.LinkedHashSet;
14 import java.util.Objects;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
18 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ActionStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveSchemaNode;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.InputEffectiveStatementImpl;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.OutputEffectiveStatementImpl;
32
33 final class ActionEffectiveStatementImpl extends AbstractEffectiveSchemaNode<ActionStatement>
34         implements ActionDefinition, ActionEffectiveStatement {
35     private final ContainerSchemaNode input;
36     private final ContainerSchemaNode output;
37     private final Set<TypeDefinition<?>> typeDefinitions;
38     private final Set<GroupingDefinition> groupings;
39     private final boolean augmenting;
40     private final boolean addedByUses;
41
42     ActionEffectiveStatementImpl(
43             final StmtContext<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> ctx) {
44         super(ctx);
45         this.input = firstEffective(InputEffectiveStatementImpl.class);
46         this.output = firstEffective(OutputEffectiveStatementImpl.class);
47
48         // initSubstatements
49         final Set<GroupingDefinition> groupingsInit = new HashSet<>();
50         final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
51         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
52             if (effectiveStatement instanceof GroupingDefinition) {
53                 final GroupingDefinition groupingDefinition = (GroupingDefinition) effectiveStatement;
54                 groupingsInit.add(groupingDefinition);
55             }
56             if (effectiveStatement instanceof TypedefEffectiveStatement) {
57                 final TypedefEffectiveStatement typeDef = (TypedefEffectiveStatement) effectiveStatement;
58                 final TypeDefinition<?> type = typeDef.getTypeDefinition();
59                 if (!mutableTypeDefinitions.contains(type)) {
60                     mutableTypeDefinitions.add(type);
61                 } else {
62                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
63                 }
64             }
65         }
66         this.groupings = ImmutableSet.copyOf(groupingsInit);
67         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
68
69         // initCopyType
70         final CopyHistory copyTypesFromOriginal = ctx.getCopyHistory();
71         if (copyTypesFromOriginal.contains(CopyType.ADDED_BY_USES_AUGMENTATION)) {
72             this.addedByUses = this.augmenting = true;
73         } else {
74             this.augmenting = copyTypesFromOriginal.contains(CopyType.ADDED_BY_AUGMENTATION);
75             this.addedByUses = copyTypesFromOriginal.contains(CopyType.ADDED_BY_USES);
76         }
77     }
78
79     @Override
80     public ContainerSchemaNode getInput() {
81         return input;
82     }
83
84     @Override
85     public ContainerSchemaNode getOutput() {
86         return output;
87     }
88
89     @Override
90     public Set<TypeDefinition<?>> getTypeDefinitions() {
91         return typeDefinitions;
92     }
93
94     @Override
95     public Set<GroupingDefinition> getGroupings() {
96         return groupings;
97     }
98
99     @Override
100     public boolean isAugmenting() {
101         return augmenting;
102     }
103
104     @Override
105     public boolean isAddedByUses() {
106         return addedByUses;
107     }
108
109     @Override
110     public int hashCode() {
111         return Objects.hash(getQName(), getPath());
112     }
113
114     @Override
115     public boolean equals(final Object obj) {
116         if (this == obj) {
117             return true;
118         }
119
120         if (obj == null) {
121             return false;
122         }
123
124         if (getClass() != obj.getClass()) {
125             return false;
126         }
127
128         final ActionEffectiveStatementImpl other = (ActionEffectiveStatementImpl) obj;
129         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
130     }
131
132     @Override
133     public String toString() {
134         return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).add("input", input)
135                 .add("output", output).toString();
136     }
137 }