Tim Driven Development | Test until the fear goes away
Tushar Goel wrote:Corrected one is as below:
We are trying to make you think about it.Tushar Goel wrote:But i considering unique id.. No 2 customer can have same id...
. . .
confirm that you cannot get duplicate IDs
You should always use the same fields in hash code and equals.
Tushar Goel wrote:It is not possible to have same ID for two customers
... but by adding extra checks i think it is making doubly sure to have unique object only.
If they aren't done correctly in the place where they should be done, then making your equals() method incompatible.
In which case you need to handle those conditions. I can think of three ways to handle the shutdown problem but one involves a database and we decided earlier not to use databases in this instance. Consider a shutdown hook on the runtime which does one of these thingsTushar Goel wrote: . . . I am unable to think about the cases in which customerId is duplicated(except the case when application is shutdown and restarted again or the case of
multithreaded.). . . .
And the way to correct it is to make sure a null never gets that far.Tushar Goel wrote: . . . my current hashCode implementation is wrong as when i passed null to the fields it throws NullPointerException.. I need to correct that one too...
. . .
You need to go through the constructor and tease it apart with pencil and paper and confirm whether there are any circumstances in which you can get a duplicate ID. I presume that is what you did earlier when you said shut-down or threading. In which case check what you did to confirm whether your conclusion is correct.Tushar Goel wrote: . . . Because if my implementation is right then as Campbell suggested i need not to use anything else except customerID in equals() and hasCode().
. . .
Consider a shutdown hook on the runtime which does one of these things
If you are using AtomicInteger, doesn't that obviate any problems caused by threading? Have you checked?
you did earlier when you said shut-down or threading. In which case check what you did to confirm whether your conclusion is correct.
What if you have two Bank instances in different threads? Can you get duplicate IDs in those instances?
Consider a shutdown hook on the runtime which does one of these things
Now let's see you write that in Java8 notation without defining a Runnable class!
Try here for the wrong spelling.
Now let's see you write that in Java8 notation without defining a Runnable class!
Is LocalDate immutable?
Do you really want to reset names with set methods?
whether you should or shouldn't use getClass in an equals method.
What validation of names are you doing in those set methods?
Why are you using a method rather than the field in equals? Why are you using else after a return?
You may have checked that before, but I couldn't remember, so I asked it again.Tushar Goel wrote:
Is LocalDate immutable?
Yes it is.. . . .