This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am using org.apache.commons.lang.builder.HashCodeBuilder to generate the hashcode of an object . Can anyone explain where to use the HashCodeBuilder() and where HashCodeBuilder(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber) has to be used . I read the docs but couldnt understand . Can anyone elaborate .
You would apply a SomethingBuilder to the class itself; what it does is take all the fields and works out their hash codes. Then it takes a starting value (initialNonZero...) and in turn adds those hash codes to it. After each addition it multiplies the total by the other non-zero number. Then it turns that operation into Java™ code and inserts it into the class as a hashCode method.
You use it when writing the code. You can get similar functionality from IDEs, where you have an option to "insert equals/hashCode method from fields".
Thank you for your prompt reply but what i am asking is the advantage of one constructor over the other ie the HashCodeBuilder() and the
HashCodeBuilder(int startValue, int multiplier). Can you please explain ?
Joined: Oct 13, 2005
It allows you to specify the multiplication factor.You can start off with a different value from 0 for hash and a different value from 0x1f (31) for multiplier.
That makes a really really big difference, particularly if you know all about has algorithms and can think of a better algorithm . . . and if you believe that, you'll believe anything.
They think there are people who want the option of choosing their own multiplier and starting value. Remember this is a library class. When you design ordinary classes, you want to give them the smallest public interface yo ucan get away with. When you design library classes, you tend to put things in because it is possible somebody would want them. So library classes tend to ahve big public interfaces.