Skip to content

Commit 99f9f77

Browse files
committed
Reviewed changes and code tidy
1 parent acc8eaf commit 99f9f77

26 files changed

Lines changed: 259 additions & 297 deletions

src/main/java/net/openhft/chronicle/testframework/CloseableUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public enum CloseableUtil {
2323
public static void closeQuietly(AutoCloseable closeable) {
2424
try {
2525
if (closeable != null) { // Ensuring the closeable is not null to avoid NullPointerException
26-
closeable.close();
26+
closeable.close();
2727
}
2828
} catch (Exception e) {
2929
LOGGER.warn("Error closing", e); // Logging any exceptions that occur during closing

src/main/java/net/openhft/chronicle/testframework/Combination.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@
3636
public final class Combination {
3737

3838
// Suppresses default constructor, ensuring non-instantiability.
39-
private Combination() {}
39+
private Combination() {
40+
}
4041

4142
/**
4243
* Creates and returns a stream of all possible combinations of the given elements.
4344
* <p>
4445
* The order of the combinations in the stream is unspecified.
4546
*
46-
* @param <T> element type
47+
* @param <T> element type
4748
* @param items the array of items to combine
4849
* @return a stream of sets containing all possible combinations of the given elements
4950
* @throws NullPointerException if the provided {@code items} array is {@code null}.
@@ -60,7 +61,7 @@ public static <T> Stream<Set<T>> of(@NotNull final T... items) {
6061
* <p>
6162
* The order of the combinations in the stream is unspecified.
6263
*
63-
* @param <T> element type
64+
* @param <T> element type
6465
* @param items a collection of items to combine
6566
* @return a stream of sets containing all possible combinations of the given elements
6667
* @throws NullPointerException if the provided {@code items} collection is {@code null}.
@@ -78,7 +79,7 @@ public static <T> Stream<Set<T>> of(@NotNull final Collection<T> items) {
7879
* It is unspecified if the method lazily consumes the provided stream before providing
7980
* the result or not.
8081
*
81-
* @param <T> element type
82+
* @param <T> element type
8283
* @param items a stream of items to combine
8384
* @return a stream of sets containing all possible combinations of the given elements
8485
* @throws NullPointerException if the provided {@code items} stream is {@code null}.
@@ -88,4 +89,3 @@ public static <T> Stream<Set<T>> of(@NotNull final Stream<T> items) {
8889
return CombinationUtil.of(items);
8990
}
9091
}
91-

src/main/java/net/openhft/chronicle/testframework/FlakyTestRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public static Builder<RuntimeException> builderUnchecked(@NotNull final Runnable
9292
private static RunnableThrows<RuntimeException> asRunnableThrows(Runnable runnable) {
9393
return runnable::run;
9494
}
95+
9596
/**
9697
* Defines the interface for constructing and configuring a flaky test runner.
9798
* <p>
@@ -112,7 +113,6 @@ private static RunnableThrows<RuntimeException> asRunnableThrows(Runnable runnab
112113
* </pre>
113114
*
114115
* @param <X> Type of exception that can be thrown by the runnable object
115-
*
116116
* @see FlakyTestRunner
117117
* @see FlakyTestRunner.RunnableThrows
118118
*/

src/main/java/net/openhft/chronicle/testframework/Product.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,63 +281,80 @@ public interface TriFunction<T, U, V, R> {
281281
}
282282

283283

284+
/**
285+
* An interface representing an object that has a first component.
286+
* This can be used to add functionality to objects that are made up of multiple components.
287+
*
288+
* @param <T> The type of the first component.
289+
*/
284290
public interface HasFirst<T> {
291+
285292
/**
286-
* Returns the first component.
293+
* Returns the first component of the object.
294+
* This method should be implemented to return the first component of the multi-component object.
287295
*
288-
* @return the first component
296+
* @return The first component of the object.
289297
*/
290298
T first();
291299
}
292300

293301
/**
294-
* Interface representing an object that has a second component of type {@code U}.
302+
* An interface representing an object that has a second component.
303+
* This can be used to add functionality to objects that are made up of multiple components.
295304
*
296-
* @param <U> Type of the second component
305+
* @param <U> The type of the second component.
297306
*/
298307
public interface HasSecond<U> {
308+
299309
/**
300-
* Returns the second component.
310+
* Returns the second component of the object.
311+
* This method should be implemented to return the second component of the multi-component object.
301312
*
302-
* @return the second component
313+
* @return The second component of the object.
303314
*/
304315
U second();
305316
}
306-
307317
/**
308318
* Interface representing an object that has a third component of type {@code V}.
319+
* This can be used to add functionality to objects that are made up of three components.
309320
*
310321
* @param <V> Type of the third component
311322
*/
312323
public interface HasThird<V> {
324+
313325
/**
314-
* Returns the third component.
326+
* Returns the third component of the object.
327+
* This method should be implemented to return the third component of the multi-component object.
315328
*
316-
* @return the third component
329+
* @return The third component of the object.
317330
*/
318331
V third();
319332
}
320333

321334
/**
322335
* A Product2 is a composite object comprising two components.
323-
* It's a part of a tuple-like structure with two elements.
336+
* It's a part of a tuple-like structure with two elements, extending the HasFirst and HasSecond interfaces.
337+
* This provides a way to group two related objects together into a single unit.
324338
*
325339
* @param <T> Type of the first component
326340
* @param <U> Type of the second component
327341
*/
328342
public interface Product2<T, U> extends HasFirst<T>, HasSecond<U> {
329343

344+
// Interface doesn't have any additional methods or fields, but inherits those from HasFirst and HasSecond.
330345
}
331346

332347
/**
333348
* A Product3 is a composite object comprising three components.
334-
* It's a part of a tuple-like structure with three elements.
349+
* It's a part of a tuple-like structure with three elements, extending the HasFirst, HasSecond, and HasThird interfaces.
350+
* This provides a way to group three related objects together into a single unit.
335351
*
336352
* @param <T> Type of the first component
337353
* @param <U> Type of the second component
338354
* @param <V> Type of the third component
339355
*/
340356
public interface Product3<T, U, V> extends HasFirst<T>, HasSecond<U>, HasThird<V> {
341-
}
342357

358+
// Interface doesn't have any additional methods or fields, but inherits those from HasFirst, HasSecond, and HasThird.
359+
}
343360
}

src/main/java/net/openhft/chronicle/testframework/Waiters.java

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
import 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+
*/
917
public 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

Comments
 (0)