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 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);
80 public String getFormattedRevision() {
81 return revision == null ? null : revision.toString();
85 * Returns the namespace of the module which is specified as argument of
86 * YANG Module <b><font color="#00FF00">namespace</font></b> keyword.
88 * @return URI format of the namespace of the module
90 public URI getNamespace() {
95 * Returns the revision date for the module.
97 * @return date of the module revision which is specified as argument of
98 * YANG Module <b><font color="#339900">revison</font></b> keyword
100 public Optional<Revision> getRevision() {
101 return Optional.ofNullable(revision);
105 public int hashCode() {
107 hash = Objects.hash(namespace, revision);
113 public boolean equals(final Object obj) {
117 if (!(obj instanceof QNameModule)) {
120 final QNameModule other = (QNameModule) obj;
121 return Objects.equals(revision, other.revision) && Objects.equals(namespace, other.namespace);
125 * Returns a namespace in form defined by section 5.6.4. of {@link https://tools.ietf.org/html/rfc6020}, for example
126 * {@code http://example.acme.com/system?revision=2008-04-01}.
128 * @return Namespace in form defined by section 5.6.4. of {@link https://tools.ietf.org/html/rfc6020}.
129 * @throws URISyntaxException on incorrect namespace definition
132 URI getRevisionNamespace() throws URISyntaxException {
133 final String query = revision == null ? "" : "revision=" + revision.toString();
134 return new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(), namespace.getPort(),
135 namespace.getPath(), query, namespace.getFragment());
139 public String toString() {
140 return MoreObjects.toStringHelper(QNameModule.class).omitNullValues().add("ns", getNamespace())
141 .add("rev", revision).toString();