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 java.io.Serializable;
13 import java.net.URISyntaxException;
14 import java.util.Date;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.objcache.ObjectCache;
17 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 public final class QNameModule implements Immutable, Serializable {
22 private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QNameModule.class);
23 private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
24 private static final QNameModule NULL_INSTANCE = new QNameModule(null, null);
25 private static final long serialVersionUID = 1L;
28 private final URI namespace;
31 private final Date revision;
34 private volatile String formattedRevision;
36 private QNameModule(final URI namespace, final Date revision) {
37 this.namespace = namespace;
38 this.revision = revision;
42 * Look up specified module in the global cache and return a shared reference.
44 * @param module Module instance
45 * @return Cached instance, according to {@link ObjectCache} policy.
47 public static QNameModule cachedReference(final QNameModule module) {
48 return CACHE.getReference(module);
52 * Create a new QName module instance with specified namespace/revision.
54 * @param namespace Module namespace
55 * @param revision Module revision
56 * @return A new, potentially shared, QNameModule instance
58 public static QNameModule create(final URI namespace, final Date revision) {
59 if (namespace == null && revision == null) {
63 return new QNameModule(namespace, revision);
66 public String getFormattedRevision() {
67 if (revision == null) {
71 String ret = formattedRevision;
73 ret = SimpleDateFormatUtil.getRevisionFormat().format(revision);
74 formattedRevision = ret;
81 * Returns the namespace of the module which is specified as argument of
82 * YANG Module <b><font color="#00FF00">namespace</font></b> keyword.
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 Module <b><font color="#339900">revison</font></b> keyword
96 public Date getRevision() {
101 public int hashCode() {
102 final int prime = 31;
103 int result = (namespace == null) ? 0 : namespace.hashCode();
104 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
109 public boolean equals(final Object obj) {
113 if (!(obj instanceof QNameModule)) {
116 final QNameModule other = (QNameModule) obj;
117 if (revision == null) {
118 if (other.revision != null) {
121 } else if (!revision.equals(other.revision)) {
124 if (namespace == null) {
125 if (other.namespace != null) {
128 } else if (!namespace.equals(other.namespace)) {
135 * Returns a namespace in form defined by section 5.6.4. of {@link https
136 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
137 * the method will return <code>null</code> <br>
138 * example "http://example.acme.com/system?revision=2008-04-01"
140 * @return namespace in form defined by section 5.6.4. of {@link https
141 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
142 * defined, the method will return <code>null</code>
145 URI getRevisionNamespace() {
147 if (namespace == null) {
152 if (revision != null) {
153 query = "revision=" + getFormattedRevision();
156 URI compositeURI = null;
158 compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
159 namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
160 } catch (URISyntaxException e) {
167 public String toString() {
168 return MoreObjects.toStringHelper(this).omitNullValues().add("ns", getNamespace()).add("rev", getFormattedRevision()).toString();