Record Class SemanticVersion
- Record Components:
major- The major version component (for breaking API changes).minor- The minor version component (for backwards-compatible functionality).patch- The patch version component (for backwards-compatible bug fixes).preRelease- The list of pre-release identifiers (seeSemanticVersion.PreReleaseIdentifier).buildMetadata- The list of build metadata identifiers (seeSemanticVersion.BuildIdentifier).
- All Implemented Interfaces:
Comparable<SemanticVersion>, Version<SemanticVersion>
This is an immutable representation of a SemVer string.
Implementation Note:
This class has a natural ordering that is inconsistent with equals. Semantic Versioning (SemVer) does not consider build metadata when determining precedence, however it is included naturally forequals(Object) to maintain principle of least surprise.
Therefore, if you wish to compare Semantic Versions for equality per the
SemVer specification, you should use compareTo(SemanticVersion)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA builder for aSemanticVersion.static final recordA build metadata identifier.static enumTypes of Semantic Versioning (SemVer) increments.static classA pre-release build identifier. -
Constructor Summary
ConstructorsConstructorDescriptionSemanticVersion(int major, int minor, int patch, List<SemanticVersion.PreReleaseIdentifier> preRelease, List<SemanticVersion.BuildIdentifier> buildMetadata) Constructor for semantic version representations. -
Method Summary
Modifier and TypeMethodDescription@NonNull SemanticVersion.BuilderConstruct aSemanticVersion.Builderthat has the current version's values set as initial values.static @NonNull SemanticVersion.Builderbuilder()Construct a newSemanticVersion.BuilderforSemanticVersionrepresentations.@NonNull List<SemanticVersion.BuildIdentifier> The list of build metadata identifiers (seeSemanticVersion.BuildIdentifier).intcompareTo(@NonNull SemanticVersion other) Compares this version with the specified version.final booleanIndicates whether some other object is "equal to" this one.booleanWhether the version has build metadata.final inthashCode()Returns a hash code value for this object.booleanWhether the version is a pre-release version.intmajor()Returns the value of themajorrecord component.intminor()Returns the value of theminorrecord component.intpatch()Returns the value of thepatchrecord component.@NonNull List<SemanticVersion.PreReleaseIdentifier> The list of pre-release identifiers (seeSemanticVersion.PreReleaseIdentifier).@NonNull StringtoString()Returns a string representation of this record class.@NonNull SemanticVersionwithIncremented(@NonNull SemanticVersion.IncrementType incrementType) Increments the version by performing an increment of the specifiedSemanticVersion.IncrementType.@NonNull SemanticVersionwithIncrementedPreRelease(@NonNull String name) Increments the specified pre-release version.@NonNull SemanticVersionwithIncrementedPreRelease(@NonNull String name, int initialValue) Increments the specified pre-release version.Methods inherited from interface Version
isEquivalent, isGreaterThan, isLessThan
-
Constructor Details
-
SemanticVersion
public SemanticVersion(int major, int minor, int patch, List<SemanticVersion.PreReleaseIdentifier> preRelease, List<SemanticVersion.BuildIdentifier> buildMetadata) Constructor for semantic version representations.If
preReleaseorbuildMetadataare null, they are replaced with empty lists. They are also both frozen to ensure they are immutable.- Parameters:
major- The major version component (for breaking API changes).minor- The minor version component (for backwards-compatible functionality).patch- The patch version component (for backwards-compatible bug fixes).preRelease- The list of pre-release identifiers (seeSemanticVersion.PreReleaseIdentifier).buildMetadata- The list of build metadata identifiers (seeSemanticVersion.BuildIdentifier).
-
-
Method Details
-
builder
Construct a newSemanticVersion.BuilderforSemanticVersionrepresentations.- Returns:
- The new builder instance.
-
asBuilder
Construct aSemanticVersion.Builderthat has the current version's values set as initial values.- Returns:
- The new builder instance.
-
isPreRelease
public boolean isPreRelease()Whether the version is a pre-release version.This is true when there are one, or more, pre-release components in the version string.
- Returns:
- True if, and only if, this is a pre-release version.
-
hasBuildMetadata
public boolean hasBuildMetadata()Whether the version has build metadata.This is true when there are one, or more, build metadata components in the version string.
- Returns:
- True if, and only if, this version string has build metadata.
-
preRelease
The list of pre-release identifiers (seeSemanticVersion.PreReleaseIdentifier).The constructor for
SemanticVersionensures this is non-null (non pre-release versions have an empty list, instead).- Returns:
- The list of
SemanticVersion.PreReleaseIdentifiers.
-
buildMetadata
The list of build metadata identifiers (seeSemanticVersion.BuildIdentifier).The constructor for
SemanticVersionensures this is non-null (versions with no build metadata have an empty list, instead).- Returns:
- The list of
SemanticVersion.BuildIdentifiers.
-
compareTo
Description copied from interface:VersionCompares this version with the specified version.The comparison adheres to the
Comparablespecification and semantics.- Specified by:
compareToin interfaceComparable<SemanticVersion>- Specified by:
compareToin interfaceVersion<SemanticVersion>- Parameters:
other- the object to be compared.- Returns:
- a negative integer if this version is less than the other version, zero if the versions are equal or a positive integer if this version is greater than the other version.
-
withIncremented
public @NonNull SemanticVersion withIncremented(@NonNull SemanticVersion.IncrementType incrementType) Increments the version by performing an increment of the specifiedSemanticVersion.IncrementType.This method increments the version to the next non-prerelease version. To increment a pre-release version, use
withIncrementedPreRelease(String)orwithIncrementedPreRelease(String, int).- Parameters:
incrementType- The type of increment to perform.- Returns:
- A new
SemanticVersion, derived from the current one by performing the specified increment. - See Also:
-
withIncrementedPreRelease
Increments the specified pre-release version.The behavior is identical to
withIncrementedPreRelease(String, int)except theinitialValueis defined as one for convenience.- Parameters:
name- The name of the pre-release version track.- Returns:
- The incremented version.
- See Also:
-
withIncrementedPreRelease
Increments the specified pre-release version.Unlike most other functions in the versioning library, this method is fairly opinionated and describes a specific (but interoperable) approach to versioning with pre-release versions. If you need more specific behavior, it may be preferable to implement your own increment method using the
SemanticVersionAPI.If a pre-release version component is present in the string that matches the given name and there is a numeric component immediately following it, the numeric component is incremented. Otherwise, to avoid modifying unintended parts of the version string, the name and
initialValueare instead appended to the new version string. If there are multiple occurrences, the first one is matched and incremented.To avoid errors, the
namemust not be a numeric identifier when parsed as a pre-release identifier. If it is, aVersioningExceptionis thrown.- Parameters:
name- The name of the pre-release version track (must not be numeric).initialValue- The initial value to use (if the pre-release track is not present in the version string). The initial version must be positive.- Returns:
- The incremented version.
- Throws:
VersioningException- If the specified name is numeric or if the initial value is negative.- See Also:
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.- Specified by:
toStringin interfaceVersion<SemanticVersion>- Specified by:
toStringin classRecord- Returns:
- a string representation of this object
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes.- Specified by:
equalsin interfaceVersion<SemanticVersion>- Specified by:
equalsin classRecord- Parameters:
o- the object with which to compare- Returns:
trueif this object is the same as theoargument;falseotherwise.- See Also:
-
major
-
minor
-
patch
-