Use lambdas

This commit is contained in:
Gary Gregory 2022-11-06 10:52:36 -05:00
parent 8e4c50f928
commit 62b1cc6354
7 changed files with 58 additions and 118 deletions

View File

@ -79,30 +79,27 @@ public interface IndexProducer {
*/
static IndexProducer fromBitMapProducer(final BitMapProducer producer) {
Objects.requireNonNull(producer, "producer");
return new IndexProducer() {
@Override
public boolean forEachIndex(final IntPredicate consumer) {
final LongPredicate longPredicate = new LongPredicate() {
int wordIdx = 0;
return consumer -> {
final LongPredicate longPredicate = new LongPredicate() {
int wordIdx = 0;
@Override
public boolean test(long word) {
int i = wordIdx;
while (word != 0) {
if ((word & 1) == 1) {
if (!consumer.test(i)) {
return false;
}
@Override
public boolean test(long word) {
int i = wordIdx;
while (word != 0) {
if ((word & 1) == 1) {
if (!consumer.test(i)) {
return false;
}
word >>>= 1;
i++;
}
wordIdx += 64;
return true;
word >>>= 1;
i++;
}
};
return producer.forEachBitMap(longPredicate::test);
}
wordIdx += 64;
return true;
}
};
return producer.forEachBitMap(longPredicate::test);
};
}

View File

@ -30,24 +30,12 @@ public abstract class AbstractBitMapProducerTest {
/**
* A testing consumer that always returns false.
*/
public static final LongPredicate FALSE_CONSUMER = new LongPredicate() {
@Override
public boolean test(final long arg0) {
return false;
}
};
public static final LongPredicate FALSE_CONSUMER = arg0 -> false;
/**
* A testing consumer that always returns true.
*/
public static final LongPredicate TRUE_CONSUMER = new LongPredicate() {
@Override
public boolean test(final long arg0) {
return true;
}
};
public static final LongPredicate TRUE_CONSUMER = arg0 -> true;
/**
* Creates a producer with some data.
@ -99,19 +87,15 @@ public abstract class AbstractBitMapProducerTest {
// test BitMapProducers of different length send 0 for missing values.
final int[] count = new int[3];
final LongBiPredicate lbp = new LongBiPredicate() {
@Override
public boolean test(final long x, final long y) {
if (x == 0) {
count[0]++;
}
if (y == 0) {
count[1]++;
}
count[2]++;
return true;
final LongBiPredicate lbp = (x, y) -> {
if (x == 0) {
count[0]++;
}
if (y == 0) {
count[1]++;
}
count[2]++;
return true;
};
createEmptyProducer().forEachBitMapPair(createProducer(), lbp);
assertEquals(count[2], count[0]);
@ -151,13 +135,9 @@ public abstract class AbstractBitMapProducerTest {
// test BitMapProducers of different length send 0 for missing values.
final int[] count = new int[1];
final LongBiPredicate lbp = new LongBiPredicate() {
@Override
public boolean test(final long x, final long y) {
count[0]++;
return false;
}
final LongBiPredicate lbp = (x, y) -> {
count[0]++;
return false;
};
createProducer().forEachBitMapPair(createEmptyProducer(), lbp);
assertEquals(1, count[0]);

View File

@ -37,17 +37,13 @@ public abstract class AbstractCountingBloomFilterTest<T extends CountingBloomFil
protected int[] bigHashCounts = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0};
protected final BitCountProducer maximumValueProducer = new BitCountProducer() {
@Override
public boolean forEachCount(final BitCountProducer.BitCountConsumer consumer) {
for (int i = 1; i < 18; i++) {
if (!consumer.test(i, Integer.MAX_VALUE)) {
return false;
}
protected final BitCountProducer maximumValueProducer = consumer -> {
for (int i = 1; i < 18; i++) {
if (!consumer.test(i, Integer.MAX_VALUE)) {
return false;
}
return true;
}
return true;
};
/**

View File

@ -20,7 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.function.IntPredicate;
import org.junit.jupiter.api.Test;
@ -28,26 +27,14 @@ public class BitMapProducerFromIndexProducerTest extends AbstractBitMapProducerT
@Override
protected BitMapProducer createProducer() {
final IndexProducer iProducer = new IndexProducer() {
@Override
public boolean forEachIndex(final IntPredicate consumer) {
return consumer.test(0) && consumer.test(1) && consumer.test(63) && consumer.test(64)
&& consumer.test(127) && consumer.test(128);
}
};
final IndexProducer iProducer = consumer -> consumer.test(0) && consumer.test(1) && consumer.test(63) && consumer.test(64)
&& consumer.test(127) && consumer.test(128);
return BitMapProducer.fromIndexProducer(iProducer, 200);
}
@Override
protected BitMapProducer createEmptyProducer() {
final IndexProducer iProducer = new IndexProducer() {
@Override
public boolean forEachIndex(final IntPredicate consumer) {
return true;
}
};
final IndexProducer iProducer = consumer -> true;
return BitMapProducer.fromIndexProducer(iProducer, 200);
}

View File

@ -20,7 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
import java.util.function.IntPredicate;
import org.junit.jupiter.api.Test;
@ -56,17 +55,13 @@ public class BitMapProducerFromLongArrayTest extends AbstractBitMapProducerTest
@Test
public void testFromIndexProducer() {
final int limit = Integer.SIZE + Long.SIZE;
final IndexProducer iProducer = new IndexProducer() {
@Override
public boolean forEachIndex(final IntPredicate consumer) {
for (int i = 0; i < limit; i++) {
if (!consumer.test(i)) {
return false;
}
final IndexProducer iProducer = consumer -> {
for (int i = 0; i < limit; i++) {
if (!consumer.test(i)) {
return false;
}
return true;
}
return true;
};
final BitMapProducer producer = BitMapProducer.fromIndexProducer(iProducer, limit);
final List<Long> lst = new ArrayList<>();

View File

@ -28,27 +28,19 @@ public class DefaultBitCountProducerTest extends AbstractBitCountProducerTest {
@Override
protected BitCountProducer createProducer() {
return new BitCountProducer() {
@Override
public boolean forEachCount(final BitCountConsumer consumer) {
for (final int i : values) {
if (!consumer.test(i, 1)) {
return false;
}
return consumer -> {
for (final int i : values) {
if (!consumer.test(i, 1)) {
return false;
}
return true;
}
return true;
};
}
@Override
protected BitCountProducer createEmptyProducer() {
return new BitCountProducer() {
@Override
public boolean forEachCount(final BitCountConsumer consumer) {
return true;
}
};
return consumer -> true;
}
@Override

View File

@ -22,7 +22,6 @@ import java.util.Arrays;
import java.util.BitSet;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.IntPredicate;
import org.junit.jupiter.api.Test;
@ -38,28 +37,22 @@ public class DefaultIndexProducerTest extends AbstractIndexProducerTest {
@Override
protected IndexProducer createProducer() {
return new IndexProducer() {
@Override
public boolean forEachIndex(final IntPredicate predicate) {
Objects.requireNonNull(predicate);
for (final int i : values) {
if (!predicate.test(i)) {
return false;
}
return predicate -> {
Objects.requireNonNull(predicate);
for (final int i : values) {
if (!predicate.test(i)) {
return false;
}
return true;
}
return true;
};
}
@Override
protected IndexProducer createEmptyProducer() {
return new IndexProducer() {
@Override
public boolean forEachIndex(final IntPredicate predicate) {
Objects.requireNonNull(predicate);
return true;
}
return predicate -> {
Objects.requireNonNull(predicate);
return true;
};
}