Skip to content

Commit

Permalink
Make line separator configurable in RecursiveCollectionLineAggregator
Browse files Browse the repository at this point in the history
Resolves #4594
  • Loading branch information
fmbenhassine committed May 21, 2024
1 parent 67b7f38 commit 0898276
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,16 +18,19 @@

import java.util.Collection;

import org.springframework.util.Assert;

/**
* An implementation of {@link LineAggregator} that concatenates a collection of items of
* a common type with the system line separator.
* a common type with a line separator.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class RecursiveCollectionLineAggregator<T> implements LineAggregator<Collection<T>> {

private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private String lineSeparator = System.lineSeparator();

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

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

/**
* Set the line separator to use. Defaults to the System's line separator.
* @param lineSeparator the line separator to use. Must not be {@code null}.
* @since 5.2
*/
public void setLineSeparator(String lineSeparator) {
Assert.notNull(lineSeparator, "The line separator must not be null");
this.lineSeparator = lineSeparator;
}

@Override
public String aggregate(Collection<T> items) {
StringBuilder builder = new StringBuilder();
for (T value : items) {
builder.append(delegate.aggregate(value)).append(LINE_SEPARATOR);
builder.append(delegate.aggregate(value)).append(lineSeparator);
}
return builder.delete(builder.length() - LINE_SEPARATOR.length(), builder.length()).toString();
return builder.delete(builder.length() - lineSeparator.length(), builder.length()).toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.util.Collections;

import org.junit.jupiter.api.Test;

import org.springframework.util.StringUtils;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -28,9 +29,7 @@
* @author Mahmoud Ben Hassine
*
*/
class RecursiveCollectionItemTransformerTests {

private static final String LINE_SEPARATOR = System.getProperty("line.separator");
class RecursiveCollectionLineAggregatorTests {

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

Expand All @@ -41,9 +40,18 @@ void testSetDelegateAndPassInString() {
}

@Test
void testTransformList() {
void testAggregateListWithDefaultLineSeparator() {
String result = aggregator.aggregate(Arrays.asList(StringUtils.commaDelimitedListToStringArray("foo,bar")));
String[] array = StringUtils.delimitedListToStringArray(result, System.lineSeparator());
assertEquals("foo", array[0]);
assertEquals("bar", array[1]);
}

@Test
void testAggregateListWithCustomLineSeparator() {
aggregator.setLineSeparator("#");
String result = aggregator.aggregate(Arrays.asList(StringUtils.commaDelimitedListToStringArray("foo,bar")));
String[] array = StringUtils.delimitedListToStringArray(result, LINE_SEPARATOR);
String[] array = StringUtils.delimitedListToStringArray(result, "#");
assertEquals("foo", array[0]);
assertEquals("bar", array[1]);
}
Expand Down

0 comments on commit 0898276

Please sign in to comment.