001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.item.file.transform;
017
018import org.springframework.util.Assert;
019
020/**
021 * An abstract {@link LineAggregator} implementation that utilizes a
022 * {@link FieldExtractor} to convert the incoming object to an array of its
023 * parts. Extending classes must decide how those parts will be aggregated
024 * together.
025 * 
026 * @author Dan Garrette
027 * @since 2.0
028 */
029public abstract class ExtractorLineAggregator<T> implements LineAggregator<T> {
030
031        private FieldExtractor<T> fieldExtractor = new PassThroughFieldExtractor<T>();
032
033        /**
034         * Public setter for the field extractor responsible for splitting an input
035         * object up into an array of objects. Defaults to
036         * {@link PassThroughFieldExtractor}.
037         * 
038         * @param fieldExtractor The field extractor to set
039         */
040        public void setFieldExtractor(FieldExtractor<T> fieldExtractor) {
041                this.fieldExtractor = fieldExtractor;
042        }
043
044        /**
045         * Extract fields from the given item using the {@link FieldExtractor} and
046         * then aggregate them. Any null field returned by the extractor will be
047         * replaced by an empty String. Null items are not allowed.
048         * 
049         * @see org.springframework.batch.item.file.transform.LineAggregator#aggregate(java.lang.Object)
050         */
051    @Override
052        public String aggregate(T item) {
053                Assert.notNull(item, "Item is required");
054                Object[] fields = this.fieldExtractor.extract(item);
055
056                //
057                // Replace nulls with empty strings
058                //
059                Object[] args = new Object[fields.length];
060                for (int i = 0; i < fields.length; i++) {
061                        if (fields[i] == null) {
062                                args[i] = "";
063                        }
064                        else {
065                                args[i] = fields[i];
066                        }
067                }
068
069                return this.doAggregate(args);
070        }
071
072        /**
073         * Aggregate provided fields into single String.
074         * 
075         * @param fields An array of the fields that must be aggregated
076         * @return aggregated string
077         */
078        protected abstract String doAggregate(Object[] fields);
079}