Claudenw commented on a change in pull request #258:
URL: 
https://github.com/apache/commons-collections/pull/258#discussion_r805173454



##########
File path: 
src/test/java/org/apache/commons/collections4/bloomfilter/hasher/AbstractHasherTest.java
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.hasher;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.commons.collections4.bloomfilter.AbstractIndexProducerTest;
+import org.apache.commons.collections4.bloomfilter.IndexProducer;
+import org.apache.commons.collections4.bloomfilter.Shape;
+import org.junit.Test;
+
+public abstract class AbstractHasherTest extends AbstractIndexProducerTest {
+
+    protected abstract Hasher createHasher();
+
+    protected abstract Hasher createEmptyHasher();
+
+    /**
+     * The shape of the Hashers filters for testing.
+     * <ul>
+     *  <li>Hash functions (k) = 17
+     *  <li>Number of bits (m) = 72
+     * </ul>
+     * @return the testing shape.
+     */
+    protected final Shape getTestShape() {
+        return Shape.fromKM(17, 72);
+    }
+
+    @Override
+    protected IndexProducer createProducer() {
+        return createHasher().indices(getTestShape());
+    }
+
+    @Override
+    protected IndexProducer createEmptyProducer() {
+        return createEmptyHasher().indices(getTestShape());
+    }
+
+    @Test
+    public void testSize() {
+        assertEquals(1, createHasher().size());
+        assertEquals(0, createEmptyHasher().size());
+    }
+
+    @Test
+    public void testIsEmpty() {
+        assertFalse(createHasher().isEmpty());
+        assertTrue(createEmptyHasher().isEmpty());
+    }
+}

Review comment:
       SingleItemHasherCollection dropped and with it the size() and isEmpty() 
methods on the Hasher as they are not needed.  Added `abstract int 
getHasherSize(Hasher)`  method to Abstract testing class as the concrete 
testing classes can figure out how bit the hashers are.

##########
File path: 
src/main/java/org/apache/commons/collections4/bloomfilter/SimpleHasher.java
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.Objects;
+import java.util.function.IntPredicate;
+
+/**
+ * A Hasher that implements combinatorial hashing as as described by
+ * <a 
href="https://www.eecs.harvard.edu/~michaelm/postscripts/tr-02-05.pdf";>Krisch 
and Mitzenmacher</a>.
+ * <p>
+ * Common use for this hasher is to generate a byte array as the output of a 
hashing
+ * or MessageDigest algorithm.</p>
+ *
+ * @since 4.5
+ */
+public final class SimpleHasher implements Hasher {
+    /**
+     * A default increment used when the requested increment is zero. This is 
the same
+     * default increment used in Java's SplittableRandom random number 
generator.  It is the
+     * fractional representation of the golden ratio (0.618...) with a base of 
2^64.
+     */
+    public static final long DEFAULT_INCREMENT = 0x9e3779b97f4a7c15L;

Review comment:
       remove it and added a public method called `getDefaultIncrement()` so 
that this implementation can be dropped into environments where a different 
default was selected by simply overloading that method.  Documented such in 
method.

##########
File path: 
src/main/java/org/apache/commons/collections4/bloomfilter/SimpleHasher.java
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.Objects;
+import java.util.function.IntPredicate;
+
+/**
+ * A Hasher that implements combinatorial hashing as as described by
+ * <a 
href="https://www.eecs.harvard.edu/~michaelm/postscripts/tr-02-05.pdf";>Krisch 
and Mitzenmacher</a>.
+ * <p>
+ * Common use for this hasher is to generate a byte array as the output of a 
hashing
+ * or MessageDigest algorithm.</p>
+ *
+ * @since 4.5
+ */
+public final class SimpleHasher implements Hasher {
+    /**
+     * A default increment used when the requested increment is zero. This is 
the same
+     * default increment used in Java's SplittableRandom random number 
generator.  It is the
+     * fractional representation of the golden ratio (0.618...) with a base of 
2^64.
+     */
+    public static final long DEFAULT_INCREMENT = 0x9e3779b97f4a7c15L;
+
+    /**
+     * The initial hash value.
+     */
+    private final long initial;
+
+    /**
+     * The value to increment the hash value by.
+     */
+    private final long increment;
+
+    /**
+     * Convert bytes to long.
+     * @param byteArray the byte array to extract the values from.
+     * @param offset the offset to start extraction from.
+     * @param len the length of the extraction, may be longer than 8.
+     * @return
+     */
+    private static long toLong(byte[] byteArray, int offset, int len) {
+        long val = 0;
+        len = Math.min(len, Long.BYTES);
+        for (int i = 0; i < len; i++) {
+            val <<= 8;
+            val |= (byteArray[offset + i] & 0x00FF);
+        }
+        return val;
+    }
+
+    /**
+     * Constructs the SimpleHasher from a byte array.
+     * <p>The byte array is split in 2 and each half is interpreted as a long 
value.
+     * Excess bytes are ignored.  This simplifies the conversion from a Digest 
or hasher algorithm output
+     * to the two values used by the SimpleHasher.</p>
+     * <p><em>If the second long is zero the DEFAULT_INCREMENT is used 
instead.</em></p>
+     * @param buffer the buffer to extract the longs from.
+     * @throws IllegalArgumentException is buffer length is zero.
+     */
+    public SimpleHasher(byte[] buffer) {
+        if (buffer.length == 0) {
+            throw new IllegalArgumentException("buffer length must be greater 
than 0");
+        }
+        int segment = buffer.length / 2;
+        this.initial = toLong(buffer, 0, segment);
+        long possibleIncrement = toLong(buffer, segment, buffer.length - 
segment);
+        this.increment = possibleIncrement == 0 ? DEFAULT_INCREMENT : 
possibleIncrement;
+    }
+
+    /**
+     * Constructs the SimpleHasher from 2 longs.  The long values will be 
interpreted as unsigned values.

Review comment:
       updated.




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to