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;
@ -73,24 +73,21 @@ public class CountingBloomFilter extends AbstractBloomFilter {
* @param counts A map of data counts.
* @param shape The shape of the resulting filter.
*/
public CountingBloomFilter(final Map<Integer,Integer> counts, final Shape shape) {
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)
{
throw new IllegalArgumentException( "dataMap has an item with an index less than 0" );
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) {
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)
{
this.counts.put( e.getKey(), e.getValue() );
}});
throw new IllegalArgumentException("dataMap has an item with an value less than 0");
} else if (e.getValue() > 0) {
this.counts.put(e.getKey(), e.getValue());
}
});
}
/**
@ -167,9 +164,8 @@ public class CountingBloomFilter extends AbstractBloomFilter {
@Override
public void merge(final BloomFilter other) {
verifyShape(other);
if (other instanceof CountingBloomFilter)
{
merge(((CountingBloomFilter)other).counts.keySet().iterator());
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,16 +206,15 @@ public class CountingBloomFilter extends AbstractBloomFilter {
*/
public void remove(final BloomFilter other) {
verifyShape(other);
if (other instanceof CountingBloomFilter)
{
remove(((CountingBloomFilter)other).counts.keySet().stream());
if (other instanceof CountingBloomFilter) {
remove(((CountingBloomFilter) other).counts.keySet().stream());
} else {
remove(BitSet.valueOf(other.getBits()).stream().boxed());
}
}
/**
* 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

@ -50,10 +50,9 @@ public final class SetOperations {
* @return the Cosine similarity.
*/
public static double cosineSimilarity(final BloomFilter first, final BloomFilter second) {
verifyShape(first,second);
verifyShape(first, second);
final int numerator = first.andCardinality(second);
return numerator==0?0:numerator / (Math.sqrt(first.cardinality()) * Math.sqrt(second.cardinality()));
return numerator == 0 ? 0 : numerator / (Math.sqrt(first.cardinality()) * Math.sqrt(second.cardinality()));
}
/**
@ -138,10 +137,10 @@ public final class SetOperations {
* @return the Jaccard similarity.
*/
public static double jaccardSimilarity(final BloomFilter first, final BloomFilter second) {
verifyShape(first,second);
verifyShape(first, second);
final int orCard = first.orCardinality(second);
// if the orCard is zero then the hamming distance will also be zero.
return orCard==0?0:hammingDistance(first,second) / (double) orCard;
return orCard == 0 ? 0 : hammingDistance(first, second) / (double) orCard;
}
/**

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

@ -37,38 +37,37 @@ public class BitSetBloomFilterTest extends AbstractBloomFilterTest {
*/
@Test
public void andCardinalityTest_BitSetBloomFilter() {
final Hasher hasher = new StaticHasher( Arrays.asList( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ).iterator(), shape );
final Hasher hasher = new StaticHasher(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).iterator(), shape);
final BitSetBloomFilter bf = createFilter(hasher, shape);
Hasher hasher2 = new StaticHasher( Arrays.asList( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ).iterator(), shape );
Hasher hasher2 = new StaticHasher(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).iterator(), shape);
BitSetBloomFilter bf2 = createFilter(hasher2, shape);
assertEquals( 10, bf.andCardinality(bf2));
assertEquals( 10, bf2.andCardinality(bf));
assertEquals(10, bf.andCardinality(bf2));
assertEquals(10, bf2.andCardinality(bf));
hasher2 = new StaticHasher( Arrays.asList( 1, 2, 3, 4, 5 ).iterator(), shape );
hasher2 = new StaticHasher(Arrays.asList(1, 2, 3, 4, 5).iterator(), shape);
bf2 = createFilter(hasher2, shape);
assertEquals( 5, bf.andCardinality(bf2));
assertEquals( 5, bf2.andCardinality(bf));
assertEquals(5, bf.andCardinality(bf2));
assertEquals(5, bf2.andCardinality(bf));
hasher2 = new StaticHasher( Arrays.asList( 11, 12, 13, 14, 15 ).iterator(), shape );
hasher2 = new StaticHasher(Arrays.asList(11, 12, 13, 14, 15).iterator(), shape);
bf2 = createFilter(hasher2, shape);
assertEquals( 0, bf.andCardinality(bf2));
assertEquals( 0, bf2.andCardinality(bf));
assertEquals(0, bf.andCardinality(bf2));
assertEquals(0, bf2.andCardinality(bf));
}
@Override
protected BitSetBloomFilter createEmptyFilter(final Shape shape) {
return new BitSetBloomFilter( shape );
return new BitSetBloomFilter(shape);
}
@Override
protected BitSetBloomFilter createFilter(final Hasher hasher, final Shape shape) {
return new BitSetBloomFilter( hasher, shape );
return new BitSetBloomFilter(hasher, shape);
}
/**
@ -77,20 +76,19 @@ public class BitSetBloomFilterTest extends AbstractBloomFilterTest {
@Test
public void mergeTest_BitSetBloomFilter() {
final List<Integer> lst = Arrays.asList( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ,16 ,17 );
final Hasher hasher = new StaticHasher( lst.iterator(), shape );
final List<Integer> lst = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17);
final Hasher hasher = new StaticHasher(lst.iterator(), shape);
final BitSetBloomFilter bf = createFilter(hasher, shape);
final List<Integer> lst2 = Arrays.asList( 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 ,26 ,27 );
final Hasher hasher2 = new StaticHasher( lst2.iterator(), shape );
final List<Integer> lst2 = Arrays.asList(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
final Hasher hasher2 = new StaticHasher(lst2.iterator(), shape);
final BloomFilter bf2 = new BitSetBloomFilter(hasher2, shape);
bf.merge(bf2);
assertEquals(27, bf.cardinality());
}
/**
@ -98,29 +96,27 @@ public class BitSetBloomFilterTest extends AbstractBloomFilterTest {
*/
@Test
public void xorCardinalityTest_BitSetBloomFilter() {
final Hasher hasher = new StaticHasher( Arrays.asList( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ).iterator(), shape );
final Hasher hasher = new StaticHasher(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).iterator(), shape);
final BitSetBloomFilter bf = createFilter(hasher, shape);
Hasher hasher2 = new StaticHasher( Arrays.asList( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ).iterator(), shape );
Hasher hasher2 = new StaticHasher(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).iterator(), shape);
BitSetBloomFilter bf2 = createFilter(hasher2, shape);
assertEquals( 0, bf.xorCardinality(bf2));
assertEquals( 0, bf2.xorCardinality(bf));
assertEquals(0, bf.xorCardinality(bf2));
assertEquals(0, bf2.xorCardinality(bf));
hasher2 = new StaticHasher( Arrays.asList( 1, 2, 3, 4, 5 ).iterator(), shape );
hasher2 = new StaticHasher(Arrays.asList(1, 2, 3, 4, 5).iterator(), shape);
bf2 = createFilter(hasher2, shape);
assertEquals( 5, bf.xorCardinality(bf2));
assertEquals( 5, bf2.xorCardinality(bf));
assertEquals(5, bf.xorCardinality(bf2));
assertEquals(5, bf2.xorCardinality(bf));
hasher2 = new StaticHasher( Arrays.asList( 11, 12, 13, 14, 15 ).iterator(), shape );
hasher2 = new StaticHasher(Arrays.asList(11, 12, 13, 14, 15).iterator(), shape);
bf2 = createFilter(hasher2, shape);
assertEquals( 15, bf.xorCardinality(bf2));
assertEquals( 15, bf2.xorCardinality(bf));
assertEquals(15, bf.xorCardinality(bf2));
assertEquals(15, bf2.xorCardinality(bf));
}
}

