diff --git a/docs/src/site/docbook/reference/readersAndWriters.xml b/docs/src/site/docbook/reference/readersAndWriters.xml index d8befc122..d4189ffe4 100644 --- a/docs/src/site/docbook/reference/readersAndWriters.xml +++ b/docs/src/site/docbook/reference/readersAndWriters.xml @@ -1951,15 +1951,15 @@ that contains another ItemReader. For example: - public class CompositeItemWriter implements ItemWriter { + public class CompositeItemWriter<T> implements ItemWriter<T> { - ItemWriter itemWriter; + ItemWriter<T> itemWriter; - public CompositeItemWriter(ItemWriter itemWriter) { + public CompositeItemWriter(ItemWriter<T> itemWriter) { this.itemWriter = itemWriter; } - public void write(Object item) throws Exception { + public void write(T item) throws Exception { //Add business logic here @@ -1990,10 +1990,10 @@ just want to modify the item. For this scenario, Spring Batch provides the ItemTransformer interface: - public interface ItemTransformer { + public interface ItemProcessor<I, O> { - Object transform(Object item) throws Exception; - } + O process(I item) throws Exception; +} An ItemTransformer is very simple, given one object, transorm it and return another. The object provided may or may not @@ -2015,7 +2015,7 @@ public Bar(Foo foo) {} } - public class FooTransformer implements ItemTransformer{ + public class FooTransformer implements ItemProcessor{ //Preform simple transformation, convert a Foo to a Barr public Object transform(Object item) throws Exception { @@ -2126,7 +2126,7 @@ BarTransformer can be 'chained' together to give the resultant Foobar: - CompositeItemTransformer compositeTransformer = new CompositeItemTransformer(); + CompositeItemProcessor compositeTransformer = new CompositeItemProcessor(); List itemTransformers = new ArrayList(); itemTransformers.add(new FooTransformer()); itemTransformers.add(new BarTransformer()); @@ -2299,15 +2299,15 @@ basic contract of ItemReader, read: - public class CustomItemReader implements ItemReader{ + public class CustomItemReader<T> implements ItemReader<T>{ - List items; + List<T> items; - public CustomItemReader(List items) { + public CustomItemReader(List<T> items) { this.items = items; } - public Object read() throws Exception, UnexpectedInputException, + public T read() throws Exception, UnexpectedInputException, NoWorkFoundException, ParseException { if (!items.isEmpty()) { @@ -2326,12 +2326,12 @@ thus satisfying the most basic requirements of an ItemReader, as illustrated below: - List items = new ArrayList(); + List<String> items = new ArrayList<String>(); items.add("1"); items.add("2"); items.add("3"); - ItemReader itemReader = new CustomItemReader(items); + ItemReader itemReader = new CustomItemReader<String>(items); assertEquals("1", itemReader.read()); assertEquals("2", itemReader.read()); assertEquals("3", itemReader.read()); @@ -2351,17 +2351,17 @@ empty, but we'll need to add code to them in order to support the rollback scenario: - public class CustomItemReader implements ItemReader{ + public class CustomItemReader<T> implements ItemReader<T>{ - List items; + List<T> items; int currentIndex = 0; int lastMarkedIndex = 0; - public CustomItemReader(List items) { + public CustomItemReader(List<T> items) { this.items = items; } - public Object read() throws Exception, UnexpectedInputException, + public T read() throws Exception, UnexpectedInputException, NoWorkFoundException, ParseException { if (currentIndex < items.size()) { @@ -2434,49 +2434,51 @@ implemented with the ItemStream interface: - public class CustomItemReader implements ItemReader, ItemStream{ + public class CustomItemReader<T> implements ItemReader<T>, ItemStream{ - List items; + List<T> items; int currentIndex = 0; int lastMarkedIndex = 0; - private static String CURRENT_INDEX = "current.index"; + private static final String CURRENT_INDEX = "current.index"; - public CustomItemReader(List items) { + public CustomItemReader(List<T> items) { this.items = items; } - public Object read() throws Exception, UnexpectedInputException, + public T read() throws Exception, UnexpectedInputException, NoWorkFoundException, ParseException { if (currentIndex < items.size()) { return items.get(currentIndex++); } - return null; + + return null; } public void mark() throws MarkFailedException { lastMarkedIndex = currentIndex; - }; + } public void reset() throws ResetFailedException { currentIndex = lastMarkedIndex; } - public void open(ExecutionContext executionContext) throws ItemStreamTException { + public void open(ExecutionContext executionContext) throws ItemStreamException { + if(executionContext.containsKey(CURRENT_INDEX)){ currentIndex = new Long(executionContext.getLong(CURRENT_INDEX)).intValue(); } else{ currentIndex = 0; lastMarkedIndex = 0; - } + } } + public void close(ExecutionContext executionContext) throws ItemStreamException {} + public void update(ExecutionContext executionContext) throws ItemStreamException { executionContext.putLong(CURRENT_INDEX, new Long(currentIndex).longValue()); }; - - public void close(ExecutionContext executionContext) throws ItemStreamException {} } On each call to ItemStream @@ -2494,11 +2496,11 @@ assertEquals("1", itemReader.read()); ((ItemStream)itemReader).update(executionContext); - List items = new ArrayList(); + List<String> items = new ArrayList<String>(); items.add("1"); items.add("2"); items.add("3"); - itemReader = new CustomItemReader(items); + itemReader = new CustomItemReader<String>(items); ((ItemStream)itemReader).open(executionContext); assertEquals("2", itemReader.read()); @@ -2532,11 +2534,11 @@ example. As with the ItemReader example, a List will be used in order to keep the example as simple as possible: - public class CustomItemWriter implements ItemWriter{ + public class CustomItemWriter<T> implements ItemWriter<T>{ - List output = new ArrayList(); + List<T> output = new ArrayList<T>(); - public void write(Object item) throws Exception { + public void write(T item) throws Exception { output.add(item); } @@ -2563,12 +2565,12 @@ flush and clear should be implemented to allow for a buffering solution: - public class CustomItemWriter implements ItemWriter{ + public class CustomItemWriter<T> implements ItemWriter<T>{ - List output = new ArrayList(); - List buffer = new ArrayList(); + List<T> output = new ArrayList<T>(); + List<T> buffer = new ArrayList<T>(); - public void write(Object item) throws Exception { + public void write(T item) throws Exception { buffer.add(item); } @@ -2577,9 +2579,8 @@ } public void flush() throws FlushFailedException { - for(Iterator it = buffer.iterator(); it.hasNext();){ - output.add(it.next()); - it.remove(); + for(T t:buffer){ + output.add(t); } } }