anthill.util
Class SHADigest

java.lang.Object
  |
  +--anthill.util.SHADigest
All Implemented Interfaces:
Closeness, Comparable, Externalizable, Serializable
Direct Known Subclasses:
DocumentKey, SHADocumentId, SHAHashedKeyword

public class SHADigest
extends Object
implements Comparable, Closeness, Externalizable

The class SHADigest computes the digest (or hash) of the specified content, and enables various other classes to compare the digests and store them in a HashMap or other collections in need of a hashCode method.

Version:
$Revision: 1.7 $
Author:
Hein Meling
See Also:
Serialized Form

Field Summary
protected static MessageDigest digest
          Message digest generator used to produce hash values for strings
static int DIGEST_SIZE
          Byte size of the digest values
static String DIGEST_TYPE
          Name of the digest function to be used
protected  int hashcode
          To avoid recomputing the hash of a big integer we save it
protected static Random random
          Proxy for obtaining random numbers.
static String RANDOM_TYPE
          Name of the random function to be used
protected  BigInteger rdigest
          A big integer for the hash code of the specified keyword.
 
Constructor Summary
SHADigest()
          Default constructor for externalization.
SHADigest(boolean secure)
          Creates a new SHADigest instance based on a randomly generated SHA hash.
 
Method Summary
 int compareTo(Object obj)
           
 int compareTo(SHADigest kwHash)
           
protected  void computeDigest(String str)
          Compute a SHA-1 hash value for the specified string
protected  void computeHash()
          Computes the hash code for the object.
protected  void computeRandomSHA()
          Compute a random SHA-1 hash value for a simulated hash.
 boolean equals(Object obj)
          Check whether this SHADigest is equal to the specified object.
 byte[] getBytes()
          Returns the bytes representing the hashed value of the keyword.
 int hashCode()
          Returns a hash code value for the object.
protected  void initRandom(boolean secure)
          Initialize a random generator.
 int isCloser(Object x, Object y)
          Compares this object against the provided objects for closeness.
 void readExternal(ObjectInput in)
          Marshals this SHADigest to the specified input stream.
 String toString()
          Returns a string representation of the object.
 void writeExternal(ObjectOutput out)
          Marshals this SHADigest to the specified output stream.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

rdigest

protected BigInteger rdigest
A big integer for the hash code of the specified keyword.


hashcode

protected int hashcode
To avoid recomputing the hash of a big integer we save it


RANDOM_TYPE

public static final String RANDOM_TYPE
Name of the random function to be used

See Also:
Constant Field Values

DIGEST_TYPE

public static final String DIGEST_TYPE
Name of the digest function to be used

See Also:
Constant Field Values

DIGEST_SIZE

public static final int DIGEST_SIZE
Byte size of the digest values

See Also:
Constant Field Values

random

protected static Random random
Proxy for obtaining random numbers.


digest

protected static MessageDigest digest
Message digest generator used to produce hash values for strings

Constructor Detail

SHADigest

public SHADigest()
Default constructor for externalization.


SHADigest

public SHADigest(boolean secure)
Creates a new SHADigest instance based on a randomly generated SHA hash.

Parameters:
secure - if true, the secure random generator will be used; otherwise the standard random generator is used.
Method Detail

getBytes

public byte[] getBytes()
Returns the bytes representing the hashed value of the keyword.


computeDigest

protected void computeDigest(String str)
Compute a SHA-1 hash value for the specified string

Parameters:
str - The string for which to compute a SHA-1 hash.

computeRandomSHA

protected void computeRandomSHA()
Compute a random SHA-1 hash value for a simulated hash.


computeHash

protected void computeHash()
Computes the hash code for the object.


initRandom

protected void initRandom(boolean secure)
Initialize a random generator.

Parameters:
secure - if true, the secure random generator will be used; otherwise the standard random generator is used.

isCloser

public int isCloser(Object x,
                    Object y)
Description copied from interface: Closeness
Compares this object against the provided objects for closeness. An implementation of this method must return an int according to the following rules:
    -1 is returned if the value of x is considered closer to this object than the value of y.
    +1 is returned if the value of y is considered closer to this object than the value of x.
     0 is returned if the values of x and y are equally close to this object.
  

Specified by:
isCloser in interface Closeness

compareTo

public int compareTo(Object obj)
              throws ClassCastException
Specified by:
compareTo in interface Comparable
ClassCastException

compareTo

public int compareTo(SHADigest kwHash)

equals

public boolean equals(Object obj)
Check whether this SHADigest is equal to the specified object.

Overrides:
equals in class Object

hashCode

public int hashCode()
Returns a hash code value for the object.

Overrides:
hashCode in class Object

toString

public String toString()
Returns a string representation of the object.

Overrides:
toString in class Object

readExternal

public void readExternal(ObjectInput in)
                  throws IOException
Marshals this SHADigest to the specified input stream.

Specified by:
readExternal in interface Externalizable
Parameters:
in - an ObjectInput stream object
Throws:
IOException - if an error occurs

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Marshals this SHADigest to the specified output stream.

Specified by:
writeExternal in interface Externalizable
Parameters:
out - an ObjectOutput stream object
Throws:
IOException - if an error occurs