diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/BeanDelimitingLineAggregator.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/BeanDelimitingLineAggregator.java deleted file mode 100644 index 814e07d0a..000000000 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/BeanDelimitingLineAggregator.java +++ /dev/null @@ -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 implements LineAggregator, InitializingBean { - - private BeanWrapperFieldExtractor extractor; - - private DelimitedLineAggregator delimitedLineAggregator = new DelimitedLineAggregator(); - - /** - * @param names names of properties of the aggregated item that will be - * included in the resulting string. Must not be null. - */ - public void setNames(String[] names) { - extractor = new BeanWrapperFieldExtractor(); - 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."); - } -} diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/DelimitedLineAggregator.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/DelimitedLineAggregator.java index d7543f374..05a815962 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/DelimitedLineAggregator.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/DelimitedLineAggregator.java @@ -21,7 +21,7 @@ import org.springframework.util.StringUtils; * @author Dave Syer * */ -public class DelimitedLineAggregator implements LineAggregator { +public class DelimitedLineAggregator extends ExtractorLineAggregator { private String delimiter = ","; @@ -33,11 +33,11 @@ public class DelimitedLineAggregator implements LineAggregator { 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); } } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/ExtractorLineAggregator.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/ExtractorLineAggregator.java new file mode 100644 index 000000000..caa191ffe --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/ExtractorLineAggregator.java @@ -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 implements LineAggregator { + + private FieldExtractor fieldExtractor = new PassThroughFieldExtractor(); + + /** + * 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 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); +} diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FormatterLineAggregator.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FormatterLineAggregator.java index 8e9a7657e..32bbf0df3 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FormatterLineAggregator.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FormatterLineAggregator.java @@ -29,12 +29,10 @@ import org.springframework.util.Assert; * * @author Dave Syer */ -public class FormatterLineAggregator implements LineAggregator { +public class FormatterLineAggregator extends ExtractorLineAggregator { private String format; - private FieldExtractor fieldExtractor = new PassThroughFieldExtractor(); - private Locale locale = Locale.getDefault(); private int maximumLength = 0; @@ -69,16 +67,6 @@ public class FormatterLineAggregator implements LineAggregator { 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 fieldExtractor) { - this.fieldExtractor = fieldExtractor; - } /** * Public setter for the locale. @@ -89,19 +77,13 @@ public class FormatterLineAggregator implements LineAggregator { } /** - * 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 -" diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/BeanDelimitingLineAggregatorTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/BeanDelimitingLineAggregatorTests.java deleted file mode 100644 index 6448cf8fd..000000000 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/BeanDelimitingLineAggregatorTests.java +++ /dev/null @@ -1,55 +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 static junit.framework.Assert.assertEquals; - -import org.junit.Test; - -/** - * @author Dan Garrette - * @since 2.0 - */ -public class BeanDelimitingLineAggregatorTests { - - private BeanDelimitingLineAggregator aggregator = new BeanDelimitingLineAggregator(); - - @Test - public void testAggregate() throws Exception { - aggregator.setNames(new String[] { "first", "last", "born" }); - aggregator.afterPropertiesSet(); - - String first = "Alan"; - String last = "Turing"; - int born = 1912; - - Name n = new Name(first, last, born); - String value = aggregator.aggregate(n); - - assertEquals("Alan,Turing,1912", value); - } - - @Test(expected = IllegalArgumentException.class) - public void testNamesMustBeSet() throws Exception { - aggregator.afterPropertiesSet(); - } - - @Test(expected = IllegalArgumentException.class) - public void testNamesMustNotBeNull() throws Exception { - aggregator.setNames(null); - } -} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/DelimitedLineAggregatorTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/DelimitedLineAggregatorTests.java index 7c10a3445..e14a86fa4 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/DelimitedLineAggregatorTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/DelimitedLineAggregatorTests.java @@ -25,7 +25,7 @@ import org.junit.Test; */ public class DelimitedLineAggregatorTests { - private DelimitedLineAggregator aggregator = new DelimitedLineAggregator(); + private DelimitedLineAggregator aggregator = new DelimitedLineAggregator(); @Test public void testSetDelimiter() { diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/order/FlatFileOrderAggregatorTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/order/FlatFileOrderAggregatorTests.java index 92152ef02..a91708ca3 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/order/FlatFileOrderAggregatorTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/order/FlatFileOrderAggregatorTests.java @@ -50,7 +50,7 @@ public class FlatFileOrderAggregatorTests { order.setTotalPrice(BigDecimal.valueOf(0)); // create aggregator stub - LineAggregator aggregator = new DelimitedLineAggregator(); + LineAggregator aggregator = new DelimitedLineAggregator(); // create map of aggregators and set it to writer Map> aggregators = new HashMap>();