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