7881a9de7668ace8abaf5c3ed9ea58afe4c7cfcc
[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 /**
30  * Internal implementation of ContainerEffectiveStatement.
31  *
32  * @deprecated This class is visible only for historical purposes and is going to be hidden.
33  */
34 // FIXME: 4.0.0: hide this class
35 @Deprecated
36 public final class ContainerEffectiveStatementImpl extends AbstractEffectiveContainerSchemaNode<ContainerStatement>
37         implements ContainerEffectiveStatement, DerivableSchemaNode {
38     private final @NonNull ImmutableSet<ActionDefinition> actions;
39     private final @NonNull ImmutableSet<NotificationDefinition> notifications;
40     private final @Nullable ContainerSchemaNode original;
41     private final boolean presence;
42
43     ContainerEffectiveStatementImpl(
44             final StmtContext<QName, ContainerStatement, EffectiveStatement<QName, ContainerStatement>> ctx) {
45         super(ctx);
46         this.original = (ContainerSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
47         final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
48         final Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
49         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
50             if (effectiveStatement instanceof ActionDefinition) {
51                 actionsBuilder.add((ActionDefinition) effectiveStatement);
52             }
53
54             if (effectiveStatement instanceof NotificationDefinition) {
55                 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
56             }
57         }
58
59         this.actions = actionsBuilder.build();
60         this.notifications = notificationsBuilder.build();
61         presence = findFirstEffectiveSubstatement(PresenceEffectiveStatement.class).isPresent();
62     }
63
64     @Override
65     public Optional<ContainerSchemaNode> getOriginal() {
66         return Optional.ofNullable(original);
67     }
68
69     @Override
70     public Set<ActionDefinition> getActions() {
71         return actions;
72     }
73
74     @Override
75     public Set<NotificationDefinition> getNotifications() {
76         return notifications;
77     }
78
79     @Override
80     public boolean isPresenceContainer() {
81         return presence;
82     }
83
84     @Override
85     public int hashCode() {
86         final int prime = 31;
87         int result = 1;
88         result = prime * result + Objects.hashCode(getQName());
89         result = prime * result + Objects.hashCode(getPath());
90         return result;
91     }
92
93     @Override
94     public boolean equals(final Object obj) {
95         if (this == obj) {
96             return true;
97         }
98         if (obj == null) {
99             return false;
100         }
101         if (getClass() != obj.getClass()) {
102             return false;
103         }
104         final ContainerEffectiveStatementImpl other = (ContainerEffectiveStatementImpl) obj;
105         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
106     }
107
108     @Override
109     public String toString() {
110         return "container " + getQName().getLocalName();
111     }
112 }