@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Dog)) return false; Dog dog = (Dog) o; if (heightInCm != dog.heightInCm) return false; if (lengthInCm != dog.lengthInCm) return false; if (neutered != dog.neutered) return false; if (weightInKg != dog.weightInKg) return false; if (color != null ? !color.equals(dog.color) : dog.color != null) return false; if (name != null ? !name.equals(dog.name) : dog.name != null) return false; if (sex != dog.sex) return false; return true; }
/** * Implemented without class checking * */ @Override public boolean equals(Object o) { if (this == o) return true; return o.toString().equals(this.toString()); }
@Test public void sanityCheckTestDomain() { Identifier pistachio2Id = new Identifier("Pistachio"); Beagle pistachio2 = (Beagle) new Beagle().name(pistachio2Id).color("tricolor") .sex(MALE).weight(7).length(50l).height(30l).neutered(false); Identifier pistachio3Id = new Identifier("Pistachio 2"); Beagle pistachio3 = (Beagle) new Beagle().name(pistachio3Id).color("tricolor") .sex(MALE).weight(7).length(50l).height(30l).neutered(true); assertNotEquals(pistachio2Id, pistachio3Id); assertNotEquals(pistachio2Id.hashCode(), pistachio3Id.hashCode()); Identifier2 id2 = new Identifier2("22"); Identifier2 id3 = new Identifier2("23"); assertNotEquals(id2, id3); assertNotEquals(id2.hashCode(), id3.hashCode()); assertEquals(pistachio, pistachio2); assertEquals(pistachio.hashCode(), pistachio2.hashCode()); assertNotEquals(pistachio, pistachio3); assertNotEquals(pistachio.hashCode(), pistachio3.hashCode()); pistachio.bay(10, 30); skinny.bay(20,32); juno.herd(); }
@Override public int hashCode() { int result = getName() != null ? getName().hashCode() : 0; result = 31 * result + (getColor() != null ? getColor().hashCode() : 0); result = 31 * result + getWeight(); result = 31 * result + (int) (getLengthInCm() ^ (getLengthInCm() >>> 32)); result = 31 * result + (int) (getHeight() ^ (getHeight() >>> 32)); result = 31 * result + (getSex() != null ? getSex().hashCode() : 0); result = 31 * result + (isNeutered() ? 1 : 0); return result; }