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