Standardise computation of signatures.

This commit is contained in:
aherbert 2020-03-12 15:50:37 +00:00
parent 33d6ddc7f9
commit 8fb518e6a1
5 changed files with 62 additions and 8 deletions

View File

@ -22,7 +22,6 @@ import java.nio.LongBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunction;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunctionIdentity;
/**
* An implementation of HashFunction that
@ -43,6 +42,8 @@ public final class MD5Cyclic implements HashFunction {
/**
* The signature for this hash function.
*
* <p>TODO: Make static akin to a serialVersionUID?
*/
private final long signature;
@ -61,7 +62,7 @@ public final class MD5Cyclic implements HashFunction {
// This should not happen
throw new IllegalStateException("Missing the standard MD5 message digest algorithm", e);
}
signature = apply(HashFunctionIdentity.prepareSignatureBuffer(this), 0);
signature = Signatures.getSignature(this);
}
@Override

View File

@ -18,7 +18,6 @@ package org.apache.commons.collections4.bloomfilter.hasher.function;
import org.apache.commons.codec.digest.MurmurHash3;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunction;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunctionIdentity;
/**
* An implementation of HashFunction that
@ -32,6 +31,8 @@ public final class Murmur128x86Cyclic implements HashFunction {
/**
* The name of this hash method.
*
* <p>TODO: Should this be changed to "Murmur3_128_x64"?
*/
public static final String NAME = "Murmur3_x64_128";
@ -42,6 +43,8 @@ public final class Murmur128x86Cyclic implements HashFunction {
/**
* The signature for this hash function.
*
* <p>TODO: Make static akin to a serialVersionUID?
*/
private final long signature;
@ -49,7 +52,7 @@ public final class Murmur128x86Cyclic implements HashFunction {
* Constructs a Murmur3 x64 128 hash.
*/
public Murmur128x86Cyclic() {
signature = apply(HashFunctionIdentity.prepareSignatureBuffer(this), 0);
signature = Signatures.getSignature(this);
}
@Override

View File

@ -18,7 +18,6 @@ package org.apache.commons.collections4.bloomfilter.hasher.function;
import org.apache.commons.codec.digest.MurmurHash3;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunction;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunctionIdentity;
/**
* An implementation of HashFunction that
@ -32,11 +31,15 @@ public final class Murmur32x86Iterative implements HashFunction {
/**
* The name of this hash function.
*
* <p>TODO: Should this be changed to "Murmur3_32_x86"?
*/
public static final String NAME = "Murmur3_x86_32";
/**
* The signature for this hash function.
*
* <p>TODO: Make static akin to a serialVersionUID?
*/
private final long signature;
@ -44,7 +47,7 @@ public final class Murmur32x86Iterative implements HashFunction {
* Constructs a Murmur3 x86 32 hash
*/
public Murmur32x86Iterative() {
signature = apply(HashFunctionIdentity.prepareSignatureBuffer(this), 0);
signature = Signatures.getSignature(this);
}
@Override

View File

@ -18,7 +18,6 @@ package org.apache.commons.collections4.bloomfilter.hasher.function;
import java.util.Arrays;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunction;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunctionIdentity;
/**
* An implementation of HashFunction that
@ -39,6 +38,8 @@ public final class ObjectsHashIterative implements HashFunction {
/**
* The signature for this hash function.
*
* <p>TODO: Make static akin to a serialVersionUID?
*/
private final long signature;
@ -51,7 +52,7 @@ public final class ObjectsHashIterative implements HashFunction {
* Constructs a hash that uses the Objects.hash method to has values.
*/
public ObjectsHashIterative() {
signature = apply(HashFunctionIdentity.prepareSignatureBuffer(this), 0);
signature = Signatures.getSignature(this);
}
@Override

View File

@ -0,0 +1,46 @@
/*
* 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.function;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunction;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunctionIdentity;
/**
* Allow computation of HashFunction signatures.
* @since 4.5
*/
final class Signatures {
/** No instances. */
private Signatures() {}
/**
* Gets the standard signature for the hash function. The signature is prepared as:
* <pre><code>
* int seed = 0;
* return hashFunction.apply(HashFunctionIdentity.prepareSignatureBuffer(hashFunction), seed);
* </code></pre>
*
* @param hashFunction the hash function
* @return the signature
* @see HashFunctionIdentity#prepareSignatureBuffer(HashFunctionIdentity)
* @see HashFunction#apply(byte[], int)
*/
static long getSignature(HashFunction hashFunction) {
return hashFunction.apply(HashFunctionIdentity.prepareSignatureBuffer(hashFunction), 0);
}
}