Skip to content

Commit 1f73aad

Browse files
committed
Add support for column labels to CSVEncoder.
1 parent ea29f3a commit 1f73aad

File tree

5 files changed

+86
-22
lines changed

5 files changed

+86
-22
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
subprojects {
1616
group = 'org.httprpc'
17-
version = '7.2.3'
17+
version = '7.3'
1818

1919
apply plugin: 'java-library'
2020
apply plugin: 'maven-publish'

httprpc-client/src/main/java/org/httprpc/io/CSVEncoder.java

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,90 @@
2727
* CSV encoder.
2828
*/
2929
public class CSVEncoder extends Encoder<Iterable<? extends Map<String, ?>>> {
30-
private List<String> keys;
30+
/**
31+
* Class representing a column.
32+
*/
33+
public static final class Column {
34+
private String key;
35+
private String label;
36+
37+
/**
38+
* Constructs a new column.
39+
*
40+
* @param key
41+
* The column key.
42+
*/
43+
public Column(String key) {
44+
this(key, key);
45+
}
46+
47+
/**
48+
* Constructs a new column.
49+
*
50+
* @param key
51+
* The column key.
52+
*
53+
* @param label
54+
* The column label.
55+
*/
56+
public Column(String key, String label) {
57+
this.key = key;
58+
this.label = label;
59+
}
60+
61+
/**
62+
* Returns the column key.
63+
*
64+
* @return
65+
* The column key.
66+
*/
67+
public String getKey() {
68+
return key;
69+
}
70+
71+
/**
72+
* Returns the column label.
73+
*
74+
* @return
75+
* The column label.
76+
*/
77+
public String getLabel() {
78+
return label;
79+
}
80+
}
81+
82+
private List<Column> columns;
3183
private char delimiter;
3284

85+
private static final char DEFAULT_DELIMITER = ',';
86+
3387
/**
3488
* Constructs a new CSV encoder.
3589
*
36-
* @param keys
37-
* The output column keys.
90+
* @param columns
91+
* The output columns.
3892
*/
39-
public CSVEncoder(List<String> keys) {
40-
this(keys, ',');
93+
public CSVEncoder(List<Column> columns) {
94+
this(columns, DEFAULT_DELIMITER);
4195
}
4296

4397
/**
4498
* Constructs a new CSV encoder.
4599
*
46-
* @param keys
47-
* The output column keys.
100+
* @param columns
101+
* The output columns.
48102
*
49103
* @param delimiter
50104
* The character to use as a field delimiter.
51105
*/
52-
public CSVEncoder(List<String> keys, char delimiter) {
106+
public CSVEncoder(List<Column> columns, char delimiter) {
53107
super(StandardCharsets.ISO_8859_1);
54108

55-
if (keys == null) {
109+
if (columns == null) {
56110
throw new IllegalArgumentException();
57111
}
58112

59-
this.keys = keys;
113+
this.columns = columns;
60114
this.delimiter = delimiter;
61115
}
62116

@@ -66,16 +120,16 @@ public void write(Iterable<? extends Map<String, ?>> values, Writer writer) thro
66120

67121
int i = 0;
68122

69-
for (String key : keys) {
70-
if (key == null) {
123+
for (Column column : columns) {
124+
if (column == null) {
71125
continue;
72126
}
73127

74128
if (i > 0) {
75129
writer.write(delimiter);
76130
}
77131

78-
encode(key, writer);
132+
encode(column.getLabel(), writer);
79133

80134
i++;
81135
}
@@ -85,16 +139,16 @@ public void write(Iterable<? extends Map<String, ?>> values, Writer writer) thro
85139
for (Map<String, ?> map : values) {
86140
i = 0;
87141

88-
for (String key : keys) {
89-
if (key == null) {
142+
for (Column column : columns) {
143+
if (column == null) {
90144
continue;
91145
}
92146

93147
if (i > 0) {
94148
writer.write(delimiter);
95149
}
96150

97-
encode(BeanAdapter.valueAt(map, key), writer);
151+
encode(BeanAdapter.valueAt(map, column.getKey()), writer);
98152

99153
i++;
100154
}

httprpc-client/src/test/java/org/httprpc/io/CSVEncoderTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Date;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.stream.Collectors;
2526

2627
import static org.httprpc.util.Collections.entry;
2728
import static org.httprpc.util.Collections.listOf;
@@ -31,7 +32,7 @@
3132
public class CSVEncoderTest {
3233
@Test
3334
public void testWrite() throws IOException {
34-
String expected = "\"a\",\"b\",\"c\",\"d.e\",\"f\",\"g\"\r\n"
35+
String expected = "\"1\",\"2\",\"3\",\"d.e\",\"f\",\"g\"\r\n"
3536
+ "\"A,B,\"\"C\"\" \",1,2.0,true,0,3\r\n"
3637
+ "\" D\r\nÉ\r\nF\r\n\",2,4.0,,,\r\n";
3738

@@ -55,7 +56,16 @@ public void testWrite() throws IOException {
5556

5657
StringWriter writer = new StringWriter();
5758

58-
CSVEncoder csvEncoder = new CSVEncoder(listOf("a", "b", "c", "d.e", "f", "g"));
59+
List<CSVEncoder.Column> columns = listOf(
60+
new CSVEncoder.Column("a", "1"),
61+
new CSVEncoder.Column("b", "2"),
62+
new CSVEncoder.Column("c", "3"),
63+
new CSVEncoder.Column("d.e"),
64+
new CSVEncoder.Column("f"),
65+
new CSVEncoder.Column("g")
66+
);
67+
68+
CSVEncoder csvEncoder = new CSVEncoder(columns);
5969

6070
csvEncoder.write(values, writer);
6171

httprpc-server/src/main/java/org/httprpc/io/TemplateEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public Object apply(Object value, String argument, Locale locale, TimeZone timeZ
279279
}
280280

281281
// CSV escape modifier
282-
private static class CSVEscapeModifier implements TemplateEncoder.Modifier {
282+
private static class CSVEscapeModifier implements Modifier {
283283
@Override
284284
public Object apply(Object value, String argument, Locale locale, TimeZone timeZone) {
285285
if (value instanceof CharSequence) {

httprpc-test/src/test/java/org/httprpc/WebServiceProxyTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public void testCustomPost() throws IOException {
237237

238238
URL imageTestURL = WebServiceProxyTest.class.getResource("test.jpg");
239239

240-
webServiceProxy.setRequestHandler((outputStream) -> {
240+
webServiceProxy.setRequestHandler(outputStream -> {
241241
try (InputStream inputStream = imageTestURL.openStream()) {
242242
int b;
243243
while ((b = inputStream.read()) != EOF) {
@@ -261,7 +261,7 @@ public void testPut() throws IOException {
261261

262262
URL textTestURL = WebServiceProxyTest.class.getResource("test.txt");
263263

264-
webServiceProxy.setRequestHandler((outputStream) -> {
264+
webServiceProxy.setRequestHandler(outputStream -> {
265265
try (InputStream inputStream = textTestURL.openStream()) {
266266
int b;
267267
while ((b = inputStream.read()) != EOF) {

0 commit comments

Comments
 (0)