Skip to content

Commit

Permalink
Fix default value of comment prefix in FlatFileItemReaderBuilder
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fmbenhassine committed Dec 2, 2019
1 parent a5e7f19 commit d7ff73b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* about the problematic line and its line number.
*
* @author Robert Kasanicky
* @author Mahmoud Ben Hassine
*/
public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements
ResourceAwareItemReaderItemStream<T>, InitializingBean {
Expand All @@ -50,6 +51,8 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
// default encoding for input files
public static final String DEFAULT_CHARSET = Charset.defaultCharset().name();

public static final String[] DEFAULT_COMMENT_PREFIXES = new String[] { "#" };

private RecordSeparatorPolicy recordSeparatorPolicy = new SimpleRecordSeparatorPolicy();

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

private int lineCount = 0;

private String[] comments = new String[] { "#" };
private String[] comments = DEFAULT_COMMENT_PREFIXES;

private boolean noInput = false;

Expand Down Expand Up @@ -134,7 +137,7 @@ public void setBufferedReaderFactory(BufferedReaderFactory bufferedReaderFactory

/**
* Setter for comment prefixes. Can be used to ignore header lines as well by using e.g. the first couple of column
* names as a prefix.
* names as a prefix. Defaults to {@link #DEFAULT_COMMENT_PREFIXES}.
*
* @param comments an array of comment line prefixes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public class FlatFileItemReaderBuilder<T> {

private Resource resource;

private List<String> comments = new ArrayList<>();
private List<String> comments =
new ArrayList<>(Arrays.asList(FlatFileItemReader.DEFAULT_COMMENT_PREFIXES));

private int linesToSkip = 0;

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

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

/**
* An array of Strings that indicate lines that are comments (and therefore skipped by
* the reader.
* Set an array of Strings that indicate lines that are comments (and therefore skipped by
* the reader). This method overrides the default comment prefixes which are
* {@link FlatFileItemReader#DEFAULT_COMMENT_PREFIXES}.
*
* @param comments an array of strings to identify comments.
* @return The current instance of the builder.
* @see FlatFileItemReader#setComments(String[])
*/
public FlatFileItemReaderBuilder<T> comments(String... comments) {
this.comments.addAll(Arrays.asList(comments));
this.comments = Arrays.asList(comments);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public void testEmptyComments() throws Exception {
public void testDefaultComments() throws Exception {
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>()
.name("fooReader")
.resource(getResource("1,2,3\n4,5,6"))
.resource(getResource("1,2,3\n4,5,6\n#this is a default comment"))
.delimited()
.names("first", "second", "third")
.targetType(Foo.class)
Expand Down

0 comments on commit d7ff73b

Please sign in to comment.