Skip to content

Commit 4d99f43

Browse files
florian-serraillefmbenhassine
authored andcommitted
Add empty collection handling to RecursiveCollectionLineAggregator
Cherry-picked from dbf489e
1 parent 25e33fb commit 4d99f43

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2024 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -56,6 +56,9 @@ public void setLineSeparator(String lineSeparator) {
5656

5757
@Override
5858
public String aggregate(Collection<T> items) {
59+
if (items.isEmpty()) {
60+
return "";
61+
}
5962
StringBuilder builder = new StringBuilder();
6063
for (T value : items) {
6164
builder.append(delegate.aggregate(value)).append(lineSeparator);

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/RecursiveCollectionLineAggregatorTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2024 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -33,6 +33,12 @@ class RecursiveCollectionLineAggregatorTests {
3333

3434
private final RecursiveCollectionLineAggregator<String> aggregator = new RecursiveCollectionLineAggregator<>();
3535

36+
@Test
37+
void testSetDelegateAndPassEmptyCollection() {
38+
aggregator.setDelegate(item -> "bar");
39+
assertEquals("", aggregator.aggregate(Collections.emptyList()));
40+
}
41+
3642
@Test
3743
void testSetDelegateAndPassInString() {
3844
aggregator.setDelegate(item -> "bar");

0 commit comments

Comments
 (0)