All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.spring.mcache.HashCodeCalculator Maven / Gradle / Ivy

package org.onetwo.common.spring.mcache;

/*****
 * copy from spring modules
 *
 */
public class HashCodeCalculator {
	private static final int INITIAL_HASH = 17;

	  private static final int MULTIPLIER = 37;

	  private long checkSum;

	  /**
	   * Counts the number of times {@link #append(int)} is executed.
	   * It is also used to build {@link #checkSum} and
	   * {@link #hashCode}.
	   */
	  private int count;

	  /**
	   * Hash code to build;
	   */
	  private int hashCode;

	  /**
	   * Constructor.
	   */
	  public HashCodeCalculator() {
	    super();
	    hashCode = INITIAL_HASH;
	  }

	  /**
	   * Recalculates {@link #checkSum} and
	   * {@link #hashCode} using the specified value.
	   *
	   * @param value
	   *          the specified value.
	   */
	  public void append(int value) {
	    count++;
	    int valueToAppend = count * value;

	    hashCode = MULTIPLIER * hashCode + (valueToAppend ^ (valueToAppend >>> 16));
	    checkSum += valueToAppend;
	  }

	  /**
	   * @return the number that ensures that the combination hashCode/checSum is
	   *         unique
	   */
	  public long getCheckSum() {
	    return checkSum;
	  }

	  /**
	   * @return the calculated hash code
	   */
	  public int getHashCode() {
	    return hashCode;
	  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy