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 */ 016 017package org.springframework.batch.item.file.transform; 018 019import java.util.Collection; 020 021 022/** 023 * An implementation of {@link LineAggregator} that concatenates a collection of 024 * items of a common type with the system line separator. 025 * 026 * @author Dave Syer 027 * 028 */ 029public class RecursiveCollectionLineAggregator<T> implements LineAggregator<Collection<T>> { 030 031 private static final String LINE_SEPARATOR = System.getProperty("line.separator"); 032 033 private LineAggregator<T> delegate = new PassThroughLineAggregator<>(); 034 035 /** 036 * Public setter for the {@link LineAggregator} to use on single items, that 037 * are not Strings. This can be used to strategise the conversion of 038 * collection and array elements to a String.<br> 039 * 040 * @param delegate the line aggregator to set. Defaults to a pass through. 041 */ 042 public void setDelegate(LineAggregator<T> delegate) { 043 this.delegate = delegate; 044 } 045 046 /* 047 * (non-Javadoc) 048 * @see org.springframework.batch.item.file.transform.LineAggregator#aggregate(java.lang.Object) 049 */ 050 @Override 051 public String aggregate(Collection<T> items) { 052 StringBuilder builder = new StringBuilder(); 053 for (T value : items) { 054 builder.append(delegate.aggregate(value)).append(LINE_SEPARATOR); 055 } 056 return builder.delete(builder.length() - LINE_SEPARATOR.length(), builder.length()).toString(); 057 } 058 059}