ac939a00475f029ff147e65a2fdf2c05296865e4
[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 import com.google.common.base.Optional;
12 import java.net.URI;
13 import java.util.Date;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
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 public class ModuleIdentifierImpl implements ModuleIdentifier {
23     private final QNameModule qnameModule;
24     private final String name;
25
26     public ModuleIdentifierImpl(final String name, final Optional<URI> namespace, final Optional<Date> revision) {
27         this.name = checkNotNull(name);
28         this.qnameModule = QNameModule.create(namespace.orNull(), revision.orNull());
29     }
30
31     @Override
32     public QNameModule getQNameModule() {
33         return qnameModule;
34     }
35
36     @Override
37     public Date getRevision() {
38         return qnameModule.getRevision();
39     }
40
41     @Override
42     public String getName() {
43         return name;
44     }
45
46     @Override
47     public URI getNamespace() {
48         return qnameModule.getNamespace();
49     }
50
51     @Override
52     public String toString() {
53         return "ModuleIdentifierImpl{" +
54                 "name='" + name + '\'' +
55                 ", namespace=" + getNamespace() +
56                 ", revision=" + qnameModule.getFormattedRevision() +
57                 '}';
58     }
59
60     @Override
61     public boolean equals(final Object o) {
62         if (this == o) {
63             return true;
64         }
65         if (!(o instanceof ModuleIdentifier)) {
66             return false;
67         }
68
69         ModuleIdentifier that = (ModuleIdentifier) o;
70
71         if (!name.equals(that.getName())) {
72             return false;
73         }
74
75         // only fail if this namespace is non-null
76         if (getNamespace() != null && !getNamespace().equals(that.getNamespace())) {
77             return false;
78         }
79         // only fail if this revision is non-null
80         if (getRevision() != null && !getRevision().equals(that.getRevision())) {
81             return false;
82         }
83
84         return true;
85     }
86
87     @Override
88     public int hashCode() {
89         return name.hashCode();
90     }
91 }