1313
1414import java .util .Optional ;
1515
16+ /**
17+ * A representation of a <a href="https://semver.org">Semantic Version</a>.
18+ * <p>
19+ * It contains the version core (major.minor.patch), pre-release and build.
20+ * <p>
21+ * The natural order of semantic versions is defined in <a href="https://semver.org/#spec-item-11">the spec</a>.
22+ * @since 1.0.0
23+ *
24+ * @apiNote This interface is not meant to be implemented and might become {@code sealed} in future releases.
25+ */
1626@ ApiStatus .NonExtendable
1727public interface SemanticVersion extends Comparable <SemanticVersion > {
1828
29+ /**
30+ * Creates a SemanticVersion with the given major, minor and patch versions.
31+ *
32+ * @param major the major version
33+ * @param minor the minor version
34+ * @param patch the patch version
35+ * @return a SemanticVersion representing the version {@code major}.{@code minor}.{@code patch}.
36+ */
1937 static SemanticVersion of (int major , int minor , int patch ) {
2038 return new VersionCore (major , minor , patch );
2139 }
2240
41+ /**
42+ * Parses a string into a full semantic version, including pre-releases and builds.
43+ * <p>
44+ * The given string must represent a valid semantic version, otherwise the parsing will fail.
45+ *
46+ * @param version the raw version string.
47+ * @return the parsed version.
48+ * @throws de.eldoria.semvertools.VersionParseException if the given string does not express
49+ * a valid semantic version.
50+ */
2351 static SemanticVersion parse (String version ) {
2452 SemVerLexer lexer = new SemVerLexer ();
2553 return new SemVerParser (version , lexer .lex (version )).parse ();
2654 }
2755
56+ /**
57+ * The major version of this semantic version.
58+ * <p>
59+ * This is a non-negative integer value.
60+ *
61+ * @return the major version.
62+ */
2863 int major ();
2964
65+ /**
66+ * The minor version of this semantic version.
67+ * <p>
68+ * This is a non-negative integer value.
69+ *
70+ * @return the minor version.
71+ */
3072 int minor ();
3173
74+ /**
75+ * The patch version of this semantic version.
76+ * <p>
77+ * This is a non-negative integer value.
78+ *
79+ * @return the patch version.
80+ */
3281 int patch ();
3382
83+ /**
84+ * The pre-release version, if available.
85+ * <p>
86+ * If this semantic version does not have a pre-release version, {@link Optional#empty()} is returned.
87+ *
88+ * @return the pre-release version, wrapped into an {@link Optional}.
89+ */
3490 Optional <PreRelease > preRelease ();
3591
92+ /**
93+ * The build metadata, if available.
94+ * <p>
95+ * If this semantic version does not have build metadata, {@link Optional#empty()} is returned.
96+ *
97+ * @return the build metadata, wrapped into an {@link Optional}.
98+ */
3699 Optional <Build > build ();
37100
101+ /**
102+ * Returns a semantic version that only differs from this in its major version.
103+ * <p>
104+ * If the new major version is equal to the current major version, the returned semantic version
105+ * is equal to this semantic version.
106+ *
107+ * @param major the new major version.
108+ * @return the semantic version with the new major version.
109+ */
38110 SemanticVersion withMajor (int major );
39111
112+ /**
113+ * Increases the major version by 1.
114+ * <p>
115+ * This is equivalent to <pre>{@code
116+ * SemanticVersion version = ...;
117+ * version = version.withMajor(version.major() + 1);
118+ * }</pre>
119+ *
120+ * @return the semantic version with a major version greater by 1 to this major version.
121+ */
40122 default SemanticVersion increaseMajor () {
41123 return this .withMajor (this .major () + 1 );
42124 }
43125
126+ /**
127+ * Returns a semantic version that only differs from this in its minor version.
128+ * <p>
129+ * If the new minor version is equal to the current minor version, the returned semantic version
130+ * is equal to this semantic version.
131+ *
132+ * @param minor the new minor version.
133+ * @return the semantic version with the new minor version.
134+ */
44135 SemanticVersion withMinor (int minor );
45136
137+ /**
138+ * Increases the minor version by 1.
139+ * <p>
140+ * This is equivalent to <pre>{@code
141+ * SemanticVersion version = ...;
142+ * version = version.withMinor(version.minor() + 1);
143+ * }</pre>
144+ *
145+ * @return the semantic version with a minor version greater by 1 to this minor version.
146+ */
46147 default SemanticVersion increaseMinor () {
47148 return this .withMinor (this .minor () + 1 );
48149 }
49150
151+ /**
152+ * Returns a semantic version that only differs from this in its patch version.
153+ * <p>
154+ * If the new patch version is equal to the current patch version, the returned semantic version
155+ * is equal to this semantic version.
156+ *
157+ * @param patch the new patch version.
158+ * @return the semantic version with the new patch version.
159+ */
50160 SemanticVersion withPatch (int patch );
51161
162+ /**
163+ * Increases the patch version by 1.
164+ * <p>
165+ * This is equivalent to <pre>{@code
166+ * SemanticVersion version = ...;
167+ * version = version.withPatch(version.patch() + 1);
168+ * }</pre>
169+ *
170+ * @return the semantic version with a minor version greater by 1 to this minor version.
171+ */
52172 default SemanticVersion increasePatch () {
53173 return this .withPatch (this .patch () + 1 );
54174 }
55175
176+ /**
177+ * Returns a semantic version that only differs from this in its pre-release version.
178+ * <p>
179+ * If the new pre-release version is equal to the current pre-release version, the returned semantic version
180+ * is equal to this semantic version.
181+ * <p>
182+ * If the new pre-release version is {@code null}, the returned semantic version
183+ * won't contain a pre-release version.
184+ *
185+ * @param preRelease the new pre-release version.
186+ * @return the semantic version with the new pre-release version.
187+ */
56188 SemanticVersion withPreRelease (@ Nullable PreRelease preRelease );
57189
190+ /**
191+ * Returns a semantic version that only differs from this in its build metadata.
192+ * <p>
193+ * If the new build metadata is equal to the current build metadata, the returned semantic version
194+ * is equal to this semantic version.
195+ * <p>
196+ * If the new build metadata is {@code null}, the returned semantic version won't contain build metadata.
197+ *
198+ * @param build the new build metadata.
199+ * @return the semantic version with the new build metadata.
200+ */
58201 SemanticVersion withBuild (@ Nullable Build build );
59202
60203 /**
@@ -78,4 +221,23 @@ default boolean precedes(SemanticVersion other) {
78221 default boolean succeeds (SemanticVersion other ) {
79222 return compareTo (other ) > 0 ;
80223 }
224+
225+ /**
226+ * Returns whether this semantic version is equal to the given object.
227+ * <p>
228+ * <b>Note:</b> Two semantic versions are considered equal if major version, minor version,
229+ * patch version, pre-release version and build metadata are equal. This differs from
230+ * {@link SemanticVersion#compareTo(Object)}, as build metadata is considered
231+ * for the {@code equals} method only. If you want to check if two semantic versions are equal, you can:
232+ * <ul>
233+ * <li>strip the build metadata before using this method</li>
234+ * <li>use {@code versionA.compareTo(versionB) == 0}</li>
235+ * <li>use {@code !(versionA.succeeds(versionB) || versionA.precedes(versionB))}</li>
236+ * </ul>
237+ *
238+ * @param o the object to compare to.
239+ * @return {@code true} if both semantic versions are equal.
240+ */
241+ @ Override
242+ boolean equals (Object o );
81243}
0 commit comments