Bug 6871: [YANG 1.1] Allow "must" in "input", "output" and "notification" statements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / GroupingEffectiveStatementImpl.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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.List;
14 import java.util.Objects;
15 import javax.annotation.Nonnull;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
19 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingStatement;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26
27 public class GroupingEffectiveStatementImpl extends
28         AbstractEffectiveDocumentedDataNodeContainer<QName, GroupingStatement> implements GroupingDefinition {
29     private final QName qname;
30     private final SchemaPath path;
31     private final boolean addedByUses;
32     private final Set<NotificationDefinition> notifications;
33     private final List<UnknownSchemaNode> unknownNodes;
34
35     public GroupingEffectiveStatementImpl(
36             final StmtContext<QName, GroupingStatement, EffectiveStatement<QName, GroupingStatement>> ctx) {
37         super(ctx);
38
39         qname = ctx.getStatementArgument();
40         path = ctx.getSchemaPath().get();
41
42         addedByUses = ctx.getCopyHistory().contains(CopyType.ADDED_BY_USES);
43
44         final ImmutableSet.Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
45         final ImmutableList.Builder<UnknownSchemaNode> b = ImmutableList.builder();
46         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
47             if (effectiveStatement instanceof NotificationDefinition) {
48                 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
49             }
50             if (effectiveStatement instanceof UnknownSchemaNode) {
51                 b.add((UnknownSchemaNode) effectiveStatement);
52             }
53         }
54
55         this.notifications = notificationsBuilder.build();
56         unknownNodes = b.build();
57     }
58
59     @Nonnull
60     @Override
61     public QName getQName() {
62         return qname;
63     }
64
65     @Nonnull
66     @Override
67     public SchemaPath getPath() {
68         return path;
69     }
70
71     @Override
72     public boolean isAddedByUses() {
73         return addedByUses;
74     }
75
76     @Nonnull
77     @Override
78     public Set<NotificationDefinition> getNotifications() {
79         return notifications;
80     }
81
82     @Override
83     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
84         return unknownNodes;
85     }
86
87     @Override
88     public int hashCode() {
89         final int prime = 31;
90         int result = 1;
91         result = prime * result + Objects.hashCode(qname);
92         result = prime * result + Objects.hashCode(path);
93         return result;
94     }
95
96     @Override
97     public boolean equals(final Object obj) {
98         if (this == obj) {
99             return true;
100         }
101         if (obj == null) {
102             return false;
103         }
104         if (getClass() != obj.getClass()) {
105             return false;
106         }
107         final GroupingEffectiveStatementImpl other = (GroupingEffectiveStatementImpl) obj;
108         return Objects.equals(qname, other.qname) && Objects.equals(path, other.path);
109     }
110
111     @Override
112     public String toString() {
113         return GroupingEffectiveStatementImpl.class.getSimpleName() + "[" + "qname=" + qname + "]";
114     }
115 }