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 java.util.Objects;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.objcache.ObjectCache;
18 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public final class QNameModule implements Immutable, Serializable {
23 private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QNameModule.class);
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 ObjectCache} policy.
48 public static QNameModule cachedReference(final QNameModule module) {
49 return CACHE.getReference(module);
53 * Create a new QName module instance with specified namespace/revision.
55 * @param namespace Module namespace
56 * @param revision Module revision
57 * @return A new, potentially shared, QNameModule instance
59 public static QNameModule create(final URI namespace, final Date revision) {
60 if (namespace == null && revision == null) {
64 return new QNameModule(namespace, revision);
67 public String getFormattedRevision() {
68 if (revision == null) {
72 String ret = formattedRevision;
74 ret = SimpleDateFormatUtil.getRevisionFormat().format(revision);
75 formattedRevision = ret;
82 * Returns the namespace of the module which is specified as argument of
83 * YANG Module <b><font color="#00FF00">namespace</font></b> keyword.
85 * @return URI format of the namespace of the module
87 public URI getNamespace() {
92 * Returns the revision date for the module.
94 * @return date of the module revision which is specified as argument of
95 * YANG Module <b><font color="#339900">revison</font></b> keyword
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 (!Objects.equals(revision, other.revision)) {
121 if (!Objects.equals(namespace, other.namespace)) {
128 * Returns a namespace in form defined by section 5.6.4. of {@link https
129 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
130 * the method will return <code>null</code> <br>
131 * example "http://example.acme.com/system?revision=2008-04-01"
133 * @return namespace in form defined by section 5.6.4. of {@link https
134 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
135 * defined, the method will return <code>null</code>
138 URI getRevisionNamespace() {
140 if (namespace == null) {
145 if (revision != null) {
146 query = "revision=" + getFormattedRevision();
149 URI compositeURI = null;
151 compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
152 namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
153 } catch (final URISyntaxException e) {
160 public String toString() {
161 return MoreObjects.toStringHelper(this).omitNullValues().add("ns", getNamespace()).add("rev", getFormattedRevision()).toString();