Code Monkey home page Code Monkey logo

betterbuilder's Introduction

BetterBuilder

GitHub license Build status Version

BetterBuilder is a Java annotation processor used for automatically generating better builder codes(builder design pattern), which can make coding much more comfortable.

Why better ?

Getting BetterBuilder

BetterBuilder doesn't add any runtime dependencies to your codes.

Directly reach the jar

Download from releases. (Just add it to your classpath)

Maven

BetterBuilder(v1.0.8) has already been published to Central https://repo1.maven.org/maven2/.

Example Maven settings:

<dependency>
  <groupId>cn.mpy634</groupId>
  <artifactId>BetterBuilder</artifactId>
  <version>1.0.8</version>
  <scope>provided</scope>
</dependency>

Usage

Simple example; See how to customize

Given a class "Student":

import cn.mpy634.annotation.BetterBuilder;
// All configurations are default.
@BetterBuilder
public class Student {
    private String name;
    private Integer ID;
}

The compiled code could be :

public class Student {
    private String name;
    private Integer ID;
    public Student ID(Integer ID) {this.ID = ID;return this;}
    public Integer ID() {return this.ID;}
    public Student name(String name) {this.name = name;return this;}
    public String name() {return this.name;}
    public static Student.StudentBuilder builder() {return new Student.StudentBuilder();}
    public Student(String name, Integer ID) {this.name = name;this.ID = ID;}
    public Student() {}
    public static class StudentBuilder {
        private String name;
        private Integer ID;
        private StudentBuilder() {}
        public Student.StudentBuilder name(String name) {this.name = name;return this;}
        public Student.StudentBuilder ID(Integer ID) {this.ID = ID;return this;}
        public Student build() {return new Student(this.name, this.ID);}
    }
}

Therefore you can code like

Student stu = Student.builder().ID(xx).name(xx)....build().ID(xx).name(xx)... .

You can also customize BetterBuilder.

Customization

FluentSet switch

Once make fluentSet = false, BetterBuilder will not generate set methods.

@BetterBuilder(fluentSet = false)
public class Student {
    ...
}

FluentGet switch

Once make fluentGet = false, BetterBuilder will not generate get methods.

@BetterBuilder(fluentGet = false)
public class Student {
    ...
}

Set type

Make setType = 0 / 1 to change the return type of generated set methods.

Given a field private Integer ID;, 2 kinds of set methods are available.

When setType = 0, which is default( strongly suggested ):

@BetterBuilder(setType = 0)
public class Student {
    private Integer ID;
    public Student ID(Integer ID){this.ID = ID; return this;}
}

when setType = 1, set methods will return nothing:

@BetterBuilder(setType = 1)
public class Student {
    private Integer ID;
    public void ID(Integer ID){this.ID = ID;}
}

NoBuilder switch

Once make BUILDER_TYPE = BuilderType.NO_BUILDER, BetterBuilder will not generate builder methods (nor the allArgsConstructor).

@BetterBuilder(BUILDER_TYPE = BuilderType.NO_BUILDER)
public class Student {
    ...
}

Field ignore

Make any fields annotated with @IgnoreGet or @IgnoreSet, BetterBuilder will not generate the get or set methods for them.

@BetterBuilder
public class Student {
    @IgnoreSet
    private String 牛;
    @IgnoreGet
    private Integer 年;
    @IgnoreGet
    @IgnoreSet
    private Student 大;
    private List<Boolean> 吉;
}

It is for those fields that aren't allowed to be changed or accessed after initialization.

Type-Safe Builder

When we use builder pattern to generate our object, some fields are supposed to be initialized. But the classic pattern does not guarantee this.

BetterBuilder provides a type-safe builder pattern. Once the fields annotated with @Required haven't been initialized, the goal object will not be generated ( Instead, an IllegalArgumentException will be thrown ).

@BetterBuilder(BUILDER_TYPE = BuilderType.TYPE_SAFE, fluentSet = false, fluentGet = true)
public class TypeSafe {
    @BetterBuilder.Required
    private Integer ID;
    @BetterBuilder.Required
    private String name;
    private Boolean PID;
    private Long PID79211;
}

You can also see the detail files : type-safe and type-safe-test.

Todo list

  • fluent - builder / test
    • noBuilder
  • fluent - set / test
    • chain set options
    • ignore set
  • fluent - get / test
    • ignore get
  • compatible with lombok
  • type-safe builder

...

Others

  • Any bugs or suggestions? Plz make PRs or issues.

betterbuilder's People

Contributors

leodpen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.