03fde57ee243baae7c91f5b6d64f0483fbb23fdf
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / 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.stmt.rfc6020.effective;
9
10 import java.util.Date;
11 import java.util.Objects;
12 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
13 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
14 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
17
18 public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement> implements
19         ModuleImport {
20
21     private final String moduleName;
22     private final Date revision;
23     private final String prefix;
24
25     public ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
26         super(ctx);
27
28         moduleName = ctx.getStatementArgument();
29
30         RevisionDateEffectiveStatementImpl revisionDateStmt = firstEffective(RevisionDateEffectiveStatementImpl.class);
31         this.revision = (revisionDateStmt == null) ? SimpleDateFormatUtil.DEFAULT_DATE_IMP : revisionDateStmt.argument();
32
33         PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
34         if (prefixStmt != null ) {
35             this.prefix = prefixStmt.argument();
36         } else {
37             throw new MissingSubstatementException("Prefix is mandatory substatement of import statement",
38                     ctx.getStatementSourceReference());
39         }
40     }
41
42     @Override
43     public String getModuleName() {
44         return moduleName;
45     }
46
47     @Override
48     public Date getRevision() {
49         return revision;
50     }
51
52     @Override
53     public String getPrefix() {
54         return prefix;
55     }
56
57     @Override
58     public int hashCode() {
59         final int prime = 31;
60         int result = 1;
61         result = prime * result + Objects.hashCode(moduleName);
62         result = prime * result + Objects.hashCode(revision);
63         result = prime * result + Objects.hashCode(prefix);
64         return result;
65     }
66
67     @Override
68     public boolean equals(final Object obj) {
69         if (this == obj) {
70             return true;
71         }
72         if (obj == null) {
73             return false;
74         }
75         if (getClass() != obj.getClass()) {
76             return false;
77         }
78         ImportEffectiveStatementImpl other = (ImportEffectiveStatementImpl) obj;
79         if (!Objects.equals(getModuleName(), other.getModuleName())) {
80             return false;
81         }
82         if (!Objects.equals(getRevision(), other.getRevision())) {
83             return false;
84         }
85         if (!Objects.equals(getPrefix(), other.getPrefix())) {
86             return false;
87         }
88         return true;
89     }
90
91     @Override
92     public String toString() {
93         return ImportEffectiveStatementImpl.class.getSimpleName() + "[moduleName=" + moduleName + ", revision="
94                 + revision + ", prefix=" + prefix + "]";
95     }
96 }