Giter Club home page Giter Club logo

fluder's Introduction

Fluder

Java CI with Gradle, Sonar and Codecov CodeScene Code Health CodeScene System Mastery

Flu(ent) (Buil)der generated by an annotation processor.

This force to build an object by giving the needed data in specific order.

See https://dzone.com/articles/fluent-builder-pattern

@Buildable

Mark a class for wich you want a fluent builder to be generated.

Parameters

builderName

Change the generated builder class name. Empty actually use the target class name appending 'Builder' to it. Remember that's a class name, the first letter will be upper case. You can use '$' that will be replaced by the target class name.

creatorName

Change the generated creator class name. Empty actually use the target class name appending 'Creator' to it. Remember that's a class name, the first letter will be upper case. You can use '$' that will be replaced by the target class name.

intermediatePrefix default ""

For the chaining, FluderProcessor generating multiple intermediate interfaces. This can put a prefix behind these interfaces. Remember that's an interface name, the first letter will be upper case.

buildMethodName default "build"

Change the finisher 'build()' call to your own.
Remember that's a method name, the first letter will be lower case.

instanceMethodName default "getInstance"

Change the initial 'getInstance' call to your own. Remember that's a method name, the first letter will be lower case.

Configuration annotations

@Group

Not already implemented

@Ignore

Ignore the field

@Name

Rename the setter, setContent can be transformed to setBody with @Name("body") on the 'content' field.

@Optional

An @Optional field can be defined in any order (after the required fields). You can give a default value via @Optional or the one defined at the field initialisation will be kept.

@Order

Change/force the definition order of the fields. If you use it, you must specify it for each fields.

transient/final

Transient/final fields are ignored.

non public

The builder use reflection API to build a private field

non public constructor

When you make a builder, you generally hide the possibility to instanciate the target object without this Builder. Then the private constructor is accessed via the reflection API.

Validation annotation

The builder can handle some javax.validation annotations

@Nonnull / @NotNull

This will add a check in the setter to ensure that it's a non nullable value that will be set. It throws an IllegalArgumentException when passing a null value when building.

@Override
	public EmailFrom setTo(java.lang.String in){
		if(in==null){
			throw new IllegalArgumentException("setTo can't be called with a null parameter. The target field is marked as @Nonnull");
		}
		this.to = in;
		return this;
	}

@NotEmpty

This will add a check in the setter to ensure that it's a non empty value that will be set. It throws an IllegalArgumentException when passing an empty value when building.

@Override
	public EmailFrom setTo(java.lang.String in){
		if(in==null){
			throw new IllegalArgumentException("setTo can't be called with a null parameter. The target field is marked as @Nonnull");
		}
		this.to = in;
		return this;
	}

Example

This Email

@Buildable
public class Email {

    @Nonnull
    @Order(0)
    String to;
    @NotNull
    @Order(2)
    String subject;
    @Order(3)
    @Name("body")
    String content;
    @Optional("\"bcc-default\"")
    String bcc;
    @Optional
    String cc;
    @Order(1)
    private String from;

    @Ignore
    private Date date;

    transient Object notInBuilder;

    //Hide me forcing the builder usage
    private Email(){
        super();
    }
}

Can now be created like this

Email email = EmailBuilder.getInstance()
                .setTo("to")
                .setFrom("from")
                .setSubject("subject")
                .setBody("content")
                //.setBcc("bcc")
                //.setCc("cc")
                .build();

fluder's People

Contributors

tokazio avatar

Watchers

 avatar  avatar

fluder's Issues

KSP implem

Provide a kotlin KSP implementation and kotlin code generation in this case

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.