Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Java Immutable Builder with tight guided fluent interface

By: a guest on Aug 26th, 2010  |  syntax: Java 5  |  size: 2.86 KB  |  views: 212  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * Example of an immutable pojo with a built-in builder. (getters left out for brevity)
  3.  * Admittedly most of the ugliness comes from the syntactic cruft of Java's way of doing closures
  4.  * and the lack of method aliases. Scala translation is here: http://pastebin.com/TASbJC5y
  5.  * I think this kind of builder could be automatically generated by a compile-time tool - for
  6.  * my next trick I'll write one.
  7.  * @author twitter.com/benhardy
  8.  */
  9. public final class PersonFeatures {
  10.     public final int userId;
  11.     public final int age;
  12.     public final int children;
  13.     public final int dogs;
  14.  
  15.     private PersonFeatures(int userId, int age, int children, int dogs) {
  16.         this.userId = userId;
  17.         this.age = age;
  18.         this.children = children;
  19.         this.dogs = dogs;
  20.     }
  21.  
  22.     // give each stage of the builder only one callable method,
  23.     // which returns the next stage
  24.  
  25.     public interface BuilderUserIdSpec {
  26.         public BuilderAgeSpec userId(int v);
  27.     }
  28.     public interface BuilderAgeSpec {
  29.         public BuilderChildrenSpec age(int v);
  30.     }
  31.     public interface BuilderChildrenSpec {
  32.         public BuilderDogsSpec children(int v);
  33.     }
  34.     public interface BuilderDogsSpec {
  35.         public BuilderFinal dogs(int v);
  36.     }
  37.     public interface BuilderFinal {
  38.         public PersonFeatures build();
  39.     }
  40.  
  41.     /* the builder method returns its first stage which allows you to set userId, and so on */
  42.  
  43.     public static BuilderUserIdSpec builder() {
  44.         return new BuilderUserIdSpec() {
  45.             @Override public BuilderAgeSpec userId(final int _userId) {
  46.                  return new BuilderAgeSpec() {
  47.                      @Override public BuilderChildrenSpec age(final int _age) {
  48.                          return new BuilderChildrenSpec() {
  49.                              @Override public BuilderDogsSpec children(final int _children) {
  50.                                  return new BuilderDogsSpec() {
  51.                                      public BuilderFinal dogs(final int _dogs) {
  52.                                          return new BuilderFinal() {
  53.                                             @Override public PersonFeatures build() {
  54.                                                 return new PersonFeatures(_userId, _age, _children, _dogs);
  55.                                             }
  56.                                          };
  57.                                      }
  58.                                  };
  59.                              }
  60.                          };
  61.                      }
  62.                  };
  63.             }
  64.         };
  65.     }
  66.    
  67.     /**
  68.      * example usage. pretty obvious what is going on. only one way to do it. IDE will help.
  69.      */
  70.     public static void main(String[]args) {
  71.         PersonFeatures person = PersonFeatures.builder().userId(4437).age(22).children(0).dogs(1).build();
  72.     }
  73. }
clone this paste RAW Paste Data