Sunday, April 19, 2020

[Spring] Simplify code with lombok

What is Lombok?

Lombok is a JAVA library that makes it easy to work with mechanically created DTOs and VOs. You can use the following annunciations to define multiple methods for VO.

AnnotationDescription
@DataAutomatically defines the getter, setter, equals, hashCode, toString methods for DTO, VO.
@GetterAutomatically defines the getter method for DTO, VO.
@SetterAutomatically defines setter methods for DTO, VO.
@ToStringAutomatically defines the toString method for DTO, VO.
@EqualsAndHashCodeAutomatically defines the DTO, Equals for VO, and hashCode methods.
@BuilderEnables the application of builder patterns to DTO, VO.
@NonNullPrevents field values from being null.
@SynchronizedApply synchronization.
@SneakyThrowsThrow an exception if an error occurs.

Using Lombok in the Spring

Maven
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.10</version> 
</dependency>

Before using Lombok
public class Company {
    private final Person founder;
    private String name;
    private List<Person> employees;
    
    private Company(final Person founder) {
        this.founder = founder;
    }
    
    public static Company of(final Person founder) {
        return new Company(founder);
    }
    
    public Person getFounder() {
        return founder;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(final String name) {
        this.name = name;
    }
    
    public List<Person> getEmployees() {
        return employees;
    }
    
    public void setEmployees(final List<Person> employees) {
        this.employees = employees;
    }
    
    @Override
    public boolean equals(final java.lang.Object o) {
        if (o == this) return true;
        if (o == null) return false;
        if (o.getClass() != this.getClass()) return false;
        final Company other = (Company)o;
        if (this.founder == null ? other.founder != null : !this.founder.equals(other.founder)) return false;
        if (this.name == null ? other.name != null : !this.name.equals(other.name)) return false;
        if (this.employees == null ? other.employees != null : !this.employees.equals(other.employees)) return false;
        return true;
    }
    
    @Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = result * PRIME + (this.founder == null ? 0 : this.founder.hashCode());
        result = result * PRIME + (this.name == null ? 0 : this.name.hashCode());
        result = result * PRIME + (this.employees == null ? 0 : this.employees.hashCode());
        return result;
    }
    
    @Override
    public String toString() {
        return "Company(founder=" + founder + ", name=" + name + ", employees=" + employees + ")";
    }
}


After using Lombok
@Data
public class Company {
    private final Person founder;
    private String name;
    private List<Person> employees;
}
With @Data, VO operations end with only field declarations, making it simple and easy to maintain.

No comments:

Post a Comment