Skip to content

Commit 2f99cdc

Browse files
author
Sabrina Lawrey
committed
added javadoc to:
CloseableUtil Combination Delegation ExecutorServiceUtil FlakyTestRunner GcControls NetworkUtil Permutation Product Series ThreadUtil Waiters
1 parent 94f9004 commit 2f99cdc

12 files changed

Lines changed: 526 additions & 265 deletions

File tree

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,30 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55

6+
/**
7+
* Utility class to handle closing of {@link java.io.Closeable} or {@link AutoCloseable} resources.
8+
* This class provides a method to close a resource without throwing any exceptions, logging them instead.
9+
* Since this class is defined as an enum without instances, it cannot be instantiated and is essentially a utility class.
10+
*/
611
public enum CloseableUtil {
7-
;
12+
; // This enum has no instances, acting as a utility class
813

14+
// Logger to log warning messages if any exceptions occur while closing the resource
915
private static final Logger LOGGER = LoggerFactory.getLogger(CloseableUtil.class);
1016

1117
/**
12-
* Close a {@link java.io.Closeable}, logging any exceptions thrown
18+
* Closes the provided {@link AutoCloseable} resource quietly, without throwing any exceptions.
19+
* If an exception does occur while closing the resource, it is logged as a warning and not propagated.
1320
*
14-
* @param closeable the closeable to close
21+
* @param closeable the closeable resource to close, may be any object implementing {@link AutoCloseable}
1522
*/
1623
public static void closeQuietly(AutoCloseable closeable) {
1724
try {
25+
if (closeable != null) { // Ensuring the closeable is not null to avoid NullPointerException
1826
closeable.close();
27+
}
1928
} catch (Exception e) {
20-
LOGGER.warn("Error closing", e);
29+
LOGGER.warn("Error closing", e); // Logging any exceptions that occur during closing
2130
}
2231
}
2332
}

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121

2222
import java.util.Collection;
2323
import java.util.Set;
24-
import java.util.stream.Collectors;
2524
import java.util.stream.Stream;
2625

2726
import static java.util.Objects.requireNonNull;
2827

2928
/**
30-
* General Combination support. The class eagerly calculates all combinations.
29+
* Utility class that provides functionality to generate all possible combinations
30+
* of a given set of elements. The class eagerly calculates the combinations.
31+
* <p>
32+
* This class cannot be instantiated.
3133
*
3234
* @author Per Minborg
3335
*/
@@ -37,14 +39,14 @@ public final class Combination {
3739
private Combination() {}
3840

3941
/**
40-
* Creates and returns all possible combinations of the given elements.
42+
* Creates and returns a stream of all possible combinations of the given elements.
4143
* <p>
4244
* The order of the combinations in the stream is unspecified.
4345
*
4446
* @param <T> element type
45-
* @param items to combine
46-
* @return all possible combinations of the given elements
47-
* @throws NullPointerException if the provided {@code items} list is {@code null}.
47+
* @param items the array of items to combine
48+
* @return a stream of sets containing all possible combinations of the given elements
49+
* @throws NullPointerException if the provided {@code items} array is {@code null}.
4850
*/
4951
@SafeVarargs
5052
@SuppressWarnings("varargs") // Creating a Set from an array is safe
@@ -54,36 +56,36 @@ public static <T> Stream<Set<T>> of(@NotNull final T... items) {
5456
}
5557

5658
/**
57-
* Creates and returns all possible combinations of the given elements.
59+
* Creates and returns a stream of all possible combinations of the given elements.
5860
* <p>
5961
* The order of the combinations in the stream is unspecified.
6062
*
6163
* @param <T> element type
62-
* @param items to combine
63-
* @return all possible combinations of the given elements
64-
* @throws NullPointerException if the provided {@code items} list is {@code null}.
64+
* @param items a collection of items to combine
65+
* @return a stream of sets containing all possible combinations of the given elements
66+
* @throws NullPointerException if the provided {@code items} collection is {@code null}.
6567
*/
6668
public static <T> Stream<Set<T>> of(@NotNull final Collection<T> items) {
6769
requireNonNull(items);
6870
return CombinationUtil.of(items);
6971
}
7072

7173
/**
72-
* Creates and returns all possible combinations of the given elements.
74+
* Creates and returns a stream of all possible combinations of the given elements.
7375
* <p>
7476
* The order of the combinations in the stream is unspecified.
7577
* <p>
76-
* It is unspecified if the method lazily consume the provided stream before providing
78+
* It is unspecified if the method lazily consumes the provided stream before providing
7779
* the result or not.
7880
*
7981
* @param <T> element type
80-
* @param items to combine
81-
* @return all possible combinations of the given elements
82+
* @param items a stream of items to combine
83+
* @return a stream of sets containing all possible combinations of the given elements
8284
* @throws NullPointerException if the provided {@code items} stream is {@code null}.
8385
*/
8486
public static <T> Stream<Set<T>> of(@NotNull final Stream<T> items) {
8587
requireNonNull(items);
8688
return CombinationUtil.of(items);
8789
}
90+
}
8891

