comparison flys-artifacts/src/main/java/de/intevation/flys/exports/StepCSVWriter.java @ 3468:f37e7e8907cb

merged flys-artifacts/2.8.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:39 +0200
parents 475dd07c2cb1
children
comparison
equal deleted inserted replaced
3387:5ffad8bde8ad 3468:f37e7e8907cb
1 package de.intevation.flys.exports;
2
3 import au.com.bytecode.opencsv.CSVWriter;
4
5 import java.util.ArrayList;
6 import java.util.Arrays;
7
8 /**
9 * Class to overcome shortcoming of CSVWriter to accept String-Arrays only.
10 * The StepCSVWriter buffers incoming values, such that rows in a csv can be
11 * created more dynamically. Do not forget to call flush().
12 */
13 public class StepCSVWriter {
14
15 /** Writer to use when calling flush. */
16 CSVWriter writer = null;
17 /** Buffer of strings (values). */
18 ArrayList<String> buffer;
19
20
21 /** Trivial constructor. */
22 public StepCSVWriter() {
23 buffer = new ArrayList<String>();
24 }
25
26
27 /** Set writer. */
28 public void setCSVWriter(CSVWriter writer) {
29 this.writer = writer;
30 }
31
32
33 /** Add a value to next flush. */
34 public void addNext(String value) {
35 buffer.add(value);
36 }
37
38
39 /** Add many values to next flush. */
40 public void addNexts(String ... values) {
41 buffer.addAll(Arrays.asList(values));
42 }
43
44
45 /** Write the row with csvwriter. */
46 public void flush() {
47 writer.writeNext(buffer.toArray(new String[buffer.size()]));
48 buffer.clear();
49 }
50 }
51 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org