Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. public class ImmutableUser {
  2.     private final String username;
  3.     private final String password;
  4.     private final String firstname;
  5.     private final String lastname;
  6.     private final String email;
  7.     private final Date creationDate;
  8.     private ImmutableUser(UserBuilder builder) {
  9.         this.username = builder.username;
  10.         this.password = builder.password;
  11.         this.creationDate = builder.creationDate;
  12.         this.firstname = builder.firstname;
  13.         this.lastname = builder.lastname;
  14.         this.email = builder.email;
  15.     }
  16.    
  17.     public static class UserBuilder {
  18.         private final String username;
  19.         private final String password;
  20.         private final Date creationDate;
  21.         private String firstname;
  22.         private String lastname;
  23.         private String email;
  24.         public UserBuilder(String username, String password) {
  25.             this.username = username;
  26.             this.password = password;
  27.             this.creationDate = new Date();
  28.         }
  29.  
  30.         public UserBuilder firstName(String firsname) {
  31.             this.firstname = firsname;
  32.             return this;
  33.         }
  34.  
  35.         public UserBuilder lastName(String lastname) { 
  36.             this.lastname = lastname;
  37.             return this;
  38.         }
  39.  
  40.         public ImmutableUser build() {
  41.             return new ImmutableUser(this);
  42.         }
  43.     }
  44.  
  45.     public Date getCreationDate() {
  46.         return new Date(creationDate.getTime());
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement