BUG-8123: be better at guessing identifiers
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / RevisionSourceIdentifier.java
index b12b2360a4447bf7c8fc8086e6047d074d5f14d0..0f47513fae59cabca2d4eeac176e5f9c4ebaca50 100644 (file)
@@ -7,10 +7,16 @@
  */
 package org.opendaylight.yangtools.yang.model.repo.api;
 
+import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_FILE_EXTENSION;
+
 import com.google.common.annotations.Beta;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import java.text.ParseException;
+import java.util.AbstractMap.SimpleImmutableEntry;
+import java.util.Map.Entry;
 import java.util.Objects;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 
 /**
  * YANG Schema revision source identifier
@@ -40,14 +46,13 @@ public final class RevisionSourceIdentifier extends SourceIdentifier {
     /**
      *
      * Creates new YANG Schema revision source identifier for sources without
-     * revision. {@link SourceIdentifier#NOT_PRESENT_FORMATTED_REVISION} as
-     * default revision.
+     * a revision.
      *
      * @param name
      *            Name of schema
      */
     RevisionSourceIdentifier(final String name) {
-        this(name, NOT_PRESENT_FORMATTED_REVISION);
+        super(name);
     }
 
     /**
@@ -73,7 +78,7 @@ public final class RevisionSourceIdentifier extends SourceIdentifier {
      *            default value will be used.
      */
     RevisionSourceIdentifier(final String name, final Optional<String> formattedRevision) {
-        this(name, formattedRevision.or(NOT_PRESENT_FORMATTED_REVISION));
+        super(name, formattedRevision);
     }
 
     /**
@@ -104,10 +109,8 @@ public final class RevisionSourceIdentifier extends SourceIdentifier {
     }
 
     /**
-     *
      * Creates new YANG Schema revision source identifier for sources without
-     * revision. {@link SourceIdentifier#NOT_PRESENT_FORMATTED_REVISION} as
-     * default revision.
+     * a revision.
      *
      * @param moduleName
      *            Name of schema
@@ -116,6 +119,46 @@ public final class RevisionSourceIdentifier extends SourceIdentifier {
         return new RevisionSourceIdentifier(moduleName);
     }
 
+    /**
+     * Creates a new RevisionSourceIdentifier based on the provided file name.
+     *
+     * @param fileName file name
+     * @throws IllegalArgumentException when fileName does not have correct format
+     * @throws NullPointerException when fileName is null
+     * @throws ParseException if the revision part has invalid format
+     */
+    public static RevisionSourceIdentifier fromFileName(final String fileName) throws ParseException {
+        final Entry<String, String> split = splitFileName(fileName);
+        if (split.getValue() != null) {
+            SimpleDateFormatUtil.getRevisionFormat().parse(split.getValue());
+        }
+
+        return new RevisionSourceIdentifier(split.getKey(), Optional.fromNullable(split.getValue()));
+    }
+
+    /**
+     * Creates a new RevisionSourceIdentifier based on the provided file name. This variant ignores any revision
+     * part.
+     *
+     * @param fileName file name
+     * @throws IllegalArgumentException when fileName does not have correct format
+     * @throws NullPointerException when fileName is null
+     */
+    public static RevisionSourceIdentifier fromFileNameLenientRevision(final String fileName) {
+        final Entry<String, String> split = splitFileName(fileName);
+        return new RevisionSourceIdentifier(split.getKey(), Optional.fromNullable(split.getValue()));
+    }
+
+    private static Entry<String, String> splitFileName(final String fileName) {
+        Preconditions.checkArgument(fileName.endsWith(RFC6020_YANG_FILE_EXTENSION),
+            "File name '%s' does not end with %s", fileName, RFC6020_YANG_FILE_EXTENSION);
+
+        final String noExt = fileName.substring(0, fileName.length() - RFC6020_YANG_FILE_EXTENSION.length());
+        final int atIndex = noExt.indexOf('@');
+        return atIndex == -1 ? new SimpleImmutableEntry<>(noExt, null)
+                : new SimpleImmutableEntry<>(noExt.substring(0, atIndex), noExt.substring(atIndex + 1));
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
@@ -139,6 +182,13 @@ public final class RevisionSourceIdentifier extends SourceIdentifier {
 
     @Override
     public String toString() {
-        return "RevisionSourceIdentifier [name=" + getName() + "@" + getRevision() + "]";
+        final StringBuilder sb = new StringBuilder("RevisionSourceIdentifier [name=");
+        sb.append(getName());
+
+        final String rev = getRevision();
+        if (rev != null) {
+            sb.append('@').append(rev);
+        }
+        return sb.append(']').toString();
     }
 }