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.
Annotation | Description |
@Data | Automatically defines the getter, setter, equals, hashCode, toString methods for DTO, VO. |
@Getter | Automatically defines the getter method for DTO, VO. |
@Setter | Automatically defines setter methods for DTO, VO. |
@ToString | Automatically defines the toString method for DTO, VO. |
@EqualsAndHashCode | Automatically defines the DTO, Equals for VO, and hashCode methods. |
@Builder | Enables the application of builder patterns to DTO, VO. |
@NonNull | Prevents field values from being null. |
@Synchronized | Apply synchronization. |
@SneakyThrows | Throw 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