Class Version

  • All Implemented Interfaces:
    IComparable<Version>, Comparable<Version>

    @Immutable
    public final class Version
    extends Object
    implements IComparable<Version>
    This class represents a single version object. It supports 4 elements: major version (integer), minor version (integer), micro version (integer) and a qualifier (string).
    Author:
    Philip Helger
    • Field Detail

      • DEFAULT_VERSION_STRING

        public static final String DEFAULT_VERSION_STRING
        default version if nothing is specified.
        See Also:
        Constant Field Values
      • DEFAULT_VERSION

        public static final Version DEFAULT_VERSION
      • DEFAULT_PRINT_ZERO_ELEMENTS

        public static final boolean DEFAULT_PRINT_ZERO_ELEMENTS
        Default value for printing zero elements in getAsString
        See Also:
        Constant Field Values
    • Constructor Detail

      • Version

        public Version​(@Nonnegative
                       int nMajor)
        Create a new version with major version only.
        Parameters:
        nMajor - major version
        Throws:
        IllegalArgumentException - if the parameter is < 0
      • Version

        public Version​(@Nonnegative
                       int nMajor,
                       @Nonnegative
                       int nMinor)
        Create a new version with major and minor version only.
        Parameters:
        nMajor - major version
        nMinor - minor version
        Throws:
        IllegalArgumentException - if any of the parameters is < 0
      • Version

        public Version​(@Nonnegative
                       int nMajor,
                       @Nonnegative
                       int nMinor,
                       @Nonnegative
                       int nMicro)
        Create a new version with major, minor and micro version number. The qualifier remains null.
        Parameters:
        nMajor - major version
        nMinor - minor version
        nMicro - micro version
        Throws:
        IllegalArgumentException - if any of the parameters is < 0
      • Version

        public Version​(@Nonnegative
                       int nMajor,
                       @Nonnegative
                       int nMinor,
                       @Nonnegative
                       int nMicro,
                       @Nullable
                       String sQualifier)
        Create a new version with 3 integer values and a qualifier.
        Parameters:
        nMajor - major version
        nMinor - minor version
        nMicro - micro version
        sQualifier - the version qualifier - may be null. If a qualifier is supplied, it may neither contain the "." or the "," character since they are used to determine the fields of a version and to separate 2 versions in a VersionRange.
        Throws:
        IllegalArgumentException - if any of the numeric parameters is < 0 or if the qualifier contains a forbidden character
    • Method Detail

      • hasQualifier

        public boolean hasQualifier()
      • compareTo

        public int compareTo​(@Nonnull
                             Version rhs)
        Compares two Version objects.
        Specified by:
        compareTo in interface Comparable<Version>
        Parameters:
        rhs - the version to compare to
        Returns:
        < 0 if this is less than rhs; > 0 if this is greater than rhs, and 0 if they are equal.
        Throws:
        IllegalArgumentException - if the parameter is null
      • getAsString

        @Nonnull
        public String getAsString​(boolean bPrintZeroElements)
        Get the string representation of the version number.
        Parameters:
        bPrintZeroElements - If true than trailing zeroes are printed, otherwise printed zeroes are not printed.
        Returns:
        Never null.
      • getAsString

        @Nonnull
        public String getAsString​(boolean bPrintZeroElements,
                                  boolean bPrintAtLeastMajorAndMinor)
        Get the string representation of the version number.
        Parameters:
        bPrintZeroElements - If true than trailing zeroes are printed, otherwise printed zeroes are not printed.
        bPrintAtLeastMajorAndMinor - true if major and minor part should always be printed, independent of their value
        Returns:
        Never null.
      • getAsStringMajorMinor

        @Nonnull
        @Nonempty
        public String getAsStringMajorMinor()
        Get the string representation of the version number but only major and minor version number.
        Returns:
        Never null.
      • getAsStringMajorMinorMicro

        @Nonnull
        @Nonempty
        public String getAsStringMajorMinorMicro()
        Get the string representation of the version number but only major and minor and micro version number.
        Returns:
        Never null.
      • hashCode

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

        @Nonnull
        public static Version parse​(@Nullable
                                    String sVersionString)
        Construct a version object from a string.
        EBNF:
        version ::= major( '.' minor ( '.' micro ( ( '.' | '-' ) qualifier )? )? )?
        major ::= number
        minor ::= number
        micro ::= number
        qualifier ::= .+
        Parameters:
        sVersionString - the version string to be interpreted as a version
        Returns:
        The parsed Version object.
        Throws:
        IllegalArgumentException - if any of the parameters is < 0
      • parseDotOnly

        @Nonnull
        public static Version parseDotOnly​(@Nullable
                                           String sVersionString)
        Construct a version object from a string.
        EBNF:
        version ::= major( '.' minor ( '.' micro ( '.' qualifier )? )? )?
        major ::= number
        minor ::= number
        micro ::= number
        qualifier ::= .+
        Parameters:
        sVersionString - the version string to be interpreted as a version
        Returns:
        The parsed Version object.
        Throws:
        IllegalArgumentException - if any of the parameters is < 0
        Since:
        v10.0.1