Class Version

  • All Implemented Interfaces:
    Comparable<Version>

    public final class Version
    extends Object
    implements Comparable<Version>
    A component version

    Version identifiers have four components.

    1. Major version. A non-negative integer.
    2. Minor version. A non-negative integer.
    3. Micro version. A non-negative integer.
    4. Qualifier. An ascii text string. See Version(String) for the format of the qualifier string.

    Unspecified version component is equivalent to 0 (or the empty string for qualifier).

    Version objects are immutable.

    Author:
    bratseth
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Version emptyVersion
      The empty version
    • Constructor Summary

      Constructors 
      Constructor Description
      Version()
      Creates an empty version
      Version​(int major)
      Creates a version identifier from the specified numerical components.
      Version​(int major, int minor)
      Creates a version identifier from the specified numerical components.
      Version​(int major, int minor, int micro)
      Creates a version identifier from the specified numerical components.
      Version​(int major, int minor, int micro, String qualifier)
      Creates a version identifier from the specified components.
      Version​(com.yahoo.text.Utf8Array versionString)
      Creates a version identifier from the specified string.
      Version​(String versionString)
      Creates a version identifier from the specified string.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(Version other)
      Compares this Version object to another version.
      boolean equals​(Object object)
      Compares this Version to another.
      static Version fromString​(String versionString)
      Returns new Version(versionString), or Version.emptyVersion if the input string is null or ""
      int getMajor()
      Returns the major component of this version, or 0 if not specified
      int getMicro()
      Returns the micro component of this version, or 0 if not specified
      int getMinor()
      Returns the minor component of this version, or 0 if not specified
      String getQualifier()
      Returns the qualifier component of this version, or "" if not specified
      int hashCode()  
      boolean isAfter​(Version other)
      Returns whether this version number is strictly higher than the given version.
      boolean isBefore​(Version other)
      Returns whether this version number is strictly lower than the given version.
      boolean isEmpty()
      Returns whether this equals the empty version
      String toFullString()
      Returns the string representation of this version identifier as major.minor.micro.qualifier, omitting .qualifier if qualifier empty or unspecified
      VersionSpecification toSpecification()
      Creates a version specification that only matches this version
      String toString()
      Returns the string representation of this version identifier as major.minor.micro.qualifier, omitting the remaining parts after reaching the first unspecified component.
      com.yahoo.text.Utf8Array toUtf8()  
    • Field Detail

      • emptyVersion

        public static final Version emptyVersion
        The empty version
    • Constructor Detail

      • Version

        public Version()
        Creates an empty version
      • Version

        public Version​(int major)
        Creates a version identifier from the specified numerical components.
        Parameters:
        major - major component of the version identifier
        Throws:
        IllegalArgumentException - If the numerical components are negative.
      • Version

        public Version​(int major,
                       int minor)
        Creates a version identifier from the specified numerical components.
        Parameters:
        major - major component of the version identifier
        minor - minor component of the version identifier
        Throws:
        IllegalArgumentException - If the numerical components are negative.
      • Version

        public Version​(int major,
                       int minor,
                       int micro)
        Creates a version identifier from the specified numerical components.
        Parameters:
        major - major component of the version identifier
        minor - minor component of the version identifier
        micro - micro component of the version identifier
        Throws:
        IllegalArgumentException - If the numerical components are negative.
      • Version

        public Version​(int major,
                       int minor,
                       int micro,
                       String qualifier)
        Creates a version identifier from the specified components.
        Parameters:
        major - major component of the version identifier
        minor - minor component of the version identifier
        micro - micro component of the version identifier
        qualifier - Qualifier component of the version identifier, or null if not specified
        Throws:
        IllegalArgumentException - if the numerical components are negative the qualifier string contains non-word/digit-characters, or an earlier component is not specified but a later one is
      • Version

        public Version​(String versionString)
        Creates a version identifier from the specified string.

        Version strings follows this grammar (same as Osgi versions):

         version ::= major('.'minor('.'micro('.'qualifier)?)?)?
         major ::= digit+
         minor ::= digit+
         micro ::= digit+
         qualifier ::= (alpha|digit|'_'|'-')+
         digit ::= [0..9]
         alpha ::= [a..zA..Z]
         
        Parameters:
        versionString - String representation of the version identifier
        Throws:
        IllegalArgumentException - If version is improperly formatted.
      • Version

        public Version​(com.yahoo.text.Utf8Array versionString)
        Creates a version identifier from the specified string.

        Version strings follows this grammar (same as Osgi versions):

         version ::= major('.'minor('.'micro('.'qualifier)?)?)?
         major ::= digit+
         minor ::= digit+
         micro ::= digit+
         qualifier ::= (alpha|digit|'_'|'-')+
         digit ::= [0..9]
         alpha ::= [a..zA..Z]
         
        Parameters:
        versionString - String representation of the version identifier
        Throws:
        IllegalArgumentException - If version is improperly formatted.
    • Method Detail

      • fromString

        public static Version fromString​(String versionString)
        Returns new Version(versionString), or Version.emptyVersion if the input string is null or ""
      • toFullString

        public String toFullString()
        Returns the string representation of this version identifier as major.minor.micro.qualifier, omitting .qualifier if qualifier empty or unspecified

        This string form is part of the API of Version and will never change.

      • getMajor

        public int getMajor()
        Returns the major component of this version, or 0 if not specified
      • getMinor

        public int getMinor()
        Returns the minor component of this version, or 0 if not specified
      • getMicro

        public int getMicro()
        Returns the micro component of this version, or 0 if not specified
      • getQualifier

        public String getQualifier()
        Returns the qualifier component of this version, or "" if not specified
      • toString

        public String toString()
        Returns the string representation of this version identifier as major.minor.micro.qualifier, omitting the remaining parts after reaching the first unspecified component. Unspecified version component is equivalent to 0 (or the empty string for qualifier).

        The string representation of a Version specified here is a part of the API and will never change.

        Overrides:
        toString in class Object
      • toUtf8

        public com.yahoo.text.Utf8Array toUtf8()
      • hashCode

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

        public boolean isEmpty()
        Returns whether this equals the empty version
      • equals

        public boolean equals​(Object object)
        Compares this Version to another.

        A version is considered to be equal to another version if the major, minor and micro components are equal and the qualifier component is equal (using String.equals).

        Overrides:
        equals in class Object
        Parameters:
        object - The Version object to be compared.
        Returns:
        true if object is a Version and is equal to this object; false otherwise.
      • compareTo

        public int compareTo​(Version other)
        Compares this Version object to another version.

        A version is considered to be less than another version if its major component is less than the other version's major component, or the major components are equal and its minor component is less than the other version's minor component, or the major and minor components are equal and its micro component is less than the other version's micro component, or the major, minor and micro components are equal and it's qualifier component is less than the other version's qualifier component (using String.compareTo).

        A version is considered to be equal to another version if the major, minor and micro components are equal and the qualifier component is equal (using String.compareTo).

        Unspecified numeric components are treated as 0, unspecified qualifier is treated as the empty string.

        Specified by:
        compareTo in interface Comparable<Version>
        Parameters:
        other - the Version object to be compared.
        Returns:
        A negative integer, zero, or a positive integer if this object is less than, equal to, or greater than the specified Version object.
        Throws:
        ClassCastException - if the specified object is not a Version.
      • toSpecification

        public VersionSpecification toSpecification()
        Creates a version specification that only matches this version