BloomFilterExtractor.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.bloomfilter;

  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import java.util.Objects;
  22. import java.util.concurrent.atomic.AtomicReference;
  23. import java.util.function.BiPredicate;
  24. import java.util.function.Predicate;

  25. /**
  26.  * Produces Bloom filters from a collection (for example, {@link LayeredBloomFilter}).
  27.  *
  28.  * @since 4.5.0-M2
  29.  */
  30. @FunctionalInterface
  31. public interface BloomFilterExtractor {

  32.     /**
  33.      * Creates a BloomFilterExtractor from an array of Bloom filters.
  34.      *
  35.      * <ul>
  36.      * <li>The asBloomFilterArray() method returns a copy of the original array with references to the original filters.</li>
  37.      * <li>The forEachBloomFilterPair() method uses references to the original filters.</li>
  38.      * </ul>
  39.      * <p>
  40.      * <em>All modifications to the Bloom filters are reflected in the original filters</em>
  41.      * </p>
  42.      *
  43.      * @param <T>     The BloomFilter type.
  44.      * @param filters The filters to be returned by the extractor.
  45.      * @return THe BloomFilterExtractor containing the filters.
  46.      */
  47.     static <T extends BloomFilter<T>> BloomFilterExtractor fromBloomFilterArray(final BloomFilter<?>... filters) {
  48.         Objects.requireNonNull(filters, "filters");
  49.         return new BloomFilterExtractor() {

  50.             /**
  51.              * This implementation returns a copy the original array, the contained Bloom filters are references to the originals, any modifications to them are
  52.              * reflected in the original filters.
  53.              */
  54.             @Override
  55.             public BloomFilter[] asBloomFilterArray() {
  56.                 return filters.clone();
  57.             }

  58.             /**
  59.              * This implementation uses references to the original filters. Any modifications to the filters are reflected in the originals.
  60.              */
  61.             @Override
  62.             public boolean processBloomFilterPair(final BloomFilterExtractor other, final BiPredicate<BloomFilter, BloomFilter> func) {
  63.                 final CountingPredicate<BloomFilter> p = new CountingPredicate<>(filters, func);
  64.                 return other.processBloomFilters(p) && p.processRemaining();
  65.             }

  66.             @Override
  67.             public boolean processBloomFilters(final Predicate<BloomFilter> predicate) {
  68.                 return Arrays.stream(filters).allMatch(predicate);
  69.             }
  70.         };
  71.     }

  72.     /**
  73.      * Return an array of the Bloom filters in the collection.
  74.      * <p>
  75.      * <em>Implementations should specify if the array contains deep copies, immutable instances, or references to the filters in the collection.</em>
  76.      * </p>
  77.      * <p>
  78.      * The default method returns a deep copy of the enclosed filters.
  79.      * </p>
  80.      *
  81.      * @return An array of Bloom filters.
  82.      */
  83.     default BloomFilter[] asBloomFilterArray() {
  84.         final List<BloomFilter> filters = new ArrayList<>();
  85.         processBloomFilters(f -> filters.add(f.copy()));
  86.         return filters.toArray(new BloomFilter[0]);
  87.     }

  88.     /**
  89.      * Create a standard (non-layered) Bloom filter by merging all of the layers. If the filter is empty this method will return an empty Bloom filter.
  90.      *
  91.      * @return the merged bloom filter, never null.
  92.      * @throws NullPointerException if this call did not process any filters.
  93.      */
  94.     default BloomFilter flatten() {
  95.         final AtomicReference<BloomFilter> ref = new AtomicReference<>();
  96.         processBloomFilters(x -> {
  97.             if (ref.get() == null) {
  98.                 ref.set(new SimpleBloomFilter(x.getShape()));
  99.             }
  100.             return ref.get().merge(x);
  101.         });
  102.         return Objects.requireNonNull(ref.get(), "No filters.");
  103.     }

  104.     /**
  105.      * Applies the {@code func} to each Bloom filter pair in order. Will apply all of the Bloom filters from the other BloomFilterExtractor to this extractor.
  106.      * If either {@code this} extractor or {@code other} extractor has fewer BloomFilters the method will provide {@code null} for all excess calls to the
  107.      * {@code func}.
  108.      *
  109.      * <p>
  110.      * <em>This implementation returns references to the Bloom filter. Other implementations should specify if the array contains deep copies, immutable
  111.      * instances, or references to the filters in the collection.</em>
  112.      * </p>
  113.      *
  114.      * @param other The other BloomFilterExtractor that provides the y values in the (x,y) pair.
  115.      * @param func  The function to apply.
  116.      * @return {@code true} if the {@code func} returned {@code true} for every pair, {@code false} otherwise.
  117.      */
  118.     default boolean processBloomFilterPair(final BloomFilterExtractor other, final BiPredicate<BloomFilter, BloomFilter> func) {
  119.         final CountingPredicate<BloomFilter> p = new CountingPredicate<>(asBloomFilterArray(), func);
  120.         return other.processBloomFilters(p) && p.processRemaining();
  121.     }

  122.     /**
  123.      * Executes a Bloom filter Predicate on each Bloom filter in the collection. The ordering of the Bloom filters is not specified by this interface.
  124.      *
  125.      * @param bloomFilterPredicate the predicate to evaluate each Bloom filter with.
  126.      * @return {@code false} when the first filter fails the predicate test. Returns {@code true} if all filters pass the test.
  127.      */
  128.     boolean processBloomFilters(Predicate<BloomFilter> bloomFilterPredicate);
  129. }