|
Page 3 of 6
package persons; import java.io.Serializable; interface Person extends Serializable { String getFirstName(); String getLastName(); }public final class PersonImpl { private PersonImpl() throws UnsupportedOperationException { throw new UnsupportedOperationException(); }public static Person getPerson(final String firstName , final String lastName) throws NullPointerException { if(firstName == null || lastName == null) { throw new NullPointerException(); } else { return new _Person(firstName, lastName); } } private static final class _Person implements Person { private final String firstName; private final String lastName;_Person(final String firstName, final String lastName) { this.firstName = firstName; this.lastName = lastName; }public String getFirstName() { return firstName; }public String getLastName() { return lastName; }@Override public boolean equals(final Object o) { if(this == o) { return true; } else if(!(o instanceof Person)) { return false; } else { final Person p = (Person)o; return firstName.equals(p.getFirstName()) && lastName.equals(p.getLastName()); } } @Override public int hashCode() { // let's make it simple... don't know about you, but I'm out of breath :) return firstName.hashCode() ^ lastName.hashCode(); } } }
|