[Spring] CompositeMap & CompositeIterator
CompositeMap, CompositeIterator, org.springframework.util.CompositeMap, composite map
정의
org.springframework.util.CompositeMap<K,V> 는 두 개의 Map 을 하나의 view 로 합쳐서 보여주는 read-mostly 자료구조. 첫 번째 Map 이 우선, 없으면 두 번째 Map 을 본다.
org.springframework.util.CompositeIterator<E> 는 여러 Iterator 를 순차적으로 이어 붙여 하나의 iterator 처럼 제공.
둘 다 “여러 소스의 데이터를 하나로 본다” 는 pattern 의 도우미.
시각화
CompositeMap
Map<String, Integer> primary = Map.of("a", 1, "b", 2);
Map<String, Integer> secondary = Map.of("b", 99, "c", 3);
CompositeMap<String, Integer> composite = new CompositeMap<>(primary, secondary);
composite.get("a"); // 1 (primary)
composite.get("b"); // 2 (primary 우선)
composite.get("c"); // 3 (secondary)
primary 가 secondary 를 덮어쓰는 view. 환경별 설정 (default + override) 처럼 layered 데이터에 적합.
put 은 primary 에만 들어간다. secondary 는 일반적으로 read-only 취급.
CompositeIterator
List<Integer> a = List.of(1, 2, 3);
List<Integer> b = List.of(4, 5);
CompositeIterator<Integer> it = new CompositeIterator<>();
it.add(a.iterator());
it.add(b.iterator());
while (it.hasNext()) System.out.print(it.next() + " ");
// → 1 2 3 4 5
여러 컬렉션을 합치지 않고 순차 순회만 필요할 때 유용. 메모리 절약.
사용 사례
- 환경별 properties (default → environment-specific 우선)
- 여러 cache 의 layered lookup (L1 → L2)
- Spring 내부 BeanDefinition 의 parent-child relation 일부
한계
- thread-safe 가 아니다
- secondary 수정에 대한 의미 약함 (구현체 의존)
💬 댓글