Last active
June 27, 2022 17:44
-
-
Save SupaHam/dad1db6406596c5f8e4b221ff473831c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.base.Objects; | |
import com.google.common.base.Preconditions; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* https://gist.github.com/SupaHam/dad1db6406596c5f8e4b221ff473831c | |
* | |
* @author SupaHam (https://github.com/SupaHam) | |
*/ | |
public class NMSVersion implements Comparable<NMSVersion> { | |
public static final NMSVersion V1_8_R1 = NMSVersion.fromString("v1_8_R1"); | |
public static final NMSVersion V1_8_R2 = NMSVersion.fromString("v1_8_R2"); | |
public static final NMSVersion V1_8_R3 = NMSVersion.fromString("v1_8_R3"); | |
public static final NMSVersion V1_9_R1 = NMSVersion.fromString("v1_9_R1"); | |
public static final NMSVersion V1_10_R1 = NMSVersion.fromString("v1_10_R1"); | |
private static final Pattern VERSION_PATTERN = Pattern.compile("^v(\\d+)_(\\d+)_R(\\d+)"); | |
private final int major; | |
private final int minor; | |
private final int release; | |
public static NMSVersion fromString(String string) { | |
Preconditions.checkNotNull(string, "string cannot be null."); | |
Matcher matcher = VERSION_PATTERN.matcher(string); | |
Preconditions.checkArgument(matcher.matches(), string + " is not in valid version format. e.g. v1_10_R1"); | |
return new NMSVersion(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3))); | |
} | |
private NMSVersion(int major, int minor, int release) { | |
this.major = major; | |
this.minor = minor; | |
this.release = release; | |
} | |
public boolean isHigherThan(NMSVersion o) { | |
return compareTo(o) > 0; | |
} | |
public boolean isHigherThanOrEqualTo(NMSVersion o) { | |
return compareTo(o) >= 0; | |
} | |
public boolean isLowerThan(NMSVersion o) { | |
return compareTo(o) < 0; | |
} | |
public boolean isLowerThanOrEqualTo(NMSVersion o) { | |
return compareTo(o) <= 0; | |
} | |
public int getMajor() { | |
return major; | |
} | |
public int getMinor() { | |
return minor; | |
} | |
public int getRelease() { | |
return release; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (o == null || getClass() != o.getClass()) { | |
return false; | |
} | |
NMSVersion that = (NMSVersion) o; | |
return major == that.major && | |
minor == that.minor && | |
release == that.release; | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hashCode(major, minor, release); | |
} | |
@Override | |
public String toString() { | |
return "v" + major + "_" + minor + "_R" + release; | |
} | |
@Override | |
public int compareTo(NMSVersion o) { | |
if (major < o.major) { | |
return -1; | |
} else if (major > o.major) { | |
return 1; | |
} else { // equal major | |
if (minor < o.minor) { | |
return -1; | |
} else if (minor > o.minor) { | |
return 1; | |
} else { // equal minor | |
if (release < o.release) { | |
return -1; | |
} else if (release > o.release) { | |
return 1; | |
} else { | |
return 0; // o is the same version as this. | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NMSVersionTest { | |
@Test | |
public void testMajor() throws Exception { | |
NMSVersion v2_9_R1 = NMSVersion.fromString("v2_9_R1"); | |
Assert.assertEquals(2, v2_9_R1.getMajor()); | |
Assert.assertEquals(9, v2_9_R1.getMinor()); | |
Assert.assertEquals(1, v2_9_R1.getRelease()); | |
Assert.assertEquals(v2_9_R1.toString(), "v2_9_R1"); | |
Assert.assertTrue(v2_9_R1.isHigherThan(NMSVersion.fromString("v1_10_R1"))); | |
Assert.assertTrue(v2_9_R1.isHigherThanOrEqualTo(NMSVersion.fromString("v1_9_R1"))); | |
} | |
@Test | |
public void testMinor() throws Exception { | |
NMSVersion v1_10_R1 = NMSVersion.fromString("v1_10_R1"); | |
Assert.assertEquals(1, v1_10_R1.getMajor()); | |
Assert.assertEquals(10, v1_10_R1.getMinor()); | |
Assert.assertEquals(1, v1_10_R1.getRelease()); | |
Assert.assertEquals(v1_10_R1.toString(), "v1_10_R1"); | |
Assert.assertTrue(NMSVersion.fromString("v1_9_R1").isLowerThan(v1_10_R1)); | |
Assert.assertTrue(NMSVersion.fromString("v1_9_R1").isLowerThanOrEqualTo(v1_10_R1)); | |
} | |
@Test | |
public void testRelease() throws Exception { | |
NMSVersion v1_9_R2 = NMSVersion.fromString("v1_9_R2"); | |
Assert.assertEquals(1, v1_9_R2.getMajor()); | |
Assert.assertEquals(9, v1_9_R2.getMinor()); | |
Assert.assertEquals(2, v1_9_R2.getRelease()); | |
Assert.assertEquals(v1_9_R2.toString(), "v1_9_R2"); | |
Assert.assertEquals(v1_9_R2, NMSVersion.fromString("v1_9_R2")); | |
Assert.assertTrue(v1_9_R2.isHigherThan(NMSVersion.fromString("v1_9_R1"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment