Skip to content

Commit

Permalink
Push down null checks
Browse files Browse the repository at this point in the history
- Use more language neutral exception message
- Use Objects.requireNonNull()
  • Loading branch information
garydgregory committed Aug 25, 2024
1 parent c61facc commit edcc524
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ private Builder() {
* @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);
}

Expand Down Expand Up @@ -291,19 +288,19 @@ public static <T extends BloomFilter> Builder<T> builder() {
/**
* 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();
}
Expand All @@ -313,11 +310,7 @@ private LayerManager(final Supplier<T> filterSupplier, final Predicate<LayerMana
* 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."));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ public void testAdvanceOnSaturation() {
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."));

}

Expand Down

0 comments on commit edcc524

Please sign in to comment.