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.opendaylight.yangtools.objcache.ObjectCache;
20 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public final class QNameModule implements Immutable, Serializable {
25 private static final Interner<QNameModule> INTERNER = Interners.newWeakInterner();
26 private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QNameModule.class);
27 private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
28 private static final QNameModule NULL_INSTANCE = new QNameModule(null, null);
29 private static final long serialVersionUID = 1L;
32 private final URI namespace;
35 private final Date revision;
38 private volatile String formattedRevision;
40 private QNameModule(final URI namespace, final Date revision) {
41 this.namespace = namespace;
42 this.revision = revision;
46 * Look up specified module in the global cache and return a shared reference.
48 * @param module Module instance
49 * @return Cached instance, according to {@link ObjectCache} policy.
51 * @deprecated Use {@link #intern()} instead.
54 public static QNameModule cachedReference(final QNameModule module) {
55 return CACHE.getReference(module);
59 * Return an interned reference to a equivalent QNameModule.
61 * @return Interned reference, or this object if it was interned.
63 public QNameModule intern() {
64 return INTERNER.intern(this);
68 * Create a new QName module instance with specified namespace/revision.
70 * @param namespace Module namespace
71 * @param revision Module revision
72 * @return A new, potentially shared, QNameModule instance
74 public static QNameModule create(final URI namespace, final Date revision) {
75 if (namespace == null && revision == null) {
79 return new QNameModule(namespace, revision);
82 public String getFormattedRevision() {
83 if (revision == null) {
87 String ret = formattedRevision;
89 ret = SimpleDateFormatUtil.getRevisionFormat().format(revision);
90 formattedRevision = ret;
97 * Returns the namespace of the module which is specified as argument of
98 * YANG Module <b><font color="#00FF00">namespace</font></b> keyword.
100 * @return URI format of the namespace of the module
102 public URI getNamespace() {
107 * Returns the revision date for the module.
109 * @return date of the module revision which is specified as argument of
110 * YANG Module <b><font color="#339900">revison</font></b> keyword
112 public Date getRevision() {
117 public int hashCode() {
118 final int prime = 31;
119 int result = Objects.hashCode(namespace);
120 result = prime * result + Objects.hashCode(revision);
125 public boolean equals(final Object obj) {
129 if (!(obj instanceof QNameModule)) {
132 final QNameModule other = (QNameModule) obj;
133 if (!Objects.equals(revision, other.revision)) {
136 if (!Objects.equals(namespace, other.namespace)) {
143 * Returns a namespace in form defined by section 5.6.4. of {@link https
144 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
145 * the method will return <code>null</code> <br>
146 * example "http://example.acme.com/system?revision=2008-04-01"
148 * @return namespace in form defined by section 5.6.4. of {@link https
149 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
150 * defined, the method will return <code>null</code>
153 URI getRevisionNamespace() {
155 if (namespace == null) {
160 if (revision != null) {
161 query = "revision=" + getFormattedRevision();
164 URI compositeURI = null;
166 compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
167 namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
168 } catch (final URISyntaxException e) {
175 public String toString() {
176 return MoreObjects.toStringHelper(this).omitNullValues().add("ns", getNamespace()).add("rev", getFormattedRevision()).toString();