Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 상수와 Enum
- ControllerTest
- 자바의 종류
- Oracle JDK와 OpenJDK의 차이
- 방어적 복사
- There isn't anything to compare.
- JDK
- 자바로 만들수 있는 것
- 우아한테크코스
- 자바 버전 다운 그레이드
- 윤년계산하기
- 객체지향적인 설계
- 자판기미션
- Getter Setter
- 상근날드
- 테스트 성능 개선
- 우테코4기
- java 1000번 A+B
- PR 오류
- 블랙잭 회고
- 리스코프치환원칙
- JXM
- 자바 4334
- 인수테스트
- throw 와 throws 차이
- 우테코
- ServiceTest
- 백준
- 제임스고슬링
- 프로젝트 패키지 구조
Archives
- Today
- Total
개발새발
unmodifiableList 란? 본문
기본적으로 객체들은 참조로 연결되어 있기 때문에 반환한 객체를 수정하게 되면 원본도 수정된다.
(ex) Google 공유 스프레드 시트)
의도하지 않은 수정을 막기 위해 방어적 복사에 대해서 찾아보다가
Collections에 있는 unmodifiableList 라는 메서드를 알게 되었다.
네이밍에서 알 수 있듯 수정이 불가한 List 를 만들어 반환하는 메서드인 듯 하다.
public List<Card> getValue() {
return Collections.unmodifiableList(value);
}
내부 구현 로직
unmodifiableList 는 UnmodifiableCollection 을 상속 받아 만들어져 있고,
get 메서드 외에 다른 메서드를 호출 할 경우 에러 를 던지도록 되어 있다.
즉, List를 읽기 전용으로 변환하는 것이다.
UnmodifiableList(List<? extends E> list) {
super(list);
this.list = list;
}
public boolean equals(Object o) {return o == this || list.equals(o);}
public int hashCode() {return list.hashCode();}
public E get(int index) {return list.get(index);} // 읽기 가능
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
public E remove(int index) {
throw new UnsupportedOperationException();
}
public int indexOf(Object o) {return list.indexOf(o);}
public int lastIndexOf(Object o) {return list.lastIndexOf(o);}
public boolean addAll(int index, Collection<? extends E> c) {
throw new UnsupportedOperationException();
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
throw new UnsupportedOperationException();
}
@Override
public void sort(Comparator<? super E> c) {
throw new UnsupportedOperationException();
}
public ListIterator<E> listIterator() {return listIterator(0);}
public ListIterator<E> listIterator(final int index) {
return new ListIterator<E>() {
private final ListIterator<? extends E> i
= list.listIterator(index);
public boolean hasNext() {return i.hasNext();}
public E next() {return i.next();}
public boolean hasPrevious() {return i.hasPrevious();}
public E previous() {return i.previous();}
public int nextIndex() {return i.nextIndex();}
public int previousIndex() {return i.previousIndex();}
public void remove() {
throw new UnsupportedOperationException();
}
public void set(E e) {
throw new UnsupportedOperationException();
}
public void add(E e) {
throw new UnsupportedOperationException();
}
@Override
public void forEachRemaining(Consumer<? super E> action) {
i.forEachRemaining(action);
}
};
}
public List<E> subList(int fromIndex, int toIndex) {
return new UnmodifiableList<>(list.subList(fromIndex, toIndex));
}
그럼 읽기 외 다른 연산을 할 수 없는가?
UnmodifiableList 내부 자체에선 그렇다. 그러나 아주 불가능 한 건 아니다.
원본 List 와의 참조는 연결 되어 있기 때문에 새로운 List 객체를 생성하면
UnmodifiableList 내부 데이터 값 + & 에 해당하는 연산 수행이 가능하다.
List<Card> cards = Collections.unmodifiableList(value);
public Cards(List<Card> cards) {
this.cards = new ArrayList<>(cards);
}
참고
'우아한테크코스 > level1' 카테고리의 다른 글
방어적 복사vs Unmodifiable vs copyOf 의 차이점 (1) | 2022.03.21 |
---|---|
List.copyOf 란? (0) | 2022.03.21 |
방어적 복사란? (0) | 2022.03.21 |
[레벨1] 블랙잭 회고 (2) | 2022.03.21 |
객체지향 한걸음 (0) | 2022.03.21 |