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