kinow commented on a change in pull request #160: URL: https://github.com/apache/commons-collections/pull/160#discussion_r422686697
########## File path: src/main/java/org/apache/commons/collections4/bloomfilter/SimpleBloomFilter.java ########## @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.collections4.bloomfilter; + +import java.util.function.Function; + +import org.apache.commons.collections4.bloomfilter.hasher.DynamicHasher; +import org.apache.commons.collections4.bloomfilter.hasher.Hasher; +import org.apache.commons.collections4.bloomfilter.hasher.Shape; +import org.apache.commons.collections4.bloomfilter.hasher.function.Murmur128x64Cyclic; + +/** + * A bloom filter that uses a BitSetBloomFilter to create a Bloom filter that + * can merge instances of a specific class. + * + * @param <T> The Class to merge. + * @since 4.5 + */ +public class SimpleBloomFilter<T> extends BitSetBloomFilter implements BloomFilter { + + /** + * The function that converts the instance of T to the SimpleBuilder. + * <p> + * If the object T is to be considered as a single item in the filter then + * function must create the {@code SimpleBuilder} and only call a single {@code with()} + * method.</p> + * <p> + * If the object T is to be considered as several items then the function must + * create the {@code SimpleBuilder} and call the {@code with()} method once for each item.</p> + */ + private Function<T, SimpleBuilder> func; + + /** + * Constructs a SimpleBloomFilter from the shape and function. This constructor + * creates an empty Bloom filter. + * @param hasher the Hasher to use. + * @param shape the Shape of the Bloom filter. + * @param func a Function to convert T to a SimpleBuilder. + * @see #func + */ + public SimpleBloomFilter(Hasher hasher, Shape shape, Function<T, SimpleBuilder> func) { + super(hasher, shape); + this.func = func; + } + + /** + * Constructs a SimpleBloomFilter from the shape and function. This constructor + * creates an empty Bloom filter. + * @param shape the Shape of the Bloom filter. + * @param func a Function to convert T to a SimpleBuilder. + * @see #func + */ + public SimpleBloomFilter(Shape shape, Function<T, SimpleBuilder> func) { + super(shape); + this.func = func; + } + + /** + * Constructs a SimpleBloomFilter from the shape, function and a data object. + * This constructor creates an Bloom filter populated with the data from the + * {@code data} parameter. + * @param shape the Shape of the Bloom filter. + * @param func a Function to convert T to a SimpleBuilder. + * @param data the data object to populate the filter with. + * @see #func + */ + public SimpleBloomFilter(Shape shape, Function<T, SimpleBuilder> func, T data) { + this(shape, func); + this.merge( data ); + } + + /** + * Merges a data object into this filter. + * + * <p>Note: This method should return {@code true} even if no additional bit indexes were + * enabled. A {@code false} result indicates that this filter is not ensured to contain + * the {@code data}. + * + * @param data the data to merge. + * @return true if the merge was successful + * @throws IllegalArgumentException if the shape of the other filter does not match + * the shape of this filter + */ + public boolean merge( T data ) { + return this.merge( this.func.apply( data ).build() ); + } + + + /** + * Returns {@code true} if this filter contains the object. + * Specifically this returns {@code true} if this filter is enabled for all bit indexes + * identified by the hashing of the object {@code data}. + * + * @param data the data to check for. + * @return true if this filter is enabled for all bits specified by the data object. + */ + public boolean contains( T data ) { + return this.contains( this.func.apply( data ).build() ); + } Review comment: The `merge` and `contains` functions look much simpler for the caller user :+1: ########## File path: src/main/java/org/apache/commons/collections4/bloomfilter/SimpleBloomFilter.java ########## @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.collections4.bloomfilter; + +import java.util.function.Function; + +import org.apache.commons.collections4.bloomfilter.hasher.DynamicHasher; +import org.apache.commons.collections4.bloomfilter.hasher.Hasher; +import org.apache.commons.collections4.bloomfilter.hasher.Shape; +import org.apache.commons.collections4.bloomfilter.hasher.function.Murmur128x64Cyclic; + +/** + * A bloom filter that uses a BitSetBloomFilter to create a Bloom filter that + * can merge instances of a specific class. + * + * @param <T> The Class to merge. + * @since 4.5 + */ +public class SimpleBloomFilter<T> extends BitSetBloomFilter implements BloomFilter { + + /** + * The function that converts the instance of T to the SimpleBuilder. + * <p> + * If the object T is to be considered as a single item in the filter then + * function must create the {@code SimpleBuilder} and only call a single {@code with()} + * method.</p> + * <p> + * If the object T is to be considered as several items then the function must + * create the {@code SimpleBuilder} and call the {@code with()} method once for each item.</p> + */ + private Function<T, SimpleBuilder> func; + + /** + * Constructs a SimpleBloomFilter from the shape and function. This constructor + * creates an empty Bloom filter. + * @param hasher the Hasher to use. + * @param shape the Shape of the Bloom filter. + * @param func a Function to convert T to a SimpleBuilder. + * @see #func + */ + public SimpleBloomFilter(Hasher hasher, Shape shape, Function<T, SimpleBuilder> func) { + super(hasher, shape); + this.func = func; + } + + /** + * Constructs a SimpleBloomFilter from the shape and function. This constructor + * creates an empty Bloom filter. + * @param shape the Shape of the Bloom filter. + * @param func a Function to convert T to a SimpleBuilder. + * @see #func + */ + public SimpleBloomFilter(Shape shape, Function<T, SimpleBuilder> func) { + super(shape); + this.func = func; + } + + /** + * Constructs a SimpleBloomFilter from the shape, function and a data object. + * This constructor creates an Bloom filter populated with the data from the + * {@code data} parameter. + * @param shape the Shape of the Bloom filter. + * @param func a Function to convert T to a SimpleBuilder. + * @param data the data object to populate the filter with. + * @see #func Review comment: #TodayILeaned. Didn't know we could link with the object :+1: ########## File path: src/test/java/org/apache/commons/collections4/bloomfilter/hasher/HasherBuilderTest.java ########## @@ -108,4 +110,156 @@ static String getExtendedString() { }; return String.valueOf(data); } + + /** + * Test that adding an integer into the hasher works correctly + */ + @Test + public void withIntTest() { + TestBuilder builder = new TestBuilder(); + Integer[] values = { Integer.valueOf(0), Integer.MAX_VALUE, Integer.MIN_VALUE }; + for (int i : values) { + builder.with(i); + } + + for (int i = 0; i < values.length; i++) { + Assert.assertArrayEquals(ByteBuffer.allocate(Integer.BYTES).putInt(values[i]).array(), + builder.items.get(i)); + } + } + + /** + * Test that adding a long into the hasher works correctly + */ + @Test + public void withLongTest() { + TestBuilder builder = new TestBuilder(); + Long[] values = { Long.valueOf(0), Long.MAX_VALUE, Long.MIN_VALUE }; + for (long l : values) { + builder.with(l); + } + + for (int i = 0; i < values.length; i++) { + Assert.assertArrayEquals(ByteBuffer.allocate(Long.BYTES).putLong(values[i]).array(), builder.items.get(i)); + } + } + + /** + * Test that adding a double into the hasher works correctly + */ + @Test + public void withDoubleTest() { + TestBuilder builder = new TestBuilder(); + Double[] values = { Double.valueOf(0.0), Double.MAX_VALUE, Double.MIN_VALUE, Double.MIN_NORMAL, + Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NaN }; + for (double d : values) { + builder.with(d); + } + + for (int i = 0; i < values.length; i++) { + Assert.assertArrayEquals(ByteBuffer.allocate(Double.BYTES).putDouble(values[i]).array(), + builder.items.get(i)); + } + } + + /** + * Test that adding a float into the hasher works correctly + */ + @Test + public void withFloatTest() { + TestBuilder builder = new TestBuilder(); + Float[] values = { Float.valueOf(0.0f), Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_NORMAL, + Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, Float.NaN }; + for (float f : values) { + builder.with(f); + } + + for (int i = 0; i < values.length; i++) { + Assert.assertArrayEquals(ByteBuffer.allocate(Float.BYTES).putFloat(values[i]).array(), + builder.items.get(i)); + } + } + + /** + * Test that adding a character into the hasher works correctly + */ + @Test + public void withCharTest() { + TestBuilder builder = new TestBuilder(); + Character[] values = { Character.valueOf((char) 0), Character.MAX_HIGH_SURROGATE, Character.MAX_LOW_SURROGATE, + Character.MAX_SURROGATE, Character.MAX_VALUE, Character.MIN_HIGH_SURROGATE, Character.MIN_LOW_SURROGATE, + Character.MIN_SURROGATE, Character.MIN_VALUE }; + for (char c : values) { + builder.with(c); + } + + for (int i = 0; i < values.length; i++) { + Assert.assertArrayEquals(ByteBuffer.allocate(Character.BYTES).putChar(values[i]).array(), + builder.items.get(i)); + } + } + + /** + * Test that adding a short into the hasher works correctly + */ + @Test + public void withShortTest() { + TestBuilder builder = new TestBuilder(); + Short[] values = { Short.valueOf((short) 0), Short.MAX_VALUE, Short.MIN_VALUE }; + for (short s : values) { + builder.with(s); + } + + for (int i = 0; i < values.length; i++) { + Assert.assertArrayEquals(ByteBuffer.allocate(Short.BYTES).putShort(values[i]).array(), + builder.items.get(i)); + } + } + + /** + * Test that adding a BigInteger into the hasher works correctly + */ + @Test + public void withBigIngeterTest() { + TestBuilder builder = new TestBuilder(); + BigInteger[] values = { BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.TEN, Review comment: @Claudenw it looks like `.TWO` is private in Java 8, only `ONE, `TEN`, `ZERO` are available: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html There's an error in the Java 8 build in Travis. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
