Version 1.6.2: Version.java with tags
authorNiki Roo <niki@nikiroo.be>
Wed, 5 Jul 2017 20:37:55 +0000 (22:37 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 5 Jul 2017 20:37:55 +0000 (22:37 +0200)
Makefile.base
VERSION
changelog
src/be/nikiroo/utils/Version.java
src/be/nikiroo/utils/test/VersionTest.java

index e44b5d52cd5a6d09e2a6c2cad323f4aa92f24a39..3304c83c802f47d4d5ede0e5cb33c5a252bd0552 100644 (file)
@@ -64,14 +64,16 @@ test: test-resources
 clean:
        rm -rf bin/
        @echo Removing sources taken from libs...
-       @for lib in libs/*-sources.jar; do \
-               basename "$$lib"; \
-               jar tf "$$lib" | while read -r ln; do \
-                       [ -f "src/$$ln" ] && rm "src/$$ln"; \
-               done; \
-               jar tf "$$lib" | tac | while read -r ln; do \
-                       [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
-               done; \
+       @for lib in libs/*-sources.jar libs/*-sources.patch.jar; do \
+               if [ "$$lib" != 'libs/*-sources.jar' -a "$$lib" != 'libs/*-sources.patch.jar' ]; then \
+                       basename "$$lib"; \
+                       jar tf "$$lib" | while read -r ln; do \
+                               [ -f "src/$$ln" ] && rm "src/$$ln"; \
+                       done; \
+                       jar tf "$$lib" | tac | while read -r ln; do \
+                               [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
+                       done; \
+               fi \
        done
 
 mrproper: mrpropre
diff --git a/VERSION b/VERSION
index 9c6d6293b1a8f448def89c2d5bfa63b89a24e0cc..fdd3be6df54a88720553509b7a545f73294a867e 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.6.1
+1.6.2
index 771fac9dacacb0505ea597d58e1c7449d5083896..da05339e66cbbb50234a78448f49cb074dcc7e26 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,10 @@
+Version 1.6.2
+-------------
+
+Version.java
+       Now supports "tag" on the versions (i.e., 0.0.4-niki1)
+       -> tag is "niki", tagVersion is 1
+
 Version 1.6.1
 -------------
 
index 94387373e980daf30b3ca4de01cfa72e490a0307..2f10c5eba8f982076737543002b9a3f0176596e2 100644 (file)
@@ -14,6 +14,8 @@ public class Version implements Comparable<Version> {
        private int major;
        private int minor;
        private int patch;
+       private String tag;
+       private int tagVersion;
 
        /**
         * Create a new, empty {@link Version}.
@@ -33,30 +35,110 @@ public class Version implements Comparable<Version> {
         *            the patch version
         */
        public Version(int major, int minor, int patch) {
+               this(major, minor, patch, null, -1);
+       }
+
+       /**
+        * Create a new {@link Version} with the given values.
+        * 
+        * @param major
+        *            the major version
+        * @param minor
+        *            the minor version
+        * @param patch
+        *            the patch version
+        * @param tag
+        *            a tag name for this version
+        */
+       public Version(int major, int minor, int patch, String tag) {
+               this(major, minor, patch, tag, -1);
+       }
+
+       /**
+        * Create a new {@link Version} with the given values.
+        * 
+        * @param major
+        *            the major version
+        * @param minor
+        *            the minor version
+        * @param patch
+        *            the patch version the patch version
+        * @param tag
+        *            a tag name for this version
+        * @param tagVersion
+        *            the version of the tagged version
+        */
+       public Version(int major, int minor, int patch, String tag, int tagVersion) {
+               if (tagVersion >= 0 && tag == null) {
+                       throw new java.lang.IllegalArgumentException(
+                                       "A tag version cannot be used without a tag");
+               }
+
                this.major = major;
                this.minor = minor;
                this.patch = patch;
-               this.version = String.format("%d.%d.%d", major, minor, patch);
+               this.tag = tag;
+               this.tagVersion = tagVersion;
+
+               String tagSuffix = "";
+               if (tag != null) {
+                       tagSuffix = "-" + tag
+                                       + (tagVersion >= 0 ? Integer.toString(tagVersion) : "");
+               }
+
+               this.version = String.format("%d.%d.%d%s", major, minor, patch,
+                               tagSuffix);
        }
 
        /**
         * Create a new {@link Version} with the given value, which must be in the
-        * form <tt>MAJOR.MINOR.PATCH</tt>.
+        * form <tt>MAJOR.MINOR.PATCH(-TAG(TAG_VERSION))</tt>.
         * 
         * @param version
-        *            the version (<tt>MAJOR.MINOR.PATCH</tt>)
+        *            the version (<tt>MAJOR.MINOR.PATCH</tt>,
+        *            <tt>MAJOR.MINOR.PATCH-TAG</tt> or
+        *            <tt>MAJOR.MINOR.PATCH-TAGVERSIONTAG</tt>)
         */
        public Version(String version) {
                try {
                        String[] tab = version.split("\\.");
                        this.major = Integer.parseInt(tab[0]);
                        this.minor = Integer.parseInt(tab[1]);
-                       this.patch = Integer.parseInt(tab[2]);
-                       this.version = version;
+                       if (tab[2].contains("-")) {
+                               int posInVersion = version.indexOf('.');
+                               posInVersion = version.indexOf('.', posInVersion + 1);
+                               String rest = version.substring(posInVersion + 1);
+
+                               int posInRest = rest.indexOf('-');
+                               this.patch = Integer.parseInt(rest.substring(0, posInRest));
+
+                               posInVersion = version.indexOf('-');
+                               this.tag = version.substring(posInVersion + 1);
+                               this.tagVersion = -1;
+
+                               StringBuilder str = new StringBuilder();
+                               while (!tag.isEmpty() && tag.charAt(tag.length() - 1) >= '0'
+                                               && tag.charAt(tag.length() - 1) <= '9') {
+                                       str.insert(0, tag.charAt(tag.length() - 1));
+                                       tag = tag.substring(0, tag.length() - 1);
+                               }
+
+                               if (str.length() > 0) {
+                                       this.tagVersion = Integer.parseInt(str.toString());
+                               }
+                       } else {
+                               this.patch = Integer.parseInt(tab[2]);
+                               this.tag = null;
+                               this.tagVersion = -1;
+                       }
+
+                       this.version = toString();
                } catch (Exception e) {
                        this.major = 0;
                        this.minor = 0;
                        this.patch = 0;
+                       this.tag = null;
+                       this.tagVersion = -1;
                        this.version = null;
                }
        }
@@ -97,6 +179,24 @@ public class Version implements Comparable<Version> {
                return patch;
        }
 
+       /**
+        * A tag name for this version.
+        * 
+        * @return the tag
+        */
+       public String getTag() {
+               return tag;
+       }
+
+       /**
+        * The version of the tag.
+        * 
+        * @return the tag version
+        */
+       public int getTagVersion() {
+               return tagVersion;
+       }
+
        /**
         * Check if this {@link Version} is "empty" (i.e., the version was not
         * parse-able or not given).
@@ -106,11 +206,14 @@ public class Version implements Comparable<Version> {
         * @return TRUE if it is empty
         */
        public boolean isEmpty() {
-               return major == 0 && minor == 0 && patch == 0;
+               return major == 0 && minor == 0 && patch == 0 && tag == null;
        }
 
        /**
         * Check if we are more recent than the given {@link Version}.
+        * <p>
+        * Note that a tagged version is considered newer than a non-tagged version,
+        * but two tagged versions with different tags are not comparable.
         * 
         * @param o
         *            the other {@link Version}
@@ -129,6 +232,20 @@ public class Version implements Comparable<Version> {
                        return true;
                }
 
+               // a tagged version is considered newer than a non-tagged one
+               if (major == o.major && minor == o.minor && patch == o.patch
+                               && tag != null && o.tag == null) {
+                       return true;
+               }
+
+               // 2 <> tagged versions are not comparable
+               boolean sameTag = (tag == null && o.tag == null)
+                               || (tag != null && tag.equals(o.tag));
+               if (major == o.major && minor == o.minor && patch == o.patch && sameTag
+                               && tagVersion > o.tagVersion) {
+                       return true;
+               }
+
                return false;
        }
 
@@ -140,6 +257,14 @@ public class Version implements Comparable<Version> {
         * @return TRUE if this {@link Version} is older than the given one
         */
        public boolean isOlderThan(Version o) {
+               // 2 <> tagged versions are not comparable
+               boolean sameTag = (tag == null && o.tag == null)
+                               || (tag != null && tag.equals(o.tag));
+               if (major == o.major && minor == o.minor && patch == o.patch
+                               && !sameTag) {
+                       return false;
+               }
+
                return !equals(o) && !isNewerThan(o);
        }
 
@@ -186,7 +311,10 @@ public class Version implements Comparable<Version> {
        public boolean equals(Object obj) {
                if (obj instanceof Version) {
                        Version o = (Version) obj;
-                       return o.major == major && o.minor == minor && o.patch == patch;
+                       boolean sameTag = (tag == null && o.tag == null)
+                                       || (tag != null && tag.equals(o.tag));
+                       return o.major == major && o.minor == minor && o.patch == patch
+                                       && sameTag && o.tagVersion == tagVersion;
                }
 
                return false;
index 376bd4428e5e16151e3bdd7940c4f9ed203f1d32..f6f71322f51cbb535cee0954112f964444f89cbd 100644 (file)
@@ -52,6 +52,24 @@ class VersionTest extends TestLauncher {
                        }
                });
 
+               addTest(new TestCase("Tag version") {
+                       @Override
+                       public void test() throws Exception {
+                               Version version = new Version("1.0.0-debian0");
+                               assertEquals("debian", version.getTag());
+                               assertEquals(0, version.getTagVersion());
+                               version = new Version("1.0.0-debian.0");
+                               assertEquals("debian.", version.getTag());
+                               assertEquals(0, version.getTagVersion());
+                               version = new Version("1.0.0-debian-0");
+                               assertEquals("debian-", version.getTag());
+                               assertEquals(0, version.getTagVersion());
+                               version = new Version("1.0.0-debian-12");
+                               assertEquals("debian-", version.getTag());
+                               assertEquals(12, version.getTagVersion());
+                       }
+               });
+
                addTest(new TestCase("Comparing versions") {
                        @Override
                        public void test() throws Exception {
@@ -73,6 +91,16 @@ class VersionTest extends TestLauncher {
                                                new Version(0, 0, 1).equals(new Version(0, 0, 1)));
                                assertEquals(false,
                                                new Version(0, 2, 1).equals(new Version(0, 0, 1)));
+
+                               assertEquals(true,
+                                               new Version(1, 0, 1, "my.tag.", 2).equals(new Version(
+                                                               1, 0, 1, "my.tag.", 2)));
+                               assertEquals(false,
+                                               new Version(1, 0, 1, "my.tag.", 2).equals(new Version(
+                                                               1, 0, 0, "my.tag.", 2)));
+                               assertEquals(false,
+                                               new Version(1, 0, 1, "my.tag.", 2).equals(new Version(
+                                                               1, 0, 1, "not-my.tag.", 2)));
                        }
                });
        }