Push down null checks

- Use more language neutral exception message
- Use Objects.requireNonNull()
This commit is contained in:
Gary Gregory 2024-08-25 18:56:10 -04:00
parent c61faccf9c
commit edcc524215
2 changed files with 11 additions and 18 deletions

View File

@ -76,9 +76,6 @@ public class LayerManager<T extends BloomFilter> implements BloomFilterExtractor
* @return a new LayerManager.
*/
public LayerManager<T> build() {
Objects.requireNonNull(supplier, "Supplier must not be null");
Objects.requireNonNull(extendCheck, "ExtendCheck must not be null");
Objects.requireNonNull(cleanup, "Cleanup must not be null");
return new LayerManager<>(supplier, extendCheck, cleanup, true);
}
@ -291,19 +288,19 @@ public class LayerManager<T extends BloomFilter> implements BloomFilterExtractor
/**
* Constructs a new instance.
*
* @param filterSupplier the supplier of new Bloom filters to add the the list
* @param filterSupplier the non-null supplier of new Bloom filters to add the the list
* when necessary.
* @param extendCheck The predicate that checks if a new filter should be
* @param extendCheck The non-null predicate that checks if a new filter should be
* added to the list.
* @param filterCleanup the consumer that removes any old filters from the
* @param filterCleanup the non-null consumer that removes any old filters from the
* list.
* @param initialize true if the filter list should be initialized.
*/
private LayerManager(final Supplier<T> filterSupplier, final Predicate<LayerManager<T>> extendCheck,
final Consumer<Deque<T>> filterCleanup, final boolean initialize) {
this.filterSupplier = filterSupplier;
this.extendCheck = extendCheck;
this.filterCleanup = filterCleanup;
this.filterSupplier = Objects.requireNonNull(filterSupplier, "filterSupplier");
this.extendCheck = Objects.requireNonNull(extendCheck, "extendCheck");
this.filterCleanup = Objects.requireNonNull(filterCleanup, "filterCleanup");
if (initialize) {
addFilter();
}
@ -313,11 +310,7 @@ public class LayerManager<T extends BloomFilter> implements BloomFilterExtractor
* Adds a new Bloom filter to the list.
*/
private void addFilter() {
final T bf = filterSupplier.get();
if (bf == null) {
throw new NullPointerException("filterSupplier returned null.");
}
filters.add(bf);
filters.add(Objects.requireNonNull(filterSupplier.get(), "filterSupplier.get() returned null."));
}
/**

View File

@ -89,17 +89,17 @@ public class LayerManagerTest {
public void testBuilder() {
final LayerManager.Builder<BloomFilter> underTest = LayerManager.builder();
NullPointerException npe = assertThrows(NullPointerException.class, underTest::build);
assertTrue(npe.getMessage().contains("Supplier must not be null"));
assertTrue(npe.getMessage().contains("filterSupplier"));
underTest.setSupplier(() -> null).setCleanup(null);
npe = assertThrows(NullPointerException.class, underTest::build);
assertTrue(npe.getMessage().contains("Cleanup must not be null"));
assertTrue(npe.getMessage().contains("filterCleanup"));
underTest.setCleanup(x -> {
}).setExtendCheck(null);
npe = assertThrows(NullPointerException.class, underTest::build);
assertTrue(npe.getMessage().contains("ExtendCheck must not be null"));
assertTrue(npe.getMessage().contains("extendCheck"));
npe = assertThrows(NullPointerException.class, () -> LayerManager.builder().setSupplier(() -> null).build());
assertTrue(npe.getMessage().contains("filterSupplier returned null."));
assertTrue(npe.getMessage().contains("filterSupplier.get() returned null."));
}