teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.exports; felix@2575: felix@2575: import au.com.bytecode.opencsv.CSVWriter; felix@2575: felix@2575: import java.util.ArrayList; felix@2575: import java.util.Arrays; felix@2575: felix@2575: /** felix@2575: * Class to overcome shortcoming of CSVWriter to accept String-Arrays only. felix@2575: * The StepCSVWriter buffers incoming values, such that rows in a csv can be felix@2575: * created more dynamically. Do not forget to call flush(). felix@2575: */ felix@2575: public class StepCSVWriter { felix@2575: felix@2575: /** Writer to use when calling flush. */ felix@2575: CSVWriter writer = null; felix@2575: /** Buffer of strings (values). */ felix@2575: ArrayList buffer; felix@2575: felix@2575: felix@2575: /** Trivial constructor. */ felix@2575: public StepCSVWriter() { felix@2575: buffer = new ArrayList(); felix@2575: } felix@2575: felix@2575: felix@2575: /** Set writer. */ felix@2575: public void setCSVWriter(CSVWriter writer) { felix@2575: this.writer = writer; felix@2575: } felix@2575: felix@2575: felix@2575: /** Add a value to next flush. */ felix@2575: public void addNext(String value) { felix@2575: buffer.add(value); felix@2575: } felix@2575: felix@2575: felix@2575: /** Add many values to next flush. */ felix@2575: public void addNexts(String ... values) { felix@2575: buffer.addAll(Arrays.asList(values)); felix@2575: } felix@2575: felix@2575: felix@2575: /** Write the row with csvwriter. */ felix@2575: public void flush() { felix@2575: writer.writeNext(buffer.toArray(new String[buffer.size()])); felix@2575: buffer.clear(); felix@2575: } felix@2575: } felix@2575: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :