Advertisement
NLinker

Immutable class with Builder inside

Apr 11th, 2016
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.37 KB | None | 0 0
  1. package com.vertigo.soundcloud9.stuff;
  2.  
  3. import java.util.BitSet;
  4. import java.util.Date;
  5.  
  6. interface WithBuild<T> {
  7.     T build();
  8. }
  9.  
  10. interface WithCopy<B> {
  11.     B copy();
  12. }
  13.  
  14. /** A {@code User} is an example of an immutable class */
  15. public class User implements WithCopy<User.Builder> {
  16.     private final String username;
  17.     private final String password;
  18.     private final String firstname;
  19.     private final String lastname;
  20.     private final String email;
  21.     private final Date creationDate;
  22.  
  23.     public User(Builder builder) {
  24.         this.username = builder.username;
  25.         this.password = builder.password;
  26.         this.creationDate = builder.creationDate;
  27.         this.firstname = builder.firstname;
  28.         this.lastname = builder.lastname;
  29.         this.email = builder.email;
  30.     }
  31.  
  32.     public User(String username,
  33.                 String password,
  34.                 String firstname,
  35.                 String lastname,
  36.                 String email,
  37.                 Date creationDate) {
  38.         this.username = username;
  39.         this.password = password;
  40.         this.creationDate = creationDate;
  41.         this.firstname = firstname;
  42.         this.lastname = lastname;
  43.         this.email = email;
  44.     }
  45.  
  46.     public String getUsername() {
  47.         return username;
  48.     }
  49.  
  50.     public String getPassword() {
  51.         return password;
  52.     }
  53.  
  54.     public String getFirstname() {
  55.         return firstname;
  56.     }
  57.  
  58.     public String getLastname() {
  59.         return lastname;
  60.     }
  61.  
  62.     public String getEmail() {
  63.         return email;
  64.     }
  65.  
  66.     public Date getCreationDate() {
  67.         return new Date(creationDate.getTime());
  68.     }
  69.  
  70.     @Override
  71.     public Builder copy() {
  72.         return new Builder()
  73.             .username(username)
  74.             .password(password)
  75.             .creationDate(creationDate)
  76.             .firstname(firstname)
  77.             .lastname(lastname)
  78.             .email(email);
  79.     }
  80.  
  81.     public static Builder builder() {
  82.         return new Builder();
  83.     }
  84.  
  85.     @Override
  86.     public String toString() {
  87.         //noinspection StringBufferReplaceableByString
  88.         final StringBuilder sb = new StringBuilder("User(");
  89.         sb.append("username='").append(username).append('\'');
  90.         sb.append(",password='").append(password).append('\'');
  91.         sb.append(",firstname='").append(firstname).append('\'');
  92.         sb.append(",lastname='").append(lastname).append('\'');
  93.         sb.append(",email='").append(email).append('\'');
  94.         sb.append(",creationDate=").append(creationDate);
  95.         sb.append(')');
  96.         return sb.toString();
  97.     }
  98.  
  99.     public static class Builder implements WithBuild<User> {
  100.         private final BitSet bs = new BitSet(16);
  101.         private String username;
  102.         private String password;
  103.         private String firstname;
  104.         private String lastname;
  105.         private String email;
  106.         private Date creationDate;
  107.  
  108.         private Builder() {
  109.         }
  110.  
  111.         public Builder username(String username) {
  112.             this.bs.set(0);
  113.             this.username = username;
  114.             return this;
  115.         }
  116.  
  117.         public Builder password(String password) {
  118.             this.bs.set(1);
  119.             this.password = password;
  120.             return this;
  121.         }
  122.  
  123.         public Builder firstname(String firstname) {
  124.             this.bs.set(2);
  125.             this.firstname = firstname;
  126.             return this;
  127.         }
  128.  
  129.         public Builder lastname(String lastname) {
  130.             this.bs.set(3);
  131.             this.lastname = lastname;
  132.             return this;
  133.         }
  134.  
  135.         public Builder email(String email) {
  136.             this.bs.set(4);
  137.             this.email = email;
  138.             return this;
  139.         }
  140.  
  141.         public Builder creationDate(Date creationDate) {
  142.             this.bs.set(5);
  143.             this.creationDate = creationDate;
  144.             return this;
  145.         }
  146.  
  147.         @Override
  148.         public User build() {
  149.             final BitSet all = new BitSet(bs.size());
  150.             all.set(0, 6);
  151.             if (bs.equals(all))
  152.                 return new User(
  153.                     username,
  154.                     password,
  155.                     firstname,
  156.                     lastname,
  157.                     email,
  158.                     creationDate
  159.                 );
  160.             else {
  161.                 all.andNot(bs);
  162.                 throw new RuntimeException("Initialization incomplete: " + all);
  163.             }
  164.         }
  165.     }
  166.  
  167.     public static void main(String[] args) {
  168.         try {
  169.             User user = User.builder()
  170.                 .email("vp@vmg.io")
  171.                 .firstname("Vasya")
  172.                 .lastname("Pupkin")
  173.                 .build();
  174.             System.out.println("user = " + user);
  175.         } catch (Exception e) {
  176.             System.out.println("e = " + e);
  177.         }
  178.         User user1 = User.builder()
  179.             .username("ii")
  180.             .password("secure1")
  181.             .email("ii@vmg.io")
  182.             .firstname("Ivan")
  183.             .lastname("Ivanov")
  184.             .creationDate(new Date())
  185.             .build();
  186.  
  187.         User user2 = user1.copy()
  188.             .username("pp")
  189.             .email("pp@vmg.io")
  190.             .firstname("Petr")
  191.             .lastname("Petrov")
  192.             .build();
  193.         System.out.println("user1 = " + user1);
  194.         System.out.println("user2 = " + user2);
  195.  
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement