X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fcommon%2FQNameModule.java;h=def8da86cbeeec77689b50f44c0493456a172b9f;hb=e837463d8a8fe20766fb21897cc8086fbdf201e4;hp=22428e36d4b1afbef8e76ef22c090ca8a5d44f9b;hpb=66b4cc0ecd6370f7d11662a07eaa48a967d41eff;p=yangtools.git
diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java
index 22428e36d4..def8da86cb 100644
--- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java
+++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java
@@ -7,18 +7,23 @@
*/
package org.opendaylight.yangtools.yang.common;
+import com.google.common.base.MoreObjects;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
-
+import java.util.Objects;
import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.objcache.ObjectCache;
+import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-final class QNameModule implements Immutable, Serializable {
- private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
- private static final long serialVersionUID = 1L;
+public final class QNameModule implements Immutable, Serializable {
+ private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QNameModule.class);
+ private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
+ private static final QNameModule NULL_INSTANCE = new QNameModule(null, null);
+ private static final long serialVersionUID = 1L;
//Nullable
private final URI namespace;
@@ -27,39 +32,77 @@ final class QNameModule implements Immutable, Serializable {
private final Date revision;
//Nullable
- private final String formattedRevision;
+ private volatile String formattedRevision;
private QNameModule(final URI namespace, final Date revision) {
this.namespace = namespace;
this.revision = revision;
- if(revision != null) {
- this.formattedRevision = SimpleDateFormatUtil.getRevisionFormat().format(revision);
- } else {
- this.formattedRevision = null;
+ }
+
+ /**
+ * Look up specified module in the global cache and return a shared reference.
+ *
+ * @param module Module instance
+ * @return Cached instance, according to {@link ObjectCache} policy.
+ */
+ public static QNameModule cachedReference(final QNameModule module) {
+ return CACHE.getReference(module);
+ }
+
+ /**
+ * Create a new QName module instance with specified namespace/revision.
+ *
+ * @param namespace Module namespace
+ * @param revision Module revision
+ * @return A new, potentially shared, QNameModule instance
+ */
+ public static QNameModule create(final URI namespace, final Date revision) {
+ if (namespace == null && revision == null) {
+ return NULL_INSTANCE;
}
+
+ return new QNameModule(namespace, revision);
}
- public static QNameModule create(final URI namespace, final Date revision) {
- return new QNameModule(namespace, revision);
- }
+ public String getFormattedRevision() {
+ if (revision == null) {
+ return null;
+ }
- public String getFormattedRevision() {
- return formattedRevision;
- }
+ String ret = formattedRevision;
+ if (ret == null) {
+ ret = SimpleDateFormatUtil.getRevisionFormat().format(revision);
+ formattedRevision = ret;
+ }
- public URI getNamespace() {
- return namespace;
- }
+ return ret;
+ }
- public Date getRevision() {
- return revision;
- }
+ /**
+ * Returns the namespace of the module which is specified as argument of
+ * YANG Module namespace keyword.
+ *
+ * @return URI format of the namespace of the module
+ */
+ public URI getNamespace() {
+ return namespace;
+ }
+
+ /**
+ * Returns the revision date for the module.
+ *
+ * @return date of the module revision which is specified as argument of
+ * YANG Module revison keyword
+ */
+ public Date getRevision() {
+ return revision;
+ }
@Override
public int hashCode() {
final int prime = 31;
int result = (namespace == null) ? 0 : namespace.hashCode();
- result = prime * result + ((formattedRevision == null) ? 0 : formattedRevision.hashCode());
+ result = prime * result + ((revision == null) ? 0 : revision.hashCode());
return result;
}
@@ -72,18 +115,10 @@ final class QNameModule implements Immutable, Serializable {
return false;
}
final QNameModule other = (QNameModule) obj;
- if (namespace == null) {
- if (other.namespace != null) {
- return false;
- }
- } else if (!namespace.equals(other.namespace)) {
+ if (!Objects.equals(revision, other.revision)) {
return false;
}
- if (formattedRevision == null) {
- if (other.formattedRevision != null) {
- return false;
- }
- } else if (!revision.equals(other.revision)) {
+ if (!Objects.equals(namespace, other.namespace)) {
return false;
}
return true;
@@ -108,16 +143,21 @@ final class QNameModule implements Immutable, Serializable {
String query = "";
if (revision != null) {
- query = "revision=" + formattedRevision;
+ query = "revision=" + getFormattedRevision();
}
URI compositeURI = null;
try {
compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
- } catch (URISyntaxException e) {
+ } catch (final URISyntaxException e) {
LOG.error("", e);
}
return compositeURI;
}
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this).omitNullValues().add("ns", getNamespace()).add("rev", getFormattedRevision()).toString();
+ }
}