Use the stock JRE Objects.requireNonNull() for parameter validation.

Formatting. Javadoc.
This commit is contained in:
Gary Gregory 2020-02-16 15:39:57 -05:00
parent a1ce1c2121
commit 87497d0fa1
18 changed files with 497 additions and 543 deletions

View File

@ -88,14 +88,14 @@ public interface BloomFilter {
Shape getShape();
/**
* Merge the other Bloom filter into this one.
* Merges the other Bloom filter into this one.
*
* @param other the other Bloom filter.
*/
void merge(BloomFilter other);
/**
* Merge the decomposed Bloom filter defined by the hasher into this Bloom
* Merges the decomposed Bloom filter defined by the hasher into this Bloom
* filter. The hasher provides an iterator of bit indexes to enable.
*
* @param hasher the hasher to provide the indexes.

View File

@ -47,7 +47,7 @@ import org.apache.commons.collections4.bloomfilter.hasher.StaticHasher;
public class CountingBloomFilter extends AbstractBloomFilter {
/**
* the count of entries. Each enabled bit is a key with the count for that bit
* The count of entries. Each enabled bit is a key with the count for that bit
* being the value. Entries with a value of zero are removed.
*/
private final TreeMap<Integer, Integer> counts;
@ -76,21 +76,18 @@ public class CountingBloomFilter extends AbstractBloomFilter {
public CountingBloomFilter(final Map<Integer, Integer> counts, final Shape shape) {
this(shape);
counts.entrySet().stream().forEach(e -> {
if (e.getKey() >= shape.getNumberOfBits())
{
throw new IllegalArgumentException( "dataMap has an item with an index larger than "+
(shape.getNumberOfBits()-1) );
}
else if (e.getKey() < 0)
{
if (e.getKey() >= shape.getNumberOfBits()) {
throw new IllegalArgumentException(
"dataMap has an item with an index larger than " + (shape.getNumberOfBits() - 1));
} else if (e.getKey() < 0) {
throw new IllegalArgumentException("dataMap has an item with an index less than 0");
}
if (e.getValue() < 0) {
throw new IllegalArgumentException("dataMap has an item with an value less than 0");
} else if (e.getValue() > 0)
{
} else if (e.getValue() > 0) {
this.counts.put(e.getKey(), e.getValue());
}});
}
});
}
/**
@ -167,8 +164,7 @@ public class CountingBloomFilter extends AbstractBloomFilter {
@Override
public void merge(final BloomFilter other) {
verifyShape(other);
if (other instanceof CountingBloomFilter)
{
if (other instanceof CountingBloomFilter) {
merge(((CountingBloomFilter) other).counts.keySet().iterator());
} else {
merge(BitSet.valueOf(other.getBits()).stream().iterator());
@ -182,7 +178,7 @@ public class CountingBloomFilter extends AbstractBloomFilter {
}
/**
* Merge an iterator of set bits into this filter.
* Merges an iterator of set bits into this filter.
* @param iter the iterator of bits to set.
*/
private void merge(final Iterator<Integer> iter) {
@ -199,7 +195,7 @@ public class CountingBloomFilter extends AbstractBloomFilter {
}
/**
* Decrement the counts for the bits that are on in the other BloomFilter from this
* Decrements the counts for the bits that are on in the other BloomFilter from this
* one.
*
* <p>
@ -210,8 +206,7 @@ public class CountingBloomFilter extends AbstractBloomFilter {
*/
public void remove(final BloomFilter other) {
verifyShape(other);
if (other instanceof CountingBloomFilter)
{
if (other instanceof CountingBloomFilter) {
remove(((CountingBloomFilter) other).counts.keySet().stream());
} else {
remove(BitSet.valueOf(other.getBits()).stream().boxed());
@ -219,7 +214,7 @@ public class CountingBloomFilter extends AbstractBloomFilter {
}
/**
* Decrement the counts for the bits that are on in the hasher from this
* Decrements the counts for the bits that are on in the hasher from this
* Bloom filter.
*
* <p>

View File

@ -52,7 +52,6 @@ public final class SetOperations {
public static double cosineSimilarity(final BloomFilter first, final BloomFilter second) {
verifyShape(first, second);
final int numerator = first.andCardinality(second);
return numerator == 0 ? 0 : numerator / (Math.sqrt(first.cardinality()) * Math.sqrt(second.cardinality()));
}

View File

@ -34,6 +34,7 @@ public class DynamicHasher implements Hasher {
* @since 4.5
*/
public static class Builder implements Hasher.Builder {
/**
* The list of byte[] that are to be hashed.
*/
@ -92,7 +93,7 @@ public class DynamicHasher implements Hasher {
private final Shape shape;
/**
* Creates iterator with the specified shape.
* Constructs iterator with the specified shape.
*
* @param shape
*/

View File

@ -83,7 +83,7 @@ public interface HashFunctionIdentity {
};
/**
* Get a common formatted string for general display.
* Gets a common formatted string for general display.
*
* @param identity the identity to format.
* @return the String representing the identity.
@ -93,7 +93,7 @@ public interface HashFunctionIdentity {
}
/**
* Get the signature buffer for a HashFunctionIdentity.
* Gets the signature buffer for a HashFunctionIdentity.
* <p>
* The signature of this function is calculated as:
* {@code
@ -141,7 +141,7 @@ public interface HashFunctionIdentity {
String getProvider();
/**
* Get the signature of this function. <p> The signature of this function is
* Gets the signature of this function. <p> The signature of this function is
* calculated as: {@code
* apply( String.format( "%s-%s-%s", getName(), getSignedness(), getProcess() )
* .getBytes( "UTF-8" ), 0 );

View File

@ -59,6 +59,7 @@ public final class HashFunctionIdentityImpl implements HashFunctionIdentity {
this.process = process;
this.signature = signature;
}
@Override
public String getName() {
return name;

View File

@ -38,8 +38,9 @@ public interface Hasher {
* @since 4.5
*/
interface Builder {
/**
* Build the hasher.
* Builds the hasher.
* @return the fully constructed hasher.
*/
Hasher build();
@ -78,7 +79,7 @@ public interface Hasher {
}
/**
* Return an iterator of integers that are the bits to enable in the Bloom
* Gets an iterator of integers that are the bits to enable in the Bloom
* filter based on the shape. No guarantee is made as to order
* or duplication of values.
*
@ -98,6 +99,7 @@ public interface Hasher {
/**
* Returns true if the hasher specifies no bits.
*
* @return true if the hasher does not specify any bits.
*/
boolean isEmpty();

View File

@ -82,7 +82,7 @@ public class Shape {
private final HashFunctionIdentity hashFunctionIdentity;
/**
* Create a filter configuration with the specified number of items and
* Constructs a filter configuration with the specified number of items and
* probability.
*
* @param hashFunctionIdentity The HashFunctionIdentity of the hash function this shape uses.
@ -93,9 +93,7 @@ public class Shape {
*/
public Shape(final HashFunctionIdentity hashFunctionIdentity, final double probability, final int numberOfBits,
final int numberOfHashFunctions) {
if (hashFunctionIdentity == null) {
throw new IllegalArgumentException("Hash function name may not be null");
}
Objects.requireNonNull(hashFunctionIdentity, "hashFunctionIdentity");
if (probability <= 0.0) {
throw new IllegalArgumentException("Probability must be greater than 0.0");
}
@ -133,7 +131,7 @@ public class Shape {
}
/**
* Create a filter configuration with the specified number of items and
* Constructs a filter configuration with the specified number of items and
* probability. <p> The actual probability will be approximately equal to the
* desired probability but will be dependent upon the calculated bloom filter size
* and function count. </p>
@ -144,9 +142,7 @@ public class Shape {
* (0.0,1.0).
*/
public Shape(final HashFunctionIdentity hashFunctionIdentity, final int numberOfItems, final double probability) {
if (hashFunctionIdentity == null) {
throw new IllegalArgumentException("Hash function identity may not be null");
}
Objects.requireNonNull(hashFunctionIdentity, "hashFunctionIdentity");
if (numberOfItems < 1) {
throw new IllegalArgumentException("Number of Items must be greater than 0");
}
@ -175,7 +171,7 @@ public class Shape {
}
/**
* Create a filter configuration with the specified number of items and
* Constructs a filter configuration with the specified number of items and
* probability.
*
* @param hashFunctionIdentity The HashFunctionIdentity of the hash function this shape uses.
@ -183,9 +179,7 @@ public class Shape {
* @param numberOfBits The number of bits in the filter.
*/
public Shape(final HashFunctionIdentity hashFunctionIdentity, final int numberOfItems, final int numberOfBits) {
if (hashFunctionIdentity == null) {
throw new IllegalArgumentException("Hash function name may not be null");
}
Objects.requireNonNull(hashFunctionIdentity, "hashFunctionIdentity");
if (numberOfItems < 1) {
throw new IllegalArgumentException("Number of Items must be greater than 0");
}
@ -203,7 +197,7 @@ public class Shape {
}
/**
* Create a filter configuration with the specified number of items and
* Constructs a filter configuration with the specified number of items and
* probability.
*
* @param hashFunctionIdentity The HashFunctionIdentity of the hash function this shape uses.
@ -213,9 +207,7 @@ public class Shape {
*/
public Shape(final HashFunctionIdentity hashFunctionIdentity, final int numberOfItems, final int numberOfBits,
final int numberOfHashFunctions) {
if (hashFunctionIdentity == null) {
throw new IllegalArgumentException("Hash function name may not be null");
}
Objects.requireNonNull(hashFunctionIdentity, "hashFunctionIdentity");
if (numberOfItems < 1) {
throw new IllegalArgumentException("Number of Items must be greater than 0");
}

View File

@ -34,6 +34,7 @@ public final class StaticHasher implements Hasher {
* The shape of this hasher
*/
private final Shape shape;
/**
* The ordered set of values that this hasher will return.
*/
@ -95,7 +96,7 @@ public final class StaticHasher implements Hasher {
}
/**
* Returns an iterator of integers that are the bits to enable in the Bloom
* Gets an iterator of integers that are the bits to enable in the Bloom
* filter based on the shape. The iterator will not return the same value multiple
* times. Values will be returned in ascending order.
*

View File

@ -30,6 +30,7 @@ import org.apache.commons.collections4.bloomfilter.hasher.HashFunctionIdentity;
* @since 4.5
*/
public final class Murmur128x86Cyclic implements HashFunction {
/**
* The name of this hash method.
*/

View File

@ -58,7 +58,6 @@ public class BitSetBloomFilterTest extends AbstractBloomFilterTest {
assertEquals(0, bf.andCardinality(bf2));
assertEquals(0, bf2.andCardinality(bf));
}
@Override
@ -90,7 +89,6 @@ public class BitSetBloomFilterTest extends AbstractBloomFilterTest {
assertEquals(27, bf.cardinality());
}
/**
@ -119,8 +117,6 @@ public class BitSetBloomFilterTest extends AbstractBloomFilterTest {
assertEquals(15, bf.xorCardinality(bf2));
assertEquals(15, bf2.xorCardinality(bf));
}
}

View File

@ -32,9 +32,9 @@ import org.junit.Test;
*/
public class HasherBloomFilterTest extends AbstractBloomFilterTest {
/**
* Tests that the constructor works correctly.
*
* @throws NoSuchAlgorithmException
*/
@Test
@ -58,5 +58,4 @@ public class HasherBloomFilterTest extends AbstractBloomFilterTest {
return new HasherBloomFilter(hasher, shape);
}
}

View File

@ -98,11 +98,11 @@ public class DynamicHasherBuilderTest {
/**
* Sets up the builder for testing.
*
* @throws NoSuchAlgorithmException if MD5 is not available.
*/
@Before
public void setup() throws NoSuchAlgorithmException
{
public void setup() throws NoSuchAlgorithmException {
builder = new DynamicHasher.Builder(new MD5Cyclic());
}
}

View File

@ -72,6 +72,7 @@ public class HashFunctionIdentityImplTest {
/**
* Test the constructor from component values.
*
* @param provider the name of the provider.
* @param name the name of the hash function.
* @param signedness the signedness of the hash function.
@ -80,8 +81,8 @@ public class HashFunctionIdentityImplTest {
*/
@Test
public void valuesConstructorTest() {
final HashFunctionIdentityImpl impl = new HashFunctionIdentityImpl( "Provider", "NAME",
Signedness.UNSIGNED, ProcessType.ITERATIVE, -2l);
final HashFunctionIdentityImpl impl = new HashFunctionIdentityImpl("Provider", "NAME", Signedness.UNSIGNED,
ProcessType.ITERATIVE, -2l);
assertEquals("NAME", impl.getName());
assertEquals("Provider", impl.getProvider());
assertEquals(Signedness.UNSIGNED, impl.getSignedness());

View File

@ -33,7 +33,6 @@ import org.junit.Test;
*/
public class ShapeTest {
private final HashFunctionIdentity testFunction = new HashFunctionIdentity() {
@Override
@ -59,7 +58,8 @@ public class ShapeTest {
@Override
public Signedness getSignedness() {
return Signedness.SIGNED;
}};
}
};
/*
* values from https://hur.st/bloomfilter/?n=5&p=.1&m=&k=
@ -73,86 +73,17 @@ public class ShapeTest {
* k = 3
*/
private final Shape shape = new Shape(testFunction, 5, 0.1);
/**
* Tests that if the number of bits is less than 8 an exception is thrown
*/
@Test
public void constructor__probability_bits_hash__BadNumberOfBitsTest() {
try {
new Shape(testFunction, 0.5, 6, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
}
}
/**
* Tests that invalid probability values cause and IllegalArgumentException to
* be thrown.
*/
@Test
public void constructor__probability_bits_hash_BadProbabilityTest() {
// probability should not be 0
try {
new Shape(testFunction, 0.0, 24, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
}
// probability should not be = -1
try {
new Shape(testFunction, -1.0, 24, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
}
// probability should not be < -1
try {
new Shape(testFunction, -1.5, 24, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
}
// probability should not be = 1
try {
new Shape(testFunction, 1.0, 24, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
}
// probability should not be > 1
try {
new Shape(testFunction, 2.0, 24, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
}
}
/**
* Tests that if the number of bits less than 8 an IllegalArgumentException
* is thrown.
* Tests that if the number of bits less than 8 an IllegalArgumentException is thrown.
*/
@Test
public void constructor_items_bits_BadNumberOfBitsTest() {
try {
new Shape(testFunction, 5, 6);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
} catch (final IllegalArgumentException expected) {
// expected
}
}
@ -165,23 +96,20 @@ public class ShapeTest {
try {
new Shape(testFunction, 16, 8);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
} catch (final IllegalArgumentException expected) {
// expected
}
}
/**
* Tests that if the number of items less than 1 an IllegalArgumentException
* is thrown.
* Tests that if the number of items less than 1 an IllegalArgumentException is thrown.
*/
@Test
public void constructor_items_bits_BadNumberOfItemsTest() {
try {
new Shape(testFunction, 0, 24);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
} catch (final IllegalArgumentException expected) {
// expected
}
}
@ -194,23 +122,20 @@ public class ShapeTest {
try {
new Shape(testFunction, 5, 6, 1);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
} catch (final IllegalArgumentException expected) {
// expected
}
}
/**
* Tests that if the number of hash functions is less than 1 an exception is
* thrown.
* Tests that if the number of hash functions is less than 1 an exception is thrown.
*/
@Test
public void constructor_items_bits_hash_BadNumberOfHashFunctionsTest() {
try {
new Shape(testFunction, 5, 24, 0);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
} catch (final IllegalArgumentException expected) {
// expected
}
}
@ -223,30 +148,27 @@ public class ShapeTest {
try {
new Shape(testFunction, 0, 24, 1);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
} catch (final IllegalArgumentException expected) {
// expected
}
}
/**
* Tests that if the calculated probability is greater than or equal to 1 an
* IllegalArgumentException is thrown
* Tests that if the calculated probability is greater than or equal to 1 an IllegalArgumentException is thrown
*/
@Test
public void constructor_items_bits_hash_BadProbabilityTest() {
try {
new Shape(testFunction, 4000, 8, 1);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
} catch (final IllegalArgumentException expected) {
// expected
}
}
/**
* Tests that when the number of items, number of bits and number of hash functions
* is passed the values are calculated correctly.
* Tests that when the number of items, number of bits and number of hash functions is passed the values are
* calculated correctly.
*/
@Test
public void constructor_items_bits_hashTest() {
@ -264,8 +186,7 @@ public class ShapeTest {
}
/**
* Tests that the number of items and number of bits is passed the other values are
* calculated correctly.
* Tests that the number of items and number of bits is passed the other values are calculated correctly.
*/
@Test
public void constructor_items_bitsTest() {
@ -283,8 +204,7 @@ public class ShapeTest {
}
/**
* Tests that if the number of items is less than 1 an IllegalArgumentException is
* thrown.
* Tests that if the number of items is less than 1 an IllegalArgumentException is thrown.
*/
@Test
public void constructor_items_probability_BadNumberOfItemsTest() {
@ -297,8 +217,7 @@ public class ShapeTest {
}
/**
* Tests that if the probability is less than or equal to 0 an IllegalArgumentException
* is thrown.
* Tests that if the probability is less than or equal to 0 an IllegalArgumentException is thrown.
*/
@Test
public void constructor_items_probability_BadProbabilityTest() {
@ -318,8 +237,7 @@ public class ShapeTest {
}
/**
* Tests that if calculated number of bits is greater than Integer.MAX_VALUE an
* IllegalArgumentException is thrown.
* Tests that if calculated number of bits is greater than Integer.MAX_VALUE an IllegalArgumentException is thrown.
*/
@Test
public void constructor_items_probability_NumberOfBitsOverflowTest() {
@ -353,27 +271,22 @@ public class ShapeTest {
try {
new Shape(null, 5, 72);
fail( "Should throw IllegalArgumentException");
}
catch (final IllegalArgumentException expected)
{
fail("Should throw NullPointerException");
} catch (final NullPointerException expected) {
// do nothing
}
}
/**
* Tests that the constructor with a null name, number of items, size of filter,
* and number of functions fails.
* Tests that the constructor with a null name, number of items, size of filter, and number of functions fails.
*/
@Test
public void constructor_nmk_noName() {
try {
new Shape(null, 5, 72, 17);
fail( "Should throw IllegalArgumentException");
}
catch (final IllegalArgumentException expected)
{
fail("Should throw NullPointerException");
} catch (final NullPointerException expected) {
// do nothing
}
}
@ -386,31 +299,39 @@ public class ShapeTest {
try {
new Shape(null, 5, 0.1);
fail( "Should throw IllegalArgumentException");
}
catch (final IllegalArgumentException expected)
{
fail("Should throw NullPointerException");
} catch (final NullPointerException expected) {
// do nothing
}
}
/**
* Tests that the constructor with a null name, probability, size of filter,
* and number of functions fails.
* Tests that the constructor with a null name, probability, size of filter, and number of functions fails.
*/
@Test
public void constructor_pmk_noName() {
try {
new Shape(null, 0.1, 72, 17);
fail( "Should throw IllegalArgumentException");
}
catch (final IllegalArgumentException expected)
{
fail("Should throw NullPointerException");
} catch (final NullPointerException expected) {
// do nothing
}
}
/**
* Tests that if the number of bits is less than 8 an exception is thrown
*/
@Test
public void constructor_probability_bits_hash__BadNumberOfBitsTest() {
try {
new Shape(testFunction, 0.5, 6, 1);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// expected
}
}
/**
* Tests that if the number of functions is less than 1 an exception is thrown
*/
@ -419,15 +340,60 @@ public class ShapeTest {
try {
new Shape(testFunction, 0.5, 24, 0);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
} catch (final IllegalArgumentException expected) {
// expected
}
}
/**
* Tests the calculated values of calling the constructor with the
* probability, number of bits and number of hash functions.
* Tests that invalid probability values cause and IllegalArgumentException to be thrown.
*/
@Test
public void constructor_probability_bits_hash_BadProbabilityTest() {
// probability should not be 0
try {
new Shape(testFunction, 0.0, 24, 1);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// expected
}
// probability should not be = -1
try {
new Shape(testFunction, -1.0, 24, 1);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// expected
}
// probability should not be < -1
try {
new Shape(testFunction, -1.5, 24, 1);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// expected
}
// probability should not be = 1
try {
new Shape(testFunction, 1.0, 24, 1);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// expected
}
// probability should not be > 1
try {
new Shape(testFunction, 2.0, 24, 1);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// expected
}
}
/**
* Tests the calculated values of calling the constructor with the probability, number of bits and number of hash
* functions.
*/
@Test
public void constructor_probability_bits_hashTest() {
@ -478,7 +444,8 @@ public class ShapeTest {
@Override
public Signedness getSignedness() {
return Signedness.SIGNED;
}};
}
};
assertNotEquals(new Shape(testFunction2, 4, 1.0 / 10), shape);
@ -493,5 +460,4 @@ public class ShapeTest {
assertEquals(hashCode, shape.hashCode());
}
}