Useful Lombak Tips

What is Lombok?

In their own word,

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more

Why Use Lombok

  • Reduce boilerplate code
  • Increase readability
  • Reduce work for unit test code coverage

When to NOT use Lombok?

  • More complex entity classes (non POJO classes)

Example

@Value
@Builder
public class AccountInfo {
    private String accountId;
    private String language;
    private String region;
    private String legalCountry;
}

To ensure the list has at least one entry

@Builder
public class MyCatalog {

    @Singular("productMapById")
    private Map<String, MyProduct> productById;

    @Singular("validationResponse")
    private List validationResponseList = new ArrayList<>();
}

This entry lists the Lombak tips I use all the time

Internal Builder

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder(builderMethodName = "internalBuilder")
public class MyState {
    @NotNull
    private ClassA instanceA;
    private List<ClassB> listInstanceB;
    private List<ClassC> listInstanceC;
    private List<ClassD> listInstanceD;

    public static MyState builder(ClassA a) {
        return internalBuilder().instanceA(a);
    }

    public MyState(ClassA instanceA,
                   List<ClassB> listInstanceB,
                   List<ClassC> listInstanceC,
                   List<ClassD> listInstanceD) {
        this.instanceA = instanceA;
        this.listInstanceB = listInstanceB;
        this.listInstanceC = listInstanceC;
        this.listInstanceD = listInstanceD;
    }
}

 

Hash and Compare

 

Singular