From e712c95707cc4adf6bcc66d0f57f2ae3c0dd2195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20T=C3=A1rraga=20Gim=C3=A9nez?= Date: Tue, 29 Aug 2023 11:37:57 +0200 Subject: [PATCH] utils: add VersionUtils, #TASK-4908, #TASK-4641 --- .../opencb/commons/utils/VersionUtils.java | 131 ++++++++++++++++++ .../commons/utils/VersionUtilsTest.java | 40 ++++++ 2 files changed, 171 insertions(+) create mode 100644 commons-lib/src/main/java/org/opencb/commons/utils/VersionUtils.java create mode 100644 commons-lib/src/test/java/org/opencb/commons/utils/VersionUtilsTest.java diff --git a/commons-lib/src/main/java/org/opencb/commons/utils/VersionUtils.java b/commons-lib/src/main/java/org/opencb/commons/utils/VersionUtils.java new file mode 100644 index 000000000..998de46af --- /dev/null +++ b/commons-lib/src/main/java/org/opencb/commons/utils/VersionUtils.java @@ -0,0 +1,131 @@ +package org.opencb.commons.utils; + +import org.apache.commons.lang3.StringUtils; + +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +public class VersionUtils { + + public static List order(List versions) { + return versions.stream().map(Version::new).sorted().map(Version::toString).collect(Collectors.toList()); + } + + public static boolean isMinVersion(String minVersion, String version) { + return isMinVersion(minVersion, version, false); + } + + public static boolean isMinVersion(String minVersion, String version, boolean ignorePreReleaseVersioning) { + return new Version(minVersion).compareTo(new Version(version), ignorePreReleaseVersioning) <= 0; + } + + public static class Version implements Comparable { + + private final int major; + private final int minor; + private final int patch; + private final int repatch; + private final String other; + + public static final Comparator COMPARATOR = Comparator + .comparingInt(Version::getMajor) + .thenComparingInt(Version::getMinor) + .thenComparingInt(Version::getPatch) + .thenComparingInt(Version::getRepatch) + .thenComparing((o1, o2) -> { + if (o1.other.equals(o2.other)) { + return 0; + } + if (o1.other.isEmpty()) { + return +1; + } + if (o2.other.isEmpty()) { + return -1; + } + if (o1.other.equals("-SNAPSHOT")) { + return -1; + } + if (o2.other.equals("-SNAPSHOT")) { + return +1; + } + return o1.other.compareTo(o2.other); + }); + + public static final Comparator COMPARATOR_NO_PR = Comparator + .comparingInt(Version::getMajor) + .thenComparingInt(Version::getMinor) + .thenComparingInt(Version::getPatch) + .thenComparingInt(Version::getRepatch); + + public Version(String version) { + String[] split = StringUtils.split(version, ".", 4); + if (split[0].startsWith("v")) { + major = Integer.parseInt(split[0].substring(1)); + } else { + major = Integer.parseInt(split[0]); + } + minor = Integer.parseInt(split[1]); + if (split.length == 4) { + patch = Integer.parseInt(split[2]); + String last = split[3]; + String[] split2 = StringUtils.split(last, "-+", 3); + repatch = Integer.parseInt(split2[0]); + other = last.substring(split2[0].length()); + } else if (split.length == 3) { + String last = split[2]; + String[] split2 = StringUtils.split(last, "-+", 2); + patch = Integer.parseInt(split2[0]); + repatch = 0; + other = last.substring(split2[0].length()); + } else { + patch = 0; + repatch = 0; + other = ""; + } + } + + @Override + public String toString() { + if (repatch > 0) { + return major + "." + minor + "." + patch + "." + repatch + other; + } else { + return major + "." + minor + "." + patch + other; + } + } + + @Override + public int compareTo(Version o) { + return COMPARATOR.compare(this, o); + } + + public int compareTo(Version o, boolean ignorePreReleaseVersioning) { + if (ignorePreReleaseVersioning) { + return COMPARATOR_NO_PR.compare(this, o); + } else { + return COMPARATOR.compare(this, o); + } + } + + public int getMajor() { + return major; + } + + public int getMinor() { + return minor; + } + + public int getPatch() { + return patch; + } + + public int getRepatch() { + return repatch; + } + + public String getOther() { + return other; + } + } + +} diff --git a/commons-lib/src/test/java/org/opencb/commons/utils/VersionUtilsTest.java b/commons-lib/src/test/java/org/opencb/commons/utils/VersionUtilsTest.java new file mode 100644 index 000000000..e35beb025 --- /dev/null +++ b/commons-lib/src/test/java/org/opencb/commons/utils/VersionUtilsTest.java @@ -0,0 +1,40 @@ +package org.opencb.commons.utils; + +import org.junit.Assert; +import org.junit.Test; + +public class VersionUtilsTest { + public static String getComparation(String minVersion, String version) { + int c = new VersionUtils.Version(minVersion).compareTo(new VersionUtils.Version(version)); + String comp; + if (c == 0) { + comp = "="; + } else if (c < 0) { + comp = "<"; + } else { + comp = ">"; + } + System.out.println(minVersion + "\t" + comp + "\t" + version); + return comp; + } + + @Test + public void testOrder() { + Assert.assertEquals("<", getComparation("5.2.7", "5.2.8")); + Assert.assertEquals("=", getComparation("5.2.7", "5.2.7")); + Assert.assertEquals(">", getComparation("5.2.7.1", "5.2.7.1-alpha")); + Assert.assertEquals(">", getComparation("5.2.7", "5.2.7-SNAPSHOT")); + Assert.assertEquals("<", getComparation("5.2.7-alpha", "5.2.7")); + Assert.assertEquals("<", getComparation("5.2.7-alpha", "5.2.7-beta")); + Assert.assertEquals(">", getComparation("5.2.7", "5.2.6")); + Assert.assertEquals("=", getComparation("5.2.7", "5.2.7.0")); + } + + @Test + public void testOrderShortVersions() { + Assert.assertEquals("<", getComparation("v5.2", "v5.3")); + Assert.assertEquals("=", getComparation("v5.2", "v5.2")); + Assert.assertEquals(">", getComparation("v5.2", "v5.1")); + Assert.assertEquals(">", getComparation("v5.6", "v5.2")); + } +} \ No newline at end of file