Package com.mooltiverse.oss.nyx.version
Class SemanticVersion
- java.lang.Object
-
- com.mooltiverse.oss.nyx.version.Version
-
- com.mooltiverse.oss.nyx.version.SemanticVersion
-
- All Implemented Interfaces:
Serializable
,Cloneable
,Comparable<SemanticVersion>
public class SemanticVersion extends Version implements Comparable<SemanticVersion>
The implementation of a Semantic Versioning 2.0.0 compliant version.
Instances of this class are immutable so whenever you alter some values you actually receive a new instance holding the new value, while the old one remains unchanged.
To get new instances you can also use one of thevalueOf(String)
methods.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static char
BUILD_DELIMITER
The character that marks the separation between the core or the pre-release part and the build part.static String
DEFAULT_INITIAL_VERSION
The default initial version that can be used when non version is yet available.static char
PRERELEASE_DELIMITER
The character that marks the separation between the core and the pre-release part.static String
SEMANTIC_VERSION_PATTERN
The regexp pattern taken directly from Semantic Versioning 2.0.0 used to parse semantic versions.static String
SEMANTIC_VERSION_PATTERN_RELAXED
A relaxed version of theSEMANTIC_VERSION_PATTERN
that works also when a prefix appears at the beginning of the version string or some zeroes appear in front of numbers.
-
Constructor Summary
Constructors Constructor Description SemanticVersion(int major, int minor, int patch)
Builds a new version object with the given values.SemanticVersion(int major, int minor, int patch, Object[] prereleaseIdentifiers, String[] buildIdentifiers)
Builds a new version object with the given values.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description SemanticVersion
bump(CoreIdentifiers id)
Returns a new instance with the number identified by the given value bumped.SemanticVersion
bump(String id)
Returns a new instance with the number identified by the given value bumped.SemanticVersion
bumpMajor()
Returns a new instance with the major number of this current instance incremented by one and the minor and patch numbers reset to zero.SemanticVersion
bumpMinor()
Returns a new instance with the major number of this current instance, the minor number incremented by one and the patch number reset to zero.SemanticVersion
bumpPatch()
Returns a new instance with the major and minor numbers of this current instance and the patch number incremented by one.SemanticVersion
bumpPrerelease(String id)
Returns a new instance with the number identified by the given value bumped in the prerelease part.int
compareTo(SemanticVersion v)
Compares this version with the specified version for order.boolean
equals(Object obj)
Indicates whether some other object is "equal to" this one.String
getBuild()
Returns the build part of the version, if any, ornull
otherwise.String
getBuildAttributeValue(String name)
If an attribute with the given name is present in the build part, return the identifier after that, otherwise returnnull
.String[]
getBuildIdentifiers()
Returns an array of the single identifiers of the build part of the version, if any, ornull
otherwise.String
getCore()
Returns the core part (major.minor.patch
) of the version as a string.Integer[]
getCoreIdentifiers()
Returns an array of the single identifiers of the core part of the versionint
getMajor()
Returns the major version numberint
getMinor()
Returns the minor version numberint
getPatch()
Returns the patch version numberstatic String
getPrefix(String s)
Takes the given string and tries to parse it as a semantic version with an optional prefix (which may be any string before the coremajor.minor.patch
numbers).String
getPrerelease()
Returns the prerelease part of the version, if any, ornull
otherwise.Integer
getPrereleaseAttributeValue(String name)
If an attribute with the given name is present in the prerelease part, return the identifier after that, otherwise returnnull
.Object[]
getPrereleaseIdentifiers()
Returns an array of the single identifiers of the prerelease part of the version, if any, ornull
otherwise.Scheme
getScheme()
ReturnsScheme.SEMVER
.boolean
hasBuildAttribute(String name)
Returnstrue
if an attribute with the given name is present in the build part,false
otherwise.int
hashCode()
Returns a hash code value for the object.boolean
hasPrereleaseAttribute(String name)
Returnstrue
if an attribute with the given name is present in the prerelease part,false
otherwise.static boolean
isLegal(String s)
Returnstrue
if the given string is a legal semantic version which, for example, can be parsed usingvalueOf(String)
without exceptions.static boolean
isLegal(String s, boolean lenient)
Returnstrue
if the given string is a legal semantic version which, for example, can be parsed usingvalueOf(String, boolean)
without exceptions.SemanticVersion
removeBuildAttribute(String name, boolean removeValue)
Returns a new instance with the new attribute removed from the build part, if any was present, otherwise the same version is returned.SemanticVersion
removePrereleaseAttribute(String name, boolean removeValue)
Returns a new instance with the new attribute removed from the prerelease part, if any was present, otherwise the same version is returned.static String
sanitize(String s)
Performs all of the sanitizations in the given string by sanitizing, in order, the prefix and leading zeroes in numeric identifiers.static String
sanitizeNumbers(String s)
Takes the given string and tries to parse it as a semantic version number, even with illegal characters or prefix.static String
sanitizePrefix(String s)
Takes the given string and tries to parse it as a semantic version with an optional prefix (which may be any string before the coremajor.minor.patch
numbers).SemanticVersion
setBuild(String... identifiers)
Returns a new version object with the build part set to the given values.SemanticVersion
setBuildAttribute(String name)
Returns a new version object with the new attribute added or replaced in the build part.SemanticVersion
setBuildAttribute(String name, String value)
Returns a new version object with the new attribute added or replaced in the build part.SemanticVersion
setCore(int major, int minor, int patch)
Returns a new version object with themajor
,minor
andpatch
numbers set to the given values.SemanticVersion
setMajor(int major)
Returns a new version object with themajor
number set to the given value.SemanticVersion
setMinor(int minor)
Returns a new version object with theminor
number set to the given value.SemanticVersion
setPatch(int patch)
Returns a new version object with thepatch
number set to the given value.SemanticVersion
setPrerelease(Object... identifiers)
Returns a new version object with the prerelease part set to the given values.SemanticVersion
setPrereleaseAttribute(String name)
Returns a new version object with the new attribute added or replaced in the prerelease part.SemanticVersion
setPrereleaseAttribute(String name, Integer value)
Returns a new version object with the new attribute added or replaced in the prerelease part.String
toString()
Returns a string representation of the object.static SemanticVersion
valueOf(String s)
Returns a SemanticVersion instance representing the specified String value.static SemanticVersion
valueOf(String s, boolean sanitize)
This method is a shorthand forvalueOf(String)
andsanitize(String)
.
-
-
-
Field Detail
-
DEFAULT_INITIAL_VERSION
public static final String DEFAULT_INITIAL_VERSION
The default initial version that can be used when non version is yet available.- See Also:
- Constant Field Values
-
PRERELEASE_DELIMITER
public static final char PRERELEASE_DELIMITER
The character that marks the separation between the core and the pre-release part. Note that this character is used as separator only at the first occurrence while other occurrences are considered legal characters in the pre-release and the build identifiers.- See Also:
- Constant Field Values
-
BUILD_DELIMITER
public static final char BUILD_DELIMITER
The character that marks the separation between the core or the pre-release part and the build part.- See Also:
- Constant Field Values
-
SEMANTIC_VERSION_PATTERN_RELAXED
public static final String SEMANTIC_VERSION_PATTERN_RELAXED
A relaxed version of theSEMANTIC_VERSION_PATTERN
that works also when a prefix appears at the beginning of the version string or some zeroes appear in front of numbers. Value is: "([0-9]\\d*)\\.([0-9]\\d*)\\.([0-9]\\d*)(?:-((?:[0-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:[0-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"- See Also:
SEMANTIC_VERSION_PATTERN
, Constant Field Values
-
SEMANTIC_VERSION_PATTERN
public static final String SEMANTIC_VERSION_PATTERN
The regexp pattern taken directly from Semantic Versioning 2.0.0 used to parse semantic versions. Value is: "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SemanticVersion
public SemanticVersion(int major, int minor, int patch)
Builds a new version object with the given values.- Parameters:
major
- themajor
numberminor
- theminor
numberpatch
- thepatch
number- Throws:
IllegalArgumentException
- if one ofmajor
,minor
,patch
is negative
-
SemanticVersion
public SemanticVersion(int major, int minor, int patch, Object[] prereleaseIdentifiers, String[] buildIdentifiers)
Builds a new version object with the given values.- Parameters:
major
- themajor
numberminor
- theminor
numberpatch
- thepatch
numberprereleaseIdentifiers
- theprereleaseIdentifiers
the array ofInteger
orString
objects to use as identifiers in the prerelease block.null
items are ignored. Integers and strings representing integers must not have leading zeroes or represent negative numbers. If the array isnull
then the instance will have no prerelease blockbuildIdentifiers
- thebuildIdentifiers
the array ofString
to use as identifiers in the build block.null
items are ignored. If the array isnull
then the instance will have no build block- Throws:
IllegalArgumentException
- if one ofmajor
,minor
,patch
is negative or one object in theprereleaseIdentifiers
represents a negative integer (either when passed as anInteger
orString
) or have leading zeroes. This exception is also raised when objects in theprereleaseIdentifiers
are not of typeInteger
orString
or when string identifiers in theprereleaseIdentifiers
orbuildIdentifiers
contain illegal characters
-
-
Method Detail
-
hashCode
public int hashCode()
Returns a hash code value for the object.
-
equals
public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one. This object equals to the given one if they are the same object or they are of the same type and hold exactly the same version.- Specified by:
equals
in classVersion
- Parameters:
obj
- the reference object with which to compare.- Returns:
true
if this object is the same as theobj
argument;false
otherwise.- See Also:
Object.equals(Object)
-
compareTo
public int compareTo(SemanticVersion v)
Compares this version with the specified version for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified version.
Semantic Versioning 2.0.0 states that:
- rule #9: Pre-release versions have a lower precedence than the associated normal version.
- rule #10: Build metadata MUST be ignored when determining version precedence. Thus two versions that differ only in the build metadata, have the same precedence.
- rule #11: Precedence refers to how versions are compared to each other when ordered. Precedence MUST be calculated by separating the version into major, minor, patch and pre-release identifiers in that order (Build metadata does not figure into precedence). Precedence is determined by the first difference when comparing each of these identifiers from left to right as follows: Major, minor, and patch versions are always compared numerically. Example: 1.0.0 < 2.0.0 < 2.1.0 < 2.1.1.
When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version. Example: 1.0.0-alpha < 1.0.0.
Precedence for two pre-release versions with the same major, minor, and patch version MUST be determined by comparing each dot separated identifier from left to right until a difference is found as follows:
- identifiers consisting of only digits are compared numerically and identifiers with letters or hyphens are compared lexically in ASCII sort order.
- numeric identifiers always have lower precedence than non-numeric identifiers
- a larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal.
Note that when the specification says "lower precedence" it translates to <, which means the "less" part is supposed to appear first in order. Translating this to theComparable.compareTo(Object)
method, it means that the object with "lower precedence" returns a negative number.
However, to cope with theComparable
contract, which imposes a total ordering, we need to amend the above rules and make them s little stricter just because two versions with different values can't be considered equal (also seeequals(Object)
). With more detail:
- rule #10 is amended so that two versions that only differ in their build metadata will not return 0 (as if they the same) but their build metadata of two versions that are equal in their core and prerelease parts affects the order by their literal comparison (remember that numeric identifiers are not treated as such in build metadata and, instead, they are just treated as strings). In other words we are not ignoring build metadata as required by rule #10 but we consider it with the least priority only when the core and prerelease parts are the same. Two be consistent with rule #9, when comparing two versions with the same core and prerelease parts, when one has build metadata and the other doesn't, the one with the build metadata has lower precedence on the one without the build metadata. Example: 1.2.3+build.1 < 1.2.3 and 1.2.3-alpha.0+build.1 < 1.2.3-alpha.0.- Specified by:
compareTo
in interfaceComparable<SemanticVersion>
- Parameters:
v
- the version to be compared.- Returns:
- a negative integer, zero, or a positive integer as this version is less than, equal to, or greater than the specified version.
- See Also:
Comparable.compareTo(Object)
-
getScheme
public final Scheme getScheme()
ReturnsScheme.SEMVER
.- Specified by:
getScheme
in classVersion
- Returns:
Scheme.SEMVER
-
isLegal
public static boolean isLegal(String s)
Returnstrue
if the given string is a legal semantic version which, for example, can be parsed usingvalueOf(String)
without exceptions.
This method uses a strict criteria, without trying to sanitize the given string.- Parameters:
s
- the string version to check.- Returns:
true
if the given string represents a legal semantic version,false
otherwise.- See Also:
valueOf(String)
-
isLegal
public static boolean isLegal(String s, boolean lenient)
Returnstrue
if the given string is a legal semantic version which, for example, can be parsed usingvalueOf(String, boolean)
without exceptions.- Parameters:
s
- the string version to check.lenient
- whentrue
prefixes and non critical extra characters are tolerated even if they are not strictly legal from the semantic version specification perspective.- Returns:
true
if the given string represents a legal semantic version,false
otherwise.- See Also:
valueOf(String, boolean)
-
toString
public String toString()
Returns a string representation of the object.- Specified by:
toString
in classVersion
- Returns:
- a string representation of the object.
- See Also:
Object.toString()
-
valueOf
public static SemanticVersion valueOf(String s)
Returns a SemanticVersion instance representing the specified String value. No sanitization attempt is done.- Parameters:
s
- the string to parse- Returns:
- the new SemanticVersion instance representing the given string.
- Throws:
NullPointerException
- if the given string isnull
IllegalArgumentException
- if the given string doesn't represent a legal semantic version- See Also:
valueOf(String, boolean)
,isLegal(String)
,sanitizePrefix(String)
-
valueOf
public static SemanticVersion valueOf(String s, boolean sanitize)
This method is a shorthand forvalueOf(String)
andsanitize(String)
.
Returns a SemanticVersion instance representing the specified String value. Ifsanitize
istrue
this method will try to sanitize the given string before parsing so that if there are illegal characters like a prefix or leading zeroes in numeric identifiers they are removed.
When sanitization is enabled on a string that actually needs sanitization the string representation of the returned object will not exactly match the input value.- Parameters:
s
- the string to parsesanitize
- optionally enables sanitization before parsing- Returns:
- the new SemanticVersion instance representing the given string.
- Throws:
NullPointerException
- if the given string isnull
IllegalArgumentException
- if the given string doesn't represent a legal semantic version- See Also:
valueOf(String)
,sanitizePrefix(String)
-
sanitize
public static String sanitize(String s)
Performs all of the sanitizations in the given string by sanitizing, in order, the prefix and leading zeroes in numeric identifiers. This order is the one that yields to the highest success probability in obtaining a legal version. Invoking this method is like invoking the sanitization methodssanitizeNumbers(String)
andsanitizePrefix(String)
.- Parameters:
s
- the semantic version string to sanitize- Returns:
- the sanitized semantic string version
- Throws:
NullPointerException
- if the given string isnull
IllegalArgumentException
- if the given string doesn't represent a semantic version, even tolerating the aspects to sanitize- See Also:
sanitizePrefix(String)
,sanitizeNumbers(String)
-
sanitizeNumbers
public static String sanitizeNumbers(String s)
Takes the given string and tries to parse it as a semantic version number, even with illegal characters or prefix. All numeric identifiers in the core version (major.minor.patch
) and in the prerelease metadata are sanitized by removing all leading zeroes to make them compliant. Numeric identifiers in the build metadata part are left intact, even when they have leading zeroes. If the given string contains a prefix (seesanitizePrefix(String)
) or illegal characters they are left untouched and they are returned as they were in the input string.- Parameters:
s
- a semantic version string which may have illegal leading zeroes to be removed in the numeric identifiers in the core or the prerelease parts.- Returns:
- the string given as input with the illegal leading zeroes removed.
- Throws:
NullPointerException
- if the given string isnull
IllegalArgumentException
- if the given string doesn't represent a legal semantic version, even tolerating the prefix- See Also:
sanitize(String)
-
sanitizePrefix
public static String sanitizePrefix(String s)
Takes the given string and tries to parse it as a semantic version with an optional prefix (which may be any string before the coremajor.minor.patch
numbers). The returned string is the semantic version passed as input with the prefix removed. If no prefix is present then the returned string is the same as the one passed as input. Prefixes are often used (i.e. the 'v' used it Git tags orrelease-
,rel
etc) so this method helps in stripping those prefixes to get a compliant semantic version.- Parameters:
s
- a semantic version string which may have an additional prefix to be removed- Returns:
- the string given as input with the prefix removed, if any, or the same string passed as input if no prefix was found.
- Throws:
NullPointerException
- if the given string isnull
IllegalArgumentException
- if the given string doesn't represent a legal semantic version, even tolerating the prefix- See Also:
sanitize(String)
,getPrefix(String)
-
getPrefix
public static String getPrefix(String s)
Takes the given string and tries to parse it as a semantic version with an optional prefix (which may be any string before the coremajor.minor.patch
numbers). The returned string is the prefix before the core version number, if any, ornull
otherwise.- Parameters:
s
- a semantic version string which may have an additional prefix to be isolated- Returns:
- the prefix in the given semantic version string, if any, or
null
otherwise.null
is also returned when the given string is empty. - Throws:
NullPointerException
- if the given string isnull
IllegalArgumentException
- if the given string is not empty but doesn't represent a legal semantic version, even tolerating the prefix- See Also:
sanitizePrefix(String)
-
getMajor
public int getMajor()
Returns the major version number- Returns:
- the major version number
-
getMinor
public int getMinor()
Returns the minor version number- Returns:
- the minor version number
-
getPatch
public int getPatch()
Returns the patch version number- Returns:
- the patch version number
-
getCore
public String getCore()
Returns the core part (major.minor.patch
) of the version as a string.- Returns:
- the core part of the version as a string.
-
getCoreIdentifiers
public Integer[] getCoreIdentifiers()
Returns an array of the single identifiers of the core part of the version- Returns:
- the identifiers of the core part of the version.
-
getPrerelease
public String getPrerelease()
Returns the prerelease part of the version, if any, ornull
otherwise.- Returns:
- the prerelease part of the version.
-
getPrereleaseIdentifiers
public Object[] getPrereleaseIdentifiers()
Returns an array of the single identifiers of the prerelease part of the version, if any, ornull
otherwise.
-
getBuild
public String getBuild()
Returns the build part of the version, if any, ornull
otherwise.- Returns:
- the build part of the version.
-
getBuildIdentifiers
public String[] getBuildIdentifiers()
Returns an array of the single identifiers of the build part of the version, if any, ornull
otherwise.- Returns:
- the identifiers of the build part of the version.
-
setCore
public SemanticVersion setCore(int major, int minor, int patch)
Returns a new version object with themajor
,minor
andpatch
numbers set to the given values. This method doesn't reset any number and the prerelease and build blocks are left unchanged.- Parameters:
major
- the major number to setminor
- the minor number to setpatch
- the patch number to set- Returns:
- the new version instance
- Throws:
IllegalArgumentException
- if any of the given values is negative
-
setMajor
public SemanticVersion setMajor(int major)
Returns a new version object with themajor
number set to the given value. This method doesn't reset any number and the prerelease and build blocks are left unchanged.- Parameters:
major
- the major number to set- Returns:
- the new version instance
- Throws:
IllegalArgumentException
- if the given values is negative
-
setMinor
public SemanticVersion setMinor(int minor)
Returns a new version object with theminor
number set to the given value. This method doesn't reset any number and the prerelease and build blocks are left unchanged.- Parameters:
minor
- the minor number to set- Returns:
- the new version instance
- Throws:
IllegalArgumentException
- if the given values is negative
-
setPatch
public SemanticVersion setPatch(int patch)
Returns a new version object with thepatch
number set to the given value. This method doesn't reset any number and the prerelease and build blocks are left unchanged.- Parameters:
patch
- the patch number to set- Returns:
- the new version instance
- Throws:
IllegalArgumentException
- if the given values is negative
-
setPrerelease
public SemanticVersion setPrerelease(Object... identifiers)
Returns a new version object with the prerelease part set to the given values. If anull
value or an array of allnull
values is passed then the returned version will have no prerelease part, otherwise it will have all of the given nonnull
identifiers, with the core and build elements of this version instance.- Parameters:
identifiers
- the identifiers to use for the new version instance, ornull
to remove the prerelease block. All nonnull
items must beString
orInteger
instances.String
instances representing numeric values will be interpreted asInteger
. If the current version had a pre-release part it is completely replaced by the given identifiers.- Returns:
- the new version instance
- Throws:
IllegalArgumentException
- if some nonnull
item passed contains illegal characters, if a given number is negative or contains leading zeroes or any item is not an instance ofString
orInteger
-
setBuild
public SemanticVersion setBuild(String... identifiers)
Returns a new version object with the build part set to the given values. If anull
value or an array of allnull
values is passed then the returned version will have no build part, otherwise it will have all of the given nonnull
identifiers, with the core and prerelease elements of this version instance. If the current version had a build part it is completely replaced by the given identifiers.- Parameters:
identifiers
- the identifiers to use for the new version instance, ornull
to remove the build block- Returns:
- the new version instance
- Throws:
IllegalArgumentException
- if some nonnull
item passed contains illegal characters
-
hasPrereleaseAttribute
public boolean hasPrereleaseAttribute(String name)
Returnstrue
if an attribute with the given name is present in the prerelease part,false
otherwise.- Parameters:
name
- the name of the attribute to look up. Ifnull
or emptyfalse
is returned- Returns:
true
if an attribute with the given name is present in the prerelease part,false
otherwise.
-
hasBuildAttribute
public boolean hasBuildAttribute(String name)
Returnstrue
if an attribute with the given name is present in the build part,false
otherwise.- Parameters:
name
- the name of the attribute to look up. Ifnull
or emptyfalse
is returned- Returns:
true
if an attribute with the given name is present in the build part,false
otherwise.
-
getPrereleaseAttributeValue
public Integer getPrereleaseAttributeValue(String name)
If an attribute with the given name is present in the prerelease part, return the identifier after that, otherwise returnnull
.- Parameters:
name
- the name of the attribute to look up. Ifnull
or emptynull
is returned- Returns:
- the attribute after the given name if such attribute is found in the prerelease part and there is another attribute after it,
otherwise
null
-
getBuildAttributeValue
public String getBuildAttributeValue(String name)
If an attribute with the given name is present in the build part, return the identifier after that, otherwise returnnull
.- Parameters:
name
- the name of the attribute to look up. Ifnull
or emptynull
is returned- Returns:
- the attribute after the given name if such attribute is found in the build part and there is another attribute after it,
otherwise
null
-
setPrereleaseAttribute
public SemanticVersion setPrereleaseAttribute(String name, Integer value)
Returns a new version object with the new attribute added or replaced in the prerelease part. This method tries to be less intrusive as it only works on the given attribute (and its optional value) while leaving the other attributes unchanged.
If this version doesn't have a prerelease part, the returned version will have one with the new attribute appended (and its value as well, if notnull
).
If this version already has a prerelease part with no identifier matching the given attribute name then the returned version will have the same prerelease part as the current one with the new attribute appended (and its value as well, if notnull
).
If this version already has a prerelease part that contains an identifier matching the given attribute name then the identifier matching the attribute name is left unchanged and if the given value is notnull
, the next identifier is added or replaced with the given value.
ATTENTION: if the value is notnull
the current identifier after the name (if any) is replaced if it's a numeric identifier.
Examples of invokingsetPrereleaseAttribute("build")
withnull
value:
-1.2.3 = 1.2.3-build
-1.2.3-alpha = 1.2.3-alpha.build
-1.2.3-alpha.beta = 1.2.3-alpha.beta.build
-1.2.3+timestamp = 1.2.3-build+timestamp
-1.2.3-alpha+timestamp.20200101 = 1.2.3-alpha.build+timestamp.20200101
-1.2.3-build = 1.2.3-build
(unchanged)
-1.2.3-build.12345 = 1.2.3-build.12345
(unchanged)
-1.2.3-build.12345.timestamp.20200101 = 1.2.3-build.12345.timestamp.20200101
(unchanged)
Examples of invokingsetPrereleaseAttribute("build")
with12345
value:
-1.2.3 = 1.2.3-build.12345
-1.2.3-alpha = 1.2.3-alpha.build.12345
-1.2.3-alpha.beta = 1.2.3-alpha.beta.build.12345
-1.2.3+timestamp = 1.2.3-build.12345+timestamp
-1.2.3-alpha+timestamp.20200101 = 1.2.3-alpha.build.12345+timestamp.20200101
-1.2.3-build = 1.2.3-build.12345
-1.2.3-build.12345 = 1.2.3-build.12345
(unchanged)
-1.2.3-build.12345.timestamp.20200101 = 1.2.3-build.12345.timestamp.20200101
(unchanged)- Parameters:
name
- the name to set for the attributevalue
- the value to set for the attribute, ornull
just set the attribute name, ignoring the value- Returns:
- the new version instance
- Throws:
IllegalArgumentException
- if the given name or value contains illegal charactersNullPointerException
- if the attribute name isnull
-
setPrereleaseAttribute
public SemanticVersion setPrereleaseAttribute(String name)
Returns a new version object with the new attribute added or replaced in the prerelease part. This method is a shorthand forsetPrereleaseAttribute(value, null)
to only set a simple identifier instead of a pair.- Parameters:
name
- the name to set for the attribute- Returns:
- the new version instance
- Throws:
IllegalArgumentException
- if the given name contains illegal charactersNullPointerException
- if the attribute name isnull
- See Also:
setPrereleaseAttribute(String, Integer)
-
setBuildAttribute
public SemanticVersion setBuildAttribute(String name, String value)
Returns a new version object with the new attribute added or replaced in the build part. This method tries to be less intrusive as it only works on the given attribute (and its optional value) while leaving the other attributes unchanged.
If this version doesn't have a build part, the returned version will have one with the new attribute appended (and its value as well, if notnull
).
If this version already has a build part with no identifier matching the given attribute name then the returned version will have the same build part as the current one with the new attribute appended (and its value as well, if notnull
).
If this version already has a build part that contains an identifier matching the given attribute name then the identifier matching the attribute name is left unchanged and if the given value is notnull
, the next identifier is added or replaced with the given value.
ATTENTION: if the value is notnull
the current identifier after the name (if any) is replaced without further consideration.
Examples of invokingsetBuildAttribute("build")
withnull
value:
-1.2.3 = 1.2.3+build
-1.2.3-alpha = 1.2.3-alpha+build
-1.2.3-alpha.beta = 1.2.3-alpha.beta+build
-1.2.3+timestamp = 1.2.3+timestamp.build
-1.2.3-alpha+timestamp.20200101 = 1.2.3-alpha+timestamp.20200101.build
-1.2.3+build = 1.2.3+build
(unchanged)
-1.2.3+build.12345 = 1.2.3+build.12345
(unchanged)
-1.2.3+build.12345.timestamp.20200101 = 1.2.3+build.12345.timestamp.20200101
(unchanged)
Examples of invokingsetBuildAttribute("build")
with12345
value:
-1.2.3 = 1.2.3+build.12345
-1.2.3-alpha = 1.2.3-alpha+build.12345
-1.2.3-alpha.beta = 1.2.3-alpha.beta+build.12345
-1.2.3+timestamp = 1.2.3+timestamp.build.12345
-1.2.3-alpha+timestamp.20200101 = 1.2.3-alpha+timestamp.20200101.build.12345
-1.2.3+build = 1.2.3+build.12345
-1.2.3+build.12345 = 1.2.3+build.12345
(unchanged)
-1.2.3+build.12345.timestamp.20200101 = 1.2.3+build.12345.timestamp.20200101
(unchanged)- Parameters:
name
- the name to set for the attributevalue
- the value to set for the attribute, ornull
just set the attribute name, ignoring the value- Returns:
- the new version instance
- Throws:
IllegalArgumentException
- if the given name or value contains illegal charactersNullPointerException
- if the attribute name isnull
-
setBuildAttribute
public SemanticVersion setBuildAttribute(String name)
Returns a new version object with the new attribute added or replaced in the build part. This method is a shorthand forsetBuildAttribute(value, null)
to only set a simple identifier instead of a pair.- Parameters:
name
- the name to set for the attribute- Returns:
- the new version instance
- Throws:
IllegalArgumentException
- if the given name contains illegal charactersNullPointerException
- if the attribute name isnull
- See Also:
setBuildAttribute(String, String)
-
removePrereleaseAttribute
public SemanticVersion removePrereleaseAttribute(String name, boolean removeValue)
Returns a new instance with the new attribute removed from the prerelease part, if any was present, otherwise the same version is returned. If the attribute is found andremoveValue
istrue
then also the attribute value (the attribute after the one identified byname
) is removed, unless there are no more attributes aftername
or the value attribute is not numeric.- Parameters:
name
- the name of the attribute to remove from the prerelease part, if present. Ifnull
or empty no action is takenremoveValue
- iftrue
also the attribute aftername
is removed (if any)- Returns:
- the new instance, which might be the same of the current object if no attribute with the given
name
is present
-
removeBuildAttribute
public SemanticVersion removeBuildAttribute(String name, boolean removeValue)
Returns a new instance with the new attribute removed from the build part, if any was present, otherwise the same version is returned. If the attribute is found andremoveValue
istrue
then also the attribute value (the attribute after the one identified byname
) is removed, unless there are no more attributes aftername
.- Parameters:
name
- the name of the attribute to remove from the build part, if present. Ifnull
or empty no action is takenremoveValue
- iftrue
also the attribute aftername
is removed (if any)- Returns:
- the new instance, which might be the same of the current object if no attribute with the given
name
is present
-
bumpMajor
public SemanticVersion bumpMajor()
Returns a new instance with the major number of this current instance incremented by one and the minor and patch numbers reset to zero. Prerelease and build parts are left intact.- Returns:
- a new instance with the major number of this current instance incremented by one and the minor and patch numbers reset to zero.
-
bumpMinor
public SemanticVersion bumpMinor()
Returns a new instance with the major number of this current instance, the minor number incremented by one and the patch number reset to zero. Prerelease and build parts are left intact.- Returns:
- a new instance with the major number of this current instance, the minor number incremented by one and the patch number reset to zero.
-
bumpPatch
public SemanticVersion bumpPatch()
Returns a new instance with the major and minor numbers of this current instance and the patch number incremented by one. Prerelease and build parts are left intact.- Returns:
- a new instance with the major and minor numbers of this current instance and the patch number * incremented by one.
-
bump
public SemanticVersion bump(CoreIdentifiers id)
Returns a new instance with the number identified by the given value bumped.- Parameters:
id
- the selector of the identifier to bump- Returns:
- a new instance with the number identified by the given value bumped.
- Throws:
NullPointerException
- ifnull
is passed
-
bumpPrerelease
public SemanticVersion bumpPrerelease(String id)
Returns a new instance with the number identified by the given value bumped in the prerelease part. The core and the build blocks (if present) are left unchanged.
If this version doesn't have a prerelease block the returned version will have one, containing two identifiers: the given string and the following number.0
.
If this version already has a prerelease block without any identifier that equals the given id, then the returned version has all the previous prerelease identifiers preceded by the two new identifiers the given string and the following number.1
. If this version already has a prerelease block that contains a string identifier equal to the given id there are two options: if the selected identifier already has a numeric value that follows, the returned version will have that numeric identifier incremented by one; if the selected identifier doesn't have a numeric identifier that follows, a new numeric identifiers is added after the string with the initial value.1
.
If the version already has multiple identifiers in the prerelease block that equal to the given value then all of them will be bumped. In case they have different numeric values (or missing) each occurrence is bumped independently according to the above rules.
Examples of invokingbumpPrerelease("alpha")
on different versions:
-1.2.3 = 1.2.3-alpha.0
-1.2.3-alpha = 1.2.3-alpha.0
-1.2.3-alpha.beta = 1.2.3-alpha.0.beta
-1.2.3-gamma = 1.2.3-alpha.0.gamma
-1.2.3-gamma.delta = 1.2.3-alpha.0.gamma.delta
-1.2.3+999 = 1.2.3-alpha.0+999
-1.2.3-alpha+999 = 1.2.3-alpha.0+999
-1.2.3-alpha.beta+999 = 1.2.3-alpha.0.beta+999
-1.2.3-gamma+999 = 1.2.3-alpha.0.gamma+999
-1.2.3-gamma.delta+999 = 1.2.3-alpha.0.gamma.delta+999
-1.2.3-alpha.alpha.1.alpha.2 = 1.2.3-alpha.0.alpha.2.alpha.3
- Parameters:
id
- the selector of the identifier to bump- Returns:
- a new instance with the number identified by the given value bumped.
- Throws:
NullPointerException
- ifnull
is passedIllegalArgumentException
- if the given string is empty, contains illegal characters or represents a number
-
bump
public SemanticVersion bump(String id)
Returns a new instance with the number identified by the given value bumped. If the given value represents a core identifier (CoreIdentifiers
, namelymajor
,minor
,patch
) then that identifier is bumped, otherwise the given id is used to bump a prerelease identifier by invokingbumpPrerelease(String)
.
In other words this method is a shorthand forbump(CoreIdentifiers)
andbumpPrerelease(String)
, the latter being used only when the given id is not a core identifier.
If the version already has multiple identifiers in the prerelease block that equal to the given value then all of them will be bumped. In case they have different numeric values (or missing) each occurrence is bumped independently according to the above rules.- Specified by:
bump
in classVersion
- Parameters:
id
- the name of the identifier to bump- Returns:
- a new instance with the number identified by the given value bumped.
- Throws:
NullPointerException
- ifnull
is passedIllegalArgumentException
- if the given string is empty, contains illegal characters or represents a number- See Also:
CoreIdentifiers
,bump(CoreIdentifiers)
,bumpPrerelease(String)
-
-