@Test public void testWrite() throws IOException { String expected = "\"a\",\"b\",\"c\",\"d.e\",\"f\",\"g\"\r\n" + "\"A,B,\"\"C\"\" \",1,2.0,true,0,3\r\n" + "\" D\r\nÉ\r\nF\r\n\",2,4.0,,,\r\n"; List<Map<String, ?>> values = listOf( mapOf( entry("a", "A,B,\"C\" "), entry("b", 1), entry("c", 2.0), entry("d", mapOf( entry("e", true) )), entry("f", new Date(0)), entry("g", DayOfWeek.THURSDAY) ), mapOf( entry("a", " D\r\nÉ\r\nF\r\n"), entry("b", 2), entry("c", 4.0) ) ); StringWriter writer = new StringWriter(); CSVEncoder csvEncoder = new CSVEncoder(listOf("a", "b", "c", "d.e", "f", "g")); csvEncoder.write(values, writer); assertEquals(expected, writer.toString()); }
@Override public void write(Iterable<? extends Map<String, ?>> values, Writer writer) throws IOException { writer = new BufferedWriter(writer); int i = 0; for (String key : keys) { if (key == null) { continue; } if (i > 0) { writer.write(delimiter); } encode(key, writer); i++; } writer.write("\r\n"); for (Map<String, ?> map : values) { i = 0; for (String key : keys) { if (key == null) { continue; } if (i > 0) { writer.write(delimiter); } encode(BeanAdapter.valueAt(map, key), writer); i++; } writer.write("\r\n"); } writer.flush(); }
CSVEncoder extends Encoder<Iterable<? extends Map<String, ?>>> { @Override public void write(Iterable<? extends Map<String, ?>> values, Writer writer) throws IOException { writer = new BufferedWriter(writer); int i = 0; for (String key : keys) { if (key == null) { continue; } if (i > 0) { writer.write(delimiter); } encode(key, writer); i++; } writer.write("\r\n"); for (Map<String, ?> map : values) { i = 0; for (String key : keys) { if (key == null) { continue; } if (i > 0) { writer.write(delimiter); } encode(BeanAdapter.valueAt(map, key), writer); i++; } writer.write("\r\n"); } writer.flush(); } }
CSVEncoder extends Encoder<Iterable<? extends Map<String, ?>>> { @Override public void write(Iterable<? extends Map<String, ?>> values, Writer writer) throws IOException { writer = new BufferedWriter(writer); int i = 0; for (String key : keys) { if (key == null) { continue; } if (i > 0) { writer.write(delimiter); } encode(key, writer); i++; } writer.write("\r\n"); for (Map<String, ?> map : values) { i = 0; for (String key : keys) { if (key == null) { continue; } if (i > 0) { writer.write(delimiter); } encode(BeanAdapter.valueAt(map, key), writer); i++; } writer.write("\r\n"); } writer.flush(); } CSVEncoder(List<String> keys);  CSVEncoder(List<String> keys, char delimiter); }
CSVEncoder extends Encoder<Iterable<? extends Map<String, ?>>> { @Override public void write(Iterable<? extends Map<String, ?>> values, Writer writer) throws IOException { writer = new BufferedWriter(writer); int i = 0; for (String key : keys) { if (key == null) { continue; } if (i > 0) { writer.write(delimiter); } encode(key, writer); i++; } writer.write("\r\n"); for (Map<String, ?> map : values) { i = 0; for (String key : keys) { if (key == null) { continue; } if (i > 0) { writer.write(delimiter); } encode(BeanAdapter.valueAt(map, key), writer); i++; } writer.write("\r\n"); } writer.flush(); } CSVEncoder(List<String> keys);  CSVEncoder(List<String> keys, char delimiter); @Override void write(Iterable<? extends Map<String, ?>> values, Writer writer); }
CSVEncoder extends Encoder<Iterable<? extends Map<String, ?>>> { @Override public void write(Iterable<? extends Map<String, ?>> values, Writer writer) throws IOException { writer = new BufferedWriter(writer); int i = 0; for (String key : keys) { if (key == null) { continue; } if (i > 0) { writer.write(delimiter); } encode(key, writer); i++; } writer.write("\r\n"); for (Map<String, ?> map : values) { i = 0; for (String key : keys) { if (key == null) { continue; } if (i > 0) { writer.write(delimiter); } encode(BeanAdapter.valueAt(map, key), writer); i++; } writer.write("\r\n"); } writer.flush(); } CSVEncoder(List<String> keys);  CSVEncoder(List<String> keys, char delimiter); @Override void write(Iterable<? extends Map<String, ?>> values, Writer writer);  }