Skip to content

Commit 86d153a

Browse files
authored
Merge pull request #79 from opencb/TASK-4641
TASK-4641Add VersionUtils
2 parents 73220d8 + e712c95 commit 86d153a

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package org.opencb.commons.utils;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
import java.util.Comparator;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class VersionUtils {
10+
11+
public static List<String> order(List<String> versions) {
12+
return versions.stream().map(Version::new).sorted().map(Version::toString).collect(Collectors.toList());
13+
}
14+
15+
public static boolean isMinVersion(String minVersion, String version) {
16+
return isMinVersion(minVersion, version, false);
17+
}
18+
19+
public static boolean isMinVersion(String minVersion, String version, boolean ignorePreReleaseVersioning) {
20+
return new Version(minVersion).compareTo(new Version(version), ignorePreReleaseVersioning) <= 0;
21+
}
22+
23+
public static class Version implements Comparable<Version> {
24+
25+
private final int major;
26+
private final int minor;
27+
private final int patch;
28+
private final int repatch;
29+
private final String other;
30+
31+
public static final Comparator<Version> COMPARATOR = Comparator
32+
.comparingInt(Version::getMajor)
33+
.thenComparingInt(Version::getMinor)
34+
.thenComparingInt(Version::getPatch)
35+
.thenComparingInt(Version::getRepatch)
36+
.thenComparing((o1, o2) -> {
37+
if (o1.other.equals(o2.other)) {
38+
return 0;
39+
}
40+
if (o1.other.isEmpty()) {
41+
return +1;
42+
}
43+
if (o2.other.isEmpty()) {
44+
return -1;
45+
}
46+
if (o1.other.equals("-SNAPSHOT")) {
47+
return -1;
48+
}
49+
if (o2.other.equals("-SNAPSHOT")) {
50+
return +1;
51+
}
52+
return o1.other.compareTo(o2.other);
53+
});
54+
55+
public static final Comparator<Version> COMPARATOR_NO_PR = Comparator
56+
.comparingInt(Version::getMajor)
57+
.thenComparingInt(Version::getMinor)
58+
.thenComparingInt(Version::getPatch)
59+
.thenComparingInt(Version::getRepatch);
60+
61+
public Version(String version) {
62+
String[] split = StringUtils.split(version, ".", 4);
63+
if (split[0].startsWith("v")) {
64+
major = Integer.parseInt(split[0].substring(1));
65+
} else {
66+
major = Integer.parseInt(split[0]);
67+
}
68+
minor = Integer.parseInt(split[1]);
69+
if (split.length == 4) {
70+
patch = Integer.parseInt(split[2]);
71+
String last = split[3];
72+
String[] split2 = StringUtils.split(last, "-+", 3);
73+
repatch = Integer.parseInt(split2[0]);
74+
other = last.substring(split2[0].length());
75+
} else if (split.length == 3) {
76+
String last = split[2];
77+
String[] split2 = StringUtils.split(last, "-+", 2);
78+
patch = Integer.parseInt(split2[0]);
79+
repatch = 0;
80+
other = last.substring(split2[0].length());
81+
} else {
82+
patch = 0;
83+
repatch = 0;
84+
other = "";
85+
}
86+
}
87+
88+
@Override
89+
public String toString() {
90+
if (repatch > 0) {
91+
return major + "." + minor + "." + patch + "." + repatch + other;
92+
} else {
93+
return major + "." + minor + "." + patch + other;
94+
}
95+
}
96+
97+
@Override
98+
public int compareTo(Version o) {
99+
return COMPARATOR.compare(this, o);
100+
}
101+
102+
public int compareTo(Version o, boolean ignorePreReleaseVersioning) {
103+
if (ignorePreReleaseVersioning) {
104+
return COMPARATOR_NO_PR.compare(this, o);
105+
} else {
106+
return COMPARATOR.compare(this, o);
107+
}
108+
}
109+
110+
public int getMajor() {
111+
return major;
112+
}
113+
114+
public int getMinor() {
115+
return minor;
116+
}
117+
118+
public int getPatch() {
119+
return patch;
120+
}
121+
122+
public int getRepatch() {
123+
return repatch;
124+
}
125+
126+
public String getOther() {
127+
return other;
128+
}
129+
}
130+
131+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.opencb.commons.utils;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class VersionUtilsTest {
7+
public static String getComparation(String minVersion, String version) {
8+
int c = new VersionUtils.Version(minVersion).compareTo(new VersionUtils.Version(version));
9+
String comp;
10+
if (c == 0) {
11+
comp = "=";
12+
} else if (c < 0) {
13+
comp = "<";
14+
} else {
15+
comp = ">";
16+
}
17+
System.out.println(minVersion + "\t" + comp + "\t" + version);
18+
return comp;
19+
}
20+
21+
@Test
22+
public void testOrder() {
23+
Assert.assertEquals("<", getComparation("5.2.7", "5.2.8"));
24+
Assert.assertEquals("=", getComparation("5.2.7", "5.2.7"));
25+
Assert.assertEquals(">", getComparation("5.2.7.1", "5.2.7.1-alpha"));
26+
Assert.assertEquals(">", getComparation("5.2.7", "5.2.7-SNAPSHOT"));
27+
Assert.assertEquals("<", getComparation("5.2.7-alpha", "5.2.7"));
28+
Assert.assertEquals("<", getComparation("5.2.7-alpha", "5.2.7-beta"));
29+
Assert.assertEquals(">", getComparation("5.2.7", "5.2.6"));
30+
Assert.assertEquals("=", getComparation("5.2.7", "5.2.7.0"));
31+
}
32+
33+
@Test
34+
public void testOrderShortVersions() {
35+
Assert.assertEquals("<", getComparation("v5.2", "v5.3"));
36+
Assert.assertEquals("=", getComparation("v5.2", "v5.2"));
37+
Assert.assertEquals(">", getComparation("v5.2", "v5.1"));
38+
Assert.assertEquals(">", getComparation("v5.6", "v5.2"));
39+
}
40+
}

0 commit comments

Comments
 (0)