You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/11/06 16:02:04 UTC
[commons-collections] 02/04: Use lambdas
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git
commit 62b1cc63546b155beec7b63af8d10bf6f160bfa3
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Nov 6 10:52:36 2022 -0500
Use lambdas
---
.../collections4/bloomfilter/IndexProducer.java | 37 ++++++++---------
.../bloomfilter/AbstractBitMapProducerTest.java | 46 ++++++----------------
.../AbstractCountingBloomFilterTest.java | 14 +++----
.../BitMapProducerFromIndexProducerTest.java | 19 ++-------
.../BitMapProducerFromLongArrayTest.java | 15 +++----
.../bloomfilter/DefaultBitCountProducerTest.java | 20 +++-------
.../bloomfilter/DefaultIndexProducerTest.java | 25 +++++-------
7 files changed, 58 insertions(+), 118 deletions(-)
diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/IndexProducer.java b/src/main/java/org/apache/commons/collections4/bloomfilter/IndexProducer.java
index 245488867..8668b81fe 100644
--- a/src/main/java/org/apache/commons/collections4/bloomfilter/IndexProducer.java
+++ b/src/main/java/org/apache/commons/collections4/bloomfilter/IndexProducer.java
@@ -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);
};
}
diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBitMapProducerTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBitMapProducerTest.java
index b6320c524..cd16036d5 100644
--- a/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBitMapProducerTest.java
+++ b/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBitMapProducerTest.java
@@ -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]);
diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractCountingBloomFilterTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractCountingBloomFilterTest.java
index 69a342894..e48044fda 100644
--- a/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractCountingBloomFilterTest.java
+++ b/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractCountingBloomFilterTest.java
@@ -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;
};
/**
diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapProducerFromIndexProducerTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapProducerFromIndexProducerTest.java
index 3a89b7113..87f2a0cfd 100644
--- a/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapProducerFromIndexProducerTest.java
+++ b/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapProducerFromIndexProducerTest.java
@@ -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);
}
diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapProducerFromLongArrayTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapProducerFromLongArrayTest.java
index b445e6eeb..912ccff05 100644
--- a/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapProducerFromLongArrayTest.java
+++ b/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapProducerFromLongArrayTest.java
@@ -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<>();
diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBitCountProducerTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBitCountProducerTest.java
index 28c2172e5..54a74ff51 100644
--- a/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBitCountProducerTest.java
+++ b/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBitCountProducerTest.java
@@ -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
diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultIndexProducerTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultIndexProducerTest.java
index 5c663300a..2682a96a7 100644
--- a/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultIndexProducerTest.java
+++ b/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultIndexProducerTest.java
@@ -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;
};
}