Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.vertigo.soundcloud9.stuff;
- import java.util.BitSet;
- import java.util.Date;
- interface WithBuild<T> {
- T build();
- }
- interface WithCopy<B> {
- B copy();
- }
- /** A {@code User} is an example of an immutable class */
- public class User implements WithCopy<User.Builder> {
- private final String username;
- private final String password;
- private final String firstname;
- private final String lastname;
- private final String email;
- private final Date creationDate;
- public User(Builder builder) {
- this.username = builder.username;
- this.password = builder.password;
- this.creationDate = builder.creationDate;
- this.firstname = builder.firstname;
- this.lastname = builder.lastname;
- this.email = builder.email;
- }
- public User(String username,
- String password,
- String firstname,
- String lastname,
- String email,
- Date creationDate) {
- this.username = username;
- this.password = password;
- this.creationDate = creationDate;
- this.firstname = firstname;
- this.lastname = lastname;
- this.email = email;
- }
- 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;
- }
- public Date getCreationDate() {
- return new Date(creationDate.getTime());
- }
- @Override
- public Builder copy() {
- return new Builder()
- .username(username)
- .password(password)
- .creationDate(creationDate)
- .firstname(firstname)
- .lastname(lastname)
- .email(email);
- }
- public static Builder builder() {
- return new Builder();
- }
- @Override
- public String toString() {
- //noinspection StringBufferReplaceableByString
- final StringBuilder sb = new StringBuilder("User(");
- sb.append("username='").append(username).append('\'');
- sb.append(",password='").append(password).append('\'');
- sb.append(",firstname='").append(firstname).append('\'');
- sb.append(",lastname='").append(lastname).append('\'');
- sb.append(",email='").append(email).append('\'');
- sb.append(",creationDate=").append(creationDate);
- sb.append(')');
- return sb.toString();
- }
- public static class Builder implements WithBuild<User> {
- private final BitSet bs = new BitSet(16);
- private String username;
- private String password;
- private String firstname;
- private String lastname;
- private String email;
- private Date creationDate;
- private Builder() {
- }
- public Builder username(String username) {
- this.bs.set(0);
- this.username = username;
- return this;
- }
- public Builder password(String password) {
- this.bs.set(1);
- this.password = password;
- return this;
- }
- public Builder firstname(String firstname) {
- this.bs.set(2);
- this.firstname = firstname;
- return this;
- }
- public Builder lastname(String lastname) {
- this.bs.set(3);
- this.lastname = lastname;
- return this;
- }
- public Builder email(String email) {
- this.bs.set(4);
- this.email = email;
- return this;
- }
- public Builder creationDate(Date creationDate) {
- this.bs.set(5);
- this.creationDate = creationDate;
- return this;
- }
- @Override
- public User build() {
- final BitSet all = new BitSet(bs.size());
- all.set(0, 6);
- if (bs.equals(all))
- return new User(
- username,
- password,
- firstname,
- lastname,
- email,
- creationDate
- );
- else {
- all.andNot(bs);
- throw new RuntimeException("Initialization incomplete: " + all);
- }
- }
- }
- public static void main(String[] args) {
- try {
- User user = User.builder()
- .email("vp@vmg.io")
- .firstname("Vasya")
- .lastname("Pupkin")
- .build();
- System.out.println("user = " + user);
- } catch (Exception e) {
- System.out.println("e = " + e);
- }
- User user1 = User.builder()
- .username("ii")
- .password("secure1")
- .email("ii@vmg.io")
- .firstname("Ivan")
- .lastname("Ivanov")
- .creationDate(new Date())
- .build();
- User user2 = user1.copy()
- .username("pp")
- .email("pp@vmg.io")
- .firstname("Petr")
- .lastname("Petrov")
- .build();
- System.out.println("user1 = " + user1);
- System.out.println("user2 = " + user2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement