Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Customer {
- private String firstName;
- private String lastName;
- private Date birthday;
- private String street;
- private String city;
- private String zipcode;
- protected boolean equalFields(Customer rhs) {
- return ObjectUtils.eqFields(firstName, rhs.firstName) &&
- ObjectUtils.eqFields(lastName, rhs.lastName) &&
- ObjectUtils.eqFields(birthday, rhs.birthday) &&
- ObjectUtils.eqFields(city, rhs.city) &&
- ObjectUtils.eqFields(zipcode, rhs.zipcode);
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) return true;
- return (obj instanceof Customer &&
- ((Customer)obj).equalFields(this));
- }
- @Override
- public int hashCode() {
- int result = 21 + ObjectUtils.hash(birthday);
- result = result * 31 + ObjectUtils.hash(city);
- result = result * 31 + ObjectUtils.hash(firstName);
- result = result * 31 + ObjectUtils.hash(lastName);
- result = result * 31 + ObjectUtils.hash(street);
- return result * 31 + ObjectUtils.hash(zipcode);
- }
- }
- // this class would be public in a package say utils
- class ObjectUtils {
- public static boolean eqFields (Object a, Object b) {
- if (a != null)
- return a.equals(b);
- else
- return (b == null);
- }
- public static int hash(Object obj) {
- return ((obj == null) ? 0 : obj.hashCode());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement