public class

VersionStringComparator

extends Object
implements Comparator<T>
java.lang.Object
   ↳ com.atlassian.plugin.util.VersionStringComparator

Class Overview

Compares dotted version strings of varying length. Makes a best effort with other delimiters and non-numeric versions.

For dotted decimals, comparison is as you'd expect: 0.1 is before 0.2 is before 1.0 is before 2.0. This works for any number of dots.

More complicated version numbers are compared by splitting the version strings into components using the DELIMITER_PATTERN and comparing each component in order. The first difference found when comparing components left-to-right is returned.

Two numeric components (containing only digits) are compared as integers. A numeric component comes after any non-numeric one. Two non-numeric components are ordered by compareToIgnoreCase(String).

Summary

Constants
String COMPONENT_PATTERN
String DELIMITER_PATTERN
String VALID_VERSION_PATTERN
Public Constructors
VersionStringComparator()
Public Methods
int compare(String version1, String version2)
Compares two version strings using the algorithm described above.
static boolean isValidVersionString(String version)
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.util.Comparator

Constants

public static final String COMPONENT_PATTERN

Constant Value: "[\d\w]+"

public static final String DELIMITER_PATTERN

Constant Value: "[\.,-]"

public static final String VALID_VERSION_PATTERN

Constant Value: "[\d\w]+([\.,-][\d\w]+)*"

Public Constructors

public VersionStringComparator ()

Public Methods

public int compare (String version1, String version2)

Compares two version strings using the algorithm described above.

Returns
  • -1 if version1 is before version2, 1 if version2 is before version1, or 0 if the versions are equal.
Throws
IllegalArgumentException if either argument does not match VALID_VERSION_PATTERN.

public static boolean isValidVersionString (String version)