Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple SemVer version parsing and comparison in Java
public class Version implements Comparable<Version> {
@NonNull
public final int[] numbers;
public Version(@NonNull String version) {
final String split[] = version.split("\\-")[0].split("\\.");
numbers = new int[split.length];
for (int i = 0; i < split.length; i++) {
numbers[i] = Integer.valueOf(split[i]);
}
}
@Override
public int compareTo(@NonNull Version another) {
final int maxLength = Math.max(numbers.length, another.numbers.length);
for (int i = 0; i < maxLength; i++) {
final int left = i < numbers.length ? numbers[i] : 0;
final int right = i < another.numbers.length ? another.numbers[i] : 0;
if (left != right) {
return left < right ? -1 : 1;
}
}
return 0;
}
}
public class VersionTest {
@Test
public void newInstance_withTwoDotRelease_isParsedCorrectly() {
final Version version = new Version("1.26.6");
assertThat(version.numbers, is(new int[]{1, 26, 6}));
}
@Test
public void newInstance_withTwoDotReleaseAndPreReleaseName_isParsedCorrectly() {
final Version version = new Version("1.26.6-DEBUG");
assertThat(version.numbers, is(new int[]{1, 26, 6}));
}
@Test
public void compareTo_withEarlierVersion_isGreaterThan() {
assertThat(new Version("2.0.0").compareTo(new Version("1.0.0")), is(1));
}
@Test
public void compareTo_withSameVersion_isEqual() {
assertThat(new Version("2.0.0").compareTo(new Version("2.0.0")), is(0));
}
@Test
public void compareTo_withLaterVersion_isLessThan() {
assertThat(new Version("1.0.0").compareTo(new Version("2.0.0")), is(-1));
}
@Test
public void compareTo_withMorePreciseSameVersion_isFalse() {
assertThat(new Version("1").compareTo(new Version("1.0.0")), is(0));
}
@Test
public void compareTo_withMorePreciseEarlierVersion_isFalse() {
assertThat(new Version("2").compareTo(new Version("1.0.0")), is(1));
}
@Test
public void compareTo_withMorePreciseLaterVersion_isLessThan() {
assertThat(new Version("1").compareTo(new Version("1.0.1")), is(-1));
}
}
@brianguertin

This comment has been minimized.

Copy link
Owner Author

brianguertin commented Jul 8, 2016

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.