public class Waiters extends Object
The class provides functionality for boolean as well as generic conditions and uses a builder pattern for customization of the waiting behavior, such as the maximum time to wait and the interval to check the condition.
| Modifier and Type | Class and Description |
|---|---|
static class |
Waiters.ConditionNotMetException
An exception class that is thrown when a condition is not met within the specified time.
|
static class |
Waiters.WaiterBuilder<T> |
| Constructor and Description |
|---|
Waiters() |
| Modifier and Type | Method and Description |
|---|---|
static Waiters.WaiterBuilder<Boolean> |
builder(Supplier<Boolean> condition)
Creates a builder for waiting on a boolean condition.
|
static <T> Waiters.WaiterBuilder<T> |
builder(Supplier<T> valueSupplier,
Predicate<T> conditionTester)
Creates a builder for waiting on a generic condition, allowing customization of the condition testing.
|
static void |
waitForCondition(String message,
Supplier<Boolean> condition,
long maxTimeToWaitMs)
Waits for the specified condition to be true, throwing a
Waiters.ConditionNotMetException if the condition is not met within the specified time. |
public static void waitForCondition(String message, Supplier<Boolean> condition, long maxTimeToWaitMs)
Waiters.ConditionNotMetException if the condition is not met within the specified time.
This method is useful for synchronizing activities in concurrent programming, such as waiting for a connection to close.
message - The message to include in the exception if the condition is not met.condition - A supplier of the condition being tested, which returns true when the condition is met.maxTimeToWaitMs - The maximum amount of time to wait for the condition, in milliseconds.Waiters.ConditionNotMetException - If the condition is not met within the specified time.public static Waiters.WaiterBuilder<Boolean> builder(Supplier<Boolean> condition)
This method simplifies the creation of waiters for boolean conditions.
condition - the condition, when it returns true the waiter will complete.Waiters.WaiterBuilder for further configuration.public static <T> Waiters.WaiterBuilder<T> builder(Supplier<T> valueSupplier, Predicate<T> conditionTester)
This method provides more flexibility for defining complex waiting conditions.
T - The type of the value being tested.valueSupplier - The supplier of the value being tested, which will be regularly evaluated.conditionTester - A predicate that determines if the condition has been met based on the supplied value.Waiters.WaiterBuilder for further configuration.Copyright © 2024. All rights reserved.