From f2d93187cce97eb86b0dea281a15d89f969d1d8f Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Sat, 20 Jul 2024 22:48:37 -0300 Subject: [PATCH] Introduce composable sub-predicates --- .../common/version/VersionScheme.java | 95 +++++++++++++++++++ .../version/ParameterizedVersionTest.java | 23 +++++ 2 files changed, 118 insertions(+) create mode 100644 version/src/test/java/io/smallrye/common/version/ParameterizedVersionTest.java diff --git a/version/src/main/java/io/smallrye/common/version/VersionScheme.java b/version/src/main/java/io/smallrye/common/version/VersionScheme.java index 0401b4c5..8010f55b 100644 --- a/version/src/main/java/io/smallrye/common/version/VersionScheme.java +++ b/version/src/main/java/io/smallrye/common/version/VersionScheme.java @@ -1,6 +1,7 @@ package io.smallrye.common.version; import java.util.Comparator; +import java.util.function.Predicate; /** * A versioning scheme, which has distinct sorting, iteration, and canonicalization rules. @@ -17,6 +18,100 @@ public interface VersionScheme extends Comparator { */ int compare(String v1, String v2); + /** + * Determine if the first version is less than the second version according to this version scheme. + * + * @param base the base version + * @param other the other version + * @return {@code true} if the first version is less than the second version, or {@code false} otherwise + */ + default boolean lt(String base, String other) { + return compare(base, other) < 0; + } + + /** + * Determine if the first version is less than or equal to the second version according to this version scheme. + * + * @param base the base version + * @param other the other version + * @return {@code true} if the first version is less than or equal to the second version, or {@code false} otherwise + */ + default boolean le(String base, String other) { + return compare(base, other) <= 0; + } + + /** + * Determine if the first version is greater than or equal to the second version according to this version scheme. + * + * @param base the base version + * @param other the other version + * @return {@code true} if the first version is greater than or equal to the second version, or {@code false} otherwise + */ + default boolean gt(String base, String other) { + return compare(base, other) > 0; + } + + /** + * Determine if the first version is greater than the second version according to this version scheme. + * + * @param base the base version + * @param other the other version + * @return {@code true} if the first version is greater than the second version, or {@code false} otherwise + */ + default boolean ge(String base, String other) { + return compare(base, other) >= 0; + } + + /** + * Returns a predicate that tests if the version is equal to the base version. + * + * @param other the other version + * @return {@code true} if the first version is equal to the second version, or {@code false} otherwise + */ + default Predicate whenEquals(String other) { + return base -> equals(base, other); + } + + /** + * Returns a predicate that tests if the version is greater than or equal to the base version. + * + * @param other the other version + * @return {@code true} if the first version is less than the second version, or {@code false} otherwise + */ + default Predicate whenGt(String other) { + return base -> gt(base, other); + } + + /** + * Returns a predicate that tests if the version is greater than or equal to the base version. + * + * @param other the other version + * @return a predicate that tests if the version is greater than or equal to the base version + */ + default Predicate whenGe(String other) { + return base -> ge(base, other); + } + + /** + * Returns a predicate that tests if the version is less than or equal to the base version. + * + * @param other the other version + * @return a predicate that tests if the version is less than or equal to the base version + */ + default Predicate whenLe(String other) { + return base -> le(base, other); + } + + /** + * Returns a predicate that tests if the version is less than the base version. + * + * @param other the other version + * @return a predicate that tests if the version is less than the base version + */ + default Predicate whenLt(String other) { + return base -> lt(base, other); + } + /** * Determine if two versions are equal according to this version scheme. * diff --git a/version/src/test/java/io/smallrye/common/version/ParameterizedVersionTest.java b/version/src/test/java/io/smallrye/common/version/ParameterizedVersionTest.java new file mode 100644 index 00000000..cfef481b --- /dev/null +++ b/version/src/test/java/io/smallrye/common/version/ParameterizedVersionTest.java @@ -0,0 +1,23 @@ +package io.smallrye.common.version; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class ParameterizedVersionTest { + + @ParameterizedTest + @MethodSource("schemes") + public void shouldCompareVersions(VersionScheme scheme) { + assertThat(scheme.whenGe("1.0.0").and(scheme.whenLt("2.0.0"))) + .accepts("1.0.0", "1.1.0").rejects("2.0.0", "2.0.1", "2.1.0"); + assertThat(scheme.whenGt("1.0.0").and(scheme.whenLe("2.0.0"))) + .accepts("1.0.1", "2.0.0").rejects("1.0.0", "2.0.1", "2.1.0"); + } + + static VersionScheme[] schemes() { + return new VersionScheme[] { VersionScheme.BASIC, VersionScheme.MAVEN, VersionScheme.JPMS }; + } + +}