2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import com.google.common.annotations.Beta;
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;
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
28 public final class ModuleIdentifierImpl implements ModuleIdentifier {
29 private final QNameModule qnameModule;
30 private final String name;
31 private final SemVer semVer;
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);
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);
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);
51 public QNameModule getQNameModule() {
56 public Date getRevision() {
57 return qnameModule.getRevision();
61 public SemVer getSemanticVersion() {
66 public String getName() {
71 public URI getNamespace() {
72 return qnameModule.getNamespace();
76 public String toString() {
77 return "ModuleIdentifierImpl{"
78 + "name='" + name + '\''
79 + ", namespace=" + getNamespace()
80 + ", revision=" + qnameModule.getFormattedRevision()
81 + ", semantic version=" + semVer
86 public boolean equals(final Object obj) {
90 if (!(obj instanceof ModuleIdentifier)) {
94 ModuleIdentifier other = (ModuleIdentifier) obj;
96 if (!name.equals(other.getName())) {
100 // only fail if this namespace is non-null
101 if (getNamespace() != null && !getNamespace().equals(other.getNamespace())) {
104 // only fail if this revision is non-null
105 if (getRevision() != null && !getRevision().equals(other.getRevision())) {
109 if (!Objects.equals(getSemanticVersion(), other.getSemanticVersion())) {
117 public int hashCode() {
118 return name.hashCode();