X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FVersion.java;h=2c6f3d0913578601c9ee65dcc0c3486fd8971648;hp=613fcfe54130ef04d4df068091d7e52a26879002;hb=632cc690ac07130a51a947ee0f0b2bfae10b6595;hpb=b0376a373cca5505d0ab5c620aec7821b8fe3064 diff --git a/src/be/nikiroo/utils/Version.java b/src/be/nikiroo/utils/Version.java index 613fcfe..2c6f3d0 100644 --- a/src/be/nikiroo/utils/Version.java +++ b/src/be/nikiroo/utils/Version.java @@ -9,11 +9,20 @@ import java.io.InputStream; * * @author niki */ -public class Version { +public class Version implements Comparable { private String version; private int major; private int minor; private int patch; + private String tag; + private int tagVersion; + + /** + * Create a new, empty {@link Version}. + * + */ + public Version() { + } /** * Create a new {@link Version} with the given values. @@ -26,30 +35,104 @@ public class 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; + + this.version = generateVersion(); } /** * Create a new {@link Version} with the given value, which must be in the - * form MAJOR.MINOR.PATCH. + * form MAJOR.MINOR.PATCH(-TAG(TAG_VERSION)). * * @param version - * the version (MAJOR.MINOR.PATCH) + * the version (MAJOR.MINOR.PATCH, + * MAJOR.MINOR.PATCH-TAG or + * MAJOR.MINOR.PATCH-TAGVERSIONTAG) */ 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; + 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) + .trim()); + + posInVersion = version.indexOf('-'); + this.tag = version.substring(posInVersion + 1).trim(); + 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].trim()); + this.tag = null; + this.tagVersion = -1; + } + + this.version = generateVersion(); } catch (Exception e) { this.major = 0; this.minor = 0; this.patch = 0; + this.tag = null; + this.tagVersion = -1; this.version = null; } } @@ -90,16 +173,110 @@ public class Version { return patch; } + /** + * A tag name for this version. + * + * @return the tag + */ + public String getTag() { + return tag; + } + + /** + * The version of the tag, or -1 for no version. + * + * @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). - *

- * An empty {@link Version} is always 0.0.0. * - * @return TRUS if it is empty + * @return TRUE if it is empty */ public boolean isEmpty() { - return major == 0 && minor == 0 && patch == 0; + return version == null; + } + + /** + * Check if we are more recent 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 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; + } + + if (major == o.major && minor > o.minor) { + return true; + } + + if (major == o.major && minor == o.minor && patch > o.patch) { + 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; + } + + /** + * 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)); + if (major == o.major && minor == o.minor && patch == o.patch + && !sameTag) { + return false; + } + + return !equals(o) && !isNewerThan(o); } /** @@ -131,6 +308,38 @@ public class Version { return new Version(version); } + public int compareTo(Version o) { + if (equals(o)) { + return 0; + } else if (isNewerThan(o)) { + return 1; + } else { + return -1; + } + } + + @Override + 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 + && sameTag && o.tagVersion == tagVersion; + } + + return false; + } + + @Override + public int hashCode() { + return version == null ? 0 : version.hashCode(); + } + /** * Return a user-readable form of this {@link Version}. */ @@ -138,4 +347,19 @@ public class Version { 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); + } }