Created
September 29, 2015 12:54
-
-
Save stephan-mueller/1dad5f38ba676c2c1f0e to your computer and use it in GitHub Desktop.
An entity that represents a customer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Customer implements Serializable { | |
private Long id; | |
private String firstName; | |
private String lastName; | |
private String email; | |
public Long getId() { | |
return id; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public String getEmail() { | |
return email; | |
} | |
@Override | |
public int hashCode() { | |
if (id == null) { | |
return new Object().hashCode(); | |
} else { | |
return id.hashCode(); | |
} | |
} | |
@Override | |
public boolean equals(final Object object) { | |
if (this == object) { | |
return true; | |
} | |
if (object == null | |
|| !(object.getClass().isAssignableFrom(getClass()) && getClass().isAssignableFrom(object.getClass()))) { | |
return false; | |
} | |
Customer customer = (Customer)object; | |
return getId() != null && getId().equals(customer.getId()); | |
} | |
@Override | |
public String toString() { | |
return getClass().getSimpleName() + "#" + id; | |
} | |
/** | |
* Creates a new instance of builder {@link CustomerBuilder} for entity {@link Customer}. | |
* | |
* @return new builder instance | |
*/ | |
public static CustomerBuilder newBuilder() { | |
return new CustomerBuilder(); | |
} | |
public static class CustomerBuilder { | |
private String firstName; | |
private String lastName; | |
private String email; | |
private CustomerBuilder() { | |
} | |
/** | |
* Sets firstName to builder. | |
* | |
* @param firstName the given firstName. | |
* @return builder instance | |
*/ | |
public CustomerBuilder setFirstName(final String firstName) { | |
this.firstName = firstName; | |
return this; | |
} | |
/** | |
* Sets lastName to builder. | |
* | |
* @param lastName the given lastName. | |
* @return builder instance | |
*/ | |
public CustomerBuilder setLastName(final String lastName) { | |
this.lastName = lastName; | |
return this; | |
} | |
/** | |
* Sets email to builder. | |
* | |
* @param email the given email. | |
* @return builder instance | |
*/ | |
public CustomerBuilder setEmail(final String email) { | |
this.email = email; | |
return this; | |
} | |
public Customer build() { | |
Customer customer = new Customer(); | |
customer.firstName = firstName; | |
customer.lastName = lastName; | |
customer.email = email; | |
return customer; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment