Class VersionComparator

java.lang.Object
org.dellroad.stuff.string.VersionComparator
All Implemented Interfaces:
Comparator<String>

public class VersionComparator extends Object implements Comparator<String>
Comparator for version numbers.

Version numbers are broken up into parts, where a part is a contiguous sequence of one or more digits, or a contiguous sequence of one or more non-digits, and where the period character ('.') serves as a part separator. Then, parts are compared pair-wise until there is a difference, as determined by the Comparator provided to the constructor, or one version string runs out of parts, in which case it is less than the other.

  • Constructor Details

    • VersionComparator

      public VersionComparator(Comparator<String> partComparator)
      Primary constructor.
      Parameters:
      partComparator - compares individual parts
      Throws:
      IllegalArgumentException - if partComparator is null
    • VersionComparator

      public VersionComparator()
      Convenience constructor.

      Equivalent to VersionComparator(new VersionPartComparator()).

  • Method Details

    • compare

      public int compare(String v1, String v2)
      Specified by:
      compare in interface Comparator<String>
    • separateIntoParts

      protected List<String> separateIntoParts(String version)
      Split a version string into parts.

      The implementation in VersionComparator defines a part as a contiguous sequence of one or more digits, or a contiguous sequence of one or more non-digits, and where the period character ('.') serves as a part separator.

      Subclasses may choose to override this method to define parts differently.

      Parameters:
      version - the original version string
      Returns:
      the parts of the version string