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