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