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