[platform] adds comparison methods to Java version class

This commit is contained in:
Roman Shevchenko
2018-01-15 10:22:51 +01:00
parent 751faeec13
commit af03dee866
2 changed files with 27 additions and 2 deletions
@@ -19,7 +19,7 @@ import java.util.List;
* @see #parse(String) for examples of supported version strings
* @since 2018.1
*/
public final class JavaVersion {
public final class JavaVersion implements Comparable<JavaVersion> {
/**
* The major version.
* Corresponds to the first number of a Java 9+ version string and to the second number of Java 1.0 to 1.8 strings.
@@ -58,6 +58,23 @@ public final class JavaVersion {
this.ea = ea;
}
@Override
public int compareTo(@NotNull JavaVersion o) {
int diff = feature - o.feature;
if (diff != 0) return diff;
diff = minor - o.minor;
if (diff != 0) return diff;
diff = update - o.update;
if (diff != 0) return diff;
diff = build - o.build;
if (diff != 0) return diff;
return (ea ? 0 : 1) - (o.ea ? 0 : 1);
}
public boolean isAtLeast(int feature) {
return this.feature >= feature;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package com.intellij.util.lang
@@ -60,6 +60,14 @@ class JavaVersionTest {
assertThat(current.build).isGreaterThan(0)
}
@Test fun comparing() {
assertThat(JavaVersion.compose(9, 0, 0, 0, false)).isGreaterThan(JavaVersion.compose(8, 0, 0, 0, false))
assertThat(JavaVersion.compose(8, 1, 0, 0, false)).isGreaterThan(JavaVersion.compose(8, 0, 0, 0, false))
assertThat(JavaVersion.compose(8, 0, 1, 0, false)).isGreaterThan(JavaVersion.compose(8, 0, 0, 0, false))
assertThat(JavaVersion.compose(8, 0, 0, 1, false)).isGreaterThan(JavaVersion.compose(8, 0, 0, 0, false))
assertThat(JavaVersion.compose(8, 0, 0, 0, false)).isGreaterThan(JavaVersion.compose(8, 0, 0, 0, true))
}
private fun doTest(versionString: String,
feature: Int,
minor: Int = 0,