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 0401b4c..1bd080e 100644 --- a/version/src/main/java/io/smallrye/common/version/VersionScheme.java +++ b/version/src/main/java/io/smallrye/common/version/VersionScheme.java @@ -1,11 +1,13 @@ 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. */ public interface VersionScheme extends Comparator { + /** * Compare two versions according to this version scheme. * @@ -17,6 +19,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 base the base 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 base the base version + * @return {@code true} if the first version is less than the second version, or {@code false} otherwise + */ + default Predicate whenGt(String base) { + return other -> gt(base, other); + } + + /** + * Returns a predicate that tests if the version is greater than or equal to the base version. + * + * @param base the base 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 base the base 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 base the base 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 0000000..bd1f290 --- /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 java.util.function.Predicate; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class ParameterizedVersionTest { + + @ParameterizedTest + @MethodSource("schemes") + public void shouldCompareVersions(VersionScheme scheme) { + Predicate predicate = scheme.whenGe("1.0.0").and(scheme.whenLt("2.0.0")); + assertThat(predicate).accepts("1.0.0", "1.1.0").rejects("2.0.0", "2.0.1", "2.1.0"); + } + + static VersionScheme[] schemes() { + return new VersionScheme[] { VersionScheme.BASIC, VersionScheme.MAVEN, VersionScheme.JPMS }; + } + +}