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.common;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.collect.Interner;
14 import com.google.common.collect.Interners;
15 import java.io.Serializable;
17 import java.net.URISyntaxException;
18 import java.util.Objects;
19 import java.util.Optional;
20 import javax.annotation.Nullable;
21 import org.opendaylight.yangtools.concepts.Immutable;
23 public final class QNameModule implements Comparable<QNameModule>, Immutable, Serializable {
24 private static final Interner<QNameModule> INTERNER = Interners.newWeakInterner();
25 private static final long serialVersionUID = 3L;
27 private final URI namespace;
30 private final Revision revision;
32 private transient int hash;
34 private QNameModule(final URI namespace, final Revision revision) {
35 this.namespace = requireNonNull(namespace);
36 this.revision = revision;
40 * Return an interned reference to a equivalent QNameModule.
42 * @return Interned reference, or this object if it was interned.
44 public QNameModule intern() {
45 return INTERNER.intern(this);
49 * Create a new QName module instance with specified namespace/revision.
51 * @param namespace Module namespace
52 * @param revision Module revision
53 * @return A new, potentially shared, QNameModule instance
55 public static QNameModule create(final URI namespace, final Optional<Revision> revision) {
56 return new QNameModule(namespace, revision.orElse(null));
60 * Create a new QName module instance with specified namespace and norevision.
62 * @param namespace Module namespace
63 * @return A new, potentially shared, QNameModule instance
65 public static QNameModule create(final URI namespace) {
66 return new QNameModule(namespace, null);
70 * Create a new QName module instance with specified namespace/revision.
72 * @param namespace Module namespace
73 * @param revision Module revision
74 * @return A new, potentially shared, QNameModule instance
76 public static QNameModule create(final URI namespace, @Nullable final Revision revision) {
77 return new QNameModule(namespace, revision);
81 * Returns the namespace of the module which is specified as argument of
82 * YANG Module <b><font color="#00FF00">namespace</font></b> keyword.
84 * @return URI format of the namespace of the module
86 public URI getNamespace() {
91 * Returns the revision date for the module.
93 * @return date of the module revision which is specified as argument of
94 * YANG Module <b><font color="#339900">revison</font></b> keyword
96 public Optional<Revision> getRevision() {
97 return Optional.ofNullable(revision);
101 @SuppressWarnings("checkstyle:parameterName")
102 public int compareTo(final QNameModule o) {
103 int cmp = namespace.compareTo(o.namespace);
107 return Revision.compare(revision, o.revision);
111 public int hashCode() {
113 hash = Objects.hash(namespace, revision);
119 public boolean equals(final Object obj) {
123 if (!(obj instanceof QNameModule)) {
126 final QNameModule other = (QNameModule) obj;
127 return Objects.equals(revision, other.revision) && Objects.equals(namespace, other.namespace);
131 * Returns a namespace in form defined by section 5.6.4. of
132 * <a href=https://tools.ietf.org/html/rfc6020">RFC6020</a>, for example
133 * {@code http://example.acme.com/system?revision=2008-04-01}.
135 * @return Namespace in form defined by section 5.6.4. of RFC6020.
136 * @throws URISyntaxException on incorrect namespace definition
139 URI getRevisionNamespace() throws URISyntaxException {
140 final String query = revision == null ? "" : "revision=" + revision.toString();
141 return new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(), namespace.getPort(),
142 namespace.getPath(), query, namespace.getFragment());
146 public String toString() {
147 return MoreObjects.toStringHelper(QNameModule.class).omitNullValues().add("ns", getNamespace())
148 .add("rev", revision).toString();