Class Version

java.lang.Object
com.helger.base.version.Version
All Implemented Interfaces:
IComparable<Version>, Comparable<Version>

@Immutable public 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 Summary

    Fields
    Modifier and Type
    Field
    Description
    static final boolean
    Default value for printing zero elements in getAsString
    static final Version
     
    static final String
    default version if nothing is specified.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Version(int nMajor)
    Create a new version with major version only.
    Version(int nMajor, int nMinor)
    Create a new version with major and minor version only.
    Version(int nMajor, int nMinor, int nMicro)
    Create a new version with major, minor and micro version number.
    Version(int nMajor, int nMinor, int nMicro, @Nullable String sQualifier)
    Create a new version with 3 integer values and a qualifier.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    compareTo(@NonNull Version rhs)
    Compares two Version objects.
    boolean
     
    @NonNull String
    Get the string representation of the version number using the default setting for printing zero elements.
    @NonNull String
    getAsString(boolean bPrintZeroElements)
    Get the string representation of the version number.
    @NonNull String
    getAsString(boolean bPrintZeroElements, boolean bPrintAtLeastMajorAndMinor)
    Get the string representation of the version number.
    @NonNull @Nonempty String
    Get the string representation of the version number but only major and minor version number.
    @NonNull @Nonempty String
    Get the string representation of the version number but only major and minor and micro version number.
    final int
     
    final int
     
    final int
     
    final @Nullable String
     
    int
     
    final boolean
     
    static @NonNull Version
    parse(@Nullable String sVersionString)
    Construct a version object from a string.
    EBNF:
    version ::= major( '.' minor ( '.' micro ( ( '.' | '-' ) qualifier )?
    static @NonNull Version
    parseDotOnly(@Nullable String sVersionString)
    Construct a version object from a string.
    EBNF:
    version ::= major( '.' minor ( '.' micro ( '.' qualifier )?
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface com.helger.base.compare.IComparable

    isEQ, isGE, isGT, isLE, isLT, isNE
  • Field Details

    • DEFAULT_VERSION_STRING

      public static final String DEFAULT_VERSION_STRING
      default version if nothing is specified.
      See Also:
    • 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:
  • Constructor Details

    • 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 Details

    • getMajor

      @Nonnegative public final int getMajor()
      Returns:
      The major version number. Always ≥ 0.
    • getMinor

      @Nonnegative public final int getMinor()
      Returns:
      The minor version number. Always ≥ 0.
    • getMicro

      @Nonnegative public final int getMicro()
      Returns:
      The micro version number. Always ≥ 0.
    • getQualifier

      public final @Nullable String getQualifier()
      Returns:
      The version qualifier string. May be null.
    • hasQualifier

      public final boolean hasQualifier()
      Returns:
      true if a qualifier is present, false otherwise.
    • 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

      public @NonNull String getAsString()
      Get the string representation of the version number using the default setting for printing zero elements.
      Returns:
      Never null.
    • getAsString

      public @NonNull 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

      public @NonNull 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

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

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

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Overrides:
      toString in class Object
    • parse

      public static @NonNull 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

      public static @NonNull 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