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
This commit is contained in:
Mahmoud Ben Hassine
2019-11-28 15:08:34 +01:00
parent 152e06ad7a
commit 87f040e234
3 changed files with 15 additions and 9 deletions

View File

@@ -40,6 +40,7 @@ import org.springframework.util.StringUtils;
* 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 {
@@ -49,6 +50,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;
@@ -57,7 +60,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;
@@ -133,7 +136,7 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
/**
* 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.
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 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.
@@ -70,7 +70,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;
@@ -165,6 +166,7 @@ public class FlatFileItemReaderBuilder<T> {
/**
* 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.
@@ -176,15 +178,16 @@ public class FlatFileItemReaderBuilder<T> {
}
/**
* 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));
public FlatFileItemReaderBuilder<T> comments(String... comments) {
this.comments = Arrays.asList(comments);
return this;
}

View File

@@ -295,7 +295,7 @@ public class FlatFileItemReaderBuilderTests {
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(new String[] {"first", "second", "third"})
.targetType(Foo.class)