Created
February 25, 2014 13:22
-
-
Save zeroows/9208625 to your computer and use it in GitHub Desktop.
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 User implements Serializable { | |
private static final long serialVersionUID = 2L; | |
private Long userId; | |
private String username; | |
private String password; | |
private String firstName; | |
private String lastName; | |
private String email; | |
/** | |
* Gets a builder which is used to create User objects. | |
* | |
* @return A new Builder instance. | |
*/ | |
public static Builder getBuilder() { | |
return new Builder(); | |
} | |
/** | |
* Gets a builder which is used to create User objects. | |
* | |
* @param preBuilt | |
* the object to be modified | |
* @return A new Builder instance. | |
*/ | |
public static Builder getBuilder(User preBuilt) { | |
return new Builder(preBuilt); | |
} | |
/** | |
* Gets the full name of the User. | |
* | |
* @return The full name of the person. | |
*/ | |
public String getFullName() { | |
StringBuilder name = new StringBuilder(); | |
name.append(firstName); | |
name.append(" "); | |
name.append(lastName); | |
return name.toString(); | |
} | |
/** | |
* A Builder class used to create new User objects. | |
*/ | |
public static class Builder { | |
User built; | |
/** | |
* Creates a new Builder instance. | |
*/ | |
Builder() { | |
built = new User(); | |
} | |
/** | |
* Creates a new Builder instance. | |
*/ | |
Builder(User preBuilt) { | |
built = preBuilt; | |
} | |
/** | |
* To set the user username | |
* | |
* @param username | |
* the user username | |
* @return A Builder instance. | |
*/ | |
public Builder setUsername(String username) { | |
built.username = username; | |
return this; | |
} | |
/** | |
* To set the user password | |
* | |
* @param password | |
* the user password | |
* @return A Builder instance. | |
*/ | |
public Builder setPassword(String password) { | |
built.password = password; | |
return this; | |
} | |
/** | |
* To set the user first name | |
* | |
* @param firstName | |
* the user first name | |
* @return A Builder instance. | |
*/ | |
public Builder setFirstName(String firstName) { | |
built.firstName = firstName; | |
return this; | |
} | |
/** | |
* To set the user last name | |
* | |
* @param lastName | |
* the user last name | |
* @return A Builder instance. | |
*/ | |
public Builder setLastName(String lastName) { | |
built.lastName = lastName; | |
return this; | |
} | |
/** | |
* To set the user department | |
* | |
* @param department | |
* the user department | |
* @return A Builder instance. | |
*/ | |
public Builder setDepartment(Department department) { | |
built.department = department; | |
return this; | |
} | |
/** | |
* To set the user role | |
* | |
* @param department | |
* the user role | |
* @return A Builder instance. | |
*/ | |
public Builder setRole(Role role) { | |
if(built.roles == null) | |
built.roles = new ArrayList<Role>(); | |
built.roles.add(role); | |
return this; | |
} | |
/** | |
* Builds the new User object. | |
* | |
* @return The created User object. | |
*/ | |
public User build() { | |
return built; | |
} | |
} | |
public Long getUserId() { | |
return userId; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public String getEmail() { | |
return email; | |
} | |
@Override | |
public String toString() { | |
return ToStringBuilder.reflectionToString(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment