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