Keeping your models clean with Lombok Part 2

Keeping your models clean with Lombok Part 2

In the previous post I have talked about the common Lombok functionalities and their usage. In this post I will be focused on the functionalities that are used in certain cases depending on the coding style inside the project. The functionalities that are going to be covered in this post are the following: @With, @Accessors and @Builder.

@Accessors

The @Accessors annotation allows you to determine how the getter and setter methods are going to be generated. The default generation of the setter and getter methods by Lombok is done by adding set or get prefix, and followed by the name of the variable with the first letter being capital. For example if the variable is name, then the generated setter and getter are going to be setName() and getName().

The @Accessors annotation allows you to configure three options for the getter and setter which are:

  • chain – This parameter has default value set to false.
  • fluent – This parameter has default value set to false.
  • prefix – This parameter has empty object by default.

When chain is set to true, this is going to allow the generated setter methods to return this instead of void. Example when chain is set to true.

@Getter
@Setter
@Accessors(chain = true)
public class Geo {

    private String lat;
    private String lng;

}

// The usage of the Geo class would be the following:
Geo geo = new Geo()
                   .setLat("40.746230")
                   .setLng("-74.226860");

When fluent is set to true, this is going to allow the generated setter and getter methods to be generated without set / get prefix. Example when fluent is set to true.

@Getter
@Setter
@Accessors(fluent = true)
public class Geo {

    private String lat;
    private String lng;

}

// The usage of the Geo class would be the following:
Geo geo = new Geo();
geo.lat("40.746230");
geo.lng("-74.226860");
String lat = geo.lat();
String lng = geo.lng();

When prefix is defined the setter and getter methods are going to be created with the proper name. Lets say we have defined variable with name mLat, with the default setter / getter the method name would be generated in the following way: setMLat(), getMLat(). In order to get rid of the “M” letter inside the methods we need to use the prefix inside @Accessors annotation. Example when prefix is set with value “m”.

@Getter
@Setter
@Accessors(prefix = "m")
public class Geo {

    private String mLat;
    private String mLng;

}

// The usage of the Geo class would be the following:
Geo geo = new Geo();
geo.setLat("40.746230");
geo.setLng("-74.226860");
String lat = geo.getLat();
String lng = geo.getLng();

@With

The @With annotation is used when you want the setter methods to be generated with with prefix in front of the setter method, and the setter method is going to return this instead of returning void.
Example of Lombok @With annotation:

@With
public class Geo {

    private String lat;
    private String lng;

}

// The usage of the Geo class would be the following:

Geo geo = new Geo()
                   .withLat("40.746230")
                   .withLng("-74.226860");

@Builder

The @Builder is used when don’t want to expose setter methods and we want to initialize the object by using the builder pattern. By using the @Builder annotation, the builder pattern methods are going to be generated, and can be used out of the box. Example when using the @Builder annotation.

@Getter
@Builder
public class Geo {

    private String lat;
    private String lng;

}

// The usage of the Geo class would be the following:
Geo geo = Geo.builder()
                       .seLat("40.746230")
                       .setLng("-74.226860")
                       .build();