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