Class VersionComparator

  • All Implemented Interfaces:
    Comparator<String>

    public class VersionComparator
    extends Object
    implements Comparator<String>
    Compares two versions. The comparison is case-insensitive.

    Some qualifiers map to other qualifiers. Below is a table of those mappings.

    Qualifier Mapping
    GA Final
    a Alpha
    b Beta
    m Milestone
    cr rc

    Author:
    James R. Perkins
    • Constructor Detail

      • VersionComparator

        public VersionComparator()
        Creates a new version comparator.
    • Method Detail

      • getInstance

        public static VersionComparator getInstance()
        Returns an instance of a version comparator.
        Returns:
        a version comparator instance
      • getInstance

        public static VersionComparator getInstance​(boolean ignoreSnapshots)
        Returns an instance of a version comparator which optionally ignore the SNAPSHOT release extension. This can be useful for cases where you want to compare a version is at least a base version, not caring if it's a SNAPSHOT.
        Parameters:
        ignoreSnapshots - true to ignore the SNAPSHOT release extension, otherwise false which values a SNAPSHOT dependency less than a non-SNAPSHOT of the same version
        Returns:
        a version comparator instance
      • compareVersion

        public static int compareVersion​(String v1,
                                         String v2)
        Compares the first version against the second version.
        Parameters:
        v1 - first version
        v2 - second version
        Returns:
        0 if the versions are equal, -1 if version first version is less than the second version or 1 if the first version is greater than the second version
        See Also:
        Comparator.compare(Object, Object)
      • compareVersion

        public static int compareVersion​(boolean ignoreSnapshots,
                                         String v1,
                                         String v2)
        Compares the first version against the second version optionally ignoring if either version has a SNAPSHOT release extension. This can be useful for cases where you want to compare a version is at least a base version, not caring if it's a SNAPSHOT.

        If ignoreSnapshots is true, the version 1.0.0.Final and 1.0.0.Final-SNAPSHOT are said to be equal. If set to false, 1.0.0.Final is greater than 1.0.0.Final-SNAPSHOT.

        Parameters:
        ignoreSnapshots - true to ignore the SNAPSHOT release extension, otherwise false which values a SNAPSHOT dependency less than a non-SNAPSHOT of the same version
        v1 - the first version
        v2 - the second version
        Returns:
        0 if the versions are equal, -1 if version first version is less than the second version or 1 if the first version is greater than the second version