FileDocCategorySizeDatePackage
HashCode.javaAPI DocAndroid 1.5 API5914Wed May 06 22:41:04 BST 2009org.apache.harmony.misc

HashCode

public final class HashCode extends Object
This class is a convenience method to sequentially calculate hash code of the object based on the field values. The result depends on the order of elements appended. The exact formula is the same as for java.util.List.hashCode. If you need order independent hash code just summate, multiply or XOR all elements.

Suppose we have class:


class Thing {
long id;
String name;
float weight;
}
The hash code calculation can be expressed in 2 forms.

For maximum performance:


public int hashCode() {
int hashCode = HashCode.EMPTY_HASH_CODE;
hashCode = HashCode.combine(hashCode, id);
hashCode = HashCode.combine(hashCode, name);
hashCode = HashCode.combine(hashCode, weight);
return hashCode;
}

For convenience:

public int hashCode() {
return new HashCode().append(id).append(name).append(weight).hashCode();
}
see
java.util.List#hashCode()

Fields Summary
public static final int
EMPTY_HASH_CODE
The hashCode value before any data is appended, equals to 1.
private int
hashCode
Constructors Summary
Methods Summary
public final org.apache.harmony.misc.HashCodeappend(float value)
Appends value's hashCode to the current hashCode.

param
value new element
return
this

        hashCode = combine(hashCode, value);
        return this;
    
public final org.apache.harmony.misc.HashCodeappend(double value)
Appends value's hashCode to the current hashCode.

param
value new element
return
this

        hashCode = combine(hashCode, value);
        return this;
    
public final org.apache.harmony.misc.HashCodeappend(boolean value)
Appends value's hashCode to the current hashCode.

param
value new element
return
this

        hashCode = combine(hashCode, value);
        return this;
    
public final org.apache.harmony.misc.HashCodeappend(java.lang.Object value)
Appends value's hashCode to the current hashCode.

param
value new element
return
this

        hashCode = combine(hashCode, value);
        return this;
    
public final org.apache.harmony.misc.HashCodeappend(int value)
Appends value's hashCode to the current hashCode.

param
value new element
return
this

        hashCode = combine(hashCode, value);
        return this;
    
public final org.apache.harmony.misc.HashCodeappend(long value)
Appends value's hashCode to the current hashCode.

param
value new element
return
this

        hashCode = combine(hashCode, value);
        return this;
    
public static intcombine(int hashCode, boolean value)
Combines hashCode of previous elements sequence and value's hashCode.

param
hashCode previous hashCode value
param
value new element
return
combined hashCode

    
        int v = value ? 1231 : 1237;
        return combine(hashCode, v);
    
public static intcombine(int hashCode, long value)
Combines hashCode of previous elements sequence and value's hashCode.

param
hashCode previous hashCode value
param
value new element
return
combined hashCode

    
        int v = (int) (value ^ (value >>> 32));
        return combine(hashCode, v);
    
public static intcombine(int hashCode, float value)
Combines hashCode of previous elements sequence and value's hashCode.

param
hashCode previous hashCode value
param
value new element
return
combined hashCode

    
        int v = Float.floatToIntBits(value);
        return combine(hashCode, v);
    
public static intcombine(int hashCode, double value)
Combines hashCode of previous elements sequence and value's hashCode.

param
hashCode previous hashCode value
param
value new element
return
combined hashCode

    
        long v = Double.doubleToLongBits(value);
        return combine(hashCode, v);
    
public static intcombine(int hashCode, java.lang.Object value)
Combines hashCode of previous elements sequence and value's hashCode.

param
hashCode previous hashCode value
param
value new element
return
combined hashCode

        return combine(hashCode, value.hashCode());
    
public static intcombine(int hashCode, int value)
Combines hashCode of previous elements sequence and value's hashCode.

param
hashCode previous hashCode value
param
value new element
return
combined hashCode

        return 31 * hashCode + value;
    
public final inthashCode()
Returns accumulated hashCode

    
            
        
        return hashCode;