View File

@ -104,7 +104,7 @@ public class CountingBloomFilterTest extends AbstractBloomFilterTest {
map.put( shape.getNumberOfBits(), 1 );
try {
bf = new CountingBloomFilter( map, shape);
fail( "Should have thrown IllegalArgumentExceptionW");
fail("Should have thrown IllegalArgumentExceptionW");
} catch (final IllegalArgumentException exprected)
{
// expected
@ -114,7 +114,7 @@ public class CountingBloomFilterTest extends AbstractBloomFilterTest {
map.put( -1, 1 );
try {
bf = new CountingBloomFilter( map, shape);
fail( "Should have thrown IllegalArgumentExceptionW");
fail("Should have thrown IllegalArgumentExceptionW");
} catch (final IllegalArgumentException exprected)
{
// expected
@ -124,7 +124,7 @@ public class CountingBloomFilterTest extends AbstractBloomFilterTest {
map.put( 1, -1 );
try {
bf = new CountingBloomFilter( map, shape);
fail( "Should have thrown IllegalArgumentExceptionW");
fail("Should have thrown IllegalArgumentExceptionW");
} catch (final IllegalArgumentException exprected)
{
// expected
@ -249,7 +249,7 @@ public class CountingBloomFilterTest extends AbstractBloomFilterTest {
try {
bf.merge(bf2);
fail( "Should have thrown IllegalStateException");
fail("Should have thrown IllegalStateException");
}
catch (final IllegalStateException expected)
{
@ -428,7 +428,7 @@ public class CountingBloomFilterTest extends AbstractBloomFilterTest {
try {
bf.remove(bf2);
fail( "Should have thrown IllegalStateException");
fail("Should have thrown IllegalStateException");
}
catch (final IllegalStateException expected)
{

View File

@ -81,7 +81,7 @@ public class DefaultBloomFilterMethodsTest extends AbstractBloomFilterTest {
@Override
public void merge(final Hasher hasher) {
verifyHasher( hasher );
verifyHasher(hasher);
hasher.getBits(getShape()).forEachRemaining((IntConsumer) bitSet::set);
}
@ -89,12 +89,12 @@ public class DefaultBloomFilterMethodsTest extends AbstractBloomFilterTest {
@Override
protected AbstractBloomFilter createEmptyFilter(final Shape shape) {
return new BF( shape );
return new BF(shape);
}
@Override
protected AbstractBloomFilter createFilter(final Hasher hasher, final Shape shape) {
return new BF( hasher, shape );
return new BF(hasher, shape);
}
}

View File

@ -32,31 +32,30 @@ import org.junit.Test;
*/
public class HasherBloomFilterTest extends AbstractBloomFilterTest {
/**
* Tests that the constructor works correctly.
*
* @throws NoSuchAlgorithmException
*/
@Test
public void constructorTest_NonStatic() throws NoSuchAlgorithmException {
final Shape shape = new Shape( new MD5Cyclic(), 3, 72, 17 );
final DynamicHasher hasher = new DynamicHasher.Builder( new MD5Cyclic() ).with( "Hello").build();
final HasherBloomFilter filter = createFilter( hasher, shape );
final Shape shape = new Shape(new MD5Cyclic(), 3, 72, 17);
final DynamicHasher hasher = new DynamicHasher.Builder(new MD5Cyclic()).with("Hello").build();
final HasherBloomFilter filter = createFilter(hasher, shape);
final long[] lb = filter.getBits();
assertEquals( 2, lb.length );
assertEquals( 0x6203101001888c44L, lb[0]);
assertEquals( 0x60L, lb[1]);
assertEquals(2, lb.length);
assertEquals(0x6203101001888c44L, lb[0]);
assertEquals(0x60L, lb[1]);
}
@Override
protected AbstractBloomFilter createEmptyFilter(final Shape shape) {
return new HasherBloomFilter( shape );
return new HasherBloomFilter(shape);
}
@Override
protected HasherBloomFilter createFilter(final Hasher hasher, final Shape shape) {
return new HasherBloomFilter( hasher, shape );
return new HasherBloomFilter(hasher, shape);
}
}

View File

@ -35,7 +35,7 @@ import org.junit.Test;
public class DynamicHasherBuilderTest {
private DynamicHasher.Builder builder;
private final Shape shape = new Shape( new MD5Cyclic(), 1, Integer.MAX_VALUE, 1 );
private final Shape shape = new Shape(new MD5Cyclic(), 1, Integer.MAX_VALUE, 1);
/**
* Tests that hashing a byte works as expected.
@ -49,8 +49,8 @@ public class DynamicHasherBuilderTest {
final OfInt iter = hasher.getBits(shape);
assertTrue(iter.hasNext());
assertEquals( expected, iter.nextInt() );
assertFalse( iter.hasNext());
assertEquals(expected, iter.nextInt());
assertFalse(iter.hasNext());
}
/**
@ -64,8 +64,8 @@ public class DynamicHasherBuilderTest {
final OfInt iter = hasher.getBits(shape);
assertTrue(iter.hasNext());
assertEquals( expected, iter.nextInt() );
assertFalse( iter.hasNext());
assertEquals(expected, iter.nextInt());
assertFalse(iter.hasNext());
}
@ -92,17 +92,17 @@ public class DynamicHasherBuilderTest {
final OfInt iter = hasher.getBits(shape);
assertTrue(iter.hasNext());
assertEquals( expected, iter.nextInt() );
assertFalse( iter.hasNext());
assertEquals(expected, iter.nextInt());
assertFalse(iter.hasNext());
}
/**
* Sets up the builder for testing.
*
* @throws NoSuchAlgorithmException if MD5 is not available.
*/
@Before
public void setup() throws NoSuchAlgorithmException
{
builder = new DynamicHasher.Builder( new MD5Cyclic());
public void setup() throws NoSuchAlgorithmException {
builder = new DynamicHasher.Builder(new MD5Cyclic());
}
}

View File

@ -137,8 +137,8 @@ public class DynamicHasherTest {
*/
@Test
public void testIsEmpty() {
assertTrue( builder.build().isEmpty() );
assertFalse( builder.with("Hello").build().isEmpty() );
assertTrue(builder.build().isEmpty());
assertFalse(builder.with("Hello").build().isEmpty());
}
}

View File

@ -62,16 +62,17 @@ public class HashFunctionIdentityImplTest {
}
};
final HashFunctionIdentityImpl impl = new HashFunctionIdentityImpl( identity );
assertEquals( "NAME", impl.getName());
assertEquals( "Provider", impl.getProvider());
assertEquals( Signedness.SIGNED, impl.getSignedness());
assertEquals( ProcessType.CYCLIC, impl.getProcessType());
assertEquals( -1l, impl.getSignature());
final HashFunctionIdentityImpl impl = new HashFunctionIdentityImpl(identity);
assertEquals("NAME", impl.getName());
assertEquals("Provider", impl.getProvider());
assertEquals(Signedness.SIGNED, impl.getSignedness());
assertEquals(ProcessType.CYCLIC, impl.getProcessType());
assertEquals(-1l, impl.getSignature());
}
/**
* 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,13 +81,13 @@ public class HashFunctionIdentityImplTest {
*/
@Test
public void valuesConstructorTest() {
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());
assertEquals( ProcessType.ITERATIVE, impl.getProcessType());
assertEquals( -2l, impl.getSignature());
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());
assertEquals(ProcessType.ITERATIVE, impl.getProcessType());
assertEquals(-2l, impl.getSignature());
}
}

View File

@ -33,7 +33,6 @@ import org.junit.Test;
*/
public class ShapeTest {
private final HashFunctionIdentity testFunction = new HashFunctionIdentity() {
@Override
@ -59,439 +58,406 @@ public class ShapeTest {
@Override
public Signedness getSignedness() {
return Signedness.SIGNED;
}};
}
};
/*
* values from https://hur.st/bloomfilter/?n=5&p=.1&m=&k=
*
* n = 5
*
* p = 0.100375138 (1 in 10)
*
* m = 24 (3B)
*
* k = 3
*/
private final Shape shape = new Shape(testFunction, 5, 0.1);
/**
* 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) {
// expected
}
}
/**
* Tests that if the number of hash functions is less than 1 an exception is thrown.
*/
@Test
public void constructor_items_bits_BadNumberOfHashFunctionsTest() {
try {
new Shape(testFunction, 16, 8);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// expected
}
}
/**
* 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) {
// expected
}
}
/**
* Tests that if the number of bits is less than 8 an exception is thrown
*/
@Test
public void constructor_items_bits_hash_BadNumberOfBitsTest() {
try {
new Shape(testFunction, 5, 6, 1);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// expected
}
}
/**
* 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) {
// expected
}
}
/**
* Tests that if the number of items is less than 1 an exception is thrown.
*/
@Test
public void constructor_items_bits_hash_BadNumberOfItemsTest() {
try {
new Shape(testFunction, 0, 24, 1);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// expected
}
}
/**
* 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) {
// expected
}
}
/**
* 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() {
/*
* values from https://hur.st/bloomfilter/?n=5&m=24&k=4
*/
final Shape filterConfig = new Shape(testFunction, 5, 24, 4);
assertEquals(24, filterConfig.getNumberOfBits());
assertEquals(3, filterConfig.getNumberOfBytes());
assertEquals(4, filterConfig.getNumberOfHashFunctions());
assertEquals(5, filterConfig.getNumberOfItems());
assertEquals(0.102194782, filterConfig.getProbability(), 0.000001);
}
/**
* Tests that the number of items and number of bits is passed the other values are calculated correctly.
*/
@Test
public void constructor_items_bitsTest() {
/*
* values from https://hur.st/bloomfilter/?n=5&m=24
*/
final Shape filterConfig = new Shape(testFunction, 5, 24);
assertEquals(24, filterConfig.getNumberOfBits());
assertEquals(3, filterConfig.getNumberOfBytes());
assertEquals(3, filterConfig.getNumberOfHashFunctions());
assertEquals(5, filterConfig.getNumberOfItems());
assertEquals(0.100375138, filterConfig.getProbability(), 0.000001);
}
/**
* Tests that if the number of items is less than 1 an IllegalArgumentException is thrown.
*/
@Test
public void constructor_items_probability_BadNumberOfItemsTest() {
try {
new Shape(testFunction, 0, 1.0 / 10);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
}
/**
* Tests that if the probability is less than or equal to 0 an IllegalArgumentException is thrown.
*/
@Test
public void constructor_items_probability_BadProbabilityTest() {
try {
new Shape(testFunction, 10, 0.0);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
try {
new Shape(testFunction, 10, 1.0);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
}
/**
* Tests that if calculated number of bits is greater than Integer.MAX_VALUE an IllegalArgumentException is thrown.
*/
@Test
public void constructor_items_probability_NumberOfBitsOverflowTest() {
try {
new Shape(testFunction, Integer.MAX_VALUE, 1.0 / 10);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
}
/**
* Tests the the probability is calculated correctly.
*/
@Test
public void constructor_items_probability_Test() {
assertEquals(24, shape.getNumberOfBits());
assertEquals(3, shape.getNumberOfBytes());
assertEquals(3, shape.getNumberOfHashFunctions());
assertEquals(5, shape.getNumberOfItems());
assertEquals(0.100375138, shape.getProbability(), 0.000001);
}
/**
* Tests that the constructor with a null name, number of items and size of filter fails.
*/
@Test
public void constructor_nm_noName() {
try {
new Shape(null, 5, 72);
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.
*/
@Test
public void constructor_nmk_noName() {
try {
new Shape(null, 5, 72, 17);
fail("Should throw NullPointerException");
} catch (final NullPointerException expected) {
// do nothing
}
}
/**
* Tests that the constructor with a null name, number of items, and probability fails.
*/
@Test
public void constructor_np_noName() {
try {
new Shape(null, 5, 0.1);
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.
*/
@Test
public void constructor_pmk_noName() {
try {
new Shape(null, 0.1, 72, 17);
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
*/
@Test
public void constructor_probability_bits_hash_BadNumberOfHashFunctionsTest() {
try {
new Shape(testFunction, 0.5, 24, 0);
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 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() {
/*
* values from https://hur.st/bloomfilter/?n=5&p=.1&m=&k=
*
* n = 5
*
* p = 0.100375138 (1 in 10)
*
* m = 24 (3B)
*
* k = 3
*/
final Shape filterConfig = new Shape(testFunction, 0.1, 24, 3);
assertEquals(24, filterConfig.getNumberOfBits());
assertEquals(3, filterConfig.getNumberOfBytes());
assertEquals(3, filterConfig.getNumberOfHashFunctions());
assertEquals(5, filterConfig.getNumberOfItems());
assertEquals(0.100375138, filterConfig.getProbability(), 0.000001);
}
private final Shape shape = new Shape(testFunction, 5, 0.1);
/**
* Test equality of shape.
*/
@Test
public void equalsTest() {
/**
* 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
}
}
assertEquals(new Shape(testFunction, 5, 1.0 / 10), shape);
assertNotEquals(new Shape(testFunction, 5, 1.0 / 11), shape);
assertNotEquals(new Shape(testFunction, 4, 1.0 / 10), shape);
/**
* 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
final HashFunctionIdentity testFunction2 = new HashFunctionIdentity() {
@Override
public String getName() {
return "Test Function2";
}
// probability should not be = -1
try {
new Shape(testFunction, -1.0, 24, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
@Override
public ProcessType getProcessType() {
return ProcessType.CYCLIC;
}
// probability should not be < -1
try {
new Shape(testFunction, -1.5, 24, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
@Override
public String getProvider() {
return "Apache Commons Collection Tests";
}
// probability should not be = 1
try {
new Shape(testFunction, 1.0, 24, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
@Override
public long getSignature() {
return 0;
}
// probability should not be > 1
try {
new Shape(testFunction, 2.0, 24, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
@Override
public Signedness getSignedness() {
return Signedness.SIGNED;
}
}
};
/**
* 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)
{
//expected
}
}
assertNotEquals(new Shape(testFunction2, 4, 1.0 / 10), shape);
/**
* Tests that if the number of hash functions is less than 1 an exception is thrown.
*/
@Test
public void constructor_items_bits_BadNumberOfHashFunctionsTest() {
try {
new Shape(testFunction, 16,8);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
}
}
/**
* 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)
{
//expected
}
}
/**
* Tests that if the number of bits is less than 8 an exception is thrown
*/
@Test
public void constructor_items_bits_hash_BadNumberOfBitsTest() {
try {
new Shape(testFunction, 5, 6, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
}
}
/**
* 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)
{
//expected
}
}
/**
* Tests that if the number of items is less than 1 an exception is thrown.
*/
@Test
public void constructor_items_bits_hash_BadNumberOfItemsTest() {
try {
new Shape(testFunction, 0, 24, 1);
fail( "Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected)
{
//expected
}
}
/**
* 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)
{
//expected
}
}
/**
* 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() {
/*
* values from https://hur.st/bloomfilter/?n=5&m=24&k=4
*/
final Shape filterConfig = new Shape(testFunction, 5, 24, 4);
assertEquals(24, filterConfig.getNumberOfBits());
assertEquals(3, filterConfig.getNumberOfBytes());
assertEquals(4, filterConfig.getNumberOfHashFunctions());
assertEquals(5, filterConfig.getNumberOfItems());
assertEquals(0.102194782, filterConfig.getProbability(), 0.000001);
}
/**
* Tests that the number of items and number of bits is passed the other values are
* calculated correctly.
*/
@Test
public void constructor_items_bitsTest() {
/*
* values from https://hur.st/bloomfilter/?n=5&m=24
*/
final Shape filterConfig = new Shape(testFunction, 5, 24);
assertEquals(24, filterConfig.getNumberOfBits());
assertEquals(3, filterConfig.getNumberOfBytes());
assertEquals(3, filterConfig.getNumberOfHashFunctions());
assertEquals(5, filterConfig.getNumberOfItems());
assertEquals(0.100375138, filterConfig.getProbability(), 0.000001);
}
/**
* Tests that if the number of items is less than 1 an IllegalArgumentException is
* thrown.
*/
@Test
public void constructor_items_probability_BadNumberOfItemsTest() {
try {
new Shape( testFunction, 0, 1.0 / 10);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
}
/**
* Tests that if the probability is less than or equal to 0 an IllegalArgumentException
* is thrown.
*/
@Test
public void constructor_items_probability_BadProbabilityTest() {
try {
new Shape(testFunction, 10, 0.0);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
try {
new Shape(testFunction, 10, 1.0);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
}
/**
* Tests that if calculated number of bits is greater than Integer.MAX_VALUE an
* IllegalArgumentException is thrown.
*/
@Test
public void constructor_items_probability_NumberOfBitsOverflowTest() {
try {
new Shape( testFunction, Integer.MAX_VALUE, 1.0 / 10);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
}
/**
* Tests the the probability is calculated correctly.
*/
@Test
public void constructor_items_probability_Test() {
assertEquals(24, shape.getNumberOfBits());
assertEquals(3, shape.getNumberOfBytes());
assertEquals(3, shape.getNumberOfHashFunctions());
assertEquals(5, shape.getNumberOfItems());
assertEquals(0.100375138, shape.getProbability(), 0.000001);
}
/**
* Tests that the constructor with a null name, number of items and size of filter fails.
*/
@Test
public void constructor_nm_noName() {
try {
new Shape(null, 5, 72);
fail( "Should throw IllegalArgumentException");
}
catch (final IllegalArgumentException expected)
{
// do nothing
}
}
/**
* 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)
{
// do nothing
}
}
/**
* Tests that the constructor with a null name, number of items, and probability fails.
*/
@Test
public void constructor_np_noName() {
try {
new Shape(null, 5, 0.1);
fail( "Should throw IllegalArgumentException");
}
catch (final IllegalArgumentException expected)
{
// do nothing
}
}
/**
* 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)
{
// do nothing
}
}
/**
* Tests that if the number of functions is less than 1 an exception is thrown
*/
@Test
public void constructor_probability_bits_hash_BadNumberOfHashFunctionsTest() {
try {
new Shape(testFunction, 0.5, 24, 0);
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() {
/*
* values from https://hur.st/bloomfilter/?n=5&p=.1&m=&k=
*/
final Shape filterConfig = new Shape(testFunction, 0.1, 24, 3);
assertEquals(24, filterConfig.getNumberOfBits());
assertEquals(3, filterConfig.getNumberOfBytes());
assertEquals(3, filterConfig.getNumberOfHashFunctions());
assertEquals(5, filterConfig.getNumberOfItems());
assertEquals(0.100375138, filterConfig.getProbability(), 0.000001);
}
/**
* Test equality of shape.
*/
@Test
public void equalsTest() {
assertEquals(new Shape(testFunction, 5, 1.0 / 10), shape);
assertNotEquals(new Shape(testFunction, 5, 1.0 / 11), shape);
assertNotEquals(new Shape(testFunction, 4, 1.0 / 10), shape);
final HashFunctionIdentity testFunction2 = new HashFunctionIdentity() {
@Override
public String getName() {
return "Test Function2";
}
@Override
public ProcessType getProcessType() {
return ProcessType.CYCLIC;
}
@Override
public String getProvider() {
return "Apache Commons Collection Tests";
}
@Override
public long getSignature() {
return 0;
}
@Override
public Signedness getSignedness() {
return Signedness.SIGNED;
}};
assertNotEquals(new Shape(testFunction2, 4, 1.0 / 10), shape);
}
/**
* Test that hashCode equals hashCode of hashFunctionIdentity
*/
@Test
public void hashCodeTest() {
final int hashCode = Objects.hash(testFunction, 24, 3 );
assertEquals(hashCode, shape.hashCode());
}
}
/**
* Test that hashCode equals hashCode of hashFunctionIdentity
*/
@Test
public void hashCodeTest() {
final int hashCode = Objects.hash(testFunction, 24, 3);
assertEquals(hashCode, shape.hashCode());
}
}