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