Class VersionComparatorUtil

java.lang.Object
com.ultikits.ultitools.utils.VersionComparatorUtil

public final class VersionComparatorUtil extends Object
Semantic version comparison utility class.

The full ordering contract, since several build-gate checks in this repository depend on it:

  1. A leading version-prefix letter (v or V) is stripped before comparison.
  2. The remaining string is split into segments on both . and -.
  3. When both segments at a position parse as integers, they compare numerically.
  4. 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.
  5. A missing trailing segment (the shorter version ran out of segments) is treated as 0 for that position, so 1.2 and 1.2.0 compare equal.
The pre-release fallback is what ranks a snapshot below the plain release of the same numeric core (e.g. 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 Details

    • COMPARATOR

      public static final Comparator<String> COMPARATOR
      Singleton version comparator.
  • Constructor Details

    • VersionComparatorUtil

      private VersionComparatorUtil()
  • Method Details

    • compare

      public static int compare(String v1, String v2)
      Compares two version strings.
      Parameters:
      v1 - the first version
      v2 - the second version
      Returns:
      negative if v1 < v2, 0 if v1 == v2, positive if v1 > v2
    • isLessThan

      public static boolean isLessThan(String v1, String v2)
      Returns whether v1 is less than v2.
    • isGreaterThan

      public static boolean isGreaterThan(String v1, String v2)
      Returns whether v1 is greater than v2.
    • isEqual

      public static boolean isEqual(String v1, String v2)
      Returns whether v1 is equal to v2.
    • stripPrefix

      private static String stripPrefix(String version)
      Strips the version prefix.
    • compareSegment

      private static int compareSegment(String s1, String s2)
      Compares a single version segment.
    • parseIntOrNull

      private static Integer parseIntOrNull(String s)
      Parses an integer, returning null on failure.
    • getPreReleasePriority

      private static int getPreReleasePriority(String segment)
      Gets the pre-release priority.

      alpha < beta < rc < SNAPSHOT < (release)