EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.item.support]

COVERAGE SUMMARY FOR SOURCE FILE [ClassifierCompositeItemWriter.java]

nameclass, %method, %block, %line, %
ClassifierCompositeItemWriter.java100% (1/1)100% (3/3)100% (70/70)100% (14/14)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ClassifierCompositeItemWriter100% (1/1)100% (3/3)100% (70/70)100% (14/14)
ClassifierCompositeItemWriter (): void 100% (1/1)100% (9/9)100% (2/2)
setClassifier (Classifier): void 100% (1/1)100% (4/4)100% (2/2)
write (List): void 100% (1/1)100% (57/57)100% (10/10)

1/*
2 * Copyright 2006-2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17package org.springframework.batch.item.support;
18 
19import java.util.ArrayList;
20import java.util.HashMap;
21import java.util.List;
22import java.util.Map;
23 
24import org.springframework.batch.classify.Classifier;
25import org.springframework.batch.classify.ClassifierSupport;
26import org.springframework.batch.item.ItemWriter;
27 
28/**
29 * Calls one of a collection of ItemWriters for each item, based on a router
30 * pattern implemented through the provided {@link Classifier}.
31 * 
32 * The implementation is thread-safe if all delegates are thread-safe.
33 * 
34 * @author Dave Syer
35 * @since 2.0
36 */
37public class ClassifierCompositeItemWriter<T> implements ItemWriter<T> {
38 
39        private Classifier<T, ItemWriter<? super T>> classifier = new ClassifierSupport<T, ItemWriter<? super T>>(null);
40 
41        /**
42         * @param classifier the classifier to set
43         */
44        public void setClassifier(Classifier<T, ItemWriter<? super T>> classifier) {
45                this.classifier = classifier;
46        }
47 
48        /**
49         * Delegates to injected {@link ItemWriter} instances according to their
50         * classification by the {@link Classifier}.
51         */
52        public void write(List<? extends T> items) throws Exception {
53 
54                Map<ItemWriter<? super T>, List<T>> map = new HashMap<ItemWriter<? super T>, List<T>>();
55 
56                for (T item : items) {
57                        ItemWriter<? super T> key = classifier.classify(item);
58                        if (!map.containsKey(key)) {
59                                map.put(key, new ArrayList<T>());
60                        }
61                        map.get(key).add(item);
62                }
63 
64                for (ItemWriter<? super T> writer : map.keySet()) {
65                        writer.write(map.get(writer));
66                }
67 
68        }
69 
70}

[all classes][org.springframework.batch.item.support]
EMMA 2.0.5312 (C) Vladimir Roubtsov