Class Versions


  • public class Versions
    extends Object
    A few simple version comparison utilities.
    • Constructor Detail

      • Versions

        public Versions()
    • Method Detail

      • higherVersion

        public static String higherVersion​(String left,
                                           String right)
        Given two versions, return the higher version
        Parameters:
        left - the first version to compare
        right - the second version to compare
        Returns:
        the higher of the given versions
      • isStrictlyHigherVersion

        public static boolean isStrictlyHigherVersion​(String left,
                                                      String right)
        Returns true if the "left" version is strictly higher than the "right" version.
        Parameters:
        left - the first version to compare
        right - the second version to compare
        Returns:
        true if left is greater than right
      • isHigherOrSameVersion

        public static boolean isHigherOrSameVersion​(String left,
                                                    String right)
        Returns true if the "left" version is higher than or equal to the "right" version.
        Parameters:
        left - the first version to compare
        right - the second version to compare
        Returns:
        true if left is greater than or equal to right
      • isStrictlyLowerVersion

        public static boolean isStrictlyLowerVersion​(String left,
                                                     String right)
        Returns true if the "left" version is strictly lower than the "right" version.
        Parameters:
        left - the first version to compare
        right - the second version to compare
        Returns:
        true if left is lower than right
      • isLowerOrSameVersion

        public static boolean isLowerOrSameVersion​(String left,
                                                   String right)
        Returns true if the "left" version is lower than or equal to the "right" version.
        Parameters:
        left - the first version to compare
        right - the second version to compare
        Returns:
        true if left is less than or equal to right
      • isSameVersion

        public static boolean isSameVersion​(String left,
                                            String right)
        Returns true if the "left" version exactly equals the "right" version.
        Parameters:
        left - the first version to compare
        right - the second version to compare
        Returns:
        true if left equals right
      • versionCompare

        public static int versionCompare​(String left,
                                         String right)
        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 the Integer.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:
        Integer.signum(int)
        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.