Skip to content

Commit 86310ef

Browse files
authored
feat: Javadocs for API classes (#13)
1 parent e91b7fa commit 86310ef

File tree

7 files changed

+262
-6
lines changed

7 files changed

+262
-6
lines changed

build.gradle.kts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ dependencies {
3232
java {
3333
withSourcesJar()
3434
withJavadocJar()
35-
sourceCompatibility = JavaVersion.VERSION_11;
35+
toolchain {
36+
languageVersion.set(JavaLanguageVersion.of(11))
37+
}
3638
}
3739

3840
tasks {
@@ -55,17 +57,16 @@ tasks {
5557
compileTestJava{
5658
options.encoding = "UTF-8"
5759
}
60+
61+
javadoc {
62+
(options as StandardJavadocDocletOptions).tags?.add("apiNote:a:API Note")
63+
}
5864
}
5965

6066
license {
6167
header(project.file("HEADER.txt"))
6268
}
6369

64-
java {
65-
withJavadocJar()
66-
withSourcesJar()
67-
}
68-
6970
publishData {
7071
useEldoNexusRepos()
7172
publishComponent("java")

src/main/java/de/eldoria/semvertools/Build.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,30 @@
1010

1111
import java.util.List;
1212

13+
/**
14+
* This represents the build part of a semantic version.
15+
*
16+
* @apiNote This interface is not meant to be implemented and might become {@code sealed} in future releases.
17+
*/
1318
@ApiStatus.NonExtendable
1419
public interface Build {
1520

1621
static Build of(List<Identifier> identifiers) {
1722
return new BuildImpl(identifiers);
1823
}
1924

25+
/**
26+
* Returns a string of the dot-separated identifiers of this build.
27+
*
28+
* @return a formatted build string.
29+
*/
2030
String asString();
2131

32+
/**
33+
* Returns the list of identifiers represented by this build.
34+
* The returned list is unmodifiable.
35+
*
36+
* @return the list of identifiers.
37+
*/
2238
List<Identifier> identifiers();
2339
}

src/main/java/de/eldoria/semvertools/Identifier.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,32 @@
1212
import java.util.Optional;
1313
import java.util.function.Supplier;
1414

15+
/**
16+
* Represents either an alphanumeric or a numerical identifier.
17+
* Identifiers are {@link Comparable} as described in <a href="https://semver.org/#spec-item-11">Item 11</a> of the spec.
18+
*
19+
* @apiNote This interface is not meant to be implemented and might become {@code sealed} in future releases.
20+
*/
1521
@ApiStatus.NonExtendable
1622
public interface Identifier extends Comparable<Identifier> {
1723

24+
/**
25+
* Returns a numerical identifier representing the given non-negative integer.
26+
*
27+
* @param num a non-negative integer value.
28+
* @return a numerical identifier for the given integer.
29+
*/
1830
static Identifier of(int num) {
1931
Integers.assertNonNegative(num);
2032
return new NumericalIdentifier(num);
2133
}
2234

35+
/**
36+
* Returns an alphanumeric identifier for the given string.
37+
*
38+
* @param alphanumeric a string only containing alphanumeric characters.
39+
* @return a new alphanumeric identifier.
40+
*/
2341
static Identifier of(String alphanumeric) {
2442
return Integers.parseNonNegativeInt(alphanumeric)
2543
.map(Identifier::of)
@@ -44,5 +62,11 @@ private static Supplier<Optional<Identifier>> alphanumeric(String value) {
4462
};
4563
}
4664

65+
/**
66+
* Returns a {@link String} representation of this identifier. This normally equals
67+
* the string it was parsed from.
68+
*
69+
* @return a string representation of this identifier.
70+
*/
4771
String asString();
4872
}

src/main/java/de/eldoria/semvertools/PreRelease.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,40 @@
66

77
package de.eldoria.semvertools;
88

9+
import org.jetbrains.annotations.ApiStatus;
10+
911
import java.util.List;
1012

13+
/**
14+
* This represents the pre-release part of a semantic version.
15+
*
16+
* @apiNote This interface is not meant to be implemented and might become {@code sealed} in future releases.
17+
*/
18+
@ApiStatus.NonExtendable
1119
public interface PreRelease extends Comparable<PreRelease> {
1220

21+
/**
22+
* Creates a new pre-release with the given identifiers.
23+
*
24+
* @param identifiers the identifiers of the pre-release.
25+
* @return a new pre-release.
26+
*/
1327
static PreRelease of(List<Identifier> identifiers) {
1428
return new PreReleaseImpl(identifiers);
1529
}
1630

31+
/**
32+
* Returns a string of the dot-separated identifiers of this pre-release.
33+
*
34+
* @return a formatted pre-release string.
35+
*/
1736
String asString();
1837

38+
/**
39+
* Returns the list of identifiers represented by this pre-release.
40+
* The returned list is unmodifiable.
41+
*
42+
* @return the list of identifiers.
43+
*/
1944
List<Identifier> identifiers();
2045
}

src/main/java/de/eldoria/semvertools/SemanticVersion.java

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,191 @@
1313

1414
import 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
1727
public 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
}

src/main/java/de/eldoria/semvertools/VersionParseException.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,26 @@
66

77
package de.eldoria.semvertools;
88

9+
/**
10+
* This exception is thrown to indicate that parsing a semantic version was not successful.
11+
*/
912
public class VersionParseException extends RuntimeException {
1013

14+
/**
15+
* Creates a new VersionParseException with the specified message.
16+
*
17+
* @param message the message describing the exception details.
18+
*/
1119
public VersionParseException(String message) {
1220
super(message);
1321
}
1422

23+
/**
24+
* Creates a new VersionParseException with the specified message and the specified cause.
25+
*
26+
* @param message the message describing the exception details.
27+
* @param cause the Throwable causing this exception.
28+
*/
1529
public VersionParseException(String message, Throwable cause) {
1630
super(message, cause);
1731
}

0 commit comments

Comments
 (0)