Bug 865: Moved filesystem model cache related functionality
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / SourceIdentifier.java
index 58b011c36811cd60c3f9c0af9a1a7f93d9990988..2a688b3ff8347ede6894e73093dcd51e65f28010 100644 (file)
@@ -10,9 +10,12 @@ package org.opendaylight.yangtools.yang.model.repo.api;
 import com.google.common.annotations.Beta;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
-
+import java.util.regex.Pattern;
 import org.opendaylight.yangtools.concepts.Identifier;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.objcache.ObjectCache;
+import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 
 /**
  * YANG Schema source identifier
@@ -25,8 +28,7 @@ import org.opendaylight.yangtools.concepts.Immutable;
  * </ul>
  *
  * Source identifier is designated to be carry only necessary information
- * to look-up YANG model source and to be used by {@link AdvancedSchemaSourceProvider}
- * and similar.
+ * to look-up YANG model source and to be used by various SchemaSourceProviders.
  *
  * <b>Note:</b>On source retrieval layer it is impossible to distinguish
  * between YANG module and/or submodule unless source is present.
@@ -37,21 +39,69 @@ import org.opendaylight.yangtools.concepts.Immutable;
  */
 @Beta
 public final class SourceIdentifier implements Identifier, Immutable {
+    /**
+     * Default revision for sources without specified revision.
+     * Marks the source as oldest.
+     */
+    public static final String NOT_PRESENT_FORMATTED_REVISION = "0000-00-00";
+
+    /**
+     *
+     * Simplified compiled revision pattern in format YYYY-mm-dd, which checks
+     * only distribution of number elements.
+     * <p>
+     * For checking if supplied string is real date, use {@link SimpleDateFormatUtil}
+     * instead.
+     *
+     */
+    public static final Pattern REVISION_PATTERN = Pattern.compile("\\d\\d\\d\\d-\\d\\d-\\d\\d");
+
+
+    private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(SourceIdentifier.class);
     private static final long serialVersionUID = 1L;
     private final String revision;
     private final String name;
 
     /**
-     *
      * Creates new YANG Schema source identifier.
      *
      * @param name Name of schema
      * @param formattedRevision Revision of source in format YYYY-mm-dd
      */
-    public SourceIdentifier(final String name, final Optional<String> formattedRevision) {
-        super();
+    public SourceIdentifier(final String name, final String formattedRevision) {
         this.name = Preconditions.checkNotNull(name);
-        this.revision = formattedRevision.orNull();
+        this.revision = Preconditions.checkNotNull(formattedRevision);
+    }
+
+    /**
+     *
+     * Creates new YANG Schema source identifier.
+     *
+     * @param name Name of schema
+     * @param formattedRevision Revision of source in format YYYY-mm-dd. If not present, default value will be used.
+     */
+    public SourceIdentifier(final String name, final Optional<String> formattedRevision) {
+        this(name, formattedRevision.or(NOT_PRESENT_FORMATTED_REVISION));
+    }
+
+    /**
+     * Return a cached reference to an object equal to this object.
+     *
+     * @return A potentially shared reference, not guaranteed to be unique.
+     */
+    public SourceIdentifier cachedReference() {
+        return CACHE.getReference(this);
+    }
+
+    /**
+     *
+     * Creates new YANG Schema source identifier for sources without revision.
+     * {@link SourceIdentifier#NOT_PRESENT_FORMATTED_REVISION} as default revision.
+     *
+     * @param name Name of schema
+     */
+    public SourceIdentifier(final String name) {
+        this(name, NOT_PRESENT_FORMATTED_REVISION);
     }
 
     /**
@@ -123,7 +173,7 @@ public final class SourceIdentifier implements Identifier, Immutable {
      * Where revision is  date in format YYYY-mm-dd.
      * <p>
      *
-     * @see http://tools.ietf.org/html/rfc6020#section-5.2
+     * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
      *
      * @return Filename for this source identifier.
      */