66
77import static net .openhft .chronicle .testframework .ThreadUtil .pause ;
88
9+ /**
10+ * A utility class providing methods to create wait conditions. These conditions pause the execution of the
11+ * program until certain criteria are met or until a specified amount of time has elapsed.
12+ * <p>
13+ * The class provides functionality for boolean as well as generic conditions and uses a builder pattern
14+ * for customization of the waiting behavior, such as the maximum time to wait and the interval to check
15+ * the condition.
16+ */
917public class Waiters {
1018
1119 // Default maximum time to wait for a condition in milliseconds
@@ -24,10 +32,10 @@ public class Waiters {
2432 * <p>
2533 * This method is useful for synchronizing activities in concurrent programming, such as waiting for a connection to close.
2634 *
27- * @param message The message to include in the exception if the condition is not met
28- * @param condition A supplier of the condition being tested, which returns true when the condition is met
29- * @param maxTimeToWaitMs The maximum amount of time to wait for the condition, in milliseconds
30- * @throws ConditionNotMetException If the condition is not met within the specified time
35+ * @param message The message to include in the exception if the condition is not met.
36+ * @param condition A supplier of the condition being tested, which returns true when the condition is met.
37+ * @param maxTimeToWaitMs The maximum amount of time to wait for the condition, in milliseconds.
38+ * @throws ConditionNotMetException If the condition is not met within the specified time.
3139 */
3240 public static void waitForCondition (String message , Supplier <Boolean > condition , long maxTimeToWaitMs ) {
3341 builder (condition )
@@ -41,8 +49,8 @@ public static void waitForCondition(String message, Supplier<Boolean> condition,
4149 * <p>
4250 * This method simplifies the creation of waiters for boolean conditions.
4351 *
44- * @param condition the condition, when it returns true the waiter will complete
45- * @return the {@link WaiterBuilder} for further configuration
52+ * @param condition the condition, when it returns true the waiter will complete.
53+ * @return the {@link WaiterBuilder} for further configuration.
4654 */
4755 public static WaiterBuilder <Boolean > builder (Supplier <Boolean > condition ) {
4856 return builder (condition , IDENTITY ).message (DEFAULT_BOOLEAN_MESSAGE );
@@ -53,100 +61,100 @@ public static WaiterBuilder<Boolean> builder(Supplier<Boolean> condition) {
5361 * <p>
5462 * This method provides more flexibility for defining complex waiting conditions.
5563 *
56- * @param valueSupplier The supplier of the value being tested, which will be regularly evaluated
57- * @param conditionTester A predicate that determines if the condition has been met based on the supplied value
58- * @param <T> The type of the value being tested
59- * @return the {@link WaiterBuilder} for further configuration
64+ * @param valueSupplier The supplier of the value being tested, which will be regularly evaluated.
65+ * @param conditionTester A predicate that determines if the condition has been met based on the supplied value.
66+ * @param <T> The type of the value being tested.
67+ * @return the {@link WaiterBuilder} for further configuration.
6068 */
6169 public static <T > WaiterBuilder <T > builder (Supplier <T > valueSupplier , Predicate <T > conditionTester ) {
6270 return new WaiterBuilder <>(valueSupplier , conditionTester );
6371 }
6472
6573 public static class WaiterBuilder <T > implements Runnable {
66- // Supplier that provides a value
74+ // Supplier that provides a value
6775 private final Supplier <T > valueSupplier ;
6876
69- // Predicate to test the provided value
77+ // Predicate to test the provided value
7078 private final Predicate <T > conditionTester ;
7179
72- // Maximum time to wait for a condition in milliseconds
80+ // Maximum time to wait for a condition in milliseconds
7381 private long maxTimeToWaitMs = DEFAULT_MAX_WAIT_TIME_MS ;
7482
75- // Time interval to check the condition in milliseconds
83+ // Time interval to check the condition in milliseconds
7684 private long checkIntervalMs = DEFAULT_CHECK_INTERVAL_MS ;
7785
78- // Function to generate a message in case the condition isn't met
86+ // Function to generate a message in case the condition isn't met
7987 private Function <T , String > messageGenerator = lastValue -> String .format (DEFAULT_GENERIC_MESSAGE , lastValue );
8088
81- // Constructor
89+ // Constructor
8290 private WaiterBuilder (Supplier <T > valueSupplier , Predicate <T > conditionTester ) {
8391 this .valueSupplier = valueSupplier ;
8492 this .conditionTester = conditionTester ;
8593 }
8694
8795 /**
88- * Wait for the condition to be true, throw an {@link ConditionNotMetException}
89- * if the condition is not met in time.
90- *
91- * This method is overridden from the Runnable interface, allowing the WaiterBuilder
92- * to be run as a separate thread.
96+ * Wait for the condition to be true, throw an {@link ConditionNotMetException}
97+ * if the condition is not met in time.
98+ * <p>
99+ * This method is overridden from the Runnable interface, allowing the WaiterBuilder
100+ * to be run as a separate thread.
93101 */
94102 @ Override
95103 public void run () {
96104 long endTime = System .currentTimeMillis () + maxTimeToWaitMs ;
97105 while (true ) {
98106 final T value = valueSupplier .get ();
99107 if (conditionTester .test (value )) {
100- break ; // Condition met, break the loop
108+ break ; // Condition met, break the loop
101109 }
102110 if (System .currentTimeMillis () > endTime ) {
103- // Condition wasn't met in the allowed time, throw an exception
111+ // Condition wasn't met in the allowed time, throw an exception
104112 throw new ConditionNotMetException (value , messageGenerator .apply (value ));
105113 }
106- // Condition wasn't met yet, pause before checking again
114+ // Condition wasn't met yet, pause before checking again
107115 pause (checkIntervalMs );
108116 }
109117 }
110118
111119 /**
112- * Specifies a static message for the exception in case the condition isn't met.
120+ * Specifies a static message for the exception in case the condition isn't met.
113121 *
114- * @param message the message to be displayed when an exception is thrown
115- * @return the WaiterBuilder instance to allow for method chaining
122+ * @param message the message to be displayed when an exception is thrown.
123+ * @return the WaiterBuilder instance to allow for method chaining.
116124 */
117125 public WaiterBuilder <T > message (String message ) {
118- // Override the default message generator with a function that returns the provided message
126+ // Override the default message generator with a function that returns the provided message
119127 this .messageGenerator = lastValue -> message ;
120- return this ; // Return the instance for method chaining
128+ return this ; // Return the instance for method chaining
121129 }
122130
123131 /**
124- * Supply a function for generating the exception message in case the condition isn't met.
132+ * Supply a function for generating the exception message in case the condition isn't met.
125133 *
126- * @param messageGenerator a function that takes the last value as a parameter to create a message
127- * @return this WaiterBuilder instance to allow method chaining
134+ * @param messageGenerator a function that takes the last value as a parameter to create a message.
135+ * @return this WaiterBuilder instance to allow method chaining.
128136 */
129137 public WaiterBuilder <T > messageGenerator (Function <T , String > messageGenerator ) {
130138 this .messageGenerator = messageGenerator ;
131139 return this ;
132140 }
133141
134142 /**
135- * Specify the maximum amount of time to wait for the condition to be met.
143+ * Specify the maximum amount of time to wait for the condition to be met.
136144 *
137- * @param maxTimeToWaitMs the maximum time in milliseconds to wait for the condition
138- * @return this WaiterBuilder instance to allow method chaining
145+ * @param maxTimeToWaitMs the maximum time in milliseconds to wait for the condition.
146+ * @return this WaiterBuilder instance to allow method chaining.
139147 */
140148 public WaiterBuilder <T > maxTimeToWaitMs (long maxTimeToWaitMs ) {
141149 this .maxTimeToWaitMs = maxTimeToWaitMs ;
142150 return this ;
143151 }
144152
145153 /**
146- * Specify the interval between checks of the condition. This determines how often the condition is evaluated.
154+ * Specify the interval between checks of the condition. This determines how often the condition is evaluated.
147155 *
148- * @param checkIntervalMs the interval between checks in milliseconds
149- * @return this WaiterBuilder instance to allow method chaining
156+ * @param checkIntervalMs the interval between checks in milliseconds.
157+ * @return this WaiterBuilder instance to allow method chaining.
150158 */
151159 public WaiterBuilder <T > checkIntervalMs (long checkIntervalMs ) {
152160 this .checkIntervalMs = checkIntervalMs ;
@@ -182,4 +190,3 @@ public Object lastValue() {
182190 }
183191 }
184192}
185-
0 commit comments