public class SemVer extends java.lang.Object implements java.lang.Comparable<SemVer>
This follows Semantic Versioning 2.0.0.
Please note that camparison and hash/equals are not always in sync what they consider equal.
While comparison ignores the build metadata
, equals(Object)
and
hashCode()
take it into account. Use the compareTo(SemVer, Comparator)
with
a string comparator which considers different strings different (e.g. String.compareTo(String)
)
to get a way of sorting consistent to hash/equals. Or use equalsIgnoreBuild(SemVer)
which
ignrores the build number.
This class is deliberately not serializable. Use an intermediate string representation instead.
This class is immutable.
Modifier and Type | Class and Description |
---|---|
static class |
SemVer.Prerelease
Prerelease indicator.
|
Modifier and Type | Field and Description |
---|---|
(package private) static java.util.regex.Pattern |
SEP_DOT
Separator pattern for splitting at dots.
|
static SemVer |
VERSION_0_0_1 |
Constructor and Description |
---|
SemVer(int major,
int minor,
int patch)
Constructor.
|
SemVer(int major,
int minor,
int patch,
SemVer.Prerelease prerelease)
Constructor including prerelease data.
|
SemVer(int major,
int minor,
int patch,
SemVer.Prerelease prerelease,
java.lang.String buildString)
Constructor.
|
SemVer(int major,
int minor,
int patch,
java.lang.String buildString)
Constructor including build metadata.
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(SemVer o)
Compare this semantic version with another.
|
int |
compareTo(SemVer otherVersion,
java.util.Comparator<? super Indexable<java.lang.String>> buildCompare)
Compare this with another version, but don't ignore the build metadata.
|
boolean |
equals(java.lang.Object o) |
boolean |
equalsIgnoreBuild(SemVer other)
Equality check ignoring the build metadata.
|
Indexable<java.lang.String> |
getBuildMetaData()
Get the build metadata.
|
java.lang.String |
getBuildString()
Get the build metadata as one string.
|
int |
getMajor()
Get the major number.
|
int |
getMinor()
Get the minor number.
|
int |
getPatch()
Get the patch number.
|
SemVer.Prerelease |
getPrerelease()
Get the prerelease number.
|
int |
hashCode() |
int |
hashCodeIgnoreBuild()
Get a hash code ignoring the build metadata.
|
boolean |
isNewerThan(SemVer otherVersion)
Is this version newer than the other version?
|
SemVer |
nextMajor()
Get the next semantic version number when increasing the major version.
|
SemVer |
nextMinor()
Get the next semantic version number when increasing the minor version.
|
SemVer |
nextPatch()
Get the next semantic version number when increasing the patch version.
|
static SemVer |
parse(java.lang.String versionString)
Parse a semantic version.
|
java.lang.String |
toString() |
SemVer |
withBuild(Indexable<java.lang.String> buildMetaData)
Get the same version as this. but with different build metadata.
|
SemVer |
withBuildString(java.lang.String build)
Get the same version as this. but with different build metadata.
|
SemVer |
withNoBuild()
Get the same version as this, but with no build metadata.
|
SemVer |
withPrerelease(SemVer.Prerelease prerelease)
Get the same version as this. but with a different prerelease.
|
SemVer |
withPrereleaseFromString(java.lang.String prerelease)
Get the same version as this, but with a different prerelease.
|
static final java.util.regex.Pattern SEP_DOT
public static final SemVer VERSION_0_0_1
public SemVer(int major, int minor, int patch)
major
- non-negative major versionminor
- non-negative minor versionpatch
- non-negative patch versionjava.lang.IllegalArgumentException
- if any version number is negativepublic SemVer(int major, int minor, int patch, @Nullable SemVer.Prerelease prerelease)
major
- non-negative major versionminor
- non-negative minor versionpatch
- non-negative patch versionprerelease
- prerelease data, none if null
java.lang.IllegalArgumentException
- if any version number is negativepublic SemVer(int major, int minor, int patch, @Nullable java.lang.String buildString)
major
- non-negative major versionminor
- non-negative minor versionpatch
- non-negative patch versionbuildString
- build metadata, may only contain ASCII letters, ASCII digits and both '.' and '-'java.lang.IllegalArgumentException
- if any version number is negative, or build
contains illegal characterspublic SemVer(int major, int minor, int patch, @Nullable SemVer.Prerelease prerelease, @Nullable java.lang.String buildString)
major
- non-negative major versionminor
- non-negative minor versionpatch
- non-negative patch versionprerelease
- prerelease data, none if null
buildString
- build metadata, may only contain ASCII letters, ASCII digits and both '.' and '-'public int getMajor()
0
is considered unstable, any changes might occur in the versioned API.
Major number is increased when there are incompatible API changes.0
indicating an unstable initial development versionpublic int getMinor()
0
public int getPatch()
@Nullable public SemVer.Prerelease getPrerelease()
null
if this is no prerelease@NotNull public Indexable<java.lang.String> getBuildMetaData()
getBuildString()
@Nullable public java.lang.String getBuildString()
null
if no metadata is attachedgetBuildMetaData()
@NotNull public SemVer nextMajor()
java.lang.ArithmeticException
- if the increasing would overflow an integer@NotNull public SemVer nextMinor()
java.lang.ArithmeticException
- if the increasing would overflow an integer@NotNull public SemVer nextPatch()
java.lang.ArithmeticException
- if the increasing would overflow an integer@NotNull public SemVer withPrerelease(@Nullable SemVer.Prerelease prerelease)
prerelease
- prerelease@NotNull public SemVer withPrereleaseFromString(@Nullable java.lang.String prerelease)
prerelease
- prerelease as string@NotNull public SemVer withNoBuild()
@NotNull public SemVer withBuildString(@NotNull java.lang.String build)
build
- build metadata, must contain only ASCII letters, digits, and both '.'
and '-'
java.lang.IllegalArgumentException
- if build
contains illegal characterspublic SemVer withBuild(@NotNull Indexable<java.lang.String> buildMetaData)
buildMetaData
- build metadata, must contain only ASCII letters, digits, and both '.'
and '-'
java.lang.IllegalArgumentException
- if build
contains illegal characterspublic boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public boolean equalsIgnoreBuild(SemVer other)
equals(Object)
, but does not care for
the build metadata
.other
- other versiontrue
if this and the other version have the same major, minor and patch version, and same prerelease datafalse
otherwisepublic int hashCode()
hashCode
in class java.lang.Object
public int hashCodeIgnoreBuild()
hashCode()
, but does not use the
build metadata
.public int compareTo(@NotNull SemVer o)
build metadata
.
If you need sorting which includes it use compareTo(SemVer, Comparator)
instead.compareTo
in interface java.lang.Comparable<SemVer>
o
- other semantic versionthis < o
,this > o
, or0
if (@code this == o}public int compareTo(@NotNull SemVer otherVersion, @NotNull java.util.Comparator<? super Indexable<java.lang.String>> buildCompare)
compare method
ignores the
build metadata
, while this falls back to compare it
if all other properties are equal.otherVersion
- other version to comparebuildCompare
- comparator called if all other properties are equal,
will not receive null
values but empty strings
insteadthis < o
,this > o
, or0
if (@code this == o}public boolean isNewerThan(@NotNull SemVer otherVersion)
otherVersion
- other versiontrue
if this version is newer, false
if this is older or the same versionpublic java.lang.String toString()
toString
in class java.lang.Object