*/
package org.opendaylight.yangtools.yang.common;
+import com.google.common.base.Objects;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
-
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;
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;
private final Date revision;
//Nullable
- private String formattedRevision;
+ private volatile String formattedRevision;
private QNameModule(final URI namespace, final Date revision) {
this.namespace = namespace;
this.revision = revision;
}
+ /**
+ * 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 null;
}
- if (formattedRevision == null) {
- synchronized (this) {
- if (formattedRevision == null) {
- formattedRevision = SimpleDateFormatUtil.getRevisionFormat().format(revision);
- }
- }
+ String ret = formattedRevision;
+ if (ret == null) {
+ ret = SimpleDateFormatUtil.getRevisionFormat().format(revision);
+ formattedRevision = ret;
}
- return formattedRevision;
+ return ret;
}
/**
* Returns the namespace of the module which is specified as argument of
- * YANG {@link Module <b><font color="#00FF00">namespace</font></b>}
- * keyword.
+ * YANG Module <b><font color="#00FF00">namespace</font></b> keyword.
*
* @return URI format of the namespace of the module
*/
* Returns the revision date for the module.
*
* @return date of the module revision which is specified as argument of
- * YANG {@link Module <b><font color="#339900">revison</font></b>}
- * keyword
+ * YANG Module <b><font color="#339900">revison</font></b> keyword
*/
public Date getRevision() {
return revision;
return false;
}
final QNameModule other = (QNameModule) obj;
- if (namespace == null) {
- if (other.namespace != null) {
+ if (revision == null) {
+ if (other.revision != null) {
return false;
}
- } else if (!namespace.equals(other.namespace)) {
+ } else if (!revision.equals(other.revision)) {
return false;
}
- if (revision == null) {
- if (other.revision != null) {
+ if (namespace == null) {
+ if (other.namespace != null) {
return false;
}
- } else if (!revision.equals(other.revision)) {
+ } else if (!namespace.equals(other.namespace)) {
return false;
}
return true;
}
return compositeURI;
}
+
+ @Override
+ public String toString() {
+ return Objects.toStringHelper(this).omitNullValues().add("ns", getNamespace()).add("rev", getFormattedRevision()).toString();
+ }
}