89-
}

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,37 @@
77

88
import static java.util.Objects.requireNonNull;
99

10+
/**
11+
* Utility class to build delegator instances that forward method invocations
12+
* to a specified delegate object. This class facilitates a fluent API for customizing
13+
* the behavior of the delegator.
14+
* <p>
15+
* This class cannot be instantiated.
16+
*/
1017
public final class Delegation {
1118

1219
// Suppresses default constructor, ensuring non-instantiability.
1320
private Delegation() {
1421
}
1522

1623
/**
17-
* Creates and returns a new builder for a delegator instance that is using the provided
18-
* {@code delegate} as a delegate. I.e. methods invoked on a built instance will be delegated to the
24+
* Creates and returns a new builder for a delegator instance that will use the provided
25+
* {@code delegate} as the delegate. Method invocations on the built instance will be delegated to the
1926
* provided delegate.
2027
*
21-
* @param delegate to delegate invocations to
22-
* @param <D> provided delegate type
23-
* @return new delegator builder
24-
* @throws NullPointerException if any provided parameter is {@code null}.
28+
* @param delegate The object to delegate invocations to
29+
* @param <D> Provided delegate type
30+
* @return New delegator builder
31+
* @throws NullPointerException if the provided delegate is {@code null}.
2532
*/
2633
public static <D> Builder<Object, D> of(@NotNull final D delegate) {
2734
requireNonNull(delegate);
2835
return new DelegationBuilder<>(delegate);
2936
}
3037

3138
/**
32-
* The new instance will default it's {@link Object#toString()} method to the one of the
33-
* provided delegate.
39+
* Interface for building a delegation object. Allows customization of the type view
40+
* and the {@code toString()} method of the delegate.
3441
*
3542
* @param <T> Target type
3643
* @param <D> Delegation type
@@ -44,28 +51,29 @@ public interface Builder<T, D> {
4451
* <p>
4552
* The default view is {@link Object }.
4653
*
47-
* @param type to view the delegate as (non-null)
48-
* @param <N> the new type of how the delegate should be viewed
49-
* @return this builder
54+
* @param type The class to view the delegate as (non-null)
55+
* @param <N> The new type of how the delegate should be viewed
56+
* @return This builder, for chaining
5057
*/
5158
<N extends D> Builder<N, D> as(Class<N> type);
5259

5360
/**
54-
* Specifies the {@code tostring()) the view should use.
61+
* Specifies the {@code toString()} function the view should use.
5562
* <p>
56-
* The default view is {@link Object#toString()} }.
63+
* The default view is {@link Object#toString()}.
5764
*
58-
* @param toStringFunction to be applied to the delegate (non-null)
59-
* @return this builder
65+
* @param toStringFunction The function to be applied to the delegate (non-null)
66+
* @return This builder, for chaining
6067
*/
6168
Builder<T, D> toStringFunction(Function<? super D, String> toStringFunction);
6269

6370
/**
64-
* Creates and returns a new view of type T of the underlying delegate of type D
71+
* Creates and returns a new view of type T of the underlying delegate of type D.
72+
* <p>
73+
* This method finalizes the builder and returns the configured delegator.
6574
*
66-
* @return a new view
75+
* @return A new view of the delegate
6776
*/
6877
T build();
6978
}
70-
71-
}
79+
}

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,73 @@
33
import java.util.concurrent.ExecutorService;
44
import java.util.concurrent.TimeUnit;
55

