isSetter now recognizes Builder pattern, where this is returned instead of void
parent
6ba284e8c2
commit
52dec09346
@ -0,0 +1,82 @@
|
||||
package com.moparisthebest.jdbc.dto;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class BuilderPerson implements Person {
|
||||
|
||||
protected long personNo;
|
||||
protected Date birthDate;
|
||||
protected String firstName;
|
||||
protected String lastName;
|
||||
|
||||
private boolean setterCalled = false;
|
||||
|
||||
public BuilderPerson() {
|
||||
}
|
||||
|
||||
public BuilderPerson(Person person) {
|
||||
this.personNo = person.getPersonNo();
|
||||
this.birthDate = person.getBirthDate();
|
||||
this.lastName = person.getLastName();
|
||||
this.firstName = person.getFirstName();
|
||||
this.setterCalled = true; // for convenience
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getPersonNo() {
|
||||
return personNo;
|
||||
}
|
||||
|
||||
public BuilderPerson setPersonNo(final long personNo) {
|
||||
this.personNo = personNo;
|
||||
this.setterCalled = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getBirthDate() {
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
public BuilderPerson setBirthDate(final Date birthDate) {
|
||||
this.birthDate = birthDate;
|
||||
this.setterCalled = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public BuilderPerson setFirstName(final String firstName) {
|
||||
this.firstName = firstName;
|
||||
this.setterCalled = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public BuilderPerson setLastName(final String lastName) {
|
||||
this.lastName = lastName;
|
||||
this.setterCalled = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if(!setterCalled || (o instanceof BuilderPerson && !((BuilderPerson)o).setterCalled))
|
||||
throw new RuntimeException("setter not called");
|
||||
return PersonEqualsHashCode.equals(this, o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
if(!setterCalled)
|
||||
throw new RuntimeException("setter not called");
|
||||
return PersonEqualsHashCode.hashCode(this);
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.moparisthebest.jdbc.dto;
|
||||
|
||||
public class PersonEqualsHashCode {
|
||||
public static boolean equals(final Person thiss, final Object o) {
|
||||
if (thiss == o) return true;
|
||||
if (!(o instanceof Person)) return false;
|
||||
|
||||
final Person that = (Person) o;
|
||||
|
||||
if (thiss.getPersonNo() != that.getPersonNo()) return false;
|
||||
if (thiss.getBirthDate() != null ? !thiss.getBirthDate().equals(that.getBirthDate()) : that.getBirthDate() != null)
|
||||
return false;
|
||||
if (thiss.getFirstName() != null ? !thiss.getFirstName().equals(that.getFirstName()) : that.getFirstName() != null)
|
||||
return false;
|
||||
return thiss.getLastName() != null ? thiss.getLastName().equals(that.getLastName()) : that.getLastName() == null;
|
||||
}
|
||||
|
||||
public static int hashCode(final Person thiss) {
|
||||
int result = (int) (thiss.getPersonNo() ^ (thiss.getPersonNo() >>> 32));
|
||||
result = 31 * result + (thiss.getBirthDate() != null ? thiss.getBirthDate().hashCode() : 0);
|
||||
result = 31 * result + (thiss.getFirstName() != null ? thiss.getFirstName().hashCode() : 0);
|
||||
result = 31 * result + (thiss.getLastName() != null ? thiss.getLastName().hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue