Skip to content

Commit 53b8c23

Browse files
author
david
committed
Refactor version parsing and retrieval APIs.
Removed nullability annotations and simplified parse method in SemanticVersion. Updated VersionChecker interface to use CompletableFuture for async methods and added support for retrieving all versions.
1 parent 99cafb0 commit 53b8c23

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

version-checker/src/main/java/core/version/SemanticVersion.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,13 @@ public record SemanticVersion(int major, int minor, int patch, @Nullable String
2626
* @param string the version string to parse
2727
* @return the parsed SemanticVersion object, or null if it is not a valid SemVer
2828
*/
29-
public static @Nullable SemanticVersion parse(String string) {
30-
try {
31-
if (!string.matches(PATTERN)) return null;
32-
var parts = string.split("-", 2);
33-
var split = parts[0].split("\\.", 3);
34-
var major = Integer.parseInt(split[0]);
35-
var minor = Integer.parseInt(split[1]);
36-
var patch = Integer.parseInt(split[2]);
37-
return new SemanticVersion(major, minor, patch, parts.length == 2 ? parts[1] : null);
38-
} catch (Exception ignored) {
39-
return null;
40-
}
29+
public static SemanticVersion parse(String string) {
30+
var parts = string.split("-", 2);
31+
var split = parts[0].split("\\.", 3);
32+
var major = Integer.parseInt(split[0]);
33+
var minor = Integer.parseInt(split[1]);
34+
var patch = Integer.parseInt(split[2]);
35+
return new SemanticVersion(major, minor, patch, parts.length == 2 ? parts[1] : null);
4136
}
4237

4338
/**
Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package core.version;
22

3-
import org.jetbrains.annotations.Nullable;
3+
import org.jetbrains.annotations.Unmodifiable;
44

5-
import java.util.Optional;
6-
import java.util.function.Consumer;
5+
import java.util.Set;
6+
import java.util.concurrent.CompletableFuture;
77

88
public interface VersionChecker<N, V extends Version> {
99
/**
@@ -17,18 +17,16 @@ public interface VersionChecker<N, V extends Version> {
1717
* Parses a version of type N into a version of type V.
1818
*
1919
* @param version the version to parse
20-
* @return the parsed version of type V, or null if parsing fails
20+
* @return the parsed version of type V
2121
*/
22-
@Nullable
2322
V parseVersion(N version);
2423

2524
/**
2625
* Parses a version of type String into a version of type V.
2726
*
2827
* @param version the version to parse
29-
* @return the parsed version of type V, or null if parsing fails
28+
* @return the parsed version of type V
3029
*/
31-
@Nullable
3230
V parseVersion(String version);
3331

3432
/**
@@ -40,9 +38,23 @@ public interface VersionChecker<N, V extends Version> {
4038
boolean isSupported(N version);
4139

4240
/**
43-
* Retrieves the latest supported version of the software and passes it to the provided success {@link Consumer}.
41+
* Asynchronously retrieves the latest available version.
4442
*
45-
* @param success the consumer that will receive the latest supported version wrapped in an {@link Optional}.
43+
* @return a CompletableFuture containing the latest version
4644
*/
47-
void retrieveLatestSupportedVersion(Consumer<Optional<V>> success);
45+
CompletableFuture<V> retrieveLatestVersion();
46+
47+
/**
48+
* Retrieves all available versions asynchronously.
49+
*
50+
* @return a CompletableFuture containing a Set of all versions
51+
*/
52+
CompletableFuture<@Unmodifiable Set<V>> retrieveVersions();
53+
54+
/**
55+
* Retrieves the latest supported version of the software asynchronously.
56+
*
57+
* @return a CompletableFuture containing the latest supported version
58+
*/
59+
CompletableFuture<V> retrieveLatestSupportedVersion();
4860
}

0 commit comments

Comments
 (0)