6+
/**
7+
* Utility class providing methods to shut down an {@link ExecutorService}
8+
* and wait for its termination, with or without forcibly interrupting running tasks.
9+
* <p>
10+
* This is an enum with no instances, serving solely for namespace control.
11+
*/
612
public enum ExecutorServiceUtil {
713
;
814

915
private static final int DEFAULT_TIME_TO_WAIT_SECONDS = 5;
1016

1117
/**
12-
* Shut down an executor service, waiting for it to terminate
18+
* Shut down an executor service and wait for it to terminate within the given timeout.
1319
*
1420
* @param executorService The executor service
15-
* @param timeout The timeout
21+
* @param timeout The maximum time to wait
1622
* @param unit The unit of the timeout argument
17-
* @throws IllegalStateException if it didn't terminate
23+
* @throws IllegalStateException If the executor service didn't terminate within the timeout
1824
*/
1925
public static void shutdownAndWaitForTermination(ExecutorService executorService, long timeout, TimeUnit unit) {
2026
executorService.shutdown();
2127
waitForTermination(executorService, timeout, unit);
2228
}
2329

2430
/**
25-
* Shut down an executor service, wait 5 seconds for it to terminate
31+
* Shut down an executor service and wait 5 seconds for it to terminate.
2632
*
2733
* @param executorService The executor service
28-
* @throws IllegalStateException if it didn't terminate in time
34+
* @throws IllegalStateException If it didn't terminate within the default time of 5 seconds
2935
*/
3036
public static void shutdownAndWaitForTermination(ExecutorService executorService) {
3137
executorService.shutdown();
3238
waitForTermination(executorService, DEFAULT_TIME_TO_WAIT_SECONDS, TimeUnit.SECONDS);
3339
}
3440

3541
/**
36-
* Shut down an executor service, interrupting all threads, wait 5 seconds for it to terminate
42+
* Shut down an executor service, interrupting all threads, and wait 5 seconds for it to terminate.
3743
*
3844
* @param executorService The executor service
39-
* @throws IllegalStateException if it didn't terminate in time
45+
* @throws IllegalStateException If it didn't terminate within the default time of 5 seconds
4046
*/
4147
public static void shutdownForciblyAndWaitForTermination(ExecutorService executorService) {
4248
shutdownForciblyAndWaitForTermination(executorService, DEFAULT_TIME_TO_WAIT_SECONDS, TimeUnit.SECONDS);
4349
}
4450

4551
/**
46-
* Shut down an executor service, interrupting all threads, wait for it to terminate
52+
* Shut down an executor service, interrupting all threads, and wait for it to terminate within the given timeout.
4753
*
4854
* @param executorService The executor service
49-
* @param timeout The timeout
55+
* @param timeout The maximum time to wait
5056
* @param unit The unit of the timeout argument
51-
* @throws IllegalStateException if it didn't terminate in time
57+
* @throws IllegalStateException If it didn't terminate within the specified timeout
5258
*/
5359
public static void shutdownForciblyAndWaitForTermination(ExecutorService executorService, long timeout, TimeUnit unit) {
54-
executorService.shutdownNow();
60+
executorService.shutdownNow(); // Interrupt all running tasks
5561
waitForTermination(executorService, timeout, unit);
5662
}
5763

64+
// Waits for the ExecutorService to terminate within the specified time
5865
private static void waitForTermination(ExecutorService executorService, long timeToWait, TimeUnit units) {
5966
try {
6067
if (!executorService.awaitTermination(timeToWait, units)) {
6168
throw new IllegalStateException("ExecutorService didn't shut down");
6269
}
6370
} catch (InterruptedException e) {
64-
Thread.currentThread().interrupt();
65-
throw new IllegalStateException("Interrupted waiting for executor service to shut down");
71+
Thread.currentThread().interrupt(); // Restore interrupted status
72+
throw new IllegalStateException("Interrupted waiting for executor service to shut down", e);
6673
}
6774
}
6875
}

0 commit comments

Comments
 (0)