Package io.microsphere.util
Class Compatible<T,R>
- java.lang.Object
-
- io.microsphere.util.Compatible<T,R>
-
- Type Parameters:
T
- the type of context object associated with this compatibility check (not used directly in logic)R
- the return type of the conditional function
public class Compatible<T,R> extends java.lang.Object
A utility class to conditionally execute logic based on version comparison.Compatible
allows defining conditional logic that is executed only if the current version meets a specified condition relative to another version. It supports common version comparison operations such as equal to, greater than, less than, and their inclusive counterparts.Example Usage
Example 1: Basic Usage
Version currentVersion = Version.of("1.2.3"); Compatible<Void, String> compatible = new Compatible<>(currentVersion, v -> "Matched Condition"); // Execute logic only if current version is greater than "1.2.0" Optional<String> result = compatible.on(">", "1.2.0", v -> "Executed").call(); System.out.println(result.orElse("Not Matched")); // Output: Executed
Example 2: Chaining Conditions
Version currentVersion = Version.of("2.0.0"); Compatible<Void, String> base = new Compatible<>(currentVersion, null); // Apply multiple conditions in sequence Optional<String> result = base .on("<", "1.9.0", v -> "Older") .on("==", "2.0.0", v -> "Exact Match") .on(">=", "2.0.0", v -> "At least 2.0.0") .call(); System.out.println(result.orElse("No condition matched")); // Output: Exact Match
Example 3: Using with Class-based Version
// Automatically fetches version from the manifest of the JAR containing MyComponent.class Compatible<MyComponent, Boolean> compatibilityCheck = Compatible.of(MyComponent.class); boolean isSupported = compatibilityCheck .on(">=", "1.5.0", v -> true) .on("<", "1.5.0", v -> false) .get() != null; System.out.println("Feature supported: " + isSupported);
- See Also:
Version
,Version.Operator
-
-
Constructor Summary
Constructors Constructor Description Compatible(Version version, java.util.function.Function<Version,R> conditionalFunction)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accept(java.util.function.Consumer<R> resultConsumer)
java.util.Optional<R>
call()
R
get()
static <T> Compatible<T,?>
of(java.lang.Class<T> targetClass)
<R> Compatible<T,R>
on(Version.Operator operator, Version comparedVersion, java.util.function.Function<Version,R> conditionalFunction)
<R> Compatible<T,R>
on(java.lang.String operator, java.lang.String comparedVersion, java.util.function.Function<Version,R> conditionalFunction)
-
-
-
Method Detail
-
of
public static <T> Compatible<T,?> of(java.lang.Class<T> targetClass)
-
on
public <R> Compatible<T,R> on(java.lang.String operator, java.lang.String comparedVersion, java.util.function.Function<Version,R> conditionalFunction)
-
on
public <R> Compatible<T,R> on(Version.Operator operator, Version comparedVersion, java.util.function.Function<Version,R> conditionalFunction)
-
call
public java.util.Optional<R> call()
- Returns:
-
accept
public void accept(java.util.function.Consumer<R> resultConsumer)
-
-