Class Version
- java.lang.Object
-
- io.ultreia.java4all.util.Version
-
- All Implemented Interfaces:
Serializable,Comparable<Version>
public class Version extends Object implements Comparable<Version>, Serializable
Replace old nuiton-version (org.nuiton.version.Version).Created on 13/11/2021.
A class to represent a version.
Replace previously
org.nuiton.util.Versionclass.Definition
A version is defined of ncomponentsseparated bycomponentSeparator.Components
Components can be of two types:- Number component: a strict positive integer value
- String component: a sequence of characters which can't be either number nor component separators
Component separators
Component separator are characters which can't be alphanumeric and can be
empty character.Component separators are optional and components will be detected as soon as a character changed from a numeric string sequence to a alpha (none numeric!) sequence.
For example, version
1a2is composed of three components: {code 1},aand3.Snapshot flag
Additionally version can be qualified as aSNAPSHOT(see below section about ordering).Examples
0 (one component 0) 0-SNAPSHOT (one component 0 + SNAPSHOT flag) 1.0 (two components 1,0) 1.1 (two components 1,1) 1.1-alpha-1 (four components 1,1,alpha,1) 1.1-beta (three components 1,1,beta) 1.1-rc-1 (four components 1,1,rc,1) 1.1-a (three components 1,1,a) 1.1-a12-4.45_6432 (seven components 1,1,a,12,4,45,643)
Ordering
A version is comparable, to have all the detail of order seeVersion.VersionComparator.Immutability
The version is immutable, to create or modify a version, use theVersion.VersionBuilderAPI or shortcut methods.- Since:
- 1.0.14
- Author:
- Tony Chemit - dev@tchemit.fr
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classVersion.NumberVersionComponentstatic classVersion.StringVersionComponentstatic classVersion.VersionBuilderstatic classVersion.VersionComparatorstatic interfaceVersion.VersionComponent<C extends Comparable<C>,V extends Version.VersionComponent<C,V>>
-
Field Summary
Fields Modifier and Type Field Description protected List<Version.VersionComponent<?,?>>componentsList of components of the version.protected List<String>componentSeparatorsList of separators of the version.static charDEFAULT_JOIN_COMPONENT_SEPARATORDefault component separator.protected booleansnapshotflag to define if version is a snapshot (if so a -SNAPSHOT is added at the end of the textual representation of the version).static StringSNAPSHOT_SUFFIXSuffix of aSNAPSHOTversion in the text representation.protected Stringversionstring representation of the version.protected static Version.VersionComparatorVERSION_COMPARATORComparator of version used internally to fulfill the comparator contract.static VersionVZEROVersion V0.
-
Constructor Summary
Constructors Modifier Constructor Description protectedVersion(List<Version.VersionComponent<?,?>> components, List<String> componentSeparators, boolean snapshot)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static VersionaddSnapshot(Version version)Create a version from the given one and set to it thesnapshotstate totrue.booleanafter(Version o)booleanafterOrEquals(Version o)booleanbefore(Version o)booleanbeforeOrEquals(Version o)intcompareTo(Version o)static Version.VersionBuildercreate()static Version.VersionBuildercreate(Version version)static Version.VersionBuildercreate(String version)static Version.VersionBuildercreate(List<Comparable<?>> components)static Versiondecrements(Version version, int componentPosition)Creates a new version from this one with the number component decremented at the given position.booleanequals(Object o)static booleanequals(String version0, String version1)Tests if two versions are equals.static VersionextractVersion(Version version, int component)Create a new version containing a single component from a given version.static VersionextractVersion(Version version, int firstComponent, int lastComponent)Create a new version containing a sub set of component from a given version.Version.VersionComponent<?,?>getComponent(int level)intgetComponentCount()List<Version.VersionComponent<?,?>>getComponents()List<String>getComponentSeparators()protected Version.VersionComponent<?,?>getLastComponent()intgetNumberComponent(int componentPosition)StringgetTextComponent(int componentPosition)StringgetValidName()Convert the string representation to a java identifier compliant.StringgetVersion()static booleangreaterThan(String version0, String version1)Tests if the first version is greater than the second version.inthashCode()static Versionincrements(Version version)Creates a new version from this one incremented.static Versionincrements(Version version, char componentSeparator)Creates a new version from this one incremented.static Versionincrements(Version version, int componentPosition)Creates a new version from this one with the number component incremented at the given position.booleanisSnapshot()static VersionremoveSnapshot(Version version)Create a version from the given one and set to it thesnapshotstate tofalse.static booleansmallerThan(String version0, String version1)Tests if the first version is smaller than the second version.StringtoString()static VersionvalueOf(String version)Shortcut method to get a version from his string representation.
-
-
-
Field Detail
-
SNAPSHOT_SUFFIX
public static final String SNAPSHOT_SUFFIX
Suffix of aSNAPSHOTversion in the text representation.- See Also:
- Constant Field Values
-
VZERO
public static final Version VZERO
Version V0.
-
DEFAULT_JOIN_COMPONENT_SEPARATOR
public static final char DEFAULT_JOIN_COMPONENT_SEPARATOR
Default component separator.- See Also:
- Constant Field Values
-
VERSION_COMPARATOR
protected static final Version.VersionComparator VERSION_COMPARATOR
Comparator of version used internally to fulfill the comparator contract.
-
components
protected final List<Version.VersionComponent<?,?>> components
List of components of the version.
-
componentSeparators
protected final List<String> componentSeparators
List of separators of the version.
-
snapshot
protected final boolean snapshot
flag to define if version is a snapshot (if so a -SNAPSHOT is added at the end of the textual representation of the version).
-
version
protected transient String version
string representation of the version.
-
-
Constructor Detail
-
Version
protected Version(List<Version.VersionComponent<?,?>> components, List<String> componentSeparators, boolean snapshot)
-
-
Method Detail
-
valueOf
public static Version valueOf(String version)
Shortcut method to get a version from his string representation.- Parameters:
version- string representation of the version- Returns:
- converted version from the string representation
-
create
public static Version.VersionBuilder create()
-
create
public static Version.VersionBuilder create(String version)
-
create
public static Version.VersionBuilder create(Version version)
-
create
public static Version.VersionBuilder create(List<Comparable<?>> components)
-
addSnapshot
public static Version addSnapshot(Version version)
Create a version from the given one and set to it thesnapshotstate totrue.- Parameters:
version- version to clone- Returns:
- the cloned version with the
snapshotstate totrue - Throws:
IllegalArgumentException- ifsnapshotstate is already set totrueon the givenversion.
-
removeSnapshot
public static Version removeSnapshot(Version version)
Create a version from the given one and set to it thesnapshotstate tofalse.- Parameters:
version- version to clone- Returns:
- the cloned version with the
snapshotstate totrue - Throws:
IllegalArgumentException- ifsnapshotstate is already set tofalseon the givenversion
-
extractVersion
public static Version extractVersion(Version version, int component)
Create a new version containing a single component from a given version.- Parameters:
version- original versioncomponent- component index to extract- Returns:
- new
Versionwith a single component
-
extractVersion
public static Version extractVersion(Version version, int firstComponent, int lastComponent)
Create a new version containing a sub set of component from a given version.- Parameters:
version- original versionfirstComponent- first component indexlastComponent- last component index- Returns:
- new
Versionwith a components sub set
-
increments
public static Version increments(Version version)
Creates a new version from this one incremented.If the last component is a number, then just increments this number; otherwise add a new number component with value 1.
Example:
- 1 → 2
- 1-a → 1-a.1
- Parameters:
version- version to increment- Returns:
- the incremented version
-
increments
public static Version increments(Version version, char componentSeparator)
Creates a new version from this one incremented.If the last component is a number, then just increments this number; otherwise add a new number component with value 1.
Example:
- 1 → 2
- 1-a → 1-a.1
- Parameters:
version- version to incrementcomponentSeparator- the component separator to use the last component is a classifier- Returns:
- the incremented version
-
increments
public static Version increments(Version version, int componentPosition)
Creates a new version from this one with the number component incremented at the given position.Note: Will fail if the component at the required position is not a number.
- Parameters:
version- version to incrementcomponentPosition- position of the version component to increment- Returns:
- the incremented version
-
equals
public static boolean equals(String version0, String version1)
Tests if two versions are equals.- Parameters:
version0- the first versionversion1- the second version- Returns:
trueif versions are equals,falseotherwise.
-
decrements
public static Version decrements(Version version, int componentPosition)
Creates a new version from this one with the number component decremented at the given position.Note: Will fail if the component at the required position is not a number, or his value is 0.
- Parameters:
version- version to decrementcomponentPosition- position of the version component to increment- Returns:
- the decremented version
-
smallerThan
public static boolean smallerThan(String version0, String version1)
Tests if the first version is smaller than the second version.- Parameters:
version0- the first versionversion1- the second version- Returns:
trueifversion0is beforeversion1,falseotherwise.
-
greaterThan
public static boolean greaterThan(String version0, String version1)
Tests if the first version is greater than the second version.- Parameters:
version0- the first versionversion1- the second version- Returns:
trueifversion0is afterversion1,falseotherwise.
-
getComponents
public List<Version.VersionComponent<?,?>> getComponents()
-
isSnapshot
public boolean isSnapshot()
-
getComponentCount
public int getComponentCount()
-
getNumberComponent
public int getNumberComponent(int componentPosition)
-
getTextComponent
public String getTextComponent(int componentPosition)
-
getComponent
public Version.VersionComponent<?,?> getComponent(int level)
-
getVersion
public String getVersion()
- Returns:
- the string representation value of the version
-
getValidName
public String getValidName()
Convert the string representation to a java identifier compliant.- in java:
.is forbidden - in database (mysql, h2 ...):
.is forbidden
Forbidden values are replaced by
_character.- Returns:
- the java compliant string representation of the version
- in java:
-
compareTo
public int compareTo(Version o)
- Specified by:
compareToin interfaceComparable<Version>
-
beforeOrEquals
public boolean beforeOrEquals(Version o)
- Parameters:
o- the other version to test- Returns:
trueif current version is before or equals the given one
-
before
public boolean before(Version o)
- Parameters:
o- the other version to test- Returns:
trueif current version is before the given one
-
afterOrEquals
public boolean afterOrEquals(Version o)
- Parameters:
o- the other version to test- Returns:
trueif current version is after or equals the given one
-
after
public boolean after(Version o)
- Parameters:
o- the other version to test- Returns:
trueif current version is after the given one
-
getLastComponent
protected Version.VersionComponent<?,?> getLastComponent()
-
-