Skip to content

Commit d7ff73b

Browse files
committed
Fix default value of comment prefix in FlatFileItemReaderBuilder
Before this commit, the default value of comment prefix in FlatFileItemReaderBuilder was not consistent with the one in FlatFileItemReader. This commit changes the default value of comment prefix to # in the builder to be consistent with the reader. Resolves BATCH-2862
1 parent a5e7f19 commit d7ff73b

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* about the problematic line and its line number.
4242
*
4343
* @author Robert Kasanicky
44+
* @author Mahmoud Ben Hassine
4445
*/
4546
public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements
4647
ResourceAwareItemReaderItemStream<T>, InitializingBean {
@@ -50,6 +51,8 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
5051
// default encoding for input files
5152
public static final String DEFAULT_CHARSET = Charset.defaultCharset().name();
5253

54+
public static final String[] DEFAULT_COMMENT_PREFIXES = new String[] { "#" };
55+
5356
private RecordSeparatorPolicy recordSeparatorPolicy = new SimpleRecordSeparatorPolicy();
5457

5558
private Resource resource;
@@ -58,7 +61,7 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
5861

5962
private int lineCount = 0;
6063

61-
private String[] comments = new String[] { "#" };
64+
private String[] comments = DEFAULT_COMMENT_PREFIXES;
6265

6366
private boolean noInput = false;
6467

@@ -134,7 +137,7 @@ public void setBufferedReaderFactory(BufferedReaderFactory bufferedReaderFactory
134137

135138
/**
136139
* Setter for comment prefixes. Can be used to ignore header lines as well by using e.g. the first couple of column
137-
* names as a prefix.
140+
* names as a prefix. Defaults to {@link #DEFAULT_COMMENT_PREFIXES}.
138141
*
139142
* @param comments an array of comment line prefixes.
140143
*/

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilder.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public class FlatFileItemReaderBuilder<T> {
7676

7777
private Resource resource;
7878

79-
private List<String> comments = new ArrayList<>();
79+
private List<String> comments =
80+
new ArrayList<>(Arrays.asList(FlatFileItemReader.DEFAULT_COMMENT_PREFIXES));
8081

8182
private int linesToSkip = 0;
8283

@@ -171,6 +172,7 @@ public FlatFileItemReaderBuilder<T> currentItemCount(int currentItemCount) {
171172

172173
/**
173174
* Add a string to the list of Strings that indicate commented lines.
175+
* Defaults to {@link FlatFileItemReader#DEFAULT_COMMENT_PREFIXES}.
174176
*
175177
* @param comment the string to define a commented line.
176178
* @return The current instance of the builder.
@@ -182,15 +184,16 @@ public FlatFileItemReaderBuilder<T> addComment(String comment) {
182184
}
183185

184186
/**
185-
* An array of Strings that indicate lines that are comments (and therefore skipped by
186-
* the reader.
187+
* Set an array of Strings that indicate lines that are comments (and therefore skipped by
188+
* the reader). This method overrides the default comment prefixes which are
189+
* {@link FlatFileItemReader#DEFAULT_COMMENT_PREFIXES}.
187190
*
188191
* @param comments an array of strings to identify comments.
189192
* @return The current instance of the builder.
190193
* @see FlatFileItemReader#setComments(String[])
191194
*/
192195
public FlatFileItemReaderBuilder<T> comments(String... comments) {
193-
this.comments.addAll(Arrays.asList(comments));
196+
this.comments = Arrays.asList(comments);
194197
return this;
195198
}
196199

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public void testEmptyComments() throws Exception {
300300
public void testDefaultComments() throws Exception {
301301
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>()
302302
.name("fooReader")
303-
.resource(getResource("1,2,3\n4,5,6"))
303+
.resource(getResource("1,2,3\n4,5,6\n#this is a default comment"))
304304
.delimited()
305305
.names("first", "second", "third")
306306
.targetType(Foo.class)

0 commit comments

Comments
 (0)