2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
14 import com.google.common.base.Optional;
16 import java.util.Date;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
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
25 public class ModuleIdentifierImpl implements ModuleIdentifier {
26 private final QNameModule qnameModule;
27 private final String name;
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());
35 public QNameModule getQNameModule() {
40 public Date getRevision() {
41 return qnameModule.getRevision();
45 public String getName() {
50 public URI getNamespace() {
51 return qnameModule.getNamespace();
55 public String toString() {
56 return "ModuleIdentifierImpl{" +
57 "name='" + name + '\'' +
58 ", namespace=" + getNamespace() +
59 ", revision=" + getRevision() +
64 public boolean equals(final Object o) {
68 if (o == null || (!(o instanceof ModuleIdentifier))) {
72 ModuleIdentifier that = (ModuleIdentifier) o;
74 if (!name.equals(that.getName())) {
78 // only fail if this namespace is non-null
79 if (getNamespace() != null && !getNamespace().equals(that.getNamespace())) {
83 Date defaultRev = SimpleDateFormatUtil.DEFAULT_DATE_REV;
84 Date defaultImp = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
86 // if revision is in import only, spec says that it is undefined which
88 if (getRevision() == defaultImp ^ that.getRevision() == defaultImp) {
92 // default and none revisions taken as equal
93 if ((defaultRev.equals(getRevision()) && that.getRevision() == null)
94 || (defaultRev.equals(that.getRevision()) && getRevision() == null)) {
98 // else if none of them is default and one null
99 if (getRevision() == null ^ that.getRevision() == null) {
103 // only fail if this revision is non-null
104 if (getRevision() != null && that.getRevision() != null && !getRevision().equals(that.getRevision())) {
112 public int hashCode() {
113 return name.hashCode();