Class Version

  • All Implemented Interfaces:
    java.lang.Comparable<Version>

    public class Version
    extends java.lang.Object
    implements java.lang.Comparable<Version>
    Version identifier for capabilities such as bundles and packages.

    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. A text string. See Version(String) for the format of the qualifier string.

    Version objects are immutable.

    Since:
    1.3
    • Field Summary

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

      Constructors 
      Constructor Description
      Version​(int major, int minor, int micro)
      Creates a version identifier from the specified numerical components.
      Version​(int major, int minor, int micro, java.lang.String qualifier)
      Creates a version identifier from the specified components.
      Version​(java.lang.String version)
      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​(java.lang.Object object)
      Compares this Version object to another object.
      int getMajor()
      Returns the major component of this version identifier.
      int getMicro()
      Returns the micro component of this version identifier.
      int getMinor()
      Returns the minor component of this version identifier.
      java.lang.String getQualifier()
      Returns the qualifier component of this version identifier.
      int hashCode()
      Returns a hash code value for the object.
      static Version parseVersion​(java.lang.String version)
      Parses a version identifier from the specified string.
      java.lang.String toString()
      Returns the string representation of this version identifier.
      static Version valueOf​(java.lang.String version)
      Returns a Version object holding the version identifier in the specified String.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • emptyVersion

        public static final Version emptyVersion
        The empty version "0.0.0".
    • Constructor Detail

      • Version

        public Version​(int major,
                       int minor,
                       int micro)
        Creates a version identifier from the specified numerical components.

        The qualifier is set to the empty string.

        Parameters:
        major - Major component of the version identifier.
        minor - Minor component of the version identifier.
        micro - Micro component of the version identifier.
        Throws:
        java.lang.IllegalArgumentException - If the numerical components are negative.
      • Version

        public Version​(int major,
                       int minor,
                       int micro,
                       java.lang.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. If null is specified, then the qualifier will be set to the empty string.
        Throws:
        java.lang.IllegalArgumentException - If the numerical components are negative or the qualifier string is invalid.
      • Version

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

        Version string grammar:

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

      • parseVersion

        public static Version parseVersion​(java.lang.String version)
        Parses a version identifier from the specified string.

        See Version(String) for the format of the version string.

        Parameters:
        version - String representation of the version identifier. Leading and trailing whitespace will be ignored.
        Returns:
        A Version object representing the version identifier. If version is null or the empty string then emptyVersion will be returned.
        Throws:
        java.lang.IllegalArgumentException - If version is improperly formatted.
      • valueOf

        public static Version valueOf​(java.lang.String version)
        Returns a Version object holding the version identifier in the specified String.

        See Version(String) for the format of the version string.

        This method performs a similar function as parseVersion(String) but has the static factory valueOf(String) method signature.

        Parameters:
        version - String representation of the version identifier. Leading and trailing whitespace will be ignored. Must not be null.
        Returns:
        A Version object representing the version identifier. If version is the empty string then emptyVersion will be returned.
        Throws:
        java.lang.IllegalArgumentException - If version is improperly formatted.
        Since:
        1.8
      • getMajor

        public int getMajor()
        Returns the major component of this version identifier.
        Returns:
        The major component.
      • getMinor

        public int getMinor()
        Returns the minor component of this version identifier.
        Returns:
        The minor component.
      • getMicro

        public int getMicro()
        Returns the micro component of this version identifier.
        Returns:
        The micro component.
      • getQualifier

        public java.lang.String getQualifier()
        Returns the qualifier component of this version identifier.
        Returns:
        The qualifier component.
      • toString

        public java.lang.String toString()
        Returns the string representation of this version identifier.

        The format of the version string will be major.minor.micro if qualifier is the empty string or major.minor.micro.qualifier otherwise.

        Overrides:
        toString in class java.lang.Object
        Returns:
        The string representation of this version identifier.
      • hashCode

        public int hashCode()
        Returns a hash code value for the object.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        An integer which is a hash code value for this object.
      • equals

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

        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 java.lang.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).

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