Skip to content

Commit

Permalink
Introduce composable sub-predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jul 21, 2024
1 parent 2ac85cd commit f2d9318
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,6 +18,100 @@ public interface VersionScheme extends Comparator<String> {
*/
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<String> 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<String> 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<String> 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<String> 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<String> whenLt(String other) {
return base -> lt(base, other);
}

/**
* Determine if two versions are equal according to this version scheme.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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 };
}

}

0 comments on commit f2d9318

Please sign in to comment.