Class Version

java.lang.Object
net.luis.utils.util.Version
All Implemented Interfaces:
Comparable<Version>

public class Version extends Object implements Comparable<Version>
This class represents a version number.
The version number is represented by the following simplified pattern:

 [v]major.minor[.patch][(r.-)build][(-)suffix][(+)suffixVersion]
 
The version is composed of the following parts:
  • major: The major version number
  • minor: The minor version number
  • patch: The patch or fix version number
  • build: The current build number
  • suffix: The suffix or pre-release identifier (e.g. alpha, beta, rc, ...)
  • suffixVersion: The version of the suffix or pre-release identifier

Everything placed in square brackets is optional.
Characters in parentheses are separators, if there are multiple separators, one of them can be used.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected static final record 
    This record represents an appendable version number.
    The record is used for the build and suffix version numbers.
    static final class 
    This class represents a builder for a version number.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    The appendable build version number.
    private final int
    The major version number.
    private final int
    The minor version number.
    private final int
    The patch or fix version number.
    private final String
    The suffix or pre-release identifier.
    The appendable suffix version number.
    private static final String
    Constant for the system property 'version.default.prefixed'.
    private static final Pattern
    The pattern used to parse a version number.
    Group 1: The major version number Group 2: The minor version number Group 3: The patch or fix version number Group 6: The build number Group 8: The suffix or pre-release identifier Group 10: The version of the suffix or pre-release identifier The pattern is case-sensitive and allows the version number to be prefixed with a 'v'.
    static final Version
    A constant value for a version number of 0.0.0.
    This is the default version number.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Version(int major, int minor, int patch, @NotNull Version.AppendingVersion buildVersion, @Nullable String suffix, @NotNull Version.AppendingVersion suffixVersion)
    Constructs a new version with the given version numbers.
    The version numbers will be clamped to a minimum of 0.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the version as a builder for modifications.
    static @NotNull Version.Builder
    builder(int major, int minor)
    Creates a new version builder with the given version numbers.
    The version numbers will be clamped to a minimum of 0.
    static @NotNull Version.Builder
    builder(int major, int minor, int patch)
    Creates a new version builder with the given version numbers.
    The version numbers will be clamped to a minimum of 0.
    int
    compareTo(@NotNull Version o)
    Compares this version with the given version.
    The comparison is based on the following order:
    Major version number Minor version number Patch or fix version number Build version number
    boolean
     
    int
    Returns the build version number as an integer.
    int
    Returns the major version number as an integer.
    int
    Returns the minor version number as an integer.
    int
    Returns the patch or fix version number as an integer.
    @NotNull String
    Returns the suffix or pre-release identifier.
    int
    Gets the index of the suffix.
    The index is used to compare the suffix with other suffixes.
    The index is as follows:
    0: No or unknown suffix 1: Alpha 2: Beta 3: Release candidate 4: Release 5: Final
    int
    Returns the suffix version number as an integer.
    int
     
    static @NotNull Version
    of(int major, int minor)
    Creates a new version with the given version numbers.
    The version numbers will be clamped to a minimum of 0.
    static @NotNull Version
    of(int major, int minor, int patch)
    Creates a new version with the given version numbers.
    The version numbers will be clamped to a minimum of 0.
    static @NotNull Version
    parse(@Nullable String version)
    Parses a version from a string.
     
    @NotNull String
    toString(boolean prefixed)
    Returns the version as a string.
    The version will be formatted as follows:
    @NotNull String
    toString(boolean prefixed, boolean omitZeros)
    Returns the version as a string.
    The version will be formatted as follows:

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • VERSION_PATTERN

      private static final Pattern VERSION_PATTERN
      The pattern used to parse a version number.
      • Group 1: The major version number
      • Group 2: The minor version number
      • Group 3: The patch or fix version number
      • Group 6: The build number
      • Group 8: The suffix or pre-release identifier
      • Group 10: The version of the suffix or pre-release identifier
      The pattern is case-sensitive and allows the version number to be prefixed with a 'v'.

      The major and minor version numbers are required.
      The patch version is optional.
      They must be separated by a dot.

      The build number is optional.
      It must be separated by a dot, a hyphen, or an 'r' (release).

      The suffix and suffix-version are optional.
      If the suffix is present, it must be separated by a hyphen.
      If the suffix-version is present, it must be separated by a plus sign.

    • VERSION_DEFAULT_PREFIXED

      private static final String VERSION_DEFAULT_PREFIXED
      Constant for the system property 'version.default.prefixed'.

      If the system property is set to 'true', the version number will be prefixed with a 'v'
      when it is converted to a string via toString().
      The system property does not affect toString(boolean).

      The default value is 'true'.

      See Also:
    • ZERO

      public static final Version ZERO
      A constant value for a version number of 0.0.0.
      This is the default version number.
    • major

      private final int major
      The major version number.
    • minor

      private final int minor
      The minor version number.
    • patch

      private final int patch
      The patch or fix version number.
    • buildVersion

      private final Version.AppendingVersion buildVersion
      The appendable build version number.
    • suffix

      private final String suffix
      The suffix or pre-release identifier.
    • suffixVersion

      private final Version.AppendingVersion suffixVersion
      The appendable suffix version number.
  • Constructor Details

    • Version

      protected Version(int major, int minor, int patch, @NotNull @NotNull Version.AppendingVersion buildVersion, @Nullable @Nullable String suffix, @NotNull @NotNull Version.AppendingVersion suffixVersion)
      Constructs a new version with the given version numbers.
      The version numbers will be clamped to a minimum of 0.
      Parameters:
      major - The major version number
      minor - The minor version number
      patch - The patch or fix version number
      buildVersion - The appendable build version number, containing the separator and the version number
      suffix - The suffix or pre-release identifier
      suffixVersion - The appendable suffix version number, containing the separator and the version number
      Throws:
      NullPointerException - If the build or suffix version is null
  • Method Details

    • of

      @NotNull public static @NotNull Version of(int major, int minor)
      Creates a new version with the given version numbers.
      The version numbers will be clamped to a minimum of 0.

      If the version numbers are all 0 or less, ZERO will be returned.

      Parameters:
      major - The major version number
      minor - The minor version number
      Returns:
      The created version
      See Also:
    • of

      @NotNull public static @NotNull Version of(int major, int minor, int patch)
      Creates a new version with the given version numbers.
      The version numbers will be clamped to a minimum of 0.

      If the version numbers are all 0 or less, ZERO will be returned.

      Parameters:
      major - The major version number
      minor - The minor version number
      patch - The patch or fix version number
      Returns:
      The created version
      See Also:
    • builder

      @NotNull public static @NotNull Version.Builder builder(int major, int minor)
      Creates a new version builder with the given version numbers.
      The version numbers will be clamped to a minimum of 0.
      Parameters:
      major - The major version number
      minor - The minor version number
      Returns:
      The created version builder
      See Also:
    • builder

      @NotNull public static @NotNull Version.Builder builder(int major, int minor, int patch)
      Creates a new version builder with the given version numbers.
      The version numbers will be clamped to a minimum of 0.
      Parameters:
      major - The major version number
      minor - The minor version number
      patch - The patch or fix version number
      Returns:
      The created version builder
      See Also:
    • parse

      @NotNull public static @NotNull Version parse(@Nullable @Nullable String version)
      Parses a version from a string.

      If the given string to parse is null, empty, blank,
      or does not match the pattern (VERSION_PATTERN), ZERO will be returned.

      The following examples are valid base version strings:

      • 1.0
      • v1.0
      • 1.0.0
      • v1.0.0

      The major and minor version numbers are required and must be separated by a dot.
      The patch version is optional and must be separated by a dot.
      The build number is optional and can be separated by a dot, a hyphen, or an 'r' (release).
      Examples for valid build version strings are:

      • 1.0.0.0
      • 1.0.0-0
      • 1.0.0r0

      Note: If the patch version is not present, the build number must not be separated by a dot
      otherwise it will be considered as the patch version.

      The suffix and suffix-version are optional.
      If the suffix is present, it must be separated by a hyphen.
      If the suffix-version is present, it must be separated by a plus sign.
      Examples for valid suffix and suffix-version strings are:

      • 1.0.0-alpha
      • 1.0.0+001
      • 1.0.0-alpha+001
      • 1.0.0-0+001
      • 1.0.0r0-alpha+001
      Parameters:
      version - The version string to parse
      Returns:
      The parsed version or ZERO
    • getMajor

      public int getMajor()
      Returns the major version number as an integer.
      Returns:
      The major version
    • getMinor

      public int getMinor()
      Returns the minor version number as an integer.
      Returns:
      The minor version
    • getPatch

      public int getPatch()
      Returns the patch or fix version number as an integer.
      Returns:
      The patch or fix version or 0 if not present
    • getBuild

      public int getBuild()
      Returns the build version number as an integer.
      Returns:
      The build version or 0 if not present
    • getSuffix

      @NotNull public @NotNull String getSuffix()
      Returns the suffix or pre-release identifier.
      Returns:
      The suffix or pre-release identifier or an empty string if not present
    • getSuffixIndex

      public int getSuffixIndex()
      Gets the index of the suffix.
      The index is used to compare the suffix with other suffixes.
      The index is as follows:
      • 0: No or unknown suffix
      • 1: Alpha
      • 2: Beta
      • 3: Release candidate
      • 4: Release
      • 5: Final
      Returns:
      The index of the suffix
    • getSuffixVersion

      public int getSuffixVersion()
      Returns the suffix version number as an integer.
      Returns:
      The suffix version or 0 if not present
    • asBuilder

      @NotNull public @NotNull Version.Builder asBuilder()
      Returns the version as a builder for modifications.
      Returns:
      The version as a builder
    • compareTo

      public int compareTo(@NotNull @NotNull Version o)
      Compares this version with the given version.
      The comparison is based on the following order:
      1. Major version number
      2. Minor version number
      3. Patch or fix version number
      4. Build version number

      If the suffixes are equal, the suffix version will be compared.
      If the suffixes are not equal, the suffixes will be compared
      in this case, the suffix version will not be considered.

      The suffixes are compared based on the following order:

      1. Alpha
      2. Beta
      3. Release candidate
      4. Release
      5. Final

      The first comparison that is not equal will determine the result.

      Specified by:
      compareTo in interface Comparable<Version>
      Parameters:
      o - The version to compare with
      Returns:
      The comparison result, negative if this version is less than the given version,
      positive if this version is greater than the given version, and zero if both versions are equal
    • 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
    • toString

      @NotNull public @NotNull String toString(boolean prefixed)
      Returns the version as a string.
      The version will be formatted as follows:
      
       <major>.<minor>.<patch>[(r.-)<build>][-<suffix>][+<suffixVersion>]
       
      If the patch version is 0, it will be omitted.
      Parameters:
      prefixed - If the version should be prefixed with a 'v'
      Returns:
      The version as a string
      See Also:
    • toString

      @NotNull public @NotNull String toString(boolean prefixed, boolean omitZeros)
      Returns the version as a string.
      The version will be formatted as follows:
      
       <major>.<minor>.<patch>[(r.-)<build>][-<suffix>][+<suffixVersion>]
       
      If the patch version is 0 and omitZeros is true, it will be omitted.
      In this case, the version will be formatted as follows:
      
       <major>.<minor>[(r.-)<build>][-<suffix>][+<suffixVersion>]
       
      Parameters:
      prefixed - If the version should be prefixed with a 'v'
      omitZeros - If the patch version should be omitted if it is 0
      Returns:
      The version as a string