|
26 | 26 | import static java.util.Optional.ofNullable; |
27 | 27 |
|
28 | 28 | /** |
29 | | - * Compatible |
| 29 | + * A utility class to conditionally execute logic based on version comparison. |
30 | 30 | * |
31 | | - * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a> |
32 | | - * @since 1.0.0 |
| 31 | + * <p>{@link Compatible} allows defining conditional logic that is executed only if the current version meets a specified |
| 32 | + * condition relative to another version. It supports common version comparison operations such as equal to, greater than, |
| 33 | + * less than, and their inclusive counterparts.</p> |
| 34 | + * |
| 35 | + * <h3>Example Usage</h3> |
| 36 | + * |
| 37 | + * <h4>Example 1: Basic Usage</h4> |
| 38 | + * <pre>{@code |
| 39 | + * Version currentVersion = Version.of("1.2.3"); |
| 40 | + * Compatible<Void, String> compatible = new Compatible<>(currentVersion, v -> "Matched Condition"); |
| 41 | + * |
| 42 | + * // Execute logic only if current version is greater than "1.2.0" |
| 43 | + * Optional<String> result = compatible.on(">", "1.2.0", v -> "Executed").call(); |
| 44 | + * System.out.println(result.orElse("Not Matched")); // Output: Executed |
| 45 | + * }</pre> |
| 46 | + * |
| 47 | + * <h4>Example 2: Chaining Conditions</h4> |
| 48 | + * <pre>{@code |
| 49 | + * Version currentVersion = Version.of("2.0.0"); |
| 50 | + * Compatible<Void, String> base = new Compatible<>(currentVersion, null); |
| 51 | + * |
| 52 | + * // Apply multiple conditions in sequence |
| 53 | + * Optional<String> result = base |
| 54 | + * .on("<", "1.9.0", v -> "Older") |
| 55 | + * .on("==", "2.0.0", v -> "Exact Match") |
| 56 | + * .on(">=", "2.0.0", v -> "At least 2.0.0") |
| 57 | + * .call(); |
| 58 | + * |
| 59 | + * System.out.println(result.orElse("No condition matched")); // Output: Exact Match |
| 60 | + * }</pre> |
| 61 | + * |
| 62 | + * <h4>Example 3: Using with Class-based Version</h4> |
| 63 | + * <pre>{@code |
| 64 | + * // Automatically fetches version from the manifest of the JAR containing MyComponent.class |
| 65 | + * Compatible<MyComponent, Boolean> compatibilityCheck = Compatible.of(MyComponent.class); |
| 66 | + * |
| 67 | + * boolean isSupported = compatibilityCheck |
| 68 | + * .on(">=", "1.5.0", v -> true) |
| 69 | + * .on("<", "1.5.0", v -> false) |
| 70 | + * .get() != null; |
| 71 | + * |
| 72 | + * System.out.println("Feature supported: " + isSupported); |
| 73 | + * }</pre> |
| 74 | + * |
| 75 | + * @param <T> the type of context object associated with this compatibility check (not used directly in logic) |
| 76 | + * @param <R> the return type of the conditional function |
| 77 | + * @see Version |
| 78 | + * @see Version.Operator |
33 | 79 | */ |
34 | 80 | public class Compatible<T, R> { |
35 | 81 |
|
|
0 commit comments