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 com.google.common.base.MoreObjects;
11 import com.google.common.collect.Interner;
12 import com.google.common.collect.Interners;
13 import java.io.Serializable;
15 import java.net.URISyntaxException;
16 import java.util.Date;
17 import java.util.Objects;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public final class QNameModule implements Immutable, Serializable {
23 private static final Interner<QNameModule> INTERNER = Interners.newWeakInterner();
24 private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
25 private static final QNameModule NULL_INSTANCE = new QNameModule(null, null);
26 private static final long serialVersionUID = 2L;
29 private final URI namespace;
32 private final Date revision;
35 private transient volatile String formattedRevision;
37 private transient int hash;
39 private QNameModule(final URI namespace, final Date revision) {
40 this.namespace = namespace;
41 this.revision = revision;
45 * Return an interned reference to a equivalent QNameModule.
47 * @return Interned reference, or this object if it was interned.
49 public QNameModule intern() {
50 return INTERNER.intern(this);
54 * Create a new QName module instance with specified namespace/revision.
56 * @param namespace Module namespace
57 * @param revision Module revision
58 * @return A new, potentially shared, QNameModule instance
60 public static QNameModule create(final URI namespace, final Date revision) {
61 if (namespace == null && revision == null) {
65 return new QNameModule(namespace, revision);
68 public String getFormattedRevision() {
69 if (revision == null) {
73 String ret = formattedRevision;
75 ret = SimpleDateFormatUtil.getRevisionFormat().format(revision);
76 formattedRevision = ret;
83 * Returns the namespace of the module which is specified as argument of
84 * YANG Module <b><font color="#00FF00">namespace</font></b> keyword.
86 * @return URI format of the namespace of the module
88 public URI getNamespace() {
93 * Returns the revision date for the module.
95 * @return date of the module revision which is specified as argument of
96 * YANG Module <b><font color="#339900">revison</font></b> keyword
98 public Date getRevision() {
103 public int hashCode() {
105 hash = Objects.hash(namespace, revision);
111 public boolean equals(final Object obj) {
115 if (!(obj instanceof QNameModule)) {
118 final QNameModule other = (QNameModule) obj;
119 return Objects.equals(revision, other.revision) && Objects.equals(namespace, other.namespace);
123 * Returns a namespace in form defined by section 5.6.4. of {@link https
124 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
125 * the method will return <code>null</code> <br>
126 * example "http://example.acme.com/system?revision=2008-04-01"
128 * @return namespace in form defined by section 5.6.4. of {@link https
129 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
130 * defined, the method will return <code>null</code>
133 URI getRevisionNamespace() {
134 if (namespace == null) {
138 final String query = revision == null ? "" : "revision=" + getFormattedRevision();
140 return new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
141 namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
142 } catch (final URISyntaxException e) {
143 LOG.error("Failed to construct URI for {}", this, e);
149 public String toString() {
150 return MoreObjects.toStringHelper(QNameModule.class).omitNullValues().add("ns", getNamespace()).add("rev", getFormattedRevision()).toString();