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