|
@@ -22,11 +22,20 @@ import java.util.regex.Pattern;
|
|
|
|
|
|
import org.apache.hadoop.classification.InterfaceAudience;
|
|
|
|
|
|
+import com.google.common.collect.ComparisonChain;
|
|
|
+
|
|
|
@InterfaceAudience.Private
|
|
|
public abstract class VersionUtil {
|
|
|
|
|
|
private static final Pattern COMPONENT_GROUPS = Pattern.compile("(\\d+)|(\\D+)");
|
|
|
|
|
|
+ /**
|
|
|
+ * Suffix added by maven for nightly builds and other snapshot releases.
|
|
|
+ * These releases are considered to precede the non-SNAPSHOT version
|
|
|
+ * with the same version number.
|
|
|
+ */
|
|
|
+ private static final String SNAPSHOT_SUFFIX = "-SNAPSHOT";
|
|
|
+
|
|
|
/**
|
|
|
* This function splits the two versions on "." and performs a
|
|
|
* naturally-ordered comparison of the resulting components. For example, the
|
|
@@ -48,6 +57,11 @@ public abstract class VersionUtil {
|
|
|
* between the two versions, then the version with fewer components is
|
|
|
* considered to precede the version with more components.
|
|
|
*
|
|
|
+ * In addition to the above rules, there is one special case: maven SNAPSHOT
|
|
|
+ * releases are considered to precede a non-SNAPSHOT release with an
|
|
|
+ * otherwise identical version number. For example, 2.0-SNAPSHOT precedes
|
|
|
+ * 2.0.
|
|
|
+ *
|
|
|
* This function returns a negative integer if version1 precedes version2, a
|
|
|
* positive integer if version2 precedes version1, and 0 if and only if the
|
|
|
* two versions' components are identical in value and cardinality.
|
|
@@ -61,6 +75,11 @@ public abstract class VersionUtil {
|
|
|
* versions are equal.
|
|
|
*/
|
|
|
public static int compareVersions(String version1, String version2) {
|
|
|
+ boolean isSnapshot1 = version1.endsWith(SNAPSHOT_SUFFIX);
|
|
|
+ boolean isSnapshot2 = version2.endsWith(SNAPSHOT_SUFFIX);
|
|
|
+ version1 = stripSnapshotSuffix(version1);
|
|
|
+ version2 = stripSnapshotSuffix(version2);
|
|
|
+
|
|
|
String[] version1Parts = version1.split("\\.");
|
|
|
String[] version2Parts = version2.split("\\.");
|
|
|
|
|
@@ -87,9 +106,21 @@ public abstract class VersionUtil {
|
|
|
return component1.length() - component2.length();
|
|
|
}
|
|
|
}
|
|
|
- return version1Parts.length - version2Parts.length;
|
|
|
+
|
|
|
+ return ComparisonChain.start()
|
|
|
+ .compare(version1Parts.length, version2Parts.length)
|
|
|
+ .compare(isSnapshot2, isSnapshot1)
|
|
|
+ .result();
|
|
|
}
|
|
|
|
|
|
+ private static String stripSnapshotSuffix(String version) {
|
|
|
+ if (version.endsWith(SNAPSHOT_SUFFIX)) {
|
|
|
+ return version.substring(0, version.length() - SNAPSHOT_SUFFIX.length());
|
|
|
+ } else {
|
|
|
+ return version;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static boolean isNumeric(String s) {
|
|
|
try {
|
|
|
Integer.parseInt(s);
|