Skip to content

Commit 0898276

Browse files
committed
Make line separator configurable in RecursiveCollectionLineAggregator
Resolves #4594
1 parent 67b7f38 commit 0898276

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/RecursiveCollectionLineAggregator.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,16 +18,19 @@
1818

1919
import java.util.Collection;
2020

21+
import org.springframework.util.Assert;
22+
2123
/**
2224
* An implementation of {@link LineAggregator} that concatenates a collection of items of
23-
* a common type with the system line separator.
25+
* a common type with a line separator.
2426
*
2527
* @author Dave Syer
28+
* @author Mahmoud Ben Hassine
2629
*
2730
*/
2831
public class RecursiveCollectionLineAggregator<T> implements LineAggregator<Collection<T>> {
2932

30-
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
33+
private String lineSeparator = System.lineSeparator();
3134

3235
private LineAggregator<T> delegate = new PassThroughLineAggregator<>();
3336

@@ -41,13 +44,23 @@ public void setDelegate(LineAggregator<T> delegate) {
4144
this.delegate = delegate;
4245
}
4346

47+
/**
48+
* Set the line separator to use. Defaults to the System's line separator.
49+
* @param lineSeparator the line separator to use. Must not be {@code null}.
50+
* @since 5.2
51+
*/
52+
public void setLineSeparator(String lineSeparator) {
53+
Assert.notNull(lineSeparator, "The line separator must not be null");
54+
this.lineSeparator = lineSeparator;
55+
}
56+
4457
@Override
4558
public String aggregate(Collection<T> items) {
4659
StringBuilder builder = new StringBuilder();
4760
for (T value : items) {
48-
builder.append(delegate.aggregate(value)).append(LINE_SEPARATOR);
61+
builder.append(delegate.aggregate(value)).append(lineSeparator);
4962
}
50-
return builder.delete(builder.length() - LINE_SEPARATOR.length(), builder.length()).toString();
63+
return builder.delete(builder.length() - lineSeparator.length(), builder.length()).toString();
5164
}
5265

5366
}
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020

2121
import org.junit.jupiter.api.Test;
22+
2223
import org.springframework.util.StringUtils;
2324

2425
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -28,9 +29,7 @@
2829
* @author Mahmoud Ben Hassine
2930
*
3031
*/
31-
class RecursiveCollectionItemTransformerTests {
32-
33-
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
32+
class RecursiveCollectionLineAggregatorTests {
3433

3534
private final RecursiveCollectionLineAggregator<String> aggregator = new RecursiveCollectionLineAggregator<>();
3635

@@ -41,9 +40,18 @@ void testSetDelegateAndPassInString() {
4140
}
4241

4342
@Test
44-
void testTransformList() {
43+
void testAggregateListWithDefaultLineSeparator() {
44+
String result = aggregator.aggregate(Arrays.asList(StringUtils.commaDelimitedListToStringArray("foo,bar")));
45+
String[] array = StringUtils.delimitedListToStringArray(result, System.lineSeparator());
46+
assertEquals("foo", array[0]);
47+
assertEquals("bar", array[1]);
48+
}
49+
50+
@Test
51+
void testAggregateListWithCustomLineSeparator() {
52+
aggregator.setLineSeparator("#");
4553
String result = aggregator.aggregate(Arrays.asList(StringUtils.commaDelimitedListToStringArray("foo,bar")));
46-
String[] array = StringUtils.delimitedListToStringArray(result, LINE_SEPARATOR);
54+
String[] array = StringUtils.delimitedListToStringArray(result, "#");
4755
assertEquals("foo", array[0]);
4856
assertEquals("bar", array[1]);
4957
}

0 commit comments

Comments
 (0)