Class SemanticVersion

java.lang.Object
io.github.bakedlibs.dough.versions.SemanticVersion
All Implemented Interfaces:
Version, Comparable<Version>
Direct Known Subclasses:
MinecraftVersion

public class SemanticVersion extends Object implements Version
A SemanticVersion follows the semantic version convention. The version itself consists of three components, a major version number, a minor version number and a patch version number.

The components are read and compared in that exact order. If the patch version number is zero, it may be omitted from the version string.

Author:
TheBusyBiscuit
  • Constructor Details

    • SemanticVersion

      public SemanticVersion(int major, int minor, int patch)
      This creates a new SemanticVersion instance with the given components.
      Parameters:
      major - The major version
      minor - The minor version
      patch - The patch version
    • SemanticVersion

      public SemanticVersion(int major, int minor)
      This creates a new SemanticVersion instance with the given components.

      The patch version will be automatically assumed to be zero.

      Parameters:
      major - The major version
      minor - The minor version
  • Method Details

    • getMajorVersion

      public final int getMajorVersion()
      This returns the "major" version component of this SemanticVersion.
      Returns:
      The major version
    • getMinorVersion

      public final int getMinorVersion()
      This returns the "minor" version component of this SemanticVersion.
      Returns:
      The minor version
    • getPatchVersion

      public final int getPatchVersion()
      This returns the "patch" version component of this SemanticVersion.
      Returns:
      The patch version
    • isPatch

      public final boolean isPatch()
      This method returns whether this is a patch version. A SemanticVersion is considered a "patch" when their last component (getPatchVersion()) is greater than zero.
      Returns:
      Whether this is a patch version
    • isAtLeast

      public boolean isAtLeast(int major, int minor, int patch)
    • isAtLeast

      public boolean isAtLeast(int major, int minor)
    • isSimilar

      public boolean isSimilar(Version version)
      This returns whether this Version is "similar" to the given Version. Two Versions are considered "similar" when they can be compared to each other without causing an IncomparableVersionsException.
      Specified by:
      isSimilar in interface Version
      Parameters:
      version - The Version to check
      Returns:
      Whether the two Versions can be compared.
    • isNewerThan

      public boolean isNewerThan(@Nonnull Version version)
      This method returns whether this Version is newer than the provided Version. This also implies that the provided Version is older than this Version, see Version.isOlderThan(Version).

      This method may throw an IncomparableVersionsException if the two versions cannot be compared with each other.

      Specified by:
      isNewerThan in interface Version
      Parameters:
      version - The Version to compare this to
      Returns:
      Whether this Version is newer than the provided one.
    • isEqualTo

      public boolean isEqualTo(@Nonnull Version version)
      This method returns whether this Version is equal to the given Version.

      This method may throw an IncomparableVersionsException if the two versions cannot be compared with each other.

      Specified by:
      isEqualTo in interface Version
      Parameters:
      version - The Version to compare this to
      Returns:
      Whether the given Version is equal to this Version.
    • isOlderThan

      public boolean isOlderThan(@Nonnull Version version)
      This method returns whether this Version is older than the provided Version. This also implies that the provided Version is newer than this Version, see Version.isNewerThan(Version).

      This method may throw an IncomparableVersionsException if the two versions cannot be compared with each other.

      Specified by:
      isOlderThan in interface Version
      Parameters:
      version - The Version to compare this to
      Returns:
      Whether this Version is older than the provided one.
    • getAsString

      @Nonnull public String getAsString()
      This method returns this Version as a human-readable format. Example: 1.4.2.
      Specified by:
      getAsString in interface Version
      Returns:
      A human-readable String representation of this Version
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equalsIgnorePatch

      public boolean equalsIgnorePatch(@Nonnull SemanticVersion version)
      This method returns whether this SemanticVersion equals the provided version in all aspects except their patch number.

      This will only compare the major and minor versions.

      Parameters:
      version - The SemanticVersion to compare this to
      Returns:
      Whether the two versions are equal (ignoring their patch version)
    • equalsIgnorePatch

      public boolean equalsIgnorePatch(int majorVersion, int minorVersion)
      This method returns whether this SemanticVersion equals the provided version numbers (ignoring the patch version of this SemanticVersion).
      Parameters:
      majorVersion - The major version
      minorVersion - The minor version
      Returns:
      Whether the two versions are equal (ignoring their patch version)
    • parse

      @Nonnull public static SemanticVersion parse(@Nonnull String version)
      This method attempts to parse the given String as a SemanticVersion. If the String could not be parsed effectively, an IllegalArgumentException will be thrown.

      Note that the String should follow the SemanticVersion convention, such as "1.0.2", "1.2.10", "1.3.0" or "1.1" to name a few examples.

      Parameters:
      version - The version string to parse
      Returns:
      The resulting SemanticVersion