title: Using Lombok to create builders for classes with required and optional attributes
body: <p>Searching for a plugin to avoid boilerplate code to implement <a href="http://www.informit.com/authors/bio/8b620cf7-c360-4ee1-9b55-c7f96e7f0d18" rel="noreferrer">Joshua Bloch</a>'s <a href="http://www.informit.com/articles/article.aspx?p=1216151&amp;seqNum=2" rel="noreferrer">builder pattern</a> I found the amazing <a href="https://projectlombok.org/" rel="noreferrer">Lombok Project</a> which enables you to generate builders via annotations like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Builder
  public class Person {
      private String name;
      private String address;
      private String secondAddress;
  }
  
  PersonBuilder.builder().name("yourName").address("your Address").build();
  </code></pre>
  
  <p>As you can see, there is no boilerplate code, and you can easily create an instance of <code>Person</code> by calling the provided static <code>builder()</code> method, chaining setter-like-calls just like it works with JavaBeans-Pattern and end the chain with calling <code>build()</code>;</p>
  
  <p>One of the disadvantages of the JavaBeans-Pattern compared to builder pattern  is (from <a href="http://www.informit.com/articles/article.aspx?p=1216151&amp;seqNum=2" rel="noreferrer">Effective Java</a>):</p>
  
  <blockquote>
    <p>Because construction is split across multiple calls, <strong>a JavaBean may be in an inconsistent state partway through its construction</strong>.</p>
  </blockquote>
  
  <p>Assuming that in the above example, the first two attributes, name and address, are mandatory for constructing an instance of Person, the way Lombok implements the builder pattern enables a developer to split/shorten the construction and do something with a possibly inconsistent instance of <code>Person</code>, like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>Person p = PersonBuilder.builder().name("yourName").build();
  ...
  System.out.println(p.getAddress());
  ...
  p.setAddress("your address");
  </code></pre>
  
  <p><a href="http://www.informit.com/authors/bio/8b620cf7-c360-4ee1-9b55-c7f96e7f0d18" rel="noreferrer">Joshua Bloch</a>'s solution prefers a builder method with the mandatory attributes as parameters, so that there is no possibility that the construction is split across multiple calls, like illustrated in <a href="http://www.informit.com/articles/article.aspx?p=1216151&amp;seqNum=2" rel="noreferrer">Item 2: Consider a builder when faced with many constructor parameters</a>.</p>
  
  <p><strong>My question is:</strong>
  Is there any convenient way like annotation parameters for @Builder or something like Springs @Required or @Mandatory on attribute level to enforce Lombok to avoid offering the parameterless builder constructor and to provide a constructor with the mandatory parameters, as Joshua Bloch proposes?</p>
  
  <p>I've tried many options from <a href="https://projectlombok.org/features/Builder" rel="noreferrer">@Builder documentation</a> but couldn't find a desirable solution.</p>
  
  <p>What works for me is described as follows:</p>
  
  <ul>
  <li>defining a constructor for Person with mandatory parameters,</li>
  <li>overriding the builder constructor with a parameterized signature for the mandatory parameters.</li>
  </ul>
  
  <p>It's a bit of boilerplate, which could possibly be avoided. See my solution applied on Joshua Bloch's example bellow.</p>
  
  <pre class="lang-java prettyprint-override"><code>/**
   * Uncle Bobs builder example for constructors with many required &amp; optional parameters,
   * realized by lombok.
   * 
   */
  @AllArgsConstructor(access=AccessLevel.PRIVATE) // Let lombok generate private c-tor with all parameters, as needed by @Builder.
  @Builder(
          builderClassName="Builder", // Cosmetic. Without this option, the builder class would have the name NutritionFactsBuilder.
          toBuilder=true // Enabling creation of a builder instance based on a main class instance: NutritionFacts. 
  )
  public class NutritionFacts {
  
      // Required parameters
      private int servingSize;
      private int servings;
  
      // Optional parameters
      private int calories;
      private int fat;
      private int sodium;
      private int carbohydrate;
  
      /**
       * A builder method demanding required parameters.
       */
      public static Builder builder(int servingSize, int servings) {
          return new NutritionFacts(servingSize, servings).toBuilder();
      }
  
      /**
       * eclipse-created C-tor with required parameters.
       * Can be public for instantiating, but doesn't have to.
       */
      public NutritionFacts(int servingSize, int servings) {
          super();
          this.servingSize = servingSize;
          this.servings = servings;
      }
  
      public static void main(String[] args) {
          NutritionFacts cocaCola = NutritionFacts.builder(240, 8)
                                                  .calories(100)
                                                  .sodium(35)
                                                  .carbohydrate(27)
                                                  .build();
      }
  }
  </code></pre>