Made IndexFilter.create() return an IntPredicate
This commit is contained in:
parent
a89f3ec293
commit
711ea22488
|
@ -26,7 +26,7 @@ import java.util.function.IntPredicate;
|
|||
* <p>This is conceptually a unique filter implemented as a {@code IntPredicate}.</p>
|
||||
* @since 4.5
|
||||
*/
|
||||
public final class IndexFilter implements IntPredicate {
|
||||
public final class IndexFilter {
|
||||
private final IntPredicate tracker;
|
||||
private final int size;
|
||||
private final IntPredicate consumer;
|
||||
|
@ -37,8 +37,8 @@ public final class IndexFilter implements IntPredicate {
|
|||
* @param consumer The consumer to accept the values.
|
||||
* @return an IndexFilter optimized for the specified shape.
|
||||
*/
|
||||
public static IndexFilter create(Shape shape, IntPredicate consumer) {
|
||||
return new IndexFilter(shape, consumer);
|
||||
public static IntPredicate create(Shape shape, IntPredicate consumer) {
|
||||
return new IndexFilter(shape, consumer)::test;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,7 +68,6 @@ public final class IndexFilter implements IntPredicate {
|
|||
* @param number the number to check.
|
||||
* @return {@code true} if processing should continue, {@code false} otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean test(int number) {
|
||||
if (number >= size) {
|
||||
throw new IndexOutOfBoundsException(String.format("number too large %d >= %d", number, size));
|
||||
|
|
|
@ -173,7 +173,7 @@ public class SimpleHasher implements Hasher {
|
|||
@Override
|
||||
public boolean forEachIndex(IntPredicate consumer) {
|
||||
Objects.requireNonNull(consumer, "consumer");
|
||||
IndexFilter filter = IndexFilter.create(shape, consumer);
|
||||
IntPredicate filter = IndexFilter.create(shape, consumer);
|
||||
|
||||
int bits = shape.getNumberOfBits();
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ import java.util.BitSet;
|
|||
import java.util.List;
|
||||
import java.util.SplittableRandom;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.function.IntPredicate;
|
||||
|
||||
import org.apache.commons.collections4.bloomfilter.IndexFilter.ArrayTracker;
|
||||
import org.apache.commons.collections4.bloomfilter.IndexFilter.BitMapTracker;
|
||||
|
@ -43,7 +44,7 @@ public class IndexFilterTest {
|
|||
public void testFiltering() {
|
||||
Shape shape = Shape.fromKM(3, 12);
|
||||
List<Integer> consumer = new ArrayList<Integer>();
|
||||
IndexFilter filter = IndexFilter.create(shape, consumer::add);
|
||||
IntPredicate filter = IndexFilter.create(shape, consumer::add);
|
||||
|
||||
for (int i = 0; i < 12; i++) {
|
||||
assertTrue(filter.test(i));
|
||||
|
@ -64,7 +65,7 @@ public class IndexFilterTest {
|
|||
for (int n = 0; n < 10; n++) {
|
||||
used.clear();
|
||||
List<Integer> consumer = new ArrayList<>();
|
||||
IndexFilter filter = IndexFilter.create(shape, consumer::add);
|
||||
IntPredicate filter = IndexFilter.create(shape, consumer::add);
|
||||
|
||||
// Make random indices; these may be duplicates
|
||||
long seed = ThreadLocalRandom.current().nextLong();
|
||||
|
@ -92,46 +93,4 @@ public class IndexFilterTest {
|
|||
assertThrows(IndexOutOfBoundsException.class, () -> filter.test(-1));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor()
|
||||
throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
|
||||
Field tracker = IndexFilter.class.getDeclaredField("tracker");
|
||||
tracker.setAccessible(true);
|
||||
List<Integer> consumer = new ArrayList<Integer>();
|
||||
|
||||
// test even split
|
||||
int k = 2;
|
||||
int m = Long.SIZE;
|
||||
Shape shape = Shape.fromKM(k, m);
|
||||
IndexFilter filter = IndexFilter.create(shape, consumer::add);
|
||||
assertTrue(tracker.get(filter) instanceof ArrayTracker);
|
||||
|
||||
// test k ints < longs for m
|
||||
k = 1;
|
||||
shape = Shape.fromKM(k, m);
|
||||
filter = IndexFilter.create(shape, consumer::add);
|
||||
assertTrue(tracker.get(filter) instanceof ArrayTracker);
|
||||
|
||||
// test k ints > longs for m
|
||||
k = 3;
|
||||
shape = Shape.fromKM(k, m);
|
||||
filter = IndexFilter.create(shape, consumer::add);
|
||||
assertTrue(tracker.get(filter) instanceof BitMapTracker);
|
||||
|
||||
/* test overflows */
|
||||
shape = Shape.fromKM(2, Integer.MAX_VALUE);
|
||||
filter = IndexFilter.create(shape, consumer::add);
|
||||
assertTrue(tracker.get(filter) instanceof ArrayTracker);
|
||||
|
||||
// overflow when computing the storage of the int array
|
||||
shape = Shape.fromKM(Integer.MAX_VALUE, 123);
|
||||
filter = IndexFilter.create(shape, consumer::add);
|
||||
// *** fails ***
|
||||
assertTrue(tracker.get(filter) instanceof BitMapTracker);
|
||||
|
||||
shape = Shape.fromKM(Integer.MAX_VALUE, Integer.MAX_VALUE);
|
||||
filter = IndexFilter.create(shape, consumer::add);
|
||||
assertTrue(tracker.get(filter) instanceof BitMapTracker);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue