|
| 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 | +} |
0 commit comments