public class Customer { private String firstName; private String lastName; private Date birthday; private String street; private String city; private String zipcode; // follows the Eclipse generated result - two nulls are equal, but a // couple of trivial static functions can take out many lines of code public static boolean eqFields (Object a, Object b) { if (a != null) return a.equals(b); else return (b == null); } protected boolean equalFields(Customer rhs) { return eqFields(firstName, rhs.firstName) && eqFields(lastName, rhs.lastName) && eqFields(birthday, rhs.birthday) && eqFields(city, rhs.city) && eqFields(zipcode, rhs.zipcode); } @Override public boolean equals(Object obj) { if (this == obj) return true; return (obj instanceof CustomerOrg && ((Customer)obj).equalFields(this)); } public static int hash(Object obj) { return ((obj == null) ? 0 : obj.hashCode()); } @Override public int hashCode() { int result = 21 + hash(birthday); result = result * 31 + hash(city); result = result * 31 + hash(firstName); result = result * 31 + hash(lastName); result = result * 31 + hash(street); return result * 31 + hash(zipcode); } }
Thursday, 10 December 2009
Test
Decided on this syntax highlighter. Needed some typeface tweaking but has the great advantage that text can be copied without the line numbers if anyone would like to try some of my crazy code.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment