Class SemVer

java.lang.Object
xyz.rk0cc.josev.SemVer
All Implemented Interfaces:
Serializable, Comparable<SemVer>

public final class SemVer extends Object implements Comparable<SemVer>, Serializable
An immutable Java object for Semantic Versioning 2.0.0.
Semantic Versioning must be followed this versioning method:

  Major.Minor.Patch(-Pre release)(+build tag)

Major, minor and patch number fields are mandatory and non-negative number. It may optionally provide pre-release or build tag if needed.
In additions, it implemented Comparable which following version ordering policy.
Since:
1.0.0
See Also:
  • Field Details

  • Constructor Details

    • SemVer

      public SemVer(@Nonnegative long major, @Nonnegative long minor, @Nonnegative long patch, @Nullable String preRelease, @Nullable String build) throws NonStandardSemVerException
      Create new versioning data.
      Parameters:
      major - Non-negative number of major release.
      minor - Non-negative number of minor release.
      patch - Non-negative number of patch release.
      preRelease - Pre-release tag (if applied).
      build - Build tag (if applied).
      Throws:
      NonStandardSemVerException - If any Throwable thrown when calling value() that does not meet the regex of Semantic Versioning.
      NumberFormatException - When parsing major, minor and patch with exceeding Long's maximum (signed) value even it valid to parse.
    • SemVer

      public SemVer(@Nonnegative long major, @Nonnegative long minor, @Nonnegative long patch)
      Create new versioning data without additional tag applied.
      Parameters:
      major - Non-negative number of major release.
      minor - Non-negative number of minor release.
      patch - Non-negative number of patch release.
      Throws:
      NumberFormatException - When parsing major, minor and patch with exceeding Long's maximum (signed) value even it valid to parse.
    • SemVer

      public SemVer(@Nonnegative long major, @Nonnegative long minor, @Nullable String preRelease, @Nullable String build) throws NonStandardSemVerException
      Create new versioning data.
      Parameters:
      major - Non-negative number of major release.
      minor - Non-negative number of minor release.
      preRelease - Pre-release tag (if applied).
      build - Build tag (if applied).
      Throws:
      NonStandardSemVerException - If any Throwable thrown when calling value() that does not meet the regex of Semantic Versioning.
      NumberFormatException - When parsing major, minor and patch with exceeding Long's maximum (signed) value even it valid to parse.
    • SemVer

      public SemVer(@Nonnegative long major, @Nonnegative long minor)
      Create new versioning data without additional tag applied.
      Parameters:
      major - Non-negative number of major release.
      minor - Non-negative number of minor release.
      Throws:
      NumberFormatException - When parsing major, minor and patch with exceeding Long's maximum (signed) value even it valid to parse.
    • SemVer

      public SemVer(@Nonnegative long major, @Nullable String preRelease, @Nullable String build) throws NonStandardSemVerException
      Create new versioning data.
      Parameters:
      major - Non-negative number of major release.
      preRelease - Pre-release tag (if applied).
      build - Build tag (if applied).
      Throws:
      NonStandardSemVerException - If any Throwable thrown when calling value() that does not meet the regex of Semantic Versioning.
      NumberFormatException - When parsing major, minor and patch with exceeding Long's maximum (signed) value even it valid to parse.
    • SemVer

      public SemVer(@Nonnegative long major)
      Create new versioning data without additional tag applied.
      Parameters:
      major - Non-negative number of major release.
      Throws:
      NumberFormatException - When parsing major, minor and patch with exceeding Long's maximum (signed) value even it valid to parse.
  • Method Details

    • major

      @Nonnegative public long major()
      Get major release of this versioning.
      Returns:
      Non-negative Long value that representing major release.
    • minor

      @Nonnegative public long minor()
      Get minor release of this versioning.
      Returns:
      Non-negative Long value that representing minor release.
    • patch

      @Nonnegative public long patch()
      Get patch release of this versioning.
      Returns:
      Non-negative Long value that representing patch release.
    • preRelease

      @Nullable public String preRelease()
      Get pre-release tag of this versioning.
      Returns:
      A String of pre-release, null if not applied.
    • build

      @Nullable public String build()
      Get build tag of this versioning.
      Returns:
      A String of build, null if not applied.
    • isPreRelease

      public boolean isPreRelease()
      Determine this versioning is still under development.
      Returns:
      Check this versioning is applied non-null preRelease() or set major() as 0.
    • isGreater

      public boolean isGreater(@Nonnull SemVer compare)
      Check this version is greater than compare
      Parameters:
      compare - Another SemVer which using to compare with this.
      Returns:
      true if compareTo(SemVer) return greater than 0.
    • isLower

      public boolean isLower(@Nonnull SemVer compare)
      Check this version is lower than compare
      Parameters:
      compare - Another SemVer which using to compare with this.
      Returns:
      true if compareTo(SemVer) return lowerer than 0.
    • isGreaterOrEquals

      public boolean isGreaterOrEquals(@Nonnull SemVer compare)
      Check this version is greater or equals with compare
      Parameters:
      compare - Another SemVer which using to compare with this.
      Returns:
      true if compareTo(SemVer) return greater or equals with 0.
    • isLowerOrEquals

      public boolean isLowerOrEquals(@Nonnull SemVer compare)
      Check this version is lowerer or equals with compare
      Parameters:
      compare - Another SemVer which using to compare with this.
      Returns:
      true if compareTo(SemVer) return lower or equals with 0.
    • isSameVersionGroup

      public boolean isSameVersionGroup(@Nonnull SemVer semVer)
      Check another SemVer has the same major(), minor() and patch() with this.
      Parameters:
      semVer - A SemVer that to determine has same version group.
      Returns:
      true if it is.
    • value

      @Nonnull public String value()
      Return a String of version which follows Semantic Versioning standard.
      Returns:
      An assembled String of Semantic Versioning.
    • compareTo

      public int compareTo(@Nonnull SemVer o)
      Specified by:
      compareTo in interface Comparable<SemVer>
    • equals

      public boolean equals(Object o)
      Compare another SemVer which has equals data of versioning.
      Overrides:
      equals in class Object
      Parameters:
      o - Another Object that using to compare.
      Returns:
      true if o contains exact same data of versioning.
    • hashCode

      public int hashCode()
      Calculating hash code form all provided versioning data.
      Overrides:
      hashCode in class Object
      Returns:
      Hashed major(), minor(), patch(), preRelease() and build().
    • toString

      @Nonnull public String toString()
      Return a String of object-like context for logging purpose.
      Overrides:
      toString in class Object
      Returns:
      Completed information of versioning value.
    • parse

      @Nonnull public static SemVer parse(@Nonnull String version) throws NonStandardSemVerException
      Generating a new SemVer object by a String.
      Parameters:
      version - A String of Semantic Versioning. If it contains 'v' at the first Character of String.charAt(int), it will be omitted automatically.
      Returns:
      A Java object of Semantic Versioning.
      Throws:
      NonStandardSemVerException - If version does not follows the standard of Semantic Versioning. Or encounter a problem when handling data in JVM.
      See Also:
    • tryParse

      @Nullable public static SemVer tryParse(@Nonnull String version)
      Generating a new SemVer object by a String. However, when NonStandardSemVerException throw on parse(String), it returns null.
      Parameters:
      version - A String of version.
      Returns:
      A Java object of Semantic Versioning. Or null if version causes parse(String) throws NonStandardSemVerException.