RESOLVED - BATCH-938: Clean up DelimitedLineAggregator implementation

applied patch
This commit is contained in:
robokaso
2008-11-24 09:20:22 +00:00
parent fb9b662160
commit 0bcf2e092c
7 changed files with 68 additions and 145 deletions

View File

@@ -1,61 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.item.file.transform;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
/**
* This is a delimited line aggregator for a java bean. Given an array of
* property names, it will reflectively call getters on the item to aggregate.
*
* @author Dan Garrette
* @since 2.0
*/
public class BeanDelimitingLineAggregator<T> implements LineAggregator<T>, InitializingBean {
private BeanWrapperFieldExtractor<T> extractor;
private DelimitedLineAggregator<Object> delimitedLineAggregator = new DelimitedLineAggregator<Object>();
/**
* @param names names of properties of the aggregated item that will be
* included in the resulting string. Must not be <code>null</code>.
*/
public void setNames(String[] names) {
extractor = new BeanWrapperFieldExtractor<T>();
extractor.setNames(names);
extractor.afterPropertiesSet();
}
/**
* @param delimiter used to separate property values in the
* {@link #aggregate(Object)} result
*/
public void setDelimiter(String delimiter) {
this.delimitedLineAggregator.setDelimiter(delimiter);
}
public String aggregate(T item) {
Object[] fields = this.extractor.extract(item);
return this.delimitedLineAggregator.aggregate(fields);
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(this.extractor, "The 'names' property must be set.");
}
}

View File

@@ -21,7 +21,7 @@ import org.springframework.util.StringUtils;
* @author Dave Syer
*
*/
public class DelimitedLineAggregator<T> implements LineAggregator<T[]> {
public class DelimitedLineAggregator<T> extends ExtractorLineAggregator<T> {
private String delimiter = ",";
@@ -33,11 +33,11 @@ public class DelimitedLineAggregator<T> implements LineAggregator<T[]> {
this.delimiter = delimiter;
}
/* (non-Javadoc)
* @see org.springframework.batch.item.file.transform.LineAggregator#aggregate(java.lang.Object)
/**
* @see org.springframework.batch.item.file.transform.ExtractorLineAggregator#doAggregate(java.lang.Object[])
*/
public String aggregate(T[] item) {
return StringUtils.arrayToDelimitedString(item, delimiter);
public String doAggregate(Object[] item) {
return StringUtils.arrayToDelimitedString(item, this.delimiter);
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.item.file.transform;
import org.springframework.util.Assert;
/**
* @author Dan Garrette
* @since 2.0
*/
public abstract class ExtractorLineAggregator<T> implements LineAggregator<T> {
private FieldExtractor<T> fieldExtractor = new PassThroughFieldExtractor<T>();
/**
* Public setter for the field extractor responsible for splitting an input
* object up into an array of objects. Defaults to
* {@link PassThroughFieldExtractor}.
*
* @param fieldExtractor The field extractor to set
*/
public void setFieldExtractor(FieldExtractor<T> fieldExtractor) {
this.fieldExtractor = fieldExtractor;
}
/**
* Extract fields from the given item using the {@link FieldExtractor} and
* then aggregate them. Null items are not allowed.
*
* @see org.springframework.batch.item.file.transform.LineAggregator#aggregate(java.lang.Object)
*/
public String aggregate(T item) {
Assert.notNull(item);
return this.doAggregate(this.fieldExtractor.extract(item));
}
/**
* Aggregate provided fields into single String.
*
* @param fields An array of the fields that must be aggregated
* @return aggregated string
*/
protected abstract String doAggregate(Object[] fields);
}

View File

@@ -29,12 +29,10 @@ import org.springframework.util.Assert;
*
* @author Dave Syer
*/
public class FormatterLineAggregator<T> implements LineAggregator<T> {
public class FormatterLineAggregator<T> extends ExtractorLineAggregator<T> {
private String format;
private FieldExtractor<T> fieldExtractor = new PassThroughFieldExtractor<T>();
private Locale locale = Locale.getDefault();
private int maximumLength = 0;
@@ -69,16 +67,6 @@ public class FormatterLineAggregator<T> implements LineAggregator<T> {
this.format = format;
}
/**
* Public setter for the field extractor responsible for splitting an input
* object up into an array of objects. Defaults to
* {@link PassThroughFieldExtractor}.
*
* @param fieldExtractor the field extractor to set
*/
public void setFieldExtractor(FieldExtractor<T> fieldExtractor) {
this.fieldExtractor = fieldExtractor;
}
/**
* Public setter for the locale.
@@ -89,19 +77,13 @@ public class FormatterLineAggregator<T> implements LineAggregator<T> {
}
/**
* Aggregate provided item into single line using specified format.
*
* @param item data to be aggregated
* @return aggregated string
* @see org.springframework.batch.item.file.transform.ExtractorLineAggregator#doAggregate(java.lang.Object[])
*/
public String aggregate(T item) {
protected String doAggregate(Object[] fields) {
Assert.notNull(item);
Assert.notNull(format);
Object[] args = fieldExtractor.extract(item);
String value = String.format(locale, format, args);
String value = String.format(locale, format, fields);
if (maximumLength > 0) {
Assert.state(value.length() <= maximumLength, String.format("String overflowed in formatter -"