6e825dd666473fe878c46deb8bbc85f41c732517
[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 org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
13
14 import com.google.common.base.Optional;
15 import java.net.URI;
16 import java.util.Date;
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))) {
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
83         Date defaultRev = SimpleDateFormatUtil.DEFAULT_DATE_REV;
84         Date defaultImp = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
85
86         // if revision is in import only, spec says that it is undefined which
87         // revision to take
88         if (getRevision() == defaultImp ^ that.getRevision() == defaultImp) {
89             return true;
90         }
91
92         // default and none revisions taken as equal
93         if ((defaultRev.equals(getRevision()) && that.getRevision() == null)
94                 || (defaultRev.equals(that.getRevision()) && getRevision() == null)) {
95             return true;
96         }
97
98         // else if none of them is default and one null
99         if (getRevision() == null ^ that.getRevision() == null) {
100             return false;
101         }
102
103         // only fail if this revision is non-null
104         if (getRevision() != null && that.getRevision() != null && !getRevision().equals(that.getRevision())) {
105             return false;
106         }
107
108         return true;
109     }
110
111     @Override
112     public int hashCode() {
113         return name.hashCode();
114     }
115 }