BloomFilterManagerImpl implements BloomFilterManager<T, C>,
        Listenable<BloomFilterManagerListener<? super T, C>> { @Override public CreateFilterResult<T> createFilter(String name, C config, boolean overwrite) { T filter; T prevFilter; boolean created = false; filterMapLock.lock(); try { prevFilter = filterMap.get(name); if (overwrite || prevFilter == null || !prevFilter.valid()) { filter = factory.createFilter(config); filterMap.put(name, filter); created = true; } else { filter = prevFilter; } } finally { filterMapLock.unlock(); } if (created) { if (prevFilter != null) { notifyBloomFilterRemoved(name, prevFilter); } notifyBloomFilterCreated(name, config, filter); } return new CreateFilterResult<>(filter, created); } BloomFilterManagerImpl(BloomFilterFactory<? extends T, ? super C> factory); @Override CreateFilterResult<T> createFilter(String name, C config, boolean overwrite); @Override void addFilters(Iterable<FilterRecord<? extends T>> filters); @Nullable @Override T getFilter(String name); @Override T ensureGetValidFilter(String name); @Override List<String> getAllFilterNames(); @Override int size(); @Override void remove(String name); @Override void remove(String name, T filter); @Override Iterator<FilterRecord<T>> iterator(); @Override void addListener(BloomFilterManagerListener<? super T, C> listener); @Override boolean removeListener(BloomFilterManagerListener<? super T, C> listener);  }
@SuppressWarnings("unchecked") @Test public void testOverwriteExpiredFilter() { final ExpirableBloomFilterConfig config = new ExpirableBloomFilterConfig(); final ZonedDateTime created = ZonedDateTime.now(ZoneOffset.UTC); final ZonedDateTime expiration = created.minus(Duration.ofSeconds(10)); final BloomFilterFactory<GuavaBloomFilter, ExpirableBloomFilterConfig> mockedFactory = Mockito.mock(BloomFilterFactory.class); Mockito.when(mockedFactory.createFilter(config)) .thenReturn(new GuavaBloomFilter( config.expectedInsertions(), config.fpp(), created, expiration, null)); final BloomFilterManagerImpl<GuavaBloomFilter, ExpirableBloomFilterConfig> manager = new BloomFilterManagerImpl<>(mockedFactory); final CreateFilterResult<GuavaBloomFilter> firstCreateFilterResult = manager.createFilter(testingFilterName, config); assertThat(firstCreateFilterResult.isCreated()).isTrue(); final CreateFilterResult<GuavaBloomFilter> secondCreateFilterResult = manager.createFilter(testingFilterName, config); assertThat(secondCreateFilterResult.isCreated()).isTrue(); Mockito.verify(mockedFactory, Mockito.times(2)).createFilter(config); }