View Javadoc
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  
19  import java.util.concurrent.ThreadLocalRandom;
20  
21  /**
22   * A collection of methods and statics that represent standard hashers in testing.
23   */
24  public class TestingHashers {
25      /**
26       * Hasher that increments from 1.
27       */
28      public static final Hasher FROM1 = new IncrementingHasher(1, 1);
29  
30      /**
31       * Hasher that increments from 11.
32       */
33      public static final Hasher FROM11 = new IncrementingHasher(11, 1);
34  
35      /**
36       * Merge several Hashers together into a single Bloom filter.
37       * @param <T> The type of bloom filter.
38       * @param filter The Bloom filter to populate
39       * @param hashers The hashers to merge
40       * @return {@code filter} for chaining
41       */
42      public static <T extends BloomFilter> T mergeHashers(final T filter, final Hasher... hashers) {
43          for (final Hasher h : hashers) {
44              filter.merge(h);
45          }
46          return filter;
47      }
48  
49      /**
50       * Enables all bits in the filter.
51       * @param <T> the Bloom filter type.
52       * @param filter the Bloom filter to populate
53       * @return {@code filter} for chaining
54       */
55      public static <T extends BloomFilter> T populateEntireFilter(final T filter) {
56          return populateRange(filter, 0, filter.getShape().getNumberOfBits() - 1);
57      }
58  
59      /**
60       * Merge {@code from1} and {@code from11} into a single Bloom filter.
61       * @param <T> The type of bloom filter.
62       * @param filter The Bloom filter to populate
63       * @return {@code filter} for chaining
64       */
65      public static <T extends BloomFilter> T populateFromHashersFrom1AndFrom11(final T filter) {
66          return mergeHashers(filter, FROM1, FROM11);
67      }
68  
69      /**
70       * Enables all bits in a range (inclusive).
71       * @param <T> the Bloom filter type.
72       * @param filter the Bloom filter to populate
73       * @param start the starting bit to enable.
74       * @param end the last bit to enable.
75       * @return {@code filter} for chaining
76       */
77      public static <T extends BloomFilter> T populateRange(final T filter, final int start, final int end) {
78          filter.merge((IndexProducer) p -> {
79              for (int i = start; i <= end; i++) {
80                  if (!p.test(i)) {
81                      return false;
82                  }
83              }
84              return true;
85          });
86          return filter;
87      }
88  
89      /**
90       * Creates an EnhancedDoubleHasher hasher from 2 random longs.
91       */
92      public static Hasher randomHasher() {
93          return new EnhancedDoubleHasher( ThreadLocalRandom.current().nextLong(), ThreadLocalRandom.current().nextLong() );
94      }
95  
96      /**
97       * Do not instantiate.
98       */
99      private TestingHashers() {}
100 }