IN PROGRESS - BATCH-712: Upgrade ItemReaders to use Parameterized types

Parameterized the ItemReader interface, key implementations and collaborators.
This commit is contained in:
robokaso
2008-07-18 11:25:35 +00:00
parent d643538a10
commit e6a08ac88f
23 changed files with 93 additions and 92 deletions

View File

@@ -35,7 +35,7 @@ package org.springframework.batch.item;
* @author Lucas Ward
* @since 1.0
*/
public interface ItemReader {
public interface ItemReader<T> {
/**
* Reads a piece of input data and advance to the next one. Implementations
@@ -46,7 +46,7 @@ public interface ItemReader {
*
* @throws Exception if an underlying resource is unavailable.
*/
Object read() throws Exception, UnexpectedInputException, NoWorkFoundException, ParseException;
T read() throws Exception, UnexpectedInputException, NoWorkFoundException, ParseException;
/**
* Mark the stream so that it can be reset later and the items backed

View File

@@ -65,19 +65,19 @@ import org.springframework.util.Assert;
*
* @author Lucas Ward
*/
public class DrivingQueryItemReader implements ItemReader, InitializingBean, ItemStream {
public class DrivingQueryItemReader<T> implements ItemReader<T>, InitializingBean, ItemStream {
private boolean initialized = false;
private List<Object> keys;
private List<T> keys;
private Iterator<Object> keysIterator;
private Iterator<T> keysIterator;
private int currentIndex = 0;
private int lastCommitIndex = 0;
private KeyCollector keyCollector;
private KeyCollector<T> keyCollector;
private boolean saveState = false;
@@ -90,7 +90,7 @@ public class DrivingQueryItemReader implements ItemReader, InitializingBean, Ite
*
* @param keys
*/
public DrivingQueryItemReader(List<Object> keys) {
public DrivingQueryItemReader(List<T> keys) {
this.keys = keys;
this.keysIterator = keys.iterator();
}
@@ -101,7 +101,7 @@ public class DrivingQueryItemReader implements ItemReader, InitializingBean, Ite
* @return next key in the list if not index is not at the last element,
* null otherwise.
*/
public Object read() {
public T read() {
if (keysIterator.hasNext()) {
currentIndex++;
@@ -173,7 +173,7 @@ public class DrivingQueryItemReader implements ItemReader, InitializingBean, Ite
*
* @param keyCollector
*/
public void setKeyCollector(KeyCollector keyCollector) {
public void setKeyCollector(KeyCollector<T> keyCollector) {
this.keyCollector = keyCollector;
}

View File

@@ -97,7 +97,7 @@ import org.springframework.util.ClassUtils;
* @author Peter Zozom
* @author Robert Kasanicky
*/
public class JdbcCursorItemReader extends AbstractBufferedItemReaderItemStream implements InitializingBean {
public class JdbcCursorItemReader<T> extends AbstractBufferedItemReaderItemStream<T> implements InitializingBean {
private static Log log = LogFactory.getLog(JdbcCursorItemReader.class);
@@ -413,13 +413,14 @@ public class JdbcCursorItemReader extends AbstractBufferedItemReaderItemStream i
* Read next row and map it to item, verify cursor position if
* {@link #setVerifyCursorPosition(boolean)} is true.
*/
protected Object doRead() throws Exception {
@SuppressWarnings("unchecked")
protected T doRead() throws Exception {
try {
if (!rs.next()) {
return null;
}
int currentRow = getCurrentItemCount();
Object item = mapper.mapRow(rs, currentRow);
T item = (T) mapper.mapRow(rs, currentRow);
verifyCursorPosition(currentRow);
return item;
}

View File

@@ -10,7 +10,7 @@ import org.springframework.batch.item.ExecutionContext;
* @author Lucas Ward
* @see DrivingQueryItemReader
*/
public interface KeyCollector {
public interface KeyCollector<T> {
/**
* <p>Retrieve the keys to be iterated over. If the ExecutionContext
@@ -28,7 +28,7 @@ public interface KeyCollector {
* that could potentially be used to retrieve the correct keys.
* @return list of keys returned by the driving query (can be empty but not null)
*/
List<Object> retrieveKeys(ExecutionContext executionContext);
List<T> retrieveKeys(ExecutionContext executionContext);
/**
* Given the provided key, store it in the provided ExecutionContext. This

View File

@@ -61,8 +61,8 @@ import org.springframework.util.ClassUtils;
* @author Robert Kasanicky
* @author Dave Syer
*/
public class FlatFileItemReader extends AbstractBufferedItemReaderItemStream implements
ResourceAwareItemReaderItemStream, InitializingBean {
public class FlatFileItemReader<T> extends AbstractBufferedItemReaderItemStream<T> implements
ResourceAwareItemReaderItemStream<T>, InitializingBean {
private static Log log = LogFactory.getLog(FlatFileItemReader.class);
@@ -83,7 +83,7 @@ public class FlatFileItemReader extends AbstractBufferedItemReaderItemStream imp
private LineTokenizer tokenizer = new DelimitedLineTokenizer();
private FieldSetMapper fieldSetMapper;
private FieldSetMapper<T> fieldSetMapper;
/**
* Encapsulates the state of the input source. If it is null then we are
@@ -186,7 +186,7 @@ public class FlatFileItemReader extends AbstractBufferedItemReaderItemStream imp
*
* @param fieldSetMapper
*/
public void setFieldSetMapper(FieldSetMapper fieldSetMapper) {
public void setFieldSetMapper(FieldSetMapper<T> fieldSetMapper) {
this.fieldSetMapper = fieldSetMapper;
}
@@ -270,7 +270,7 @@ public class FlatFileItemReader extends AbstractBufferedItemReaderItemStream imp
*
* @see org.springframework.batch.item.ItemReader#read()
*/
protected Object doRead() throws Exception {
protected T doRead() throws Exception {
String line = readLine();
if (line != null) {

View File

@@ -10,7 +10,7 @@ import org.springframework.core.io.Resource;
*
* @author Robert Kasanicky
*/
public interface ResourceAwareItemReaderItemStream extends ItemReader, ItemStream {
public interface ResourceAwareItemReaderItemStream<T> extends ItemReader<T>, ItemStream {
void setResource(Resource resource);
}

View File

@@ -24,7 +24,7 @@ package org.springframework.batch.item.file.mapping;
* @author Dave Syer
*
*/
public interface FieldSetMapper {
public interface FieldSetMapper<T> {
/**
* Method used to map data obtained from a {@link FieldSet} into an object.
@@ -32,5 +32,5 @@ public interface FieldSetMapper {
* @param fs the {@link FieldSet} to map
* @param lineNum the current line number (if known), or negative if not
*/
public Object mapLine(FieldSet fs, int lineNum);
public T mapLine(FieldSet fs, int lineNum);
}

View File

@@ -23,13 +23,13 @@ package org.springframework.batch.item.file.mapping;
* @author Lucas Ward
*
*/
public class PassThroughFieldSetMapper implements FieldSetMapper, FieldSetCreator {
public class PassThroughFieldSetMapper implements FieldSetMapper<FieldSet>, FieldSetCreator {
/*
* (non-Javadoc)
* @see org.springframework.batch.io.file.FieldSetMapper#mapLine(org.springframework.batch.io.file.FieldSet)
*/
public Object mapLine(FieldSet fs, int lineNum) {
public FieldSet mapLine(FieldSet fs, int lineNum) {
return fs;
}

View File

@@ -21,7 +21,7 @@ import org.springframework.batch.item.ItemReader;
* @author Dave Syer
*
*/
public interface LineReader extends ItemReader {
public interface LineReader extends ItemReader<String> {
/**
* @return position

View File

@@ -24,7 +24,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.MarkFailedException;
@@ -51,7 +50,7 @@ import org.springframework.util.Assert;
* @author Dave Syer
* @author Rob Harrop
*/
public class ResourceLineReader implements LineReader, ItemReader {
public class ResourceLineReader implements LineReader {
private static final Collection<String> DEFAULT_COMMENTS = Collections.singleton("#");
@@ -112,7 +111,7 @@ public class ResourceLineReader implements LineReader, ItemReader {
*
* @see org.springframework.batch.item.ItemReader#read()
*/
public synchronized Object read() {
public synchronized String read() {
// Make a copy of the recordSeparatorPolicy reference, in case it is
// changed during a read operation (unlikely, but you never know)...
RecordSeparatorPolicy recordSeparatorPolicy = this.recordSeparatorPolicy;

View File

@@ -26,7 +26,7 @@ import org.springframework.util.Assert;
*
* @author Robert Kasanicky
*/
public abstract class AbstractBufferedItemReaderItemStream implements ItemReader, ItemStream {
public abstract class AbstractBufferedItemReaderItemStream<T> implements ItemReader<T>, ItemStream {
private static final String READ_COUNT = "read.count";
@@ -36,9 +36,9 @@ public abstract class AbstractBufferedItemReaderItemStream implements ItemReader
private boolean shouldReadBuffer = false;
private List<Object> itemBuffer = new ArrayList<Object>();
private List<T> itemBuffer = new ArrayList<T>();
private ListIterator<Object> itemBufferIterator = null;
private ListIterator<T> itemBufferIterator = null;
private int lastMarkedBufferIndex = 0;
@@ -51,7 +51,7 @@ public abstract class AbstractBufferedItemReaderItemStream implements ItemReader
* @return item
* @throws Exception
*/
protected abstract Object doRead() throws Exception;
protected abstract T doRead() throws Exception;
/**
* Open resources necessary to start reading input.
@@ -74,7 +74,7 @@ public abstract class AbstractBufferedItemReaderItemStream implements ItemReader
}
}
public Object read() throws Exception, UnexpectedInputException, NoWorkFoundException, ParseException {
public T read() throws Exception, UnexpectedInputException, NoWorkFoundException, ParseException {
currentItemCount++;
@@ -89,7 +89,7 @@ public abstract class AbstractBufferedItemReaderItemStream implements ItemReader
}
}
Object item = doRead();
T item = doRead();
itemBuffer.add(item);
return item;

View File

@@ -26,7 +26,7 @@ import org.springframework.batch.item.ResetFailedException;
* @author Dave Syer
*
*/
public abstract class AbstractItemReader implements ItemReader {
public abstract class AbstractItemReader<T> implements ItemReader<T> {
public void mark() throws MarkFailedException {
}

View File

@@ -25,6 +25,6 @@ import org.springframework.batch.item.ItemStreamSupport;
* @author Dave Syer
*
*/
public abstract class AbstractItemStreamItemReader extends ItemStreamSupport implements ItemReader {
public abstract class AbstractItemStreamItemReader<T> extends ItemStreamSupport implements ItemReader<T> {
}

View File

@@ -29,9 +29,9 @@ import org.springframework.util.Assert;
*
* @author Dave Syer
*/
public class DelegatingItemReader extends AbstractItemReader implements InitializingBean {
public class DelegatingItemReader<T> extends AbstractItemReader<T> implements InitializingBean {
private ItemReader itemReader;
private ItemReader<T> itemReader;
/**
* Default constructor.
@@ -43,7 +43,7 @@ public class DelegatingItemReader extends AbstractItemReader implements Initiali
/**
* Convenience constructor for setting mandatory property.
*/
public DelegatingItemReader(ItemReader itemReader) {
public DelegatingItemReader(ItemReader<T> itemReader) {
this();
this.itemReader = itemReader;
}
@@ -57,7 +57,7 @@ public class DelegatingItemReader extends AbstractItemReader implements Initiali
* @throws Exception
* @see org.springframework.batch.item.ItemReader#read()
*/
public Object read() throws Exception {
public T read() throws Exception {
return itemReader.read();
}
@@ -65,7 +65,7 @@ public class DelegatingItemReader extends AbstractItemReader implements Initiali
* Setter for input source.
* @param source
*/
public void setItemReader(ItemReader source) {
public void setItemReader(ItemReader<T> source) {
this.itemReader = source;
}

View File

@@ -28,22 +28,22 @@ import org.springframework.batch.item.ItemReader;
* @author Dave Syer
*
*/
public class ListItemReader extends AbstractItemReader {
public class ListItemReader<T> extends AbstractItemReader<T> {
private List<?> list;
private List<T> list;
public ListItemReader(List<?> list) {
public ListItemReader(List<T> list) {
// If it is a proxy we assume it knows how to deal with its own state.
// (It's probably transaction aware.)
if (AopUtils.isAopProxy(list)) {
this.list = list;
}
else {
this.list = new ArrayList<Object>(list);
this.list = new ArrayList<T>(list);
}
}
public Object read() {
public T read() {
if (!list.isEmpty()) {
return list.remove(0);
}

View File

@@ -25,7 +25,7 @@ import org.springframework.util.Assert;
* @author Lucas Ward
*
*/
public class ValidatingItemReader extends DelegatingItemReader {
public class ValidatingItemReader<T> extends DelegatingItemReader<T> {
private Validator validator;
@@ -40,8 +40,8 @@ public class ValidatingItemReader extends DelegatingItemReader {
/* (non-Javadoc)
* @see org.springframework.batch.item.reader.DelegatingItemReader#read()
*/
public Object read() throws Exception {
Object input = super.read();
public T read() throws Exception {
T input = super.read();
if(input != null){
validator.validate(input);
}

View File

@@ -8,7 +8,7 @@ import javax.xml.stream.XMLEventReader;
*
* @author Robert Kasanicky
*/
public interface EventReaderDeserializer {
public interface EventReaderDeserializer<T> {
Object deserializeFragment(XMLEventReader eventReader);
T deserializeFragment(XMLEventReader eventReader);
}

View File

@@ -30,14 +30,14 @@ import org.springframework.util.ClassUtils;
*
* @author Robert Kasanicky
*/
public class StaxEventItemReader extends AbstractBufferedItemReaderItemStream implements
ResourceAwareItemReaderItemStream, InitializingBean {
public class StaxEventItemReader<T> extends AbstractBufferedItemReaderItemStream<T> implements
ResourceAwareItemReaderItemStream<T>, InitializingBean {
private FragmentEventReader fragmentReader;
private XMLEventReader eventReader;
private EventReaderDeserializer eventReaderDeserializer;
private EventReaderDeserializer<T> eventReaderDeserializer;
private Resource resource;
@@ -57,7 +57,7 @@ public class StaxEventItemReader extends AbstractBufferedItemReaderItemStream im
* @param eventReaderDeserializer maps xml fragments corresponding to
* records to objects
*/
public void setFragmentDeserializer(EventReaderDeserializer eventReaderDeserializer) {
public void setFragmentDeserializer(EventReaderDeserializer<T> eventReaderDeserializer) {
this.eventReaderDeserializer = eventReaderDeserializer;
}
@@ -146,8 +146,8 @@ public class StaxEventItemReader extends AbstractBufferedItemReaderItemStream im
/**
* Move to next fragment and map it to item.
*/
protected Object doRead() throws Exception {
Object item = null;
protected T doRead() throws Exception {
T item = null;
if (moveCursorToNextFragment(fragmentReader)) {
fragmentReader.markStartFragment();

View File

@@ -16,7 +16,7 @@ import org.springframework.xml.transform.StaxSource;
* @author Robert Kasanicky
* @author Lucas Ward
*/
public class UnmarshallingEventReaderDeserializer implements EventReaderDeserializer {
public class UnmarshallingEventReaderDeserializer<T> implements EventReaderDeserializer<T> {
private Unmarshaller unmarshaller;
@@ -25,10 +25,11 @@ public class UnmarshallingEventReaderDeserializer implements EventReaderDeserial
this.unmarshaller = unmarshaller;
}
public Object deserializeFragment(XMLEventReader eventReader) {
Object item = null;
@SuppressWarnings("unchecked")
public T deserializeFragment(XMLEventReader eventReader) {
T item = null;
try {
item = unmarshaller.unmarshal(new StaxSource(eventReader));
item = (T) unmarshaller.unmarshal(new StaxSource(eventReader));
}
catch (IOException e) {
throw new DataAccessResourceFailureException("IO error during unmarshalling", e);

View File

@@ -29,15 +29,15 @@ import org.springframework.batch.repeat.RepeatContext;
* @author Dave Syer
*
*/
public class ItemReaderRepeatCallback implements RepeatCallback {
public class ItemReaderRepeatCallback<T> implements RepeatCallback {
ItemReader provider;
ItemReader<T> reader;
ItemWriter writer;
public ItemReaderRepeatCallback(ItemReader provider, ItemWriter writer) {
public ItemReaderRepeatCallback(ItemReader<T> reader, ItemWriter writer) {
super();
this.provider = provider;
this.reader = reader;
this.writer = writer;
}
@@ -47,7 +47,7 @@ public class ItemReaderRepeatCallback implements RepeatCallback {
* provider by calling next().
* @param provider
*/
public ItemReaderRepeatCallback(ItemReader provider) {
public ItemReaderRepeatCallback(ItemReader<T> provider) {
this(provider, null);
}
@@ -61,7 +61,7 @@ public class ItemReaderRepeatCallback implements RepeatCallback {
public ExitStatus doInIteration(RepeatContext context) throws Exception {
ExitStatus result = ExitStatus.FINISHED;
Object item = provider.read();
T item = reader.read();
if (writer != null) {
if (item != null) {

View File

@@ -10,12 +10,12 @@ import org.springframework.batch.item.sample.Foo;
*/
public abstract class CommonItemReaderTests extends TestCase {
protected ItemReader tested;
protected ItemReader<Foo> tested;
/**
* @return configured ItemReader ready for use.
*/
protected abstract ItemReader getItemReader() throws Exception;
protected abstract ItemReader<Foo> getItemReader() throws Exception;
protected void setUp() throws Exception {
tested = getItemReader();
@@ -26,19 +26,19 @@ public abstract class CommonItemReaderTests extends TestCase {
*/
public void testRead() throws Exception {
Foo foo1 = (Foo) tested.read();
Foo foo1 = tested.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) tested.read();
Foo foo2 = tested.read();
assertEquals(2, foo2.getValue());
Foo foo3 = (Foo) tested.read();
Foo foo3 = tested.read();
assertEquals(3, foo3.getValue());
Foo foo4 = (Foo) tested.read();
Foo foo4 = tested.read();
assertEquals(4, foo4.getValue());
Foo foo5 = (Foo) tested.read();
Foo foo5 = tested.read();
assertEquals(5, foo5.getValue());
assertNull(tested.read());
@@ -49,15 +49,15 @@ public abstract class CommonItemReaderTests extends TestCase {
* interval can change dynamically.
*/
public void testReset() throws Exception {
Foo foo1 = (Foo) tested.read();
Foo foo1 = tested.read();
assertEquals(1, foo1.getValue());
tested.mark();
Foo foo2 = (Foo) tested.read();
Foo foo2 = tested.read();
assertEquals(2, foo2.getValue());
Foo foo3 = (Foo) tested.read();
Foo foo3 = tested.read();
assertEquals(3, foo3.getValue());
tested.reset();
@@ -72,12 +72,12 @@ public abstract class CommonItemReaderTests extends TestCase {
assertEquals(foo3, tested.read());
Foo foo4 = (Foo) tested.read();
Foo foo4 = tested.read();
assertEquals(4, foo4.getValue());
tested.mark();
Foo foo5 = (Foo) tested.read();
Foo foo5 = tested.read();
assertEquals(5, foo5.getValue());
tested.reset();
@@ -104,7 +104,7 @@ public abstract class CommonItemReaderTests extends TestCase {
* @param tested
* the reader
*/
protected abstract void pointToEmptyInput(ItemReader tested)
protected abstract void pointToEmptyInput(ItemReader<Foo> tested)
throws Exception;
}

View File

@@ -37,10 +37,10 @@ public abstract class CommonItemStreamItemReaderTests extends CommonItemReaderTe
testedAsStream().update(executionContext);
Foo foo1 = (Foo) tested.read();
Foo foo1 = tested.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) tested.read();
Foo foo2 = tested.read();
assertEquals(2, foo2.getValue());
testedAsStream().update(executionContext);
@@ -50,7 +50,7 @@ public abstract class CommonItemStreamItemReaderTests extends CommonItemReaderTe
testedAsStream().open(executionContext);
Foo fooAfterRestart = (Foo) tested.read();
Foo fooAfterRestart = tested.read();
assertEquals(3, fooAfterRestart.getValue());
}
@@ -63,15 +63,15 @@ public abstract class CommonItemStreamItemReaderTests extends CommonItemReaderTe
testedAsStream().update(executionContext);
Foo foo1 = (Foo) tested.read();
Foo foo1 = tested.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) tested.read();
Foo foo2 = tested.read();
assertEquals(2, foo2.getValue());
tested.mark();
Foo foo3 = (Foo) tested.read();
Foo foo3 = tested.read();
assertEquals(3, foo3.getValue());
tested.reset();
@@ -83,17 +83,17 @@ public abstract class CommonItemStreamItemReaderTests extends CommonItemReaderTe
testedAsStream().open(executionContext);
Foo fooAfterRestart = (Foo) tested.read();
Foo fooAfterRestart = tested.read();
assertEquals(3, fooAfterRestart.getValue());
}
public void testReopen() throws Exception {
testedAsStream().update(executionContext);
Foo foo1 = (Foo) tested.read();
Foo foo1 = tested.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) tested.read();
Foo foo2 = tested.read();
assertEquals(2, foo2.getValue());
testedAsStream().update(executionContext);
@@ -103,7 +103,7 @@ public abstract class CommonItemStreamItemReaderTests extends CommonItemReaderTe
testedAsStream().open(executionContext);
Foo fooAfterRestart = (Foo) tested.read();
Foo fooAfterRestart = tested.read();
assertEquals(3, fooAfterRestart.getValue());
}

View File

@@ -23,8 +23,8 @@ import junit.framework.TestCase;
public class ItemProviderTests extends TestCase {
ItemReader provider = new AbstractItemReader() {
public Object read() {
ItemReader<String> provider = new AbstractItemReader<String>() {
public String read() {
return "foo";
}
};