Class Version

  • All Implemented Interfaces:
    Serializable, Comparable<Version>

    public class Version
    extends Object
    implements Comparable<Version>, Serializable
    Replace old nuiton-version (org.nuiton.version.Version).

    Created on 13/11/2021.

    A class to represent a version.

    Replace previously org.nuiton.util.Version class.

    Definition

    A version is defined of n components separated by componentSeparator.

    Components

    Components can be of two types:
    • Number component: a strict positive integer value
    • String component: a sequence of characters which can't be either number nor component separators

    Component separators

    Component separator are characters which can't be alphanumeric and can be empty character.

    Component separators are optional and components will be detected as soon as a character changed from a numeric string sequence to a alpha (none numeric!) sequence.

    For example, version 1a2 is composed of three components: {code 1}, a and 3.

    Snapshot flag

    Additionally version can be qualified as a SNAPSHOT (see below section about ordering).

    Examples

       0 (one component 0)
       0-SNAPSHOT (one component 0 + SNAPSHOT flag)
       1.0 (two components 1,0)
       1.1 (two components 1,1)
       1.1-alpha-1 (four components 1,1,alpha,1)
       1.1-beta (three components 1,1,beta)
       1.1-rc-1 (four components 1,1,rc,1)
       1.1-a  (three components 1,1,a)
       1.1-a12-4.45_6432 (seven components 1,1,a,12,4,45,643)
      

    Ordering

    A version is comparable, to have all the detail of order see Version.VersionComparator.

    Immutability

    The version is immutable, to create or modify a version, use the Version.VersionBuilder API or shortcut methods.
    Since:
    1.0.14
    Author:
    Tony Chemit - dev@tchemit.fr
    See Also:
    Serialized Form
    • Field Detail

      • SNAPSHOT_SUFFIX

        public static final String SNAPSHOT_SUFFIX
        Suffix of a SNAPSHOT version in the text representation.
        See Also:
        Constant Field Values
      • VZERO

        public static final Version VZERO
        Version V0.
      • DEFAULT_JOIN_COMPONENT_SEPARATOR

        public static final char DEFAULT_JOIN_COMPONENT_SEPARATOR
        Default component separator.
        See Also:
        Constant Field Values
      • VERSION_COMPARATOR

        protected static final Version.VersionComparator VERSION_COMPARATOR
        Comparator of version used internally to fulfill the comparator contract.
      • componentSeparators

        protected final List<String> componentSeparators
        List of separators of the version.
      • snapshot

        protected final boolean snapshot
        flag to define if version is a snapshot (if so a -SNAPSHOT is added at the end of the textual representation of the version).
      • version

        protected transient String version
        string representation of the version.
    • Method Detail

      • valueOf

        public static Version valueOf​(String version)
        Shortcut method to get a version from his string representation.
        Parameters:
        version - string representation of the version
        Returns:
        converted version from the string representation
      • addSnapshot

        public static Version addSnapshot​(Version version)
        Create a version from the given one and set to it the snapshot state to true.
        Parameters:
        version - version to clone
        Returns:
        the cloned version with the snapshot state to true
        Throws:
        IllegalArgumentException - if snapshot state is already set to true on the given version.
      • removeSnapshot

        public static Version removeSnapshot​(Version version)
        Create a version from the given one and set to it the snapshot state to false.
        Parameters:
        version - version to clone
        Returns:
        the cloned version with the snapshot state to true
        Throws:
        IllegalArgumentException - if snapshot state is already set to false on the given version
      • extractVersion

        public static Version extractVersion​(Version version,
                                             int component)
        Create a new version containing a single component from a given version.
        Parameters:
        version - original version
        component - component index to extract
        Returns:
        new Version with a single component
      • extractVersion

        public static Version extractVersion​(Version version,
                                             int firstComponent,
                                             int lastComponent)
        Create a new version containing a sub set of component from a given version.
        Parameters:
        version - original version
        firstComponent - first component index
        lastComponent - last component index
        Returns:
        new Version with a components sub set
      • increments

        public static Version increments​(Version version)
        Creates a new version from this one incremented.

        If the last component is a number, then just increments this number; otherwise add a new number component with value 1.

        Example:

        • 1 → 2
        • 1-a → 1-a.1
        Parameters:
        version - version to increment
        Returns:
        the incremented version
      • increments

        public static Version increments​(Version version,
                                         char componentSeparator)
        Creates a new version from this one incremented.

        If the last component is a number, then just increments this number; otherwise add a new number component with value 1.

        Example:

        • 1 → 2
        • 1-a → 1-a.1
        Parameters:
        version - version to increment
        componentSeparator - the component separator to use the last component is a classifier
        Returns:
        the incremented version
      • increments

        public static Version increments​(Version version,
                                         int componentPosition)
        Creates a new version from this one with the number component incremented at the given position.

        Note: Will fail if the component at the required position is not a number.

        Parameters:
        version - version to increment
        componentPosition - position of the version component to increment
        Returns:
        the incremented version
      • equals

        public static boolean equals​(String version0,
                                     String version1)
        Tests if two versions are equals.
        Parameters:
        version0 - the first version
        version1 - the second version
        Returns:
        true if versions are equals, false otherwise.
      • decrements

        public static Version decrements​(Version version,
                                         int componentPosition)
        Creates a new version from this one with the number component decremented at the given position.

        Note: Will fail if the component at the required position is not a number, or his value is 0.

        Parameters:
        version - version to decrement
        componentPosition - position of the version component to increment
        Returns:
        the decremented version
      • smallerThan

        public static boolean smallerThan​(String version0,
                                          String version1)
        Tests if the first version is smaller than the second version.
        Parameters:
        version0 - the first version
        version1 - the second version
        Returns:
        true if version0 is before version1, false otherwise.
      • greaterThan

        public static boolean greaterThan​(String version0,
                                          String version1)
        Tests if the first version is greater than the second version.
        Parameters:
        version0 - the first version
        version1 - the second version
        Returns:
        true if version0 is after version1, false otherwise.
      • getComponentSeparators

        public List<String> getComponentSeparators()
      • isSnapshot

        public boolean isSnapshot()
      • getComponentCount

        public int getComponentCount()
      • getNumberComponent

        public int getNumberComponent​(int componentPosition)
      • getTextComponent

        public String getTextComponent​(int componentPosition)
      • getVersion

        public String getVersion()
        Returns:
        the string representation value of the version
      • getValidName

        public String getValidName()
        Convert the string representation to a java identifier compliant.
        • in java: . is forbidden
        • in database (mysql, h2 ...): . is forbidden

        Forbidden values are replaced by _ character.

        Returns:
        the java compliant string representation of the version
      • beforeOrEquals

        public boolean beforeOrEquals​(Version o)
        Parameters:
        o - the other version to test
        Returns:
        true if current version is before or equals the given one
      • before

        public boolean before​(Version o)
        Parameters:
        o - the other version to test
        Returns:
        true if current version is before the given one
      • afterOrEquals

        public boolean afterOrEquals​(Version o)
        Parameters:
        o - the other version to test
        Returns:
        true if current version is after or equals the given one
      • after

        public boolean after​(Version o)
        Parameters:
        o - the other version to test
        Returns:
        true if current version is after the given one
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object