Skip to content

Commit 5341847

Browse files
committed
Fix: enforce utility class policy and test import rules in ReservoirSampling
1 parent e2a0a9a commit 5341847

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/main/java/com/thealgorithms/randomized/ReservoirSampling.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@
1414
* Time Complexity: O(n)
1515
* Space Complexity: O(k)
1616
*
17-
* Author: Michael Alexander Montoya (@cureprotocols)
17+
* @author Michael Alexander Montoya (@cureprotocols)
18+
* @see <a href="https://en.wikipedia.org/wiki/Reservoir_sampling">Reservoir Sampling - Wikipedia</a>
1819
*/
19-
public class ReservoirSampling {
20+
public final class ReservoirSampling {
21+
22+
// Prevent instantiation of utility class
23+
private ReservoirSampling() {
24+
throw new UnsupportedOperationException("Utility class");
25+
}
2026

2127
/**
2228
* Selects k random elements from a stream using reservoir sampling.

src/test/java/com/thealgorithms/randomized/ReservoirSamplingTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.thealgorithms.randomized;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
46

57
import java.util.Arrays;
68
import java.util.List;
@@ -36,7 +38,7 @@ public void testSampleSizeLessThanStreamLength() {
3638
public void testSampleSizeGreaterThanStreamLengthThrowsException() {
3739
int[] stream = {1, 2, 3};
3840

39-
Exception exception = assertThrows(IllegalArgumentException.class, () -> { ReservoirSampling.sample(stream, 5); });
41+
Exception exception = assertThrows(IllegalArgumentException.class, () -> ReservoirSampling.sample(stream, 5));
4042

4143
assertEquals("Sample size cannot exceed stream size.", exception.getMessage());
4244
}

0 commit comments

Comments
 (0)