From 44fc2b1069ab5295985d530ed5bc9cbd95b93cb7 Mon Sep 17 00:00:00 2001 From: Adam Richeimer Date: Mon, 15 Sep 2014 18:15:04 -0700 Subject: [PATCH] Change ClassifierCompositeItemWriter to honor item order The ClassifierCompositeItemWriter loops through the items passed to the write method twice. The first time to classifier each item. The second tiem to perform the actual writes (this prevents the unnecessicary IO if an error occurs during classification). A HashMap was previously used to hold the classified items (key to the writer : item) however this causes the items to be written out of order. This update changes it to a LinkedHashMap to honor the order the items come in. --- .../batch/item/support/ClassifierCompositeItemWriter.java | 4 ++-- .../item/support/ClassifierCompositeItemWriterTests.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ClassifierCompositeItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ClassifierCompositeItemWriter.java index b8ab7e146..75a5d46d8 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ClassifierCompositeItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ClassifierCompositeItemWriter.java @@ -17,7 +17,7 @@ package org.springframework.batch.item.support; import java.util.ArrayList; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -52,7 +52,7 @@ public class ClassifierCompositeItemWriter implements ItemWriter { @Override public void write(List items) throws Exception { - Map, List> map = new HashMap, List>(); + Map, List> map = new LinkedHashMap, List>(); for (T item : items) { ItemWriter key = classifier.classify(item); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/ClassifierCompositeItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/ClassifierCompositeItemWriterTests.java index de1cf6395..0405c940b 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/ClassifierCompositeItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/ClassifierCompositeItemWriterTests.java @@ -55,9 +55,9 @@ public class ClassifierCompositeItemWriterTests { map.put("foo", fooWriter ); map.put("*", defaultWriter); writer.setClassifier(new PatternMatchingClassifier>(map)); - writer.write(Arrays.asList("foo", "foo", "bar")); + writer.write(Arrays.asList("foo", "foo", "one", "two", "three")); assertEquals("[foo, foo]", foos.toString()); - assertEquals("[bar]", defaults.toString()); + assertEquals("[one, two, three]", defaults.toString()); } }