public class

Version

extends Object
implements Comparable<T>
java.lang.Object
   ↳ com.atlassian.bitbucket.util.Version

Class Overview

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.

Summary

Fields
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.
Public Constructors
Version(Integer... elements)
Constructor for accepting a pre-parsed version.
Version(String version)
Constructor that parses a version string.
Public Methods
int compareTo(Version o)
Compare two versions to determine their ordering.
boolean equals(Object obj)
int getMajor()
int getMinor()
int getPatch()
@Nonnull List<Integer> getVersion()
Retrieves an immutable copy of the ordered list of version components.
int hashCode()
String toString()
Returns a String representation of this version, with the components separated by dots.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.Comparable

Fields

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.

Public Constructors

public Version (Integer... elements)

Constructor for accepting a pre-parsed version.

Parameters
elements the version elements, starting with the most significant

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

Public Methods

public int compareTo (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.

public boolean equals (Object obj)

public int getMajor ()

Returns
  • the major version, e.g. getVersion().get(0)

public int getMinor ()

Returns
  • the minor version, e.g. getVersion().get(1)

public int getPatch ()

Returns
  • the patch version, e.g. getVersion().get(2)

@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

public int hashCode ()

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".

Returns
  • a string representation of the parsed version