RESOLVED - BATCH-779: make FieldSetMapper and LineAggregator extend ItemProcessor

FiedSetMapper, LineTokenizer and LineAggregator interfaces now extend ItemProcessor
This commit is contained in:
robokaso
2008-09-29 11:36:13 +00:00
parent 131b524dcb
commit a6fd1cecc0
47 changed files with 186 additions and 196 deletions

View File

@@ -240,7 +240,7 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
String firstLine = readRecord();
// set names in tokenizer if they haven't been set already
if (tokenizer instanceof AbstractLineTokenizer && !((AbstractLineTokenizer) tokenizer).hasNames()) {
String[] names = tokenizer.tokenize(firstLine).getValues();
String[] names = tokenizer.process(firstLine).getValues();
((AbstractLineTokenizer) tokenizer).setNames(names);
}
if (headerCallback != null) {
@@ -263,10 +263,10 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
if (record != null) {
try {
FieldSet tokenizedLine = tokenizer.tokenize(record);
return fieldSetMapper.mapLine(tokenizedLine);
FieldSet tokenizedLine = tokenizer.process(record);
return fieldSetMapper.process(tokenizedLine);
}
catch (RuntimeException ex) {
catch (Exception ex) {
// add current line count to message and re-throw
throw new FlatFileParseException("Parsing error at line: " + lineCount + " in resource="
+ resource.getDescription() + ", input=[" + record + "]", ex, record, lineCount);

View File

@@ -189,7 +189,7 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
StringBuilder lines = new StringBuilder();
int lineCount = 0;
for (T item : items) {
lines.append(lineAggregator.aggregate(item) + lineSeparator);
lines.append(lineAggregator.process(item) + lineSeparator);
lineCount++;
}
try {

View File

@@ -102,7 +102,7 @@ public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar
/**
* The bean name (id) for an object that can be populated from the field set
* that will be passed into {@link #mapLine(FieldSet)}. Typically a
* that will be passed into {@link #process(FieldSet)}. Typically a
* prototype scoped bean so that a new instance is returned for each field
* set mapped.
*
@@ -118,7 +118,7 @@ public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar
/**
* Public setter for the type of bean to create instead of using a prototype
* bean. An object of this type will be created from its default constructor
* for every call to {@link #mapLine(FieldSet)}.<br/>
* for every call to {@link #process(FieldSet)}.<br/>
*
* Either this property or the prototype bean name must be specified, but
* not both.
@@ -153,10 +153,10 @@ public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar
* the {@link DataBinder} from {@link #createBinder(Object)} has errors
* after binding).
*
* @see org.springframework.batch.item.file.mapping.FieldSetMapper#mapLine(org.springframework.batch.item.file.mapping.FieldSet)
* @see org.springframework.batch.item.file.mapping.FieldSetMapper#process(Object)
*/
@SuppressWarnings("unchecked")
public T mapLine(FieldSet fs) {
public T process(FieldSet fs) {
T copy = getBean();
DataBinder binder = createBinder(copy);
binder.bind(new MutablePropertyValues(getBeanProperties(copy, fs.getProperties())));

View File

@@ -16,6 +16,8 @@
package org.springframework.batch.item.file.mapping;
import org.springframework.batch.item.ItemProcessor;
/**
* Interface that is used to map data obtained from a {@link FieldSet} into an
* object.
@@ -24,12 +26,5 @@ package org.springframework.batch.item.file.mapping;
* @author Dave Syer
*
*/
public interface FieldSetMapper<T> {
/**
* Method used to map data obtained from a {@link FieldSet} into an object.
*
* @param fs the {@link FieldSet} to map
*/
public T mapLine(FieldSet fs);
public interface FieldSetMapper<T> extends ItemProcessor<FieldSet, T> {
}

View File

@@ -31,7 +31,7 @@ public class PassThroughFieldSetMapper implements FieldSetMapper<FieldSet> {
* org.springframework.batch.io.file.FieldSetMapper#mapLine(org.springframework
* .batch.io.file.FieldSet)
*/
public FieldSet mapLine(FieldSet fs) {
public FieldSet process(FieldSet fs) {
return fs;
}

View File

@@ -64,7 +64,7 @@ public abstract class AbstractLineTokenizer implements LineTokenizer {
*
* @return the resulting tokens
*/
public FieldSet tokenize(String line) {
public FieldSet process(String line) {
if(line == null){
line = "";

View File

@@ -36,7 +36,7 @@ public class DelimitedLineAggregator<T> implements LineAggregator<T[]> {
/* (non-Javadoc)
* @see org.springframework.batch.item.file.transform.LineAggregator#aggregate(java.lang.Object)
*/
public String aggregate(T[] item) {
public String process(T[] item) {
return StringUtils.arrayToDelimitedString(item, delimiter);
}

View File

@@ -94,7 +94,7 @@ public class FormatterLineAggregator<T> implements LineAggregator<T> {
* @param item data to be aggregated
* @return aggregated string
*/
public String aggregate(T item) {
public String process(T item) {
Assert.notNull(item);
Assert.notNull(format);

View File

@@ -16,6 +16,8 @@
package org.springframework.batch.item.file.transform;
import org.springframework.batch.item.ItemProcessor;
/**
@@ -23,12 +25,5 @@ package org.springframework.batch.item.file.transform;
*
* @author Dave Syer
*/
public interface LineAggregator<T> {
/**
* Create a string from the value provided.
*
* @param item values to be converted
* @return string
*/
public String aggregate(T item);
public interface LineAggregator<T> extends ItemProcessor<T, String>{
}

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.item.file.transform;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.file.mapping.FieldSet;
@@ -26,14 +27,5 @@ import org.springframework.batch.item.file.mapping.FieldSet;
* @author tomas.slanina
*
*/
public interface LineTokenizer {
/**
* Yields the tokens resulting from the splitting of the supplied
* <code>line</code>.
*
* @param line the line to be tokenized (can be <code>null</code>)
*
* @return the resulting tokens
*/
FieldSet tokenize(String line);
public interface LineTokenizer extends ItemProcessor<String, FieldSet> {
}

View File

@@ -6,9 +6,9 @@ public class PassThroughLineAggregator<T> implements LineAggregator<T> {
/**
* Simply convert to a String with toString().
*
* @see org.springframework.batch.item.file.transform.LineAggregator#aggregate(java.lang.Object)
* @see org.springframework.batch.item.file.transform.LineAggregator#process(java.lang.Object)
*/
public String aggregate(T item) {
public String process(T item) {
return item.toString();
}

View File

@@ -31,7 +31,7 @@ public class PrefixMatchingCompositeLineTokenizer implements LineTokenizer {
this.tokenizers = new LinkedHashMap<String, LineTokenizer>(tokenizers);
}
public FieldSet tokenize(String line) {
public FieldSet process(String line) throws Exception {
if (line == null) {
return new DefaultFieldSet(new String[0]);
@@ -61,7 +61,7 @@ public class PrefixMatchingCompositeLineTokenizer implements LineTokenizer {
throw new IllegalStateException("Could not match record to tokenizer for line=[" + line + "]");
}
return tokenizer.tokenize(line);
return tokenizer.process(line);
}
}

View File

@@ -31,10 +31,10 @@ public class RecursiveCollectionLineAggregator<T> implements LineAggregator<Coll
* (non-Javadoc)
* @see org.springframework.batch.item.file.transform.LineAggregator#aggregate(java.lang.Object)
*/
public String aggregate(Collection<T> items) {
public String process(Collection<T> items) throws Exception {
StringBuilder builder = new StringBuilder();
for (T value : items) {
builder.append(delegate.aggregate(value) + LINE_SEPARATOR);
builder.append(delegate.process(value) + LINE_SEPARATOR);
}
return builder.delete(builder.length()-LINE_SEPARATOR.length(),builder.length()).toString();
}

View File

@@ -83,7 +83,7 @@ public class AggregateItemFieldSetMapper<T> implements FieldSetMapper<AggregateI
* @return an {@link AggregateItem} that wraps the return value from the
* delegate
*/
public AggregateItem<T> mapLine(FieldSet fieldSet) {
public AggregateItem<T> process(FieldSet fieldSet) throws Exception {
if (fieldSet.readString(0).equals(begin)) {
return AggregateItem.getHeader();
@@ -92,7 +92,7 @@ public class AggregateItemFieldSetMapper<T> implements FieldSetMapper<AggregateI
return AggregateItem.getFooter();
}
return new AggregateItem<T>(delegate.mapLine(fieldSet));
return new AggregateItem<T>(delegate.process(fieldSet));
}