java.lang.Object
io.jenetics.ext.util.CsvSupport.ColumnJoiner
- Enclosing class:
CsvSupport
This class joins an array of columns into one CSV line.
Embedding and re-ordering data
Examples
Simple usagefinal var joiner = ColumnJoiner.DEFAULT;
final var csv = Arrays.stream(data)
.map(joiner::join)
.collect(CsvSupport.toCsv());
final var data = new String[][] {
{"Region", "City", "Country"},
{"06", "aixas", "ad"},
{"06", "aixirivali", "ad"},
{"06", "aixirivall", "ad"},
{"06", "aixirvall", "ad"},
{"06", "aixovall", "ad"}
};
final var embedding = new ColumnIndexes(
// Writes 'Region' as fourth column.
3,
// Write 'City' as second column.
1,
// Write 'Country' as third column.
0,
// Since the data rows have only three elements, the
// missing column data are set to an empty string.
// The last written column index will be 6, which
// results to 7 written columns
6
);
final var joiner = new ColumnJoiner(embedding);
final var csv = Arrays.stream(data)
.map(joiner::join)
.collect(CsvSupport.toCsv("\n"));
assert csv.equals("""
Country,City,,Region,,,
ad,aixas,,06,,,
ad,aixirivali,,06,,,
ad,aixirivall,,06,,,
ad,aixirvall,,06,,,
ad,aixovall,,06,,,
"""
);
- Since:
- 8.1
- Version:
- 8.1
- API Note:
- The column joiner is thread-safe and can be shared between different threads.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final CsvSupport.ColumnJoiner
Default column joiner, which is using default separator character,CsvSupport.Separator.DEFAULT
, and default quote character,CsvSupport.Quote.DEFAULT
. -
Constructor Summary
ConstructorDescriptionColumnJoiner
(CsvSupport.ColumnIndexes embedding) Create a new column joiner with the given embedding column indexes.ColumnJoiner
(CsvSupport.Quote quote) Create a new column joiner with the given parameters.ColumnJoiner
(CsvSupport.Quote quote, CsvSupport.ColumnIndexes embedding) Create a new column joiner with the given parameters.ColumnJoiner
(CsvSupport.Separator separator) Create a new column joiner with the given parameters.ColumnJoiner
(CsvSupport.Separator separator, CsvSupport.ColumnIndexes embedding) Create a new column joiner with the given parameters.ColumnJoiner
(CsvSupport.Separator separator, CsvSupport.Quote quote) Create a new column joiner with the given parameters.ColumnJoiner
(CsvSupport.Separator separator, CsvSupport.Quote quote, CsvSupport.ColumnIndexes embedding) Create a new column joiner with the given parameters. -
Method Summary
-
Field Details
-
DEFAULT
Default column joiner, which is using default separator character,CsvSupport.Separator.DEFAULT
, and default quote character,CsvSupport.Quote.DEFAULT
.
-
-
Constructor Details
-
ColumnJoiner
public ColumnJoiner(CsvSupport.Separator separator, CsvSupport.Quote quote, CsvSupport.ColumnIndexes embedding) Create a new column joiner with the given parameters.- Parameters:
separator
- the CSV separator character used by the joinerquote
- the CSV quote character used by the joinerembedding
- the column indexes to join- Throws:
NullPointerException
- if one of the parameters isnull
-
ColumnJoiner
Create a new column joiner with the given parameters.- Parameters:
separator
- the CSV separator character used by the joinerquote
- the CSV quote character used by the joiner- Throws:
NullPointerException
- if one of the parameters isnull
-
ColumnJoiner
Create a new column joiner with the given parameters.- Parameters:
separator
- the CSV separator character used by the joiner- Throws:
NullPointerException
- if one of the parameters isnull
-
ColumnJoiner
Create a new column joiner with the given parameters.- Parameters:
separator
- the CSV separator character used by the joinerembedding
- the column indexes to join- Throws:
NullPointerException
- if one of the parameters isnull
-
ColumnJoiner
Create a new column joiner with the given parameters.- Parameters:
quote
- the CSV quote character used by the joiner- Throws:
NullPointerException
- if one of the parameters isnull
-
ColumnJoiner
Create a new column joiner with the given embedding column indexes.- Parameters:
embedding
- the embedding column indexes
-
ColumnJoiner
Create a new column joiner with the given parameters.- Parameters:
quote
- the CSV quote character used by the joinerembedding
- the column indexes to join- Throws:
NullPointerException
- if one of the parameters isnull
-
-
Method Details
-
join
Joins the given CSVcolumns
, using the given separator and quote character.- Parameters:
columns
- the CSV columns to join- Returns:
- the joined CSV columns
-
join
Joins the given CSVcolumns
, using the given separator and quote character.- Parameters:
columns
- the CSV columns to join- Returns:
- the joined CSV columns
-