Class Version

java.lang.Object
com.atlassian.bitbucket.util.Version
All Implemented Interfaces:
Comparable<Version>

public class Version extends Object implements Comparable<Version>
Version represents a version. This class is useful for comparing versions, for instance when you need to determine whether the version is at least X.

Versions parsed by this class are always 0-padded to at least three places. For example, an input of "2" will result in [2, 0, 0]. More specific input, such as "1.7.9.5" will result in longer lists; 3 places is a minimum, not a maximum.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Pattern
    A Pattern that can validate a potential version String to ensure it contains a valid set of numbers and '.''s Version does not use this pattern, but it's handy for code that wants to ensure a version string is valid before turning it into a Version.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Version(Integer... elements)
    Constructor for accepting a pre-parsed version.
    Version(String version)
    Constructor that parses a version string.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Compare two versions to determine their ordering.
    boolean
     
    int
     
    int
     
    int
     
    Retrieves an immutable copy of the ordered list of version components.
    int
     
    Returns a String representation of this version, with the components separated by dots.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • STRICT_NUMERIC_VALIDATOR

      public static final Pattern STRICT_NUMERIC_VALIDATOR
      A Pattern that can validate a potential version String to ensure it contains a valid set of numbers and '.''s Version does not use this pattern, but it's handy for code that wants to ensure a version string is valid before turning it into a Version.
  • Constructor Details

    • Version

      public Version(Integer... elements)
      Constructor for accepting a pre-parsed version.
      Parameters:
      elements - the version elements, starting with the most significant
    • Version

      public Version(String version)
      Constructor that parses a version string. Version parts are determined by splitting on [.-] characters. If a non- numerical value is found (e.g. 'RC4'), the rest of the version String is ignored. In other words '1.0.1-RC.2' is considered equal to '1.0.1'
      Parameters:
      version - the version string
  • Method Details

    • compareTo

      public int compareTo(@Nonnull Version o)
      Compare two versions to determine their ordering. This class has a natural ordering that is consistent with equals.

      Note: Returns from this method should not be assumed to be -1, 0 or 1. As per the contract of Comparable, they should be evaluated as negative, 0 or positive. Implementations are free to use any positive or negative number they wish to indicate greater than and less than ordering.

      Specified by:
      compareTo in interface Comparable<Version>
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • getMajor

      public int getMajor()
      Returns:
      the major version, e.g. getVersion().get(0)
      Since:
      5.14
    • getMinor

      public int getMinor()
      Returns:
      the minor version, e.g. getVersion().get(1)
      Since:
      5.14
    • getPatch

      public int getPatch()
      Returns:
      the patch version, e.g. getVersion().get(2)
      Since:
      5.14
    • getVersion

      @Nonnull public List<Integer> getVersion()
      Retrieves an immutable copy of the ordered list of version components. The returned list is guaranteed to include at least 3 elements, for major, minor and patch versions.
      Returns:
      the version
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Returns a String representation of this version, with the components separated by dots.

      Note: For version strings which contain non-numerical, non-separator elements, the resulting representation will not match the input. For example, an initial version like "1.7.9-RC1" will result in "1.7.9" for the return value here. Padding may also be applied, so an initial version of "2" would be represented as "2.0.0".

      Overrides:
      toString in class Object
      Returns:
      a string representation of the parsed version