Bug 4662: Introduce a SemanticVersion concept - import processing
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ModuleIdentifierImpl.java
1 /*
2  * Copyright (c) 2013 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.builder.impl;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
12 import com.google.common.base.Optional;
13 import java.net.URI;
14 import java.util.Date;
15 import java.util.Objects;
16 import org.opendaylight.yangtools.concepts.SemVer;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
20
21 /**
22  * ModuleIdentifier that can be used for indexing/searching by name.
23  * Name is only non-null attribute.
24  * Equality check on namespace and revision is only triggered if they are non-null
25  *
26  */
27
28 // FIXME: This class is used widely by yang statement parser. This class should be moved to one of yang statement
29 // parser packages after removal of deprecated pre-beryllium implementation.
30 public class ModuleIdentifierImpl implements ModuleIdentifier {
31     private final QNameModule qnameModule;
32     private final String name;
33     private final SemVer semVer;
34
35     public ModuleIdentifierImpl(final String name, final Optional<URI> namespace, final Optional<Date> revision) {
36         this(name, namespace, revision, Module.DEFAULT_SEMANTIC_VERSION);
37     }
38
39     public ModuleIdentifierImpl(final String name, final Optional<URI> namespace, final Optional<Date> revision, final SemVer semVer) {
40         this.name = checkNotNull(name);
41         this.qnameModule = QNameModule.create(namespace.orNull(), revision.orNull());
42         this.semVer = (semVer == null ? Module.DEFAULT_SEMANTIC_VERSION : semVer);
43     }
44
45     @Override
46     public QNameModule getQNameModule() {
47         return qnameModule;
48     }
49
50     @Override
51     public Date getRevision() {
52         return qnameModule.getRevision();
53     }
54
55     @Override
56     public SemVer getSemanticVersion() {
57         return semVer;
58     }
59
60     @Override
61     public String getName() {
62         return name;
63     }
64
65     @Override
66     public URI getNamespace() {
67         return qnameModule.getNamespace();
68     }
69
70     @Override
71     public String toString() {
72         return "ModuleIdentifierImpl{" +
73                 "name='" + name + '\'' +
74                 ", namespace=" + getNamespace() +
75                 ", revision=" + qnameModule.getFormattedRevision() +
76                 ", semantic version=" + semVer +
77                 '}';
78     }
79
80     @Override
81     public boolean equals(final Object o) {
82         if (this == o) {
83             return true;
84         }
85         if (!(o instanceof ModuleIdentifier)) {
86             return false;
87         }
88
89         ModuleIdentifier other = (ModuleIdentifier) o;
90
91         if (!name.equals(other.getName())) {
92             return false;
93         }
94
95         // only fail if this namespace is non-null
96         if (getNamespace() != null && !getNamespace().equals(other.getNamespace())) {
97             return false;
98         }
99         // only fail if this revision is non-null
100         if (getRevision() != null && !getRevision().equals(other.getRevision())) {
101             return false;
102         }
103
104         if (!Objects.equals(getSemanticVersion(), other.getSemanticVersion())) {
105             return false;
106         }
107
108         return true;
109     }
110
111     @Override
112     public int hashCode() {
113         return name.hashCode();
114     }
115 }