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 = 1L;
29 private final URI namespace;
32 private final Date revision;
35 private volatile String formattedRevision;
37 private QNameModule(final URI namespace, final Date revision) {
38 this.namespace = namespace;
39 this.revision = revision;
43 * Look up specified module in the global cache and return a shared reference.
45 * @param module Module instance
46 * @return Cached instance, according to {@link org.opendaylight.yangtools.objcache.ObjectCache} policy.
48 * @deprecated Use {@link #intern()} instead.
51 public static QNameModule cachedReference(final QNameModule module) {
52 return module.intern();
56 * Return an interned reference to a equivalent QNameModule.
58 * @return Interned reference, or this object if it was interned.
60 public QNameModule intern() {
61 return INTERNER.intern(this);
65 * Create a new QName module instance with specified namespace/revision.
67 * @param namespace Module namespace
68 * @param revision Module revision
69 * @return A new, potentially shared, QNameModule instance
71 public static QNameModule create(final URI namespace, final Date revision) {
72 if (namespace == null && revision == null) {
76 return new QNameModule(namespace, revision);
79 public String getFormattedRevision() {
80 if (revision == null) {
84 String ret = formattedRevision;
86 ret = SimpleDateFormatUtil.getRevisionFormat().format(revision);
87 formattedRevision = ret;
94 * Returns the namespace of the module which is specified as argument of
95 * YANG Module <b><font color="#00FF00">namespace</font></b> keyword.
97 * @return URI format of the namespace of the module
99 public URI getNamespace() {
104 * Returns the revision date for the module.
106 * @return date of the module revision which is specified as argument of
107 * YANG Module <b><font color="#339900">revison</font></b> keyword
109 public Date getRevision() {
114 public int hashCode() {
115 final int prime = 31;
116 int result = Objects.hashCode(namespace);
117 result = prime * result + Objects.hashCode(revision);
122 public boolean equals(final Object obj) {
126 if (!(obj instanceof QNameModule)) {
129 final QNameModule other = (QNameModule) obj;
130 if (!Objects.equals(revision, other.revision)) {
133 if (!Objects.equals(namespace, other.namespace)) {
140 * Returns a namespace in form defined by section 5.6.4. of {@link https
141 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
142 * the method will return <code>null</code> <br>
143 * example "http://example.acme.com/system?revision=2008-04-01"
145 * @return namespace in form defined by section 5.6.4. of {@link https
146 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
147 * defined, the method will return <code>null</code>
150 URI getRevisionNamespace() {
152 if (namespace == null) {
157 if (revision != null) {
158 query = "revision=" + getFormattedRevision();
161 URI compositeURI = null;
163 compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
164 namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
165 } catch (final URISyntaxException e) {
172 public String toString() {
173 return MoreObjects.toStringHelper(this).omitNullValues().add("ns", getNamespace()).add("rev", getFormattedRevision()).toString();