| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.springframework.batch.item.file.transform; |
| 17 | |
| 18 | import org.springframework.util.Assert; |
| 19 | |
| 20 | /** |
| 21 | * An abstract {@link LineAggregator} implementation that utilizes a |
| 22 | * {@link FieldExtractor} to convert the incoming object to an array of its |
| 23 | * parts. Extending classes must decide how those parts will be aggregated |
| 24 | * together. |
| 25 | * |
| 26 | * @author Dan Garrette |
| 27 | * @since 2.0 |
| 28 | */ |
| 29 | public abstract class ExtractorLineAggregator<T> implements LineAggregator<T> { |
| 30 | |
| 31 | private FieldExtractor<T> fieldExtractor = new PassThroughFieldExtractor<T>(); |
| 32 | |
| 33 | /** |
| 34 | * Public setter for the field extractor responsible for splitting an input |
| 35 | * object up into an array of objects. Defaults to |
| 36 | * {@link PassThroughFieldExtractor}. |
| 37 | * |
| 38 | * @param fieldExtractor The field extractor to set |
| 39 | */ |
| 40 | public void setFieldExtractor(FieldExtractor<T> fieldExtractor) { |
| 41 | this.fieldExtractor = fieldExtractor; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Extract fields from the given item using the {@link FieldExtractor} and |
| 46 | * then aggregate them. Any null field returned by the extractor will be |
| 47 | * replaced by an empty String. Null items are not allowed. |
| 48 | * |
| 49 | * @see org.springframework.batch.item.file.transform.LineAggregator#aggregate(java.lang.Object) |
| 50 | */ |
| 51 | @Override |
| 52 | public String aggregate(T item) { |
| 53 | Assert.notNull(item); |
| 54 | Object[] fields = this.fieldExtractor.extract(item); |
| 55 | |
| 56 | // |
| 57 | // Replace nulls with empty strings |
| 58 | // |
| 59 | Object[] args = new Object[fields.length]; |
| 60 | for (int i = 0; i < fields.length; i++) { |
| 61 | if (fields[i] == null) { |
| 62 | args[i] = ""; |
| 63 | } |
| 64 | else { |
| 65 | args[i] = fields[i]; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return this.doAggregate(args); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Aggregate provided fields into single String. |
| 74 | * |
| 75 | * @param fields An array of the fields that must be aggregated |
| 76 | * @return aggregated string |
| 77 | */ |
| 78 | protected abstract String doAggregate(Object[] fields); |
| 79 | } |