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