X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FVersion.java;h=2c6f3d0913578601c9ee65dcc0c3486fd8971648;hp=2f10c5eba8f982076737543002b9a3f0176596e2;hb=632cc690ac07130a51a947ee0f0b2bfae10b6595;hpb=370393552d119891ac3a9ecd78c7215881a26e09 diff --git a/src/be/nikiroo/utils/Version.java b/src/be/nikiroo/utils/Version.java index 2f10c5e..2c6f3d0 100644 --- a/src/be/nikiroo/utils/Version.java +++ b/src/be/nikiroo/utils/Version.java @@ -80,14 +80,7 @@ public class Version implements Comparable { 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); + this.version = generateVersion(); } /** @@ -102,18 +95,19 @@ public class Version implements Comparable { public Version(String version) { try { String[] tab = version.split("\\."); - this.major = Integer.parseInt(tab[0]); - this.minor = Integer.parseInt(tab[1]); + this.major = Integer.parseInt(tab[0].trim()); + this.minor = Integer.parseInt(tab[1].trim()); 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)); + this.patch = Integer.parseInt(rest.substring(0, posInRest) + .trim()); posInVersion = version.indexOf('-'); - this.tag = version.substring(posInVersion + 1); + this.tag = version.substring(posInVersion + 1).trim(); this.tagVersion = -1; StringBuilder str = new StringBuilder(); @@ -127,12 +121,12 @@ public class Version implements Comparable { this.tagVersion = Integer.parseInt(str.toString()); } } else { - this.patch = Integer.parseInt(tab[2]); + this.patch = Integer.parseInt(tab[2].trim()); this.tag = null; this.tagVersion = -1; } - this.version = toString(); + this.version = generateVersion(); } catch (Exception e) { this.major = 0; this.minor = 0; @@ -189,7 +183,7 @@ public class Version implements Comparable { } /** - * The version of the tag. + * The version of the tag, or -1 for no version. * * @return the tag version */ @@ -200,13 +194,11 @@ public class Version implements Comparable { /** * Check if this {@link Version} is "empty" (i.e., the version was not * parse-able or not given). - *

- * An empty {@link Version} is always 0.0.0. * * @return TRUE if it is empty */ public boolean isEmpty() { - return major == 0 && minor == 0 && patch == 0 && tag == null; + return version == null; } /** @@ -214,12 +206,20 @@ public class Version implements Comparable { *

* Note that a tagged version is considered newer than a non-tagged version, * but two tagged versions with different tags are not comparable. + *

+ * Also, an empty version is always considered older. * * @param o * the other {@link Version} * @return TRUE if this {@link Version} is more recent than the given one */ public boolean isNewerThan(Version o) { + if (isEmpty()) { + return false; + } else if (o.isEmpty()) { + return true; + } + if (major > o.major) { return true; } @@ -251,12 +251,23 @@ public class Version implements Comparable { /** * Check if we are older than the given {@link Version}. + *

+ * Note that a tagged version is considered newer than a non-tagged version, + * but two tagged versions with different tags are not comparable. + *

+ * Also, an empty version is always considered older. * * @param o * the other {@link Version} * @return TRUE if this {@link Version} is older than the given one */ public boolean isOlderThan(Version o) { + if (o.isEmpty()) { + return false; + } else if (isEmpty()) { + return true; + } + // 2 <> tagged versions are not comparable boolean sameTag = (tag == null && o.tag == null) || (tag != null && tag.equals(o.tag)); @@ -311,6 +322,10 @@ public class Version implements Comparable { public boolean equals(Object obj) { if (obj instanceof Version) { Version o = (Version) obj; + if (isEmpty()) { + return o.isEmpty(); + } + boolean sameTag = (tag == null && o.tag == null) || (tag != null && tag.equals(o.tag)); return o.major == major && o.minor == minor && o.patch == patch @@ -332,4 +347,19 @@ public class Version implements Comparable { public String toString() { return version == null ? "[unknown]" : version; } + + /** + * Generate the clean version {@link String} from the current values. + * + * @return the clean version string + */ + private String generateVersion() { + String tagSuffix = ""; + if (tag != null) { + tagSuffix = "-" + tag + + (tagVersion >= 0 ? Integer.toString(tagVersion) : ""); + } + + return String.format("%d.%d.%d%s", major, minor, patch, tagSuffix); + } }