viceroy
Class HashValue

java.lang.Object
  |
  +--viceroy.HashValue
All Implemented Interfaces:
java.lang.Comparable

public final class HashValue
extends java.lang.Object
implements java.lang.Comparable

Represents the result value of a hash-function on some object. Note: This class is immutable.

Author:
Anat Talmy
, Oren Dobzinski

Field Summary
static int DEFAULT_PRECISION_BITS
          The default number of precision bits in this HashValue's value
protected static org.apache.log4j.Logger logger
          Logger for debug
static HashValue MAX
          Represents a MAX HashValue, i.e.
static HashValue MIN
          Represents a MIN HashValue, i.e.
protected static java.math.BigDecimal ONE_DECIMAL
          Represents a BigDecimal of the value 1.0
private  int precisionBits
          The Defualt precision bits
private static java.util.Random random
          A random number
private  java.math.BigDecimal value
          The actual value held
private static java.math.BigDecimal ZERO_DECIMAL
          Represents a BigDecimal of the value 0.0
 
Constructor Summary
  HashValue()
          creates a new HashValue with a random value (uniform distribution) with the default number of bits.
  HashValue(HashValue old, int precisionBitsToAdd, HashValue min)
          Creates a new HashValue, which is based on the given one, making it more precise, according to the given parameter.
  HashValue(int _precisionBits)
          Creates a new HashValue with a random value (uniform distribution) with the requested number of bits.
  HashValue(java.lang.String val)
          Creates a HashValue from a given string
  HashValue(java.lang.String val, int _precisionBits)
          Creates a HashValue from a given string and precision bits.
private HashValue(java.lang.String val, java.lang.String name)
           
 
Method Summary
 java.math.BigDecimal absoluteDistance(HashValue other)
          Gets the absolute distance between this and other.
 java.math.BigDecimal clockwiseDistance(HashValue other)
          Calculates the clock-wise distance from this HashValue to the given value.
 int compareTo(java.lang.Object o)
          Compares this object with the specified object for order.
 boolean equals(java.lang.Object obj)
          Checks if a given object equals to me.
private static java.math.BigDecimal getRandomInRange(int _precisionBits)
          Gets a random in the specific range
 int hashCode()
          uses the value's hashcode to return a hashcode
 boolean isInStretch(HashValue begin, HashValue end)
          Checks if this HashValue is between begin and end.
 boolean isInStretchExclusive(HashValue begin, HashValue end)
          Checks if this HashValue is between begin and end, clock-wise and exclusive.
 boolean isInStretchExclusiveNonCyclic(HashValue begin, HashValue end)
          Checks if this HashValue is in the Sretch (begin,end) but only if begin < end.
 boolean isInStretchNonCyclic(HashValue begin, HashValue end)
          Checks if this HashValue is in the Sretch (begin,end] but only if begin < end.
static int log10(double x)
          Returns the log10 of a given number NOTE<\B> This method returns a floor rounding!!!
static int log2(double x)
          Returns the log2 of a given number NOTE<\B> This method returns a floor rounding!!!
static void main(java.lang.String[] args)
          The main for that class
static int numDecimalDigits(java.math.BigInteger num)
          Returns the number of decimal digits of a given BigInteger
static int numDecimalDigitsAfterPoint(java.math.BigDecimal num)
          Returns the number of decimal digits after the point of a given BigInteger
static void testHashValue()
          Unit testing
 java.lang.String toString()
          to string
 java.math.BigDecimal value()
          Return the value of the HashValue
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

protected static org.apache.log4j.Logger logger
Logger for debug


random

private static java.util.Random random
A random number


ONE_DECIMAL

protected static final java.math.BigDecimal ONE_DECIMAL
Represents a BigDecimal of the value 1.0


ZERO_DECIMAL

private static final java.math.BigDecimal ZERO_DECIMAL
Represents a BigDecimal of the value 0.0


MIN

public static final HashValue MIN
Represents a MIN HashValue, i.e. of the value 0.0


MAX

public static final HashValue MAX
Represents a MAX HashValue, i.e. of the value 1.0


value

private java.math.BigDecimal value
The actual value held


DEFAULT_PRECISION_BITS

public static final int DEFAULT_PRECISION_BITS
The default number of precision bits in this HashValue's value

See Also:
Constant Field Values

precisionBits

private int precisionBits
The Defualt precision bits

Constructor Detail

HashValue

public HashValue()
creates a new HashValue with a random value (uniform distribution) with the default number of bits.

Throws:
java.lang.ArithmeticException - bitLength < 2.
See Also:
DEFAULT_PRECISION_BITS

HashValue

public HashValue(int _precisionBits)
Creates a new HashValue with a random value (uniform distribution) with the requested number of bits.

Throws:
java.lang.ArithmeticException - bitLength < 2.

HashValue

public HashValue(HashValue old,
                 int precisionBitsToAdd,
                 HashValue min)
Creates a new HashValue, which is based on the given one, making it more precise, according to the given parameter. Note: The new HashValue has some more precision bits, but it is always in the stretch of (min,old) clockwise. It means that min can be larger than old.

Parameters:
old - the value to base on
precisionBitsToAdd - the number of precision bits to add
min - the value that the new hash value should be bigger than. Usually it is the peer predecessor's id
Throws:
java.lang.IllegalArgumentException - if numBitsToAdd is negative

