com.netflix.util
Class HashCode

java.lang.Object
  extended by com.netflix.util.HashCode

public class HashCode
extends java.lang.Object

Helper class for computing hash codes.

The idea here is that objects that define "equals" to mean that a number of member variables are equal needs a hash code that is computed from those same member variables.

Example:

 class MyClass {
    ...
    private int membVar1;
    private OtherClass membVar2;
    private float membVar3;
    ...
    // MyClass objects are equal iff they have the same content.
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        } else if (obj == null || obj.getClass() != getClass()) {
            return false;
        }
        MyClass cObj = (MyClass)obj;
        return (membVar1 == cObj.membVar1)
            && HashCode.equalObjects(membVar2, cObj.membVar2)
            && (membVar3 == bVar3);
    }

    // The hash code needs to align with the
    // definition of equals.
    public int hashCode() {
        HashCode h = new HashCode();
        h.addValue(membVar1);
        h.addValue(membVar2);
        h.addValue(membVar2);
        return h.hashCode();
    }
    ...
 }
 


Constructor Summary
HashCode()
          Create a new HashCode object
 
Method Summary
 int addValue(boolean b)
          Augment the current computed hash code with the value b.
 int addValue(boolean[] array)
          Augment the current computed hash code with the value array.
 int addValue(byte i)
          Augment the current computed hash code with the value i.
 int addValue(byte[] array)
          Augment the current computed hash code with the value i.
 int addValue(char i)
          Augment the current computed hash code with the value i.
 int addValue(char[] array)
          Augment the current computed hash code with the value array.
 int addValue(double f)
          Augment the current computed hash code with the value f.
 int addValue(double[] array)
          Augment the current computed hash code with the value array.
 int addValue(float f)
          Augment the current computed hash code with the value f.
 int addValue(float[] array)
          Augment the current computed hash code with the value array.
 int addValue(int i)
          Augment the current computed hash code with the value i.
 int addValue(int[] array)
          Augment the current computed hash code with the value array.
 int addValue(java.lang.Object obj)
          Augment the current computed hash code with the value obj.
 int addValue(java.lang.Object[] array)
          Augment the current computed hash code with the value array.
 int addValue(short i)
          Augment the current computed hash code with the value i.
 int addValue(short[] array)
          Augment the current computed hash code with the value array.
static boolean equalObjects(java.lang.Object o1, java.lang.Object o2)
          Utility function to make it easy to compare two, possibly null, objects.
 boolean equals(java.lang.Object obj)
           
 int hashCode()
          Get the currently computed hash code value.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HashCode

public HashCode()
Create a new HashCode object

Method Detail

addValue

public int addValue(java.lang.Object obj)
Augment the current computed hash code with the value obj.

Parameters:
obj - value being added
Returns:
the new hash code.

addValue

public int addValue(boolean b)
Augment the current computed hash code with the value b.

Parameters:
b - value being added
Returns:
the new hash code.

addValue

public int addValue(byte i)
Augment the current computed hash code with the value i.

Parameters:
i - value being added
Returns:
the new hash code.

addValue

public int addValue(char i)
Augment the current computed hash code with the value i.

Parameters:
i - value being added
Returns:
the new hash code.

addValue

public int addValue(short i)
Augment the current computed hash code with the value i.

Parameters:
i - value being added
Returns:
the new hash code.

addValue

public int addValue(int i)
Augment the current computed hash code with the value i.

Parameters:
i - value being added
Returns:
the new hash code.

addValue

public int addValue(float f)
Augment the current computed hash code with the value f.

Parameters:
f - value being added
Returns:
the new hash code.

addValue

public int addValue(double f)
Augment the current computed hash code with the value f.

Parameters:
f - value being added
Returns:
the new hash code.

addValue

public int addValue(java.lang.Object[] array)
Augment the current computed hash code with the value array.

Parameters:
array - value being added
Returns:
the new hash code.

addValue

public int addValue(boolean[] array)
Augment the current computed hash code with the value array.

Parameters:
array - value being added
Returns:
the new hash code.

addValue

public int addValue(byte[] array)
Augment the current computed hash code with the value i.

Parameters:
array - value being added
Returns:
the new hash code.

addValue

public int addValue(char[] array)
Augment the current computed hash code with the value array.

Parameters:
array - value being added
Returns:
the new hash code.

addValue

public int addValue(short[] array)
Augment the current computed hash code with the value array.

Parameters:
array - value being added
Returns:
the new hash code.

addValue

public int addValue(int[] array)
Augment the current computed hash code with the value array.

Parameters:
array - value being added
Returns:
the new hash code.

addValue

public int addValue(float[] array)
Augment the current computed hash code with the value array.

Parameters:
array - value being added
Returns:
the new hash code.

addValue

public int addValue(double[] array)
Augment the current computed hash code with the value array.

Parameters:
array - value being added
Returns:
the new hash code.

equalObjects

public static boolean equalObjects(java.lang.Object o1,
                                   java.lang.Object o2)
Utility function to make it easy to compare two, possibly null, objects.

Parameters:
o1 - first object
o2 - second object
Returns:
true iff either both objects are null, or neither are null and they are equal.

hashCode

public int hashCode()
Get the currently computed hash code value.

Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object