[JAVA] Specify the default value with @Builder of Lombok

Postscript (2017/9/10)

From v1.16.16, you can specify the default value by adding the @ Builder.Default annotation.

@Builder
public class Coupon {
    @Builder.Default
    private UUID id = UUID.randomUUID();
    private String name;
    private String description;
    private ZonedDateTime expireDate;
}

By the way, Spring Boot was imported in v1.5.3, so if you are using an earlier version, you need to explicitly specify the version.

Overview

Lombok's @Builder is an annotation that automatically generates a builder class for a specified class.

@Value
@Builder
public class Coupon {
    private UUID id;
    private String name;
    private String description;
    private ZonedDateTime expireDate;
}
Coupon coupon = Coupon.builder()
        .name("Surprised coupon")
        .description("Surprised coupon")
        .expireDate(ZonedDateTime.parse("2017-01-30T23:59:59+09:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME))
        .build();

However, the property for which no value is specified in the builder call will be null. Even if you try to set a default value other than null and write it in the target class as follows, it will not work.

@Value
@Builder
public class Coupon {
    private UUID id = UUID.randomUUID(); //Does not apply
    private String name = "";            //Does not apply
    private String description;
    private ZonedDateTime expireDate;
}

Therefore, this time, I will introduce how to set the default value correctly.

How to set the default value for @ Builder

To set the default value, describe the builder class with the naming convention of target class name + Builder as shown below. The rest is nicely complemented by Lombok.

@Value
@Builder
public class Coupon {
    private UUID id;
    private String name;
    private String description;
    private ZonedDateTime expireDate;

    public static class CouponBuilder {
        private UUID id = UUID.randomUUID();
        private String name = "";
    }
}

reference

Recommended Posts

Specify the default value with @Builder of Lombok
Specify the character code of the source when building with Maven
NoSuchMethodException with lombok without default constructor
Specify the default JAVA_HOME used in buildship
Check the contents of params with pry
Bitwise operation of binary value with BigInteger
Samshin on the value of the hidden field
About the treatment of BigDecimal (with reflection)
Format the contents of LocalDate with DateTimeFormatter
Get the value of enum saved in DB by Rails with attribute_before_type_cast
Initialize Ruby array with 0 like Java, that is, set the default value to 0
Verify the contents of the argument object with Mockito
[Ruby] Arguments with keywords and default values of arguments
Manage the version of Ruby itself with rbenv
Overwrite the contents of config with Spring-boot + JUnit5
The story of tuning android apps with libGDX
Calculate the similarity score of strings with JAVA
Prepare the environment of CentOS 8 with Sakura VPS
Measure the distance of the maze with breadth-first search
I checked the number of taxis with Ruby
Speed comparison when the value side of Hash wants to retrieve all with array
Arguments with default values Take the full_title method of the Rails tutorial as an example
[Jackson] A story about converting the return value of BigDecimal type with a custom serializer.
Specify the encoding of static resources in Spring Boot
List the contents of categories created with Active Hash
JavaFX --Match the size of ImageView with other nodes
CI the architecture of Java / Kotlin applications with ArchUnit
[JUnit5] Dealing with "the reference of assertEquals is ambiguous"
Access the built-in h2db of spring boot with jdbcTemplate
Test the contents of an Excel file with JUnit
The story of making a reverse proxy with ProxyServlet
[Java] How to get the maximum value of HashMap
Monitor the internal state of Java programs with Kubernetes
Implement the UICollectionView of iOS14 with the minimum required code.
Check the behavior of Java Intrinsic Locks with bpftrace
[Rails] Cancel / change the default password validation of devise
[SwiftUI] How to specify the abbreviated position of Text
Display the average value of the evaluation as a star
Find the approximate value of log (1 + x) in Swift
Are you using the default method of the interface properly?
Control the processing flow of Spring Batch with JavaConfig.
Specify VS Code as the default editor for jshell
How is the next value of the Time object correct?
The story of making dto, dao-like with java, sqlite
Replace only part of the URL host with java
[Java] Coverage report could not be created with the combination of default method of Cobertura + interface
How to specify an array in the return value / argument of the method in the CORBA IDL file