Skip to content

Commit

Permalink
Better lambdas
Browse files Browse the repository at this point in the history
Use final
Initialize Map when instantiating
Use diamonds
Use varargs
  • Loading branch information
garydgregory committed Sep 13, 2023
1 parent b776c25 commit c182f65
Show file tree
Hide file tree
Showing 34 changed files with 145 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ public int[] asIndexArray() {
}

@Override
public int getMaxInsert(CellProducer cellProducer) {
int[] max = {Integer.MAX_VALUE};
public int getMaxInsert(final CellProducer cellProducer) {
final int[] max = {Integer.MAX_VALUE};
cellProducer.forEachCell( (x, y) -> {
int count = cells[x] / y;
final int count = cells[x] / y;
if (count < max[0]) {
max[0] = count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ default boolean isFull() {
* @see Shape
*/
default int estimateN() {
double d = getShape().estimateN(cardinality());
final double d = getShape().estimateN(cardinality());
if (Double.isInfinite(d)) {
return Integer.MAX_VALUE;
}
if (Double.isNaN(d)) {
throw new IllegalArgumentException("Cardinality too large: " + cardinality());
}
long l = Math.round(d);
final long l = Math.round(d);
return l > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) l;
}

Expand Down Expand Up @@ -283,8 +283,8 @@ default int estimateUnion(final BloomFilter other) {
*/
default int estimateIntersection(final BloomFilter other) {
Objects.requireNonNull(other, "other");
double eThis = getShape().estimateN(cardinality());
double eOther = getShape().estimateN(other.cardinality());
final double eThis = getShape().estimateN(cardinality());
final double eOther = getShape().estimateN(other.cardinality());
if (Double.isInfinite(eThis) && Double.isInfinite(eOther)) {
// if both are infinite the union is infinite and we return Integer.MAX_VALUE
return Integer.MAX_VALUE;
Expand All @@ -296,9 +296,9 @@ default int estimateIntersection(final BloomFilter other) {
} else if (Double.isInfinite(eOther)) {
estimate = Math.round(eThis);
} else {
BloomFilter union = this.copy();
final BloomFilter union = this.copy();
union.merge(other);
double eUnion = getShape().estimateN(union.cardinality());
final double eUnion = getShape().estimateN(union.cardinality());
if (Double.isInfinite(eUnion)) {
throw new IllegalArgumentException("The estimated N for the union of the filters is infinite");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ static CellProducer from(final IndexProducer producer) {
private void populate() {
if (counterCells.isEmpty()) {
producer.forEachIndex( idx -> {
CounterCell cell = new CounterCell(idx, 1);
CounterCell counter = counterCells.get(cell);
final CounterCell cell = new CounterCell(idx, 1);
final CounterCell counter = counterCells.get(cell);
if (counter == null) {
counterCells.put(cell, cell);
} else {
Expand All @@ -114,9 +114,9 @@ public int[] asIndexArray() {
}

@Override
public boolean forEachCell(CellConsumer consumer) {
public boolean forEachCell(final CellConsumer consumer) {
populate();
for (CounterCell cell : counterCells.values()) {
for (final CounterCell cell : counterCells.values()) {
if (!consumer.test(cell.idx, cell.count)) {
return false;
}
Expand All @@ -131,13 +131,13 @@ final class CounterCell implements Comparable<CounterCell> {
final int idx;
int count;

CounterCell(int idx, int count) {
CounterCell(final int idx, final int count) {
this.idx = idx;
this.count = count;
}

@Override
public int compareTo(CounterCell other) {
public int compareTo(final CounterCell other) {
return Integer.compare(idx, other.idx);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public interface CountingBloomFilter extends BloomFilter, CellProducer {
* @param bloomFilter the Bloom filter the check for.
* @return the maximum number of times the Bloom filter could have been inserted.
*/
default int getMaxInsert(BloomFilter bloomFilter) {
default int getMaxInsert(final BloomFilter bloomFilter) {
return getMaxInsert((BitMapProducer) bloomFilter);
}

Expand All @@ -104,7 +104,7 @@ default int getMaxInsert(BloomFilter bloomFilter) {
* @return the maximum number of times the IndexProducer could have been inserted.
* @see #getMaxInsert(CellProducer)
*/
default int getMaxInsert(IndexProducer idxProducer) {
default int getMaxInsert(final IndexProducer idxProducer) {
return getMaxInsert(CellProducer.from(idxProducer.uniqueIndices()) );
}

Expand All @@ -121,7 +121,7 @@ default int getMaxInsert(IndexProducer idxProducer) {
* @param hasher the Hasher to provide the indices.
* @return the maximum number of times the hasher could have been inserted.
*/
default int getMaxInsert(Hasher hasher) {
default int getMaxInsert(final Hasher hasher) {
return getMaxInsert(hasher.indices(getShape()));
}

Expand All @@ -131,12 +131,12 @@ default int getMaxInsert(Hasher hasher) {
* @param bitMapProducer the BitMapProducer to provide the indices.
* @return the maximum number of times the BitMapProducer could have been inserted.
*/
default int getMaxInsert(BitMapProducer bitMapProducer) {
default int getMaxInsert(final BitMapProducer bitMapProducer) {
if (!contains(bitMapProducer)) {
return 0;
}
long[] bitMaps = bitMapProducer.asBitMapArray();
int[] max = { Integer.MAX_VALUE };
final long[] bitMaps = bitMapProducer.asBitMapArray();
final int[] max = { Integer.MAX_VALUE };
forEachCell((x, y) -> {
if ((bitMaps[BitMap.getLongIndex(x)] & BitMap.getLongBit(x)) != 0) {
max[0] = max[0] <= y ? max[0] : y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int[] toArray() {
return size == data.length ? data : Arrays.copyOf(data, size);
}
}
Indices indices = new Indices();
final Indices indices = new Indices();
forEachIndex(indices::add);
return indices.toArray();
}
Expand All @@ -158,7 +158,7 @@ default IndexProducer uniqueIndices() {

return new IndexProducer() {
@Override
public boolean forEachIndex(IntPredicate predicate) {
public boolean forEachIndex(final IntPredicate predicate) {
for (int idx = bitSet.nextSetBit(0); idx >= 0; idx = bitSet.nextSetBit(idx + 1)) {
if (!predicate.test(idx)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private IndexUtils() {}
* @param index the index to add at.
* @return the array or a newly allocated copy of the array.
*/
static int[] ensureCapacityForAdd(int[] array, int index) {
static int[] ensureCapacityForAdd(final int[] array, final int index) {
if (index >= array.length) {
return Arrays.copyOf(array, (int) Math.min(IndexUtils.MAX_ARRAY_SIZE, Math.max(array.length * 2L, index + 1)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public class BulkTest implements Cloneable {
/**
* the name of the simple test method
*/
private String name;
private final String name;

/**
* Constructs a new {@code BulkTest} instance that will run the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,7 @@ public void get() {
assertEquals(2, CollectionUtils.get((Object) collectionA.iterator(), 2));
final Map<Integer, Integer> map = CollectionUtils.getCardinalityMap(collectionA);
// Test assumes a defined iteration order so convert to a LinkedHashMap
final Map<Integer, Integer> linkedMap = new LinkedHashMap<>();
linkedMap.putAll(map);
final Map<Integer, Integer> linkedMap = new LinkedHashMap<>(map);
assertEquals(linkedMap.entrySet().iterator().next(), CollectionUtils.get((Object) linkedMap, 0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public void testPartition() {

@Test
public void testPredicatedList() {
final Predicate<Object> predicate = o -> o instanceof String;
final Predicate<Object> predicate = String.class::isInstance;
final List<Object> list = ListUtils.predicatedList(new ArrayList<>(), predicate);
assertTrue(list instanceof PredicatedList, "returned object should be a PredicatedList");
assertAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class MapUtilsTest {
private static final String TWO = "Two";

public Predicate<Object> getPredicate() {
return o -> o instanceof String;
return String.class::isInstance;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void testNewIdentityHashSet() {

@Test
public void testpredicatedSet() {
final Predicate<Object> predicate = o -> o instanceof String;
final Predicate<Object> predicate = String.class::isInstance;
final Set<Object> set = SetUtils.predicatedSet(new HashSet<>(), predicate);
assertTrue(set instanceof PredicatedSet, "returned object should be a PredicatedSet");
assertAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public PredicatedBagTest() {
}

protected Predicate<T> stringPredicate() {
return o -> o instanceof String;
return String.class::isInstance;
}

protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public PredicatedSortedBagTest() {
}

protected Predicate<T> stringPredicate() {
return o -> o instanceof String;
return String.class::isInstance;
}

protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,12 @@ public final void testForEachBitMapPair() {

// test where the created producer does not process all records because the predicate function
// returns false before the processing is completed.
int[] limit = new int[1];
final int[] limit = new int[1];
final LongBiPredicate shortFunc = (x, y) -> {
limit[0]++;
return limit[0] < 2;
};
final BitMapProducer shortProducer = l -> {
return true;
};
final BitMapProducer shortProducer = l -> true;
assertFalse(createProducer().forEachBitMapPair(shortProducer, shortFunc));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void testMergeWithHasher() {

@Test
public void testMergeWithBitMapProducer() {
int bitMapCount = BitMap.numberOfBitMaps(getTestShape().getNumberOfBits());
final int bitMapCount = BitMap.numberOfBitMaps(getTestShape().getNumberOfBits());
for (int i = 0; i < 5; i++) {
final long[] values = new long[bitMapCount];
for (final int idx : DefaultIndexProducerTest.generateIntArray(getTestShape().getNumberOfHashFunctions(), getTestShape().getNumberOfBits())) {
Expand All @@ -134,7 +134,7 @@ public void testMergeWithBitMapProducer() {
assertTrue(lst.isEmpty());
}
// values too large
long[] values = new long[bitMapCount];
final long[] values = new long[bitMapCount];
Arrays.fill(values, Long.MAX_VALUE);
final BitMapProducer badProducer = BitMapProducer.fromBitMapArray(values);
final BloomFilter bf = createEmptyFilter(getTestShape());
Expand Down Expand Up @@ -162,11 +162,11 @@ public void testMergeWithIndexProducer() {
// value to large
final BloomFilter f1 = createEmptyFilter(getTestShape());
assertThrows(IllegalArgumentException.class,
() -> f1.merge(IndexProducer.fromIndexArray(new int[] {getTestShape().getNumberOfBits()})));
() -> f1.merge(IndexProducer.fromIndexArray(getTestShape().getNumberOfBits())));
// negative value
final BloomFilter f2 = createEmptyFilter(getTestShape());
assertThrows(IllegalArgumentException.class,
() -> f2.merge(IndexProducer.fromIndexArray(new int[] {-1})));
() -> f2.merge(IndexProducer.fromIndexArray(-1)));
}

@Test
Expand Down Expand Up @@ -218,12 +218,12 @@ public void testClear() {

@Test
public final void testNegativeIntersection() {
IndexProducer p1 = IndexProducer.fromIndexArray(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 20, 26, 28, 30, 32, 34, 35, 36, 37, 39, 40, 41, 42, 43, 45, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71);
IndexProducer p2 = IndexProducer.fromIndexArray(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
final IndexProducer p1 = IndexProducer.fromIndexArray(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 20, 26, 28, 30, 32, 34, 35, 36, 37, 39, 40, 41, 42, 43, 45, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71);
final IndexProducer p2 = IndexProducer.fromIndexArray(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);

BloomFilter filter1 = createEmptyFilter(Shape.fromKM(17, 72));
final BloomFilter filter1 = createEmptyFilter(Shape.fromKM(17, 72));
filter1.merge(p1);
BloomFilter filter2 = createEmptyFilter(Shape.fromKM(17, 72));
final BloomFilter filter2 = createEmptyFilter(Shape.fromKM(17, 72));
filter2.merge(p2);
assertEquals(0, filter1.estimateIntersection(filter2));
}
Expand All @@ -249,9 +249,9 @@ public final void testEstimateIntersection() {
assertEquals(0, bf.estimateIntersection(bf4));
assertEquals(0, bf4.estimateIntersection(bf));

int midPoint = getTestShape().getNumberOfBits() / 2;
BloomFilter bf5 = TestingHashers.populateRange(createEmptyFilter(getTestShape()), 0, midPoint);
BloomFilter bf6 = TestingHashers.populateRange(createEmptyFilter(getTestShape()), midPoint+1, getTestShape().getNumberOfBits()-1);
final int midPoint = getTestShape().getNumberOfBits() / 2;
final BloomFilter bf5 = TestingHashers.populateRange(createEmptyFilter(getTestShape()), 0, midPoint);
final BloomFilter bf6 = TestingHashers.populateRange(createEmptyFilter(getTestShape()), midPoint+1, getTestShape().getNumberOfBits()-1);
assertThrows(IllegalArgumentException.class, () -> bf5.estimateIntersection(bf6));

// infinite with infinite
Expand Down Expand Up @@ -371,8 +371,8 @@ public final void testMerge() {
assertThrows(IllegalArgumentException.class, () -> bf1.merge(new BadHasher(-1)));

// test error when bloom filter returns values out of range
Shape s = Shape.fromKM(getTestShape().getNumberOfHashFunctions(), getTestShape().getNumberOfBits() * 3);
Hasher h = new IncrementingHasher(getTestShape().getNumberOfBits() * 2, 1);
final Shape s = Shape.fromKM(getTestShape().getNumberOfHashFunctions(), getTestShape().getNumberOfBits() * 3);
final Hasher h = new IncrementingHasher(getTestShape().getNumberOfBits() * 2, 1);
final BloomFilter bf5 = new SimpleBloomFilter(s);
bf5.merge(h);
assertThrows(IllegalArgumentException.class, () -> bf1.merge(bf5));
Expand Down Expand Up @@ -441,7 +441,7 @@ public static class BadHasher implements Hasher {
IndexProducer producer;

public BadHasher(final int value) {
this.producer = IndexProducer.fromIndexArray(new int[] {value});
this.producer = IndexProducer.fromIndexArray(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ public final void testIndexConsistency() {

@Test
public void testForEachCellValues() {
int[] expectedIdx = getExpectedIndices();
int[] expectedValue = getExpectedValues();
final int[] expectedIdx = getExpectedIndices();
final int[] expectedValue = getExpectedValues();
assertEquals(expectedIdx.length, expectedValue.length, "expected index length and value length do not match");
int[] idx = {0};
final int[] idx = {0};
createProducer().forEachCell((i, j) -> {
assertEquals(expectedIdx[idx[0]], i, "bad index at " + idx[0]);
assertEquals(expectedValue[idx[0]], j, "bad value at " + idx[0]);
Expand Down
Loading

0 comments on commit c182f65

Please sign in to comment.