Package org.kiwiproject.base
Class Versions
java.lang.Object
org.kiwiproject.base.Versions
A few simple version comparison utilities.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringhigherVersion(String left, String right) Given two versions, return the higher versionstatic booleanisHigherOrSameVersion(String left, String right) Returns true if the "left" version is higher than or equal to the "right" version.static booleanisLowerOrSameVersion(String left, String right) Returns true if the "left" version is lower than or equal to the "right" version.static booleanisSameVersion(String left, String right) Returns true if the "left" version exactly equals the "right" version.static booleanisStrictlyHigherVersion(String left, String right) Returns true if the "left" version is strictly higher than the "right" version.static booleanisStrictlyLowerVersion(String left, String right) Returns true if the "left" version is strictly lower than the "right" version.static intversionCompare(String left, String right) Performs a case-insensitive, segment by segment comparison of numeric and alphanumeric version numbers.
-
Method Details
-
higherVersion
Given two versions, return the higher version- Parameters:
left- the first version to compareright- the second version to compare- Returns:
- the highest of the given versions
-
isStrictlyHigherVersion
Returns true if the "left" version is strictly higher than the "right" version.- Parameters:
left- the first version to compareright- the second version to compare- Returns:
- true if
leftis greater thanright
-
isHigherOrSameVersion
Returns true if the "left" version is higher than or equal to the "right" version.- Parameters:
left- the first version to compareright- the second version to compare- Returns:
- true if
leftis greater than or equal toright
-
isStrictlyLowerVersion
Returns true if the "left" version is strictly lower than the "right" version.- Parameters:
left- the first version to compareright- the second version to compare- Returns:
- true if
leftis lower thanright
-
isLowerOrSameVersion
Returns true if the "left" version is lower than or equal to the "right" version.- Parameters:
left- the first version to compareright- the second version to compare- Returns:
- true if
leftis less than or equal toright
-
isSameVersion
Returns true if the "left" version exactly equals the "right" version.- Parameters:
left- the first version to compareright- the second version to compare- Returns:
- true if
leftequalsright
-
versionCompare
Performs a case-insensitive, segment by segment comparison of numeric and alphanumeric version numbers. Versions are split on periods and dashes. For example, the segments of "2.5.0" are "2", "5", and "0" while the segments of "1.0.0-alpha.3" are "1", "0", "0", "alpha", and "3". Returns -1, 0, or 1 as the "left" version is less than, equal to, or greater than the "right" version. (These return values correspond to the values returned by theInteger.signum(int)function.)When a segment is determined to be non-numeric, a case-insensitive string comparison is performed. When the number of segments in the version are different, then the general logic is that the shorter segment is the higher version. This covers commons situations such as 1.0.0-SNAPSHOT, 1.0.0-alpha, and 1.0.0-beta.2, which should all be lower versions than 1.0.0.
- Parameters:
left- the first version number (e.g. "1.2.3" or "2.0.0-alpha1")right- the second version number (e.g. "1.2.4" or "2.0.0-alpha2")- Returns:
- -1 if "left" is less than "right", 0 if the versions are equal, and 1 if "left" is higher than "right"
- See Also:
- Implementation Note:
- The current implementation works best when versions have the same number of segments, e.g. comparing 2.1.0 vs 2.0.0. It also works fine with different number of segments when those different segments are numeric, such as 1.0.0 vs 1.0.0.42 (the latter is higher). It also handles most normal cases when the last segments are different and are non-numeric, e.g. 1.0.0 should be considered a higher version than 1.0.0-SNAPSHOT or 1.0.0-alpha. There are various edge cases that might report results that might not be what you expect; for example, should 2.0.0-beta.1 be a higher or lower version than 2.0.0-beta? Currently, 2.0.0-beta is reported as the higher version due to the simple implementation. However, note that 2.0.0-beta1 would be reported as higher than 2.0.0-beta (because the String "beta" is "greater than" the String "beta" using (Java) string comparison).
-