Package com.ultikits.ultitools.utils
Class VersionComparatorUtil
java.lang.Object
com.ultikits.ultitools.utils.VersionComparatorUtil
Semantic version comparison utility class.
The full ordering contract, since several build-gate checks in this repository depend on it:
- A leading version-prefix letter (
vorV) is stripped before comparison. - The remaining string is split into segments on both
.and-. - When both segments at a position parse as integers, they compare numerically.
- When exactly one segment parses as an integer, the numeric segment sorts higher (a numeric
segment is always considered newer than a non-numeric one at the same position). When neither
parses as an integer, they fall back first to a pre-release priority ordering
(
alpha < beta < rc < SNAPSHOT < a plain release), and only if that priority is equal do they compare lexicographically, case-insensitively. - A missing trailing segment (the shorter version ran out of segments) is treated as
0for that position, so1.2and1.2.0compare equal.
1.2.0-SNAPSHOT sorts below 1.2.0), since a non-numeric segment never
outranks a numeric one and SNAPSHOT's own priority (4) sits below a plain release's (5).- Since:
- 6.2.0
- Author:
- wisdomme
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intCompares two version strings.private static intcompareSegment(String s1, String s2) Compares a single version segment.private static intgetPreReleasePriority(String segment) Gets the pre-release priority.static booleanReturns whether v1 is equal to v2.static booleanisGreaterThan(String v1, String v2) Returns whether v1 is greater than v2.static booleanisLessThan(String v1, String v2) Returns whether v1 is less than v2.private static IntegerParses an integer, returning null on failure.private static StringstripPrefix(String version) Strips the version prefix.
-
Field Details
-
COMPARATOR
Singleton version comparator.
-
-
Constructor Details
-
VersionComparatorUtil
private VersionComparatorUtil()
-
-
Method Details
-
compare
Compares two version strings.- Parameters:
v1- the first versionv2- the second version- Returns:
- negative if v1 < v2, 0 if v1 == v2, positive if v1 > v2
-
isLessThan
Returns whether v1 is less than v2. -
isGreaterThan
Returns whether v1 is greater than v2. -
isEqual
Returns whether v1 is equal to v2. -
stripPrefix
Strips the version prefix. -
compareSegment
Compares a single version segment. -
parseIntOrNull
Parses an integer, returning null on failure. -
getPreReleasePriority
Gets the pre-release priority.alpha < beta < rc < SNAPSHOT < (release)
-