org.helidb.lang.hasher
Class ConstantSizeValueHasher<T extends Comparable<T>>

java.lang.Object
  extended by org.helidb.lang.hasher.ConstantSizeValueHasher<T>
Type Parameters:
T - The type of value hashed.
All Implemented Interfaces:
Hasher<T,T>

public class ConstantSizeValueHasher<T extends Comparable<T>>
extends Object
implements Hasher<T,T>

This is a generic value-preserving Hasher. It uses a Serializer for its serializing methods.

Since:
1.0
Author:
Karl Gustafsson
In_jar:
helidb-core

Constructor Summary
ConstantSizeValueHasher(Serializer<T> serializer)
          Constructor.
 
Method Summary
 T fromBytes(byte[] barr)
          Interpret the serialized hash value.
 int getHashLength()
          Get the size of the hash in bytes.
 T hash(T o)
          Hash the supplied value.
 boolean isPreservingValues()
          Does the hashing operation not modify the hashed value at all? This is true if v.equals(hasher.hash(v)) for all values v accepted by the hasher.
 byte[] toBytes(T o)
          Serialize the hash to a byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConstantSizeValueHasher

public ConstantSizeValueHasher(Serializer<T> serializer)
                        throws IllegalArgumentException
Constructor.

Parameters:
serializer - The serializer used for the serializing methods.
Throws:
IllegalArgumentException - If the serializer does not produce values of a constant size.
Method Detail

isPreservingValues

public boolean isPreservingValues()
Description copied from interface: Hasher
Does the hashing operation not modify the hashed value at all? This is true if v.equals(hasher.hash(v)) for all values v accepted by the hasher.

Specified by:
isPreservingValues in interface Hasher<T extends Comparable<T>,T extends Comparable<T>>
Returns:
true, always.

getHashLength

public int getHashLength()
Description copied from interface: Hasher
Get the size of the hash in bytes. This is the size of the byte array created by Hasher.toBytes(Comparable) and accepted by Hasher.fromBytes(byte[]).

Specified by:
getHashLength in interface Hasher<T extends Comparable<T>,T extends Comparable<T>>
Returns:
The size of the hash, in number of bytes.

hash

public T hash(T o)
                             throws NullPointerException
Description copied from interface: Hasher
Hash the supplied value.

Hashing may be a fairly expensive operation, depending on the implementation. Clients should try to reuse the result from this method as much as possible.

Specified by:
hash in interface Hasher<T extends Comparable<T>,T extends Comparable<T>>
Parameters:
o - The value to hash.
Returns:
o
Throws:
NullPointerException - If o == null and the serializer does not support null values.

toBytes

public byte[] toBytes(T o)
Description copied from interface: Hasher
Serialize the hash to a byte array.

For a given Hasher object, the byte arrays returned by this method must always be of the same size.

Specified by:
toBytes in interface Hasher<T extends Comparable<T>,T extends Comparable<T>>
Parameters:
o - The hash to serialize.
Returns:
The serialized hash.

fromBytes

public T fromBytes(byte[] barr)
Description copied from interface: Hasher
Interpret the serialized hash value.

Specified by:
fromBytes in interface Hasher<T extends Comparable<T>,T extends Comparable<T>>
Parameters:
barr - The serialized hash value.
Returns:
The hash value.