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 java.io.Serializable;
12 import java.net.URISyntaxException;
13 import java.util.Date;
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.objcache.ObjectCache;
16 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public final class QNameModule implements Immutable, Serializable {
21 private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QNameModule.class);
22 private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
23 private static final QNameModule NULL_INSTANCE = new QNameModule(null, null);
24 private static final long serialVersionUID = 1L;
27 private final URI namespace;
30 private final Date revision;
33 private volatile String formattedRevision;
35 private QNameModule(final URI namespace, final Date revision) {
36 this.namespace = namespace;
37 this.revision = revision;
41 * Look up specified module in the global cache and return a shared reference.
43 * @param module Module instance
44 * @return Cached instance, according to {@link ObjectCache} policy.
46 public static QNameModule cachedReference(final QNameModule module) {
47 return CACHE.getReference(module);
51 * Create a new QName module instance with specified namespace/revision.
53 * @param namespace Module namespace
54 * @param revision Module revision
55 * @return A new, potentially shared, QNameModule instance
57 public static QNameModule create(final URI namespace, final Date revision) {
58 if (namespace == null && revision == null) {
62 return new QNameModule(namespace, revision);
65 public String getFormattedRevision() {
66 if (revision == null) {
70 String ret = formattedRevision;
72 ret = SimpleDateFormatUtil.getRevisionFormat().format(revision);
73 formattedRevision = ret;
80 * Returns the namespace of the module which is specified as argument of
81 * YANG {@link Module <b><font color="#00FF00">namespace</font></b>}
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 {@link Module <b><font color="#339900">revison</font></b>}
97 public Date getRevision() {
102 public int hashCode() {
103 final int prime = 31;
104 int result = (namespace == null) ? 0 : namespace.hashCode();
105 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
110 public boolean equals(final Object obj) {
114 if (!(obj instanceof QNameModule)) {
117 final QNameModule other = (QNameModule) obj;
118 if (revision == null) {
119 if (other.revision != null) {
122 } else if (!revision.equals(other.revision)) {
125 if (namespace == null) {
126 if (other.namespace != null) {
129 } else if (!namespace.equals(other.namespace)) {
136 * Returns a namespace in form defined by section 5.6.4. of {@link https
137 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
138 * the method will return <code>null</code> <br>
139 * example "http://example.acme.com/system?revision=2008-04-01"
141 * @return namespace in form defined by section 5.6.4. of {@link https
142 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
143 * defined, the method will return <code>null</code>
146 URI getRevisionNamespace() {
148 if (namespace == null) {
153 if (revision != null) {
154 query = "revision=" + getFormattedRevision();
157 URI compositeURI = null;
159 compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
160 namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
161 } catch (URISyntaxException e) {