diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/LayerManager.java b/src/main/java/org/apache/commons/collections4/bloomfilter/LayerManager.java index 5e18f99ba..353c1f139 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/LayerManager.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/LayerManager.java @@ -76,9 +76,6 @@ public class LayerManager implements BloomFilterExtractor * @return a new LayerManager. */ public LayerManager 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 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 filterSupplier, final Predicate> extendCheck, final Consumer> 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 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.")); } /** diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/LayerManagerTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/LayerManagerTest.java index ca90f0d0d..5f03770c6 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/LayerManagerTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/LayerManagerTest.java @@ -89,17 +89,17 @@ public class LayerManagerTest { public void testBuilder() { final LayerManager.Builder 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.")); }