BUG-4688: Make SourceIdentifier use Revision
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / SourceIdentifier.java
index cd3a0fbefcaaed7198980ee545f7fe510654ac0b..7d22a2fcb0f4ff5568ff3fe97f2dfe8366ad70f3 100644 (file)
@@ -3,60 +3,87 @@
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/eplv10.html
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.model.repo.api;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
 
+import com.google.common.annotations.Beta;
+import com.google.common.collect.Interner;
+import com.google.common.collect.Interners;
+import java.util.Optional;
+import javax.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Identifier;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.common.YangConstants;
 
 /**
- *
- * YANG Schema source identifier
- *
- * Simple transfer object represents identifier of source for YANG schema (module or submodule),
- * which consists of
- * <ul>
- * <li>YANG schema name ({@link #getName()}
- * <li>Module revision (optional) ({link {@link #getRevision()})
- * </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.
- *
- * <b>Note:</b>On source retrieval layer it is impossible to distinguish
- * between YANG module and/or submodule unless source is present.
+ * Base class of YANG Schema source identifiers.
  *
  * <p>
- * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2 and
- * http://tools.ietf.org/html/rfc6022#section-3.1 ).
- *
+ * Source identifiers are designated to be carry only necessary information to
+ * look-up YANG model source and to be used by various SchemaSourceProviders.
  *
+ * <p>
+ * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2
+ * and http://tools.ietf.org/html/rfc6022#section-3.1 ).
  */
-public final class SourceIdentifier implements Identifier, Immutable {
-    private static final long serialVersionUID = 1L;
-    private final String revision;
+@Beta
+public abstract class SourceIdentifier implements Identifier, Immutable {
+    private static final Interner<SourceIdentifier> INTERNER = Interners.newWeakInterner();
+    private static final long serialVersionUID = 2L;
+
+    private final Revision revision;
     private final String name;
 
     /**
+     * Creates new YANG Schema source identifier for sources without revision.
+     *
+     * @param name
+     *            Name of schema
+     */
+    SourceIdentifier(final String name) {
+        this(name, (Revision) null);
+    }
+
+    /**
+     * Creates new YANG Schema source identifier.
      *
+     * @param name
+     *            Name of schema
+     * @param revision
+     *            Revision of source, may be null
+     */
+    SourceIdentifier(final String name, @Nullable final Revision revision) {
+        this.name = requireNonNull(name);
+        this.revision = revision;
+    }
+
+    /**
      * Creates new YANG Schema source identifier.
      *
-     * @param name Name of schema
-     * @param formattedRevision Revision of source in format YYYY-mm-dd
+     * @param name
+     *            Name of schema
+     * @param revision
+     *            Revision of source, possibly not present
+     */
+    SourceIdentifier(final String name, final Optional<Revision> revision) {
+        this(name, revision.orElse(null));
+    }
+
+    /**
+     * Return an interned reference to a equivalent SemVerSourceIdentifier.
+     *
+     * @return Interned reference, or this object if it was interned.
      */
-    public SourceIdentifier(final String name, final Optional<String> formattedRevision) {
-        super();
-        this.name = Preconditions.checkNotNull(name);
-        this.revision = formattedRevision.orNull();
+    public SourceIdentifier intern() {
+        return INTERNER.intern(this);
     }
 
     /**
-     * Returns model name
+     * Returns model name.
      *
      * @return model name
      */
@@ -69,96 +96,45 @@ public final class SourceIdentifier implements Identifier, Immutable {
      *
      * @return revision of source or null if revision was not supplied.
      */
-    public String getRevision() {
-        return revision;
-    }
-
-    @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + ((name == null) ? 0 : name.hashCode());
-        result = prime * result + ((revision == null) ? 0 : revision.hashCode());
-        return result;
-    }
-
-    @Override
-    public boolean equals(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
-            return false;
-        }
-        SourceIdentifier other = (SourceIdentifier) obj;
-        if (name == null) {
-            if (other.name != null) {
-                return false;
-            }
-        } else if (!name.equals(other.name)) {
-            return false;
-        }
-        if (revision == null) {
-            if (other.revision != null) {
-                return false;
-            }
-        } else if (!revision.equals(other.revision)) {
-            return false;
-        }
-        return true;
-    }
-
-    public static SourceIdentifier create(final String moduleName, final Optional<String> revision) {
-        return new SourceIdentifier(moduleName, revision);
+    public Optional<Revision> getRevision() {
+        return Optional.ofNullable(revision);
     }
 
     /**
      * Returns filename for this YANG module as specified in RFC 6020.
      *
-     * Returns filename in format
-     * <code>name ['@' revision] '.yang'</code>
-     * <p>
-     * Where revision is  date in format YYYY-mm-dd.
      * <p>
+     * Returns filename in format <code>name ['@' revision] '.yang'</code>,
+     * where revision is date in format YYYY-mm-dd.
      *
-     * @see http://tools.ietf.org/html/rfc6020#section-5.2
+     * <p>
+     * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
      *
      * @return Filename for this source identifier.
      */
     public String toYangFilename() {
-        return toYangFileName(name, Optional.fromNullable(revision));
-    }
-
-    @Override
-    public String toString() {
-        return "SourceIdentifier [name=" + name + "@" + revision + "]";
+        return toYangFileName(name, Optional.ofNullable(revision));
     }
 
     /**
      * Returns filename for this YANG module as specified in RFC 6020.
      *
-     * Returns filename in format
-     * <code>moduleName ['@' revision] '.yang'</code>
-     *
-     * Where Where revision-date is in format YYYY-mm-dd.
+     * <p>
+     * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>,
+     * where Where revision-date is in format YYYY-mm-dd.
      *
      * <p>
-     * See
-     * http://tools.ietf.org/html/rfc6020#section-5.2
+     * See http://tools.ietf.org/html/rfc6020#section-5.2
      *
      * @return Filename for this source identifier.
      */
-    public static final String toYangFileName(final String moduleName, final Optional<String> revision) {
+    public static String toYangFileName(final String moduleName, final Optional<Revision> revision) {
         StringBuilder filename = new StringBuilder(moduleName);
         if (revision.isPresent()) {
             filename.append('@');
             filename.append(revision.get());
         }
-        filename.append(".yang");
+        filename.append(YangConstants.RFC6020_YANG_FILE_EXTENSION);
         return filename.toString();
     }
-
 }