Class Version
java.lang.Object
com.github.zafarkhaja.semver.Version
- All Implemented Interfaces:
Comparable<Version>
The
Version class is the main class of the Java SemVer library.
This class implements the Facade design pattern.
It is also immutable, which makes the class thread-safe.- Since:
- 0.1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classA build-aware comparator.static classA mutable builder for the immutableVersionclass. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final MetadataVersionThe build metadata.static final Comparator<Version> A comparator that respects the build metadata when comparing versions.private static final StringA separator that separates the build metadata from the normal version or the pre-release version.private final NormalVersionThe normal version.private static final StringA separator that separates the pre-release version from the normal version.private final MetadataVersionThe pre-release version. -
Constructor Summary
ConstructorsConstructorDescriptionVersion(NormalVersion normal) Constructs aVersioninstance with the normal version.Version(NormalVersion normal, MetadataVersion preRelease) Constructs aVersioninstance with the normal version and the pre-release version.Version(NormalVersion normal, MetadataVersion preRelease, MetadataVersion build) Constructs aVersioninstance with the normal version, the pre-release version and the build metadata. -
Method Summary
Modifier and TypeMethodDescriptionintCompares this version to the other version.intcompareWithBuildsTo(Version other) Compare this version to the other version taking into account the build metadata.booleanChecks if this version equals the other version.static VersionforIntegers(int major) Creates a new instance ofVersionfor the specified version numbers.static VersionforIntegers(int major, int minor) Creates a new instance ofVersionfor the specified version numbers.static VersionforIntegers(int major, int minor, int patch) Creates a new instance ofVersionfor the specified version numbers.Returns the string representation of the build metadata.intReturns the major version number.intReturns the minor version number.Returns the string representation of the normal version.intReturns the patch version number.Returns the string representation of the pre-release version.booleangreaterThan(Version other) Checks if this version is greater than the other version.booleangreaterThanOrEqualTo(Version other) Checks if this version is greater than or equal to the other version.inthashCode()Increments the build metadata.Increments the major version.incrementMajorVersion(String preRelease) Increments the major version and appends the pre-release version.Increments the minor version.incrementMinorVersion(String preRelease) Increments the minor version and appends the pre-release version.Increments the patch version.incrementPatchVersion(String preRelease) Increments the patch version and appends the pre-release version.Increments the pre-release version.booleanChecks if this version is less than the other version.booleanlessThanOrEqualTo(Version other) Checks if this version is less than or equal to the other version.booleansatisfies(Expression expr) Checks if this version satisfies the specified SemVer Expression.booleanChecks if this version satisfies the specified SemVer Expression string.setBuildMetadata(String build) Sets the build metadata.setPreReleaseVersion(String preRelease) Sets the pre-release version.toString()static VersionCreates a new instance ofVersionas a result of parsing the specified version string.
-
Field Details
-
normal
The normal version. -
preRelease
The pre-release version. -
build
The build metadata. -
PRE_RELEASE_PREFIX
A separator that separates the pre-release version from the normal version.- See Also:
-
BUILD_PREFIX
A separator that separates the build metadata from the normal version or the pre-release version.- See Also:
-
BUILD_AWARE_ORDER
A comparator that respects the build metadata when comparing versions.
-
-
Constructor Details
-
Version
Version(NormalVersion normal) Constructs aVersioninstance with the normal version.- Parameters:
normal- the normal version
-
Version
Version(NormalVersion normal, MetadataVersion preRelease) Constructs aVersioninstance with the normal version and the pre-release version.- Parameters:
normal- the normal versionpreRelease- the pre-release version
-
Version
Version(NormalVersion normal, MetadataVersion preRelease, MetadataVersion build) Constructs aVersioninstance with the normal version, the pre-release version and the build metadata.- Parameters:
normal- the normal versionpreRelease- the pre-release versionbuild- the build metadata
-
-
Method Details
-
valueOf
Creates a new instance ofVersionas a result of parsing the specified version string.- Parameters:
version- the version string to parse- Returns:
- a new instance of the
Versionclass - Throws:
IllegalArgumentException- if the input string isNULLor emptyParseException- when invalid version string is providedUnexpectedCharacterException- is a special case ofParseException
-
forIntegers
Creates a new instance ofVersionfor the specified version numbers.- Parameters:
major- the major version number- Returns:
- a new instance of the
Versionclass - Throws:
IllegalArgumentException- if a negative integer is passed- Since:
- 0.7.0
-
forIntegers
Creates a new instance ofVersionfor the specified version numbers.- Parameters:
major- the major version numberminor- the minor version number- Returns:
- a new instance of the
Versionclass - Throws:
IllegalArgumentException- if a negative integer is passed- Since:
- 0.7.0
-
forIntegers
Creates a new instance ofVersionfor the specified version numbers.- Parameters:
major- the major version numberminor- the minor version numberpatch- the patch version number- Returns:
- a new instance of the
Versionclass - Throws:
IllegalArgumentException- if a negative integer is passed- Since:
- 0.7.0
-
satisfies
Checks if this version satisfies the specified SemVer Expression string. This method is a part of the SemVer Expressions API.- Parameters:
expr- the SemVer Expression string- Returns:
trueif this version satisfies the specified SemVer Expression orfalseotherwise- Throws:
ParseException- in case of a general parse errorLexerException- when encounters an illegal characterUnexpectedTokenException- when comes across an unexpected token- Since:
- 0.7.0
-
satisfies
Checks if this version satisfies the specified SemVer Expression. This method is a part of the SemVer Expressions API.- Parameters:
expr- the SemVer Expression- Returns:
trueif this version satisfies the specified SemVer Expression orfalseotherwise- Since:
- 0.9.0
-
incrementMajorVersion
Increments the major version.- Returns:
- a new instance of the
Versionclass
-
incrementMajorVersion
Increments the major version and appends the pre-release version.- Parameters:
preRelease- the pre-release version to append- Returns:
- a new instance of the
Versionclass - Throws:
IllegalArgumentException- if the input string isNULLor emptyParseException- when invalid version string is providedUnexpectedCharacterException- is a special case ofParseException
-
incrementMinorVersion
Increments the minor version.- Returns:
- a new instance of the
Versionclass
-
incrementMinorVersion
Increments the minor version and appends the pre-release version.- Parameters:
preRelease- the pre-release version to append- Returns:
- a new instance of the
Versionclass - Throws:
IllegalArgumentException- if the input string isNULLor emptyParseException- when invalid version string is providedUnexpectedCharacterException- is a special case ofParseException
-
incrementPatchVersion
Increments the patch version.- Returns:
- a new instance of the
Versionclass
-
incrementPatchVersion
Increments the patch version and appends the pre-release version.- Parameters:
preRelease- the pre-release version to append- Returns:
- a new instance of the
Versionclass - Throws:
IllegalArgumentException- if the input string isNULLor emptyParseException- when invalid version string is providedUnexpectedCharacterException- is a special case ofParseException
-
incrementPreReleaseVersion
Increments the pre-release version.- Returns:
- a new instance of the
Versionclass
-
incrementBuildMetadata
Increments the build metadata.- Returns:
- a new instance of the
Versionclass
-
setPreReleaseVersion
Sets the pre-release version.- Parameters:
preRelease- the pre-release version to set- Returns:
- a new instance of the
Versionclass - Throws:
IllegalArgumentException- if the input string isNULLor emptyParseException- when invalid version string is providedUnexpectedCharacterException- is a special case ofParseException
-
setBuildMetadata
Sets the build metadata.- Parameters:
build- the build metadata to set- Returns:
- a new instance of the
Versionclass - Throws:
IllegalArgumentException- if the input string isNULLor emptyParseException- when invalid version string is providedUnexpectedCharacterException- is a special case ofParseException
-
getMajorVersion
public int getMajorVersion()Returns the major version number.- Returns:
- the major version number
-
getMinorVersion
public int getMinorVersion()Returns the minor version number.- Returns:
- the minor version number
-
getPatchVersion
public int getPatchVersion()Returns the patch version number.- Returns:
- the patch version number
-
getNormalVersion
Returns the string representation of the normal version.- Returns:
- the string representation of the normal version
-
getPreReleaseVersion
Returns the string representation of the pre-release version.- Returns:
- the string representation of the pre-release version
-
getBuildMetadata
Returns the string representation of the build metadata.- Returns:
- the string representation of the build metadata
-
greaterThan
Checks if this version is greater than the other version.- Parameters:
other- the other version to compare to- Returns:
trueif this version is greater than the other version orfalseotherwise- See Also:
-
greaterThanOrEqualTo
Checks if this version is greater than or equal to the other version.- Parameters:
other- the other version to compare to- Returns:
trueif this version is greater than or equal to the other version orfalseotherwise- See Also:
-
lessThan
Checks if this version is less than the other version.- Parameters:
other- the other version to compare to- Returns:
trueif this version is less than the other version orfalseotherwise- See Also:
-
lessThanOrEqualTo
Checks if this version is less than or equal to the other version.- Parameters:
other- the other version to compare to- Returns:
trueif this version is less than or equal to the other version orfalseotherwise- See Also:
-
equals
Checks if this version equals the other version. The comparison is done by theVersion.compareTomethod. -
hashCode
-
toString
-
compareTo
Compares this version to the other version. This method does not take into account the versions' build metadata. If you want to compare the versions' build metadata use theVersion.compareWithBuildsTomethod or theVersion.BUILD_AWARE_ORDERcomparator.- Specified by:
compareToin interfaceComparable<Version>- Parameters:
other- the other version to compare to- Returns:
- a negative integer, zero or a positive integer if this version is less than, equal to or greater the the specified version
- See Also:
-
compareWithBuildsTo
Compare this version to the other version taking into account the build metadata. The method makes use of theVersion.BUILD_AWARE_ORDERcomparator.- Parameters:
other- the other version to compare to- Returns:
- integer result of comparison compatible with
that of the
Comparable.compareTomethod - See Also:
-