Cleanup use of Guava library
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AugmentEffectiveStatementImpl.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.ImmutableList;
11 import com.google.common.collect.ImmutableSet;
12 import java.net.URI;
13 import java.util.Date;
14 import java.util.List;
15 import java.util.Objects;
16 import java.util.Optional;
17 import java.util.Set;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
22 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
23 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
24 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
32
33 public final class AugmentEffectiveStatementImpl
34         extends AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
35         implements AugmentationSchema, NamespaceRevisionAware {
36     private final SchemaPath targetPath;
37     private final URI namespace;
38     private final Date revision;
39     private final Set<ActionDefinition> actions;
40     private final Set<NotificationDefinition> notifications;
41     private final List<UnknownSchemaNode> unknownNodes;
42     private final RevisionAwareXPath whenCondition;
43     private final AugmentationSchema copyOf;
44
45     public AugmentEffectiveStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement,
46             EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
47         super(ctx);
48
49         this.targetPath = ctx.getStatementArgument().asSchemaPath();
50
51         final QNameModule rootModuleQName = StmtContextUtils.getRootModuleQName(ctx);
52         this.namespace = rootModuleQName.getNamespace();
53         this.revision = rootModuleQName.getRevision();
54
55         this.copyOf = (AugmentationSchema) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
56
57         final WhenEffectiveStatementImpl whenStmt = firstEffective(WhenEffectiveStatementImpl.class);
58         this.whenCondition = whenStmt == null ? null : whenStmt.argument();
59
60         // initSubstatementCollections
61         final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
62         final ImmutableSet.Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
63         final ImmutableList.Builder<UnknownSchemaNode> listBuilder = new ImmutableList.Builder<>();
64         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
65             if (effectiveStatement instanceof ActionDefinition) {
66                 actionsBuilder.add((ActionDefinition) effectiveStatement);
67             } else if (effectiveStatement instanceof NotificationDefinition) {
68                 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
69             } else if (effectiveStatement instanceof UnknownSchemaNode) {
70                 listBuilder.add((UnknownSchemaNode) effectiveStatement);
71             }
72         }
73
74         this.actions = actionsBuilder.build();
75         this.notifications = notificationsBuilder.build();
76         this.unknownNodes = listBuilder.build();
77     }
78
79     @Override
80     public Optional<AugmentationSchema> getOriginalDefinition() {
81         return Optional.ofNullable(this.copyOf);
82     }
83
84     @Override
85     public SchemaPath getTargetPath() {
86         return targetPath;
87     }
88
89     @Override
90     public RevisionAwareXPath getWhenCondition() {
91         return whenCondition;
92     }
93
94     @Nonnull
95     @Override
96     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
97         return unknownNodes;
98     }
99
100     @Override
101     public URI getNamespace() {
102         return namespace;
103     }
104
105     @Override
106     public Set<ActionDefinition> getActions() {
107         return actions;
108     }
109
110     @Override
111     public Set<NotificationDefinition> getNotifications() {
112         return notifications;
113     }
114
115     @Override
116     public Date getRevision() {
117         return revision;
118     }
119
120     @Override
121     public int hashCode() {
122         final int prime = 17;
123         int result = 1;
124         result = prime * result + Objects.hashCode(targetPath);
125         result = prime * result + Objects.hashCode(whenCondition);
126         result = prime * result + getChildNodes().hashCode();
127         return result;
128     }
129
130     @Override
131     public boolean equals(final Object obj) {
132         if (this == obj) {
133             return true;
134         }
135         if (obj == null) {
136             return false;
137         }
138         if (getClass() != obj.getClass()) {
139             return false;
140         }
141         final AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
142         if (!Objects.equals(targetPath, other.targetPath)) {
143             return false;
144         }
145         if (!Objects.equals(whenCondition, other.whenCondition)) {
146             return false;
147         }
148         if (!getChildNodes().equals(other.getChildNodes())) {
149             return false;
150         }
151         return true;
152     }
153
154     @Override
155     public String toString() {
156         return AugmentEffectiveStatementImpl.class.getSimpleName() + "[" + "targetPath=" + targetPath + ", when="
157                 + whenCondition + "]";
158     }
159 }