item 50) 적시에 방어적 복사본을 만들라
방어적 복사
public final class Period {
private final Date start;
private final Date end;
public Period(Date start, Date end) {
this.start = new Date(start.getTime());
this.end = new Date(end.getTime());
if (start.compareTo(end) > 0)
throw new IllegalArgumentException(
start + " after " + end);
}
public Date getStart() {
return new Date(start.getTime());
}
public Date getEnd() {
return new Date(end.getTime());
}
}
Lombok Getter는 방어적 복사 불가

방어적 복사의 생략
Last updated