JAVA/JAVA
Equals 구현
lovineff
2021. 3. 22. 14:17
this 클래스의 값이 null인 경우가 있으므로, Object의 equals 함수를 사용하여 구현한다.
public class PoolContent implements Serializable {
private static final long serialVersionUID = 1L;
private String title;
private String summary;
private String image;
private String pcLink;
private String mobileLink;
private String writer;
private String regDateTime;
@Override
public boolean equals(Object obj) {
if(obj instanceof PoolContent){
PoolContent poolContent = (PoolContent) obj;
if(!Objects.equals(this.title, poolContent.getTitle())){
return false;
}
if(!Objects.equals(this.summary, poolContent.getSummary())){
return false;
}
if(!Objects.equals(this.image, poolContent.getImage())){
return false;
}
if(!Objects.equals(this.pcLink, poolContent.getPcLink())){
return false;
}
if(!Objects.equals(this.mobileLink, poolContent.getMobileLink())){
return false;
}
if(!Objects.equals(this.writer, poolContent.getWriter())){
return false;
}
if(!Objects.equals(this.regDateTime, poolContent.getRegDateTime())){
return false;
}
return true;
}
return false;
}
}
Spring에서 Equals 함수 구현 대신 @Data 어노테이션을 사용할 수 있다.
@Data 어노테이션은 다음 어노테이션을 포함한다.
@Getter @Setter @EqualsAndHashCode @AllArgsConstructor
더 자세한 정보는 아래 공식 문서를 참고한다
@Data
projectlombok.org