Skip to content

Commit 87f040e

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 152e06a commit 87f040e

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
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
@@ -40,6 +40,7 @@
4040
* about the problematic line and its line number.
4141
*
4242
* @author Robert Kasanicky
43+
* @author Mahmoud Ben Hassine
4344
*/
4445
public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements
4546
ResourceAwareItemReaderItemStream<T>, InitializingBean {
@@ -49,6 +50,8 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
4950
// default encoding for input files
5051
public static final String DEFAULT_CHARSET = Charset.defaultCharset().name();
5152

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

5457
private Resource resource;
@@ -57,7 +60,7 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
5760

5861
private int lineCount = 0;
5962

60-
private String[] comments = new String[] { "#" };
63+
private String[] comments = DEFAULT_COMMENT_PREFIXES;
6164

6265
private boolean noInput = false;
6366

@@ -133,7 +136,7 @@ public void setBufferedReaderFactory(BufferedReaderFactory bufferedReaderFactory
133136

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

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -70,7 +70,8 @@ public class FlatFileItemReaderBuilder<T> {
7070

7171
private Resource resource;
7272

73-
private List<String> comments = new ArrayList<>();
73+
private List<String> comments =
74+
new ArrayList<>(Arrays.asList(FlatFileItemReader.DEFAULT_COMMENT_PREFIXES));
7475

7576
private int linesToSkip = 0;
7677

@@ -165,6 +166,7 @@ public FlatFileItemReaderBuilder<T> currentItemCount(int currentItemCount) {
165166

166167
/**
167168
* Add a string to the list of Strings that indicate commented lines.
169+
* Defaults to {@link FlatFileItemReader#DEFAULT_COMMENT_PREFIXES}.
168170
*
169171
* @param comment the string to define a commented line.
170172
* @return The current instance of the builder.
@@ -176,15 +178,16 @@ public FlatFileItemReaderBuilder<T> addComment(String comment) {
176178
}
177179

178180
/**
179-
* An array of Strings that indicate lines that are comments (and therefore skipped by
180-
* the reader.
181+
* Set an array of Strings that indicate lines that are comments (and therefore skipped by
182+
* the reader). This method overrides the default comment prefixes which are
183+
* {@link FlatFileItemReader#DEFAULT_COMMENT_PREFIXES}.
181184
*
182185
* @param comments an array of strings to identify comments.
183186
* @return The current instance of the builder.
184187
* @see FlatFileItemReader#setComments(String[])
185188
*/
186-
public FlatFileItemReaderBuilder<T> comments(String[] comments) {
187-
this.comments.addAll(Arrays.asList(comments));
189+
public FlatFileItemReaderBuilder<T> comments(String... comments) {
190+
this.comments = Arrays.asList(comments);
188191
return this;
189192
}
190193

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
@@ -295,7 +295,7 @@ public void testEmptyComments() throws Exception {
295295
public void testDefaultComments() throws Exception {
296296
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>()
297297
.name("fooReader")
298-
.resource(getResource("1,2,3\n4,5,6"))
298+
.resource(getResource("1,2,3\n4,5,6\n#this is a default comment"))
299299
.delimited()
300300
.names(new String[] {"first", "second", "third"})
301301
.targetType(Foo.class)

0 commit comments

Comments
 (0)