YANGTOOLS-706: Retrofit EffectiveStatement interfaces into parser
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ContainerEffectiveStatementImpl.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 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
9
10 import com.google.common.collect.ImmutableSet;
11 import com.google.common.collect.ImmutableSet.Builder;
12 import java.util.Objects;
13 import java.util.Optional;
14 import java.util.Set;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24
25 public final class ContainerEffectiveStatementImpl extends AbstractEffectiveContainerSchemaNode<ContainerStatement>
26         implements ContainerEffectiveStatement, DerivableSchemaNode {
27     private final Set<ActionDefinition> actions;
28     private final Set<NotificationDefinition> notifications;
29     private final ContainerSchemaNode original;
30
31     public ContainerEffectiveStatementImpl(
32             final StmtContext<QName, ContainerStatement, EffectiveStatement<QName, ContainerStatement>> ctx) {
33         super(ctx);
34         this.original = (ContainerSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
35         final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
36         final Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
37         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
38             if (effectiveStatement instanceof ActionDefinition) {
39                 actionsBuilder.add((ActionDefinition) effectiveStatement);
40             }
41
42             if (effectiveStatement instanceof NotificationDefinition) {
43                 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
44             }
45         }
46
47         this.actions = actionsBuilder.build();
48         this.notifications = notificationsBuilder.build();
49     }
50
51     @Override
52     public Optional<ContainerSchemaNode> getOriginal() {
53         return Optional.ofNullable(original);
54     }
55
56     @Override
57     public Set<ActionDefinition> getActions() {
58         return actions;
59     }
60
61     @Override
62     public Set<NotificationDefinition> getNotifications() {
63         return notifications;
64     }
65
66     @Override
67     public int hashCode() {
68         final int prime = 31;
69         int result = 1;
70         result = prime * result + Objects.hashCode(getQName());
71         result = prime * result + Objects.hashCode(getPath());
72         return result;
73     }
74
75     @Override
76     public boolean equals(final Object obj) {
77         if (this == obj) {
78             return true;
79         }
80         if (obj == null) {
81             return false;
82         }
83         if (getClass() != obj.getClass()) {
84             return false;
85         }
86         final ContainerEffectiveStatementImpl other = (ContainerEffectiveStatementImpl) obj;
87         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
88     }
89
90     @Override
91     public String toString() {
92         return "container " + getQName().getLocalName();
93     }
94 }