9e5881530987c2d01618582067688052c24ff53f
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / submodule / SubmoduleEffectiveStatementImpl.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.submodule;
9
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
11
12 import java.util.Objects;
13 import java.util.Optional;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.common.Revision;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
24
25 final class SubmoduleEffectiveStatementImpl extends AbstractEffectiveModule<SubmoduleStatement>
26         implements SubmoduleEffectiveStatement {
27
28     private final QNameModule qnameModule;
29
30     SubmoduleEffectiveStatementImpl(
31             final StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
32         super(ctx);
33
34         final String belongsToModuleName = firstAttributeOf(ctx.declaredSubstatements(), BelongsToStatement.class);
35         final QNameModule belongsToModuleQName = ctx.getFromNamespace(ModuleNameToModuleQName.class,
36                 belongsToModuleName);
37
38         final Optional<Revision> submoduleRevision = findFirstEffectiveSubstatementArgument(
39             RevisionEffectiveStatement.class);
40         this.qnameModule = QNameModule.create(belongsToModuleQName.getNamespace(), submoduleRevision).intern();
41     }
42
43     @Override
44     public QNameModule getQNameModule() {
45         return qnameModule;
46     }
47
48     @Override
49     public int hashCode() {
50         return Objects.hash(getName(), getYangVersion(), qnameModule);
51     }
52
53     @Override
54     public boolean equals(final Object obj) {
55         if (this == obj) {
56             return true;
57         }
58         if (!(obj instanceof SubmoduleEffectiveStatementImpl)) {
59             return false;
60         }
61         final SubmoduleEffectiveStatementImpl other = (SubmoduleEffectiveStatementImpl) obj;
62         return Objects.equals(getName(), other.getName()) && qnameModule.equals(other.qnameModule)
63                 && Objects.equals(getYangVersion(), other.getYangVersion());
64     }
65 }