Class HashCodeUtil

java.lang.Object
rs.baselib.lang.HashCodeUtil

public final class HashCodeUtil extends Object
Collected methods which allow easy implementation of hashCode.

Implementation is based on the recommendations of Effective Java, by Joshua Bloch.

Example use case:
  public int hashCode(){
    int result = HashCodeUtil.SEED;
    //collect the contributions of various fields
    result = HashCodeUtil.hash(result, fPrimitive);
    result = HashCodeUtil.hash(result, fObject);
    result = HashCodeUtil.hash(result, fArray);
    return result;
  }
 
Author:
ralph
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    An initial value for a hashCode, to which is added contributions from fields.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    hash(int aSeed, boolean aBoolean)
    Hash booleans.
    static int
    hash(int aSeed, char aChar)
    Hash chars.
    static int
    hash(int aSeed, double aDouble)
    Hash doubles.
    static int
    hash(int aSeed, float aFloat)
    Hash floats.
    static int
    hash(int aSeed, int aInt)
    Hash ints.
    static int
    hash(int aSeed, long aLong)
    Hash longs.
    static int
    hash(int aSeed, Object aObject)
    aObject is a possibly-null object field, and possibly an array.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SEED

      public static final int SEED
      An initial value for a hashCode, to which is added contributions from fields. Using a non-zero value decreases collisons of hashCode values.
      See Also:
  • Constructor Details

    • HashCodeUtil

      public HashCodeUtil()
  • Method Details

    • hash

      public static int hash(int aSeed, boolean aBoolean)
      Hash booleans.
      Parameters:
      aSeed - - a previous seed
      aBoolean - - the value to combine in hash
      Returns:
      the combined hash code
    • hash

      public static int hash(int aSeed, char aChar)
      Hash chars.
      Parameters:
      aSeed - - a previous seed
      aChar - - the value to combine in hash
      Returns:
      the combined hash code
    • hash

      public static int hash(int aSeed, int aInt)
      Hash ints.
      Parameters:
      aSeed - - a previous seed
      aInt - - the value to combine in hash
      Returns:
      the combined hash code
    • hash

      public static int hash(int aSeed, long aLong)
      Hash longs.
      Parameters:
      aSeed - - a previous seed
      aLong - - the value to combine in hash
      Returns:
      the combined hash code
    • hash

      public static int hash(int aSeed, float aFloat)
      Hash floats.
      Parameters:
      aSeed - - a previous seed
      aFloat - - the value to combine in hash
      Returns:
      the combined hash code
    • hash

      public static int hash(int aSeed, double aDouble)
      Hash doubles.
      Parameters:
      aSeed - - a previous seed
      aDouble - - the value to combine in hash
      Returns:
      the combined hash code
    • hash

      public static int hash(int aSeed, Object aObject)
      aObject is a possibly-null object field, and possibly an array. If aObject is an array, then each element may be a primitive or a possibly-null object.
      Parameters:
      aSeed - - a previous seed
      aObject - - the value to combine in hash
      Returns:
      the combined hash code