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