Fold yang-model-util-ut
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / SourceIdentifier.java
index ab63daed76adb03a340fe043b596a4064acf8451..b6cb99f726fd4746def17562aed253c9e5267902 100644 (file)
@@ -7,90 +7,66 @@
  */
 package org.opendaylight.yangtools.yang.model.repo.api;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.Beta;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import com.google.common.collect.Interner;
 import com.google.common.collect.Interners;
-import java.util.regex.Pattern;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Identifier;
 import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.concepts.SemVer;
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.common.YangConstants;
 
 /**
  * Base class of YANG Schema source identifiers.
  *
+ * <p>
  * 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 ).
  */
 @Beta
 public abstract 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 Interner<SourceIdentifier> INTERNER = Interners.newWeakInterner();
+    private static final long serialVersionUID = 2L;
 
-    private static final long serialVersionUID = 1L;
-    private final String revision;
-    private final String name;
+    private final @Nullable Revision revision;
+    private final @NonNull String name;
 
     /**
-     *
      * Creates new YANG Schema source identifier for sources without revision.
-     * {@link SourceIdentifier#NOT_PRESENT_FORMATTED_REVISION} as default
-     * revision.
      *
-     * @param name
-     *            Name of schema
+     * @param name Name of schema
      */
     SourceIdentifier(final String name) {
-        this(name, NOT_PRESENT_FORMATTED_REVISION);
+        this(name, (Revision) null);
     }
 
     /**
      * 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, may be null
      */
-    SourceIdentifier(final String name, final String formattedRevision) {
-        this.name = Preconditions.checkNotNull(name);
-        this.revision = Preconditions.checkNotNull(formattedRevision);
+    SourceIdentifier(final String name, final @Nullable 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. If not present,
-     *            default value will be used.
+     * @param name Name of schema
+     * @param revision Revision of source, possibly not present
      */
-    SourceIdentifier(final String name, final Optional<String> formattedRevision) {
-        this(name, formattedRevision.or(NOT_PRESENT_FORMATTED_REVISION));
+    SourceIdentifier(final String name, final Optional<Revision> revision) {
+        this(name, revision.orElse(null));
     }
 
     /**
@@ -98,16 +74,16 @@ public abstract class SourceIdentifier implements Identifier, Immutable {
      *
      * @return Interned reference, or this object if it was interned.
      */
-    public SourceIdentifier intern() {
+    public @NonNull SourceIdentifier intern() {
         return INTERNER.intern(this);
     }
 
     /**
-     * Returns model name
+     * Returns model name.
      *
      * @return model name
      */
-    public String getName() {
+    public @NonNull String getName() {
         return name;
     }
 
@@ -116,78 +92,44 @@ public abstract class SourceIdentifier implements Identifier, Immutable {
      *
      * @return revision of source or null if revision was not supplied.
      */
-    public String getRevision() {
-        return revision;
-    }
-
-    /**
-     * <p>
-     * Since we've got two ways of model versioning (revision &amp; semantic version),
-     * this method shouldn't be called directly anymore. Eventually, callers of this method
-     * should be notified before method gets deleted.
-     * @deprecated use either
-     * <ul>
-     * <li>{@link SemVerSourceIdentifier#create(String, SemVer)}</li>
-     * <li>{@link SemVerSourceIdentifier#create(String, Optional, SemVer)}</li>
-     * <li>{@link SemVerSourceIdentifier#create(String, String, SemVer)}</li>
-     * </ul>
-     * or
-     * <ul>
-     * <li>{@link RevisionSourceIdentifier#create(String)}</li>
-     * <li>{@link RevisionSourceIdentifier#create(String, String)}</li>
-     * <li>{@link RevisionSourceIdentifier#create(String, Optional)}</li>
-     * </ul>
-     *
-     * @param moduleName
-     *            Name of schema
-
-     * @param revision
-     *            Revision of source in format YYYY-mm-dd. If not present,
-     *            default value will be used.
-     *
-     * @return particular SourceIdentifier instance
-     *
-     */
-    @Deprecated
-    public static SourceIdentifier create(final String moduleName, final Optional<String> revision) {
-        return new RevisionSourceIdentifier(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.
      *
+     * <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));
+    public @NonNull String toYangFilename() {
+        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
      *
+     * @param moduleName module name
+     * @param revision optional revision
      * @return Filename for this source identifier.
      */
-    public static String toYangFileName(final String moduleName, final Optional<String> revision) {
-        StringBuilder filename = new StringBuilder(moduleName);
+    public static @NonNull String toYangFileName(final String moduleName, final Optional<Revision> revision) {
+        final StringBuilder sb = new StringBuilder(moduleName);
         if (revision.isPresent()) {
-            filename.append('@');
-            filename.append(revision.get());
+            sb.append('@').append(revision.orElseThrow());
         }
-        filename.append(YangConstants.RFC6020_YANG_FILE_EXTENSION);
-        return filename.toString();
+        return sb.append(YangConstants.RFC6020_YANG_FILE_EXTENSION).toString();
     }
 }