Builder pattern code generation in IntelliJ

前端 未结 11 1251
猫巷女王i
猫巷女王i 2021-01-30 01:36

Is there any way to automate writing Builder patterns in IntelliJ?

For example, given this simple class:

class Film {
   private String title;
   private         


        
11条回答
  •  余生分开走
    2021-01-30 01:59

    Lombok is the easiest way to do it! (It has a Intellij plugin for syntax support https://plugins.jetbrains.com/plugin/6317-lombok-plugin)

    Just add a @Builder annotation, and behind the walls it will add a builder implementation inside the object.

    With Lombok:

    import lombok.Builder;
    import lombok.Singular;
    import java.util.Set;
    
    @Builder
    public class BuilderExample {
      @Builder.Default private long created = System.currentTimeMillis();
      private String name;
      private int age;
      @Singular private Set occupations;
    }
    

    Without Lombok:

    import java.util.Set;
    
    public class BuilderExample {
      private long created;
      private String name;
      private int age;
      private Set occupations;
      
      BuilderExample(String name, int age, Set occupations) {
        this.name = name;
        this.age = age;
        this.occupations = occupations;
      }
      
      private static long $default$created() {
        return System.currentTimeMillis();
      }
      
      public static BuilderExampleBuilder builder() {
        return new BuilderExampleBuilder();
      }
      
      public static class BuilderExampleBuilder {
        private long created;
        private boolean created$set;
        private String name;
        private int age;
        private java.util.ArrayList occupations;
        
        BuilderExampleBuilder() {
        }
        
        public BuilderExampleBuilder created(long created) {
          this.created = created;
          this.created$set = true;
          return this;
        }
        
        public BuilderExampleBuilder name(String name) {
          this.name = name;
          return this;
        }
        
        public BuilderExampleBuilder age(int age) {
          this.age = age;
          return this;
        }
        
        public BuilderExampleBuilder occupation(String occupation) {
          if (this.occupations == null) {
            this.occupations = new java.util.ArrayList();
          }
          
          this.occupations.add(occupation);
          return this;
        }
        
        public BuilderExampleBuilder occupations(Collection occupations) {
          if (this.occupations == null) {
            this.occupations = new java.util.ArrayList();
          }
    
          this.occupations.addAll(occupations);
          return this;
        }
        
        public BuilderExampleBuilder clearOccupations() {
          if (this.occupations != null) {
            this.occupations.clear();
          }
          
          return this;
        }
    
        public BuilderExample build() {
          // complicated switch statement to produce a compact properly sized immutable set omitted.
          Set occupations = ...;
          return new BuilderExample(created$set ? created : BuilderExample.$default$created(), name, age, occupations);
        }
        
        @java.lang.Override
        public String toString() {
          return "BuilderExample.BuilderExampleBuilder(created = " + this.created + ", name = " + this.name + ", age = " + this.age + ", occupations = " + this.occupations + ")";
        }
      }
    }
    

    Source: https://projectlombok.org/features/Builder

提交回复
热议问题