HashValue

public HashValue(java.lang.String val,
                 int _precisionBits)
Creates a HashValue from a given string and precision bits.

Parameters:
val - The value that the hash would be
_precisionBits - The precision bits it would use

HashValue

private HashValue(java.lang.String val,
                  java.lang.String name)

HashValue

public HashValue(java.lang.String val)
Creates a HashValue from a given string

Parameters:
val - The given value to create the HashValue
Method Detail

getRandomInRange

private static java.math.BigDecimal getRandomInRange(int _precisionBits)
Gets a random in the specific range

Parameters:
_precisionBits -
Returns:

clockwiseDistance

public java.math.BigDecimal clockwiseDistance(HashValue other)
Calculates the clock-wise distance from this HashValue to the given value. E.g.: clockwise dist (0.1,other - 0.2) = 0.1 clockwise dist (0.2,other - 0.1) = 0.9

Parameters:
other - the given HashValue, to which we calculate the distance to.
Returns:
the clock-wise distance from this HashValue to the given value. returned value is in the range [0..1)

absoluteDistance

public java.math.BigDecimal absoluteDistance(HashValue other)
Gets the absolute distance between this and other. Absolute distance is the actual distance, in the clock-wise or in the counter-clockwise direction. returned value is in the range [0..1)

Parameters:
other - the given HashValue, to which we calculate the distance to.
Returns:
the absolute distance between this and other. Absolute distance is the actual distance, in the clock-wise or in the counter-clockwise direction. returned value is in the range [0..1)

value

public java.math.BigDecimal value()
Return the value of the HashValue

Returns:
the value of the HashValue

isInStretch

public boolean isInStretch(HashValue begin,
                           HashValue end)
Checks if this HashValue is between begin and end. NOTE:<\B> this method handles also situations where begin > end !!!! where the range is 'closed' (mahzoriut) (begin,end]

Parameters:
begin - the start of the range to check (stretch)
end - the end of the range to check (stretch)
Returns:
true if this HashValue is in the given stretch, false otherwise.

isInStretchNonCyclic

public boolean isInStretchNonCyclic(HashValue begin,
                                    HashValue end)
Checks if this HashValue is in the Sretch (begin,end] but only if begin < end. i.e. without Cyclic <\B> returns true if begin= end or if this.id is not in between (begin,end]

Parameters:
begin - the start of the range to check (stretch)
end - the end of the range to check (stretch)
Returns:
true if begin= end or if this.id is not in between (begin,end]

isInStretchExclusive

public boolean isInStretchExclusive(HashValue begin,
                                    HashValue end)
Checks if this HashValue is between begin and end, clock-wise and exclusive. this method handles also situations where begin > end, where the range is 'closed' (mahzoriut) (begin,end)

Parameters:
begin - the start of the range to check (stretch)
end - the end of the range to check (stretch)
Returns:
true if this HashValue is in the given stretch, false otherwise.

isInStretchExclusiveNonCyclic

public boolean isInStretchExclusiveNonCyclic(HashValue begin,
                                             HashValue end)
Checks if this HashValue is in the Sretch (begin,end) but only if begin < end. i.e. without Cyclic <\B> returns true if begin= end or if this.id is not in between (begin,end)

Parameters:
begin - the start of the range to check (stretch)
end - the end of the range to check (stretch)
Returns:
true if begin= end or if this.id is not in between (begin,end)

hashCode

public int hashCode()
uses the value's hashcode to return a hashcode

Overrides:
hashCode in class java.lang.Object
Returns:
a hashcode

compareTo

public int compareTo(java.lang.Object o)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.

Finally, the implementer must ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.

It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o - the Object to be compared.
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Throws:
java.lang.ClassCastException - if the specified object's type prevents it from being compared to this Object.

equals

public boolean equals(java.lang.Object obj)
Checks if a given object equals to me. We are only interested in the numeric value and not in the scale.

Overrides:
equals in class java.lang.Object
Parameters:
obj - The object to compare with
Returns:
true if the id of the given obj equals to my id false otherwise and if the obj is not an instance of HashValue

toString

public java.lang.String toString()
to string

Overrides:
toString in class java.lang.Object
Returns:

log2

public static int log2(double x)
Returns the log2 of a given number NOTE<\B> This method returns a floor rounding!!!

Parameters:
x - The number to be logged
Returns:
the log2 of a given number

log10

public static int log10(double x)
Returns the log10 of a given number NOTE<\B> This method returns a floor rounding!!!

Parameters:
x - The number to be logged
Returns:
the log10 of a given number

numDecimalDigits

public static int numDecimalDigits(java.math.BigInteger num)
Returns the number of decimal digits of a given BigInteger

Parameters:
num - the given integer
Returns:
the number of decimal digits of a given BigInteger

numDecimalDigitsAfterPoint

public static int numDecimalDigitsAfterPoint(java.math.BigDecimal num)
Returns the number of decimal digits after the point of a given BigInteger

Parameters:
num - the given integer
Returns:
the number of decimal digits after the point of a given BigInteger

testHashValue

public static void testHashValue()
Unit testing


main

public static void main(java.lang.String[] args)
The main for that class

Parameters:
args -