[GitHub] [commons-collections] garydgregory commented on a change in pull request #83: WIP: Initial bloom filter code contribution

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[GitHub] [commons-collections] garydgregory commented on a change in pull request #83: WIP: Initial bloom filter code contribution

GitBox
garydgregory commented on a change in pull request #83: WIP: Initial bloom filter code contribution
URL: https://github.com/apache/commons-collections/pull/83#discussion_r323907762
 
 

 ##########
 File path: src/main/java/org/apache/commons/collections4/bloomfilters/Hash.java
 ##########
 @@ -0,0 +1,100 @@
+/*
+ * 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.bloomfilters;
+
+import java.io.Serializable;
+import java.util.BitSet;
+
+import org.apache.commons.lang3.builder.HashCodeBuilder;
+
+/**
+ * A Bloom Filter hash calculation.  This class only stores the result of an
+ * external hashs calculation.  It does not perform the calculation itself.
+ *
+ * The hash is calculated as a 128-bit value. We store this as two 64-bit
+ * values. We can then rapidly calculate the bloom filter for any given
+ * configuration.
+ *
+ */
+public class Hash implements Comparable<Hash>, Serializable {
+ /**
+ *
+ */
+ private static final long serialVersionUID = -5511398264986350484L;
+ private long h1;
+ private long h2;
+ private transient Integer hashCode;
+
+ public Hash(long h1, long h2) {
+ this.h1 = h1;
+ this.h2 = h2;
+ }
+
+ public BitSet populate(BitSet set, FilterConfig config) {
+ if (set.size() < config.getNumberOfBits()) {
+ throw new IllegalArgumentException(
+ String.format("Bitset had %s bits, %s required", set.size(), config.getNumberOfBits()));
+ }
+ for (int i = 0; i < config.getNumberOfHashFunctions(); i++) {
+ int j = Math.abs((int) ((h1 + (i * h2)) % config.getNumberOfBits()));
+ set.set(j, true);
+ }
+ return set;
+ }
+
+ public long h1() {
+ return h1;
+ }
+
+ public long h2() {
+ return h2;
+ }
+
+ @Override
+ public int compareTo(Hash other) {
+ int result = Long.compare(h1, other.h1);
+ if (result == 0) {
+ result = Long.compare(h2, other.h2);
+ }
+ return result;
+ }
+
+ @Override
+ public int hashCode() {
+ if (hashCode == null) {
+ hashCode = new HashCodeBuilder().append(h1).append(h2).build();
 
 Review comment:
   `hashCode = Objects.hash(h1, h2);`

----------------------------------------------------------------
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:
[hidden email]


With regards,
Apache Git Services