Refactor import statement implementations
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / import_ / ImportEffectiveStatementImpl.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.import_;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.collect.ImmutableList;
12 import java.util.Objects;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.concepts.SemVer;
16 import org.opendaylight.yangtools.yang.common.Revision;
17 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractDeclaredEffectiveStatement.DefaultArgument.WithSubstatements;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.DocumentedNodeMixin;
23
24 final class ImportEffectiveStatementImpl extends WithSubstatements<String, ImportStatement>
25         implements ImportEffectiveStatement, ModuleImport, DocumentedNodeMixin<String, ImportStatement> {
26     private final @Nullable Revision revision;
27     private final @Nullable SemVer semVer;
28
29     ImportEffectiveStatementImpl(final ImportStatement declared,
30             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
31             final @Nullable Revision revision, final @Nullable SemVer semVer) {
32         super(declared, substatements);
33         this.revision = revision;
34         this.semVer = semVer;
35     }
36
37     @Override
38     public String getModuleName() {
39         return argument();
40     }
41
42     @Override
43     public Optional<Revision> getRevision() {
44         return Optional.ofNullable(revision);
45     }
46
47     @Override
48     public Optional<SemVer> getSemanticVersion() {
49         return Optional.ofNullable(semVer);
50     }
51
52     @Override
53     public String getPrefix() {
54         return getDeclared().getPrefix().getValue();
55     }
56
57     @Override
58     public int hashCode() {
59         return Objects.hash(getModuleName(), revision, getPrefix(), semVer, getDescription().orElse(null),
60             getReference().orElse(null));
61     }
62
63     @Override
64     public boolean equals(final Object obj) {
65         if (this == obj) {
66             return true;
67         }
68         if (obj == null) {
69             return false;
70         }
71         if (getClass() != obj.getClass()) {
72             return false;
73         }
74         final ImportEffectiveStatementImpl other = (ImportEffectiveStatementImpl) obj;
75         return Objects.equals(getModuleName(), other.getModuleName()) && Objects.equals(revision, other.revision)
76                 && Objects.equals(semVer, other.semVer) && Objects.equals(getPrefix(), other.getPrefix())
77                 && Objects.equals(getDescription(), other.getDescription())
78                 && Objects.equals(getReference(), other.getReference());
79     }
80
81     @Override
82     public String toString() {
83         return MoreObjects.toStringHelper(this).omitNullValues()
84                 .add("moduleName", getModuleName())
85                 .add("revision", revision)
86                 .add("version", semVer)
87                 .add("prefix", getPrefix())
88                 .add("description", getDescription().orElse(null))
89                 .add("reference", getReference().orElse(null))
90                 .toString();
91     }
92 }