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 | |
17 | package org.springframework.batch.item.file.transform; |
18 | |
19 | import java.util.Collection; |
20 | |
21 | |
22 | /** |
23 | * An implementation of {@link LineAggregator} that concatenates a collection of |
24 | * items of a common type with the system line separator. |
25 | * |
26 | * @author Dave Syer |
27 | * |
28 | */ |
29 | public class RecursiveCollectionLineAggregator<T> implements LineAggregator<Collection<T>> { |
30 | |
31 | private static final String LINE_SEPARATOR = System.getProperty("line.separator"); |
32 | |
33 | private LineAggregator<T> delegate = new PassThroughLineAggregator<T>(); |
34 | |
35 | /** |
36 | * Public setter for the {@link LineAggregator} to use on single items, that |
37 | * are not Strings. This can be used to strategise the conversion of |
38 | * collection and array elements to a String.<br/> |
39 | * |
40 | * @param delegate the line aggregator to set. Defaults to a pass through. |
41 | */ |
42 | public void setDelegate(LineAggregator<T> delegate) { |
43 | this.delegate = delegate; |
44 | } |
45 | |
46 | /* |
47 | * (non-Javadoc) |
48 | * @see org.springframework.batch.item.file.transform.LineAggregator#aggregate(java.lang.Object) |
49 | */ |
50 | public String aggregate(Collection<T> items) { |
51 | StringBuilder builder = new StringBuilder(); |
52 | for (T value : items) { |
53 | builder.append(delegate.aggregate(value) + LINE_SEPARATOR); |
54 | } |
55 | return builder.delete(builder.length()-LINE_SEPARATOR.length(),builder.length()).toString(); |
56 | } |
57 | |
58 | } |