BUG-4688: Rework SchemaContext module lookups
[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.Optional;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
18
19 /**
20  * ModuleIdentifier that can be used for indexing/searching by name.
21  * Name is only non-null attribute.
22  * Equality check on namespace and revision is only triggered if they are non-null
23  *
24  * @deprecated This class will be removed with {@link ModuleIdentifier}
25  */
26 @Deprecated
27 @Beta
28 public final class ModuleIdentifierImpl implements ModuleIdentifier {
29     private final QNameModule qnameModule;
30     private final String name;
31
32     private ModuleIdentifierImpl(final String name, final Optional<URI> namespace, final Optional<Date> revision) {
33         this.name = checkNotNull(name);
34         this.qnameModule = QNameModule.create(namespace.orElse(null), revision.orElse(null));
35     }
36
37     public static ModuleIdentifier create(final String name, final Optional<Date> revision) {
38         return new ModuleIdentifierImpl(name, Optional.empty(), revision);
39     }
40
41     public static ModuleIdentifier create(final String name, final Optional<URI> namespace,
42             final Optional<Date> revision) {
43         return new ModuleIdentifierImpl(name, namespace, revision);
44     }
45
46     @Override
47     public Optional<Date> getRevision() {
48         return Optional.ofNullable(qnameModule.getRevision());
49     }
50
51     @Override
52     public String getName() {
53         return name;
54     }
55
56     @Override
57     public String toString() {
58         return "ModuleIdentifierImpl{"
59             + "name='" + name + '\''
60             + ", revision=" + qnameModule.getFormattedRevision()
61             + '}';
62     }
63
64     @Override
65     public boolean equals(final Object obj) {
66         if (this == obj) {
67             return true;
68         }
69         if (!(obj instanceof ModuleIdentifier)) {
70             return false;
71         }
72
73         ModuleIdentifier other = (ModuleIdentifier) obj;
74
75         if (!name.equals(other.getName())) {
76             return false;
77         }
78
79         // only fail if this revision is non-null
80         if (getRevision() != null && !getRevision().equals(other.getRevision())) {
81             return false;
82         }
83
84         return true;
85     }
86
87     @Override
88     public int hashCode() {
89         return name.hashCode();
90     }
91 }