Migrate {Action,Notification}NodeContainer to JDT annotations
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / container / 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.rfc7950.stmt.container;
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.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
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.DerivableSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceEffectiveStatement;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28
29 // FIXME: hide this class
30 public final class ContainerEffectiveStatementImpl extends AbstractEffectiveContainerSchemaNode<ContainerStatement>
31         implements ContainerEffectiveStatement, DerivableSchemaNode {
32     private final @NonNull Set<ActionDefinition> actions;
33     private final @NonNull Set<NotificationDefinition> notifications;
34     private final @Nullable ContainerSchemaNode original;
35     private final boolean presence;
36
37     ContainerEffectiveStatementImpl(
38             final StmtContext<QName, ContainerStatement, EffectiveStatement<QName, ContainerStatement>> ctx) {
39         super(ctx);
40         this.original = (ContainerSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
41         final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
42         final Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
43         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
44             if (effectiveStatement instanceof ActionDefinition) {
45                 actionsBuilder.add((ActionDefinition) effectiveStatement);
46             }
47
48             if (effectiveStatement instanceof NotificationDefinition) {
49                 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
50             }
51         }
52
53         this.actions = actionsBuilder.build();
54         this.notifications = notificationsBuilder.build();
55         presence = findFirstEffectiveSubstatement(PresenceEffectiveStatement.class).isPresent();
56     }
57
58     @Override
59     public Optional<ContainerSchemaNode> getOriginal() {
60         return Optional.ofNullable(original);
61     }
62
63     @Override
64     public Set<ActionDefinition> getActions() {
65         return actions;
66     }
67
68     @Override
69     public Set<NotificationDefinition> getNotifications() {
70         return notifications;
71     }
72
73     @Override
74     public boolean isPresenceContainer() {
75         return presence;
76     }
77
78     @Override
79     public int hashCode() {
80         final int prime = 31;
81         int result = 1;
82         result = prime * result + Objects.hashCode(getQName());
83         result = prime * result + Objects.hashCode(getPath());
84         return result;
85     }
86
87     @Override
88     public boolean equals(final Object obj) {
89         if (this == obj) {
90             return true;
91         }
92         if (obj == null) {
93             return false;
94         }
95         if (getClass() != obj.getClass()) {
96             return false;
97         }
98         final ContainerEffectiveStatementImpl other = (ContainerEffectiveStatementImpl) obj;
99         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
100     }
101
102     @Override
103     public String toString() {
104         return "container " + getQName().getLocalName();
105     }
106 }