X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=common%2Futil%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Futil%2FHashCodeBuilder.java;h=650c00e15043ee43a99709bec8047c30bddd77d4;hb=e4e6eb9122d8aaa5201ede13f4694804ec24f78c;hp=1b6b881556e0c8fab32154028436ade18ec64410;hpb=dd8b87bc7b7fc403735cd71fecc272d40ca9c673;p=yangtools.git diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/HashCodeBuilder.java b/common/util/src/main/java/org/opendaylight/yangtools/util/HashCodeBuilder.java index 1b6b881556..650c00e150 100644 --- a/common/util/src/main/java/org/opendaylight/yangtools/util/HashCodeBuilder.java +++ b/common/util/src/main/java/org/opendaylight/yangtools/util/HashCodeBuilder.java @@ -7,20 +7,27 @@ */ package org.opendaylight.yangtools.util; -import org.opendaylight.yangtools.concepts.Builder; - /** - * Utility class for incrementally building object hashCode by hashing together - * component objects, one by one. + * Utility class for incrementally building object hashCode by hashing together component objects, one by one. * - * @param Component objec type + * @param Component object type */ -public final class HashCodeBuilder implements Builder { +public final class HashCodeBuilder { + /** + * The value 31 was chosen because it is an odd prime. If it were even and the multiplication overflowed, + * information would be lost, as multiplication by 2 is equivalent to shifting. The advantage of using a prime is + * less clear, but it is traditional. A nice property of 31 is that the multiplication can be replaced by a shift + * and a subtraction for better performance: 31 * i == (i << 5) - i. Modern VMs do this sort of optimization + * automatically. + * + *

(from Joshua Bloch's Effective Java, Chapter 3, Item 9: Always override hashcode when you override equals, + * page 48) + */ + private static final int PRIME = 31; private int currentHash; /** - * Create a new instance, with internal hash initialized to 1, - * equivalent of HashCodeBuilder(1). + * Create a new instance, with internal hash initialized to 1, equivalent of HashCodeBuilder(1). */ public HashCodeBuilder() { this(1); @@ -36,20 +43,18 @@ public final class HashCodeBuilder implements Builder { } /** - * Determine the next hash code combining a base hash code and the - * hash code of an object. + * Determine the next hash code combining a base hash code and the hash code of an object. * * @param hashCode base hash code * @param obj Object to be added * @return Combined hash code */ public static int nextHashCode(final int hashCode, final Object obj) { - return 31 * hashCode + obj.hashCode(); + return PRIME * hashCode + obj.hashCode(); } /** - * Update the internal hash code with the hash code of a component - * object. + * Update the internal hash code with the hash code of a component object. * * @param obj Component object */ @@ -57,13 +62,12 @@ public final class HashCodeBuilder implements Builder { currentHash = nextHashCode(currentHash, obj); } - @Override - public Integer build() { - return currentHash; - } - - @Deprecated - public Integer toInstance() { + /** + * Return the currently-accumulated hash code. + * + * @return Current hash code + */ + public int build() { return currentHash; } }