001/* *********************************************************************** *
002 * project: org.matsim.*
003 *                                                                         *
004 * *********************************************************************** *
005 *                                                                         *
006 * copyright       : (C) 2016 by the members listed in the COPYING,        *
007 *                   LICENSE and WARRANTY file.                            *
008 * email           : info at matsim dot org                                *
009 *                                                                         *
010 * *********************************************************************** *
011 *                                                                         *
012 *   This program is free software; you can redistribute it and/or modify  *
013 *   it under the terms of the GNU General Public License as published by  *
014 *   the Free Software Foundation; either version 2 of the License, or     *
015 *   (at your option) any later version.                                   *
016 *   See also COPYING, LICENSE and WARRANTY file                           *
017 *                                                                         *
018 * *********************************************************************** */
019
020package org.matsim.contrib.util;
021
022import com.opencsv.CSVWriter;
023import org.matsim.core.utils.io.UncheckedIOException;
024
025import java.io.IOException;
026import java.io.Writer;
027
028public class CompactCSVWriter extends CSVWriter {
029        public static final String[] EMPTY_LINE = {};
030        public static final String EMPTY_CELL = null;
031
032        public CompactCSVWriter(Writer writer) {
033                this(writer, '\t');
034        }
035
036        public CompactCSVWriter(Writer writer, char separator) {
037                super(writer, separator, CSVWriter.NO_QUOTE_CHARACTER, '"', "\n");
038        }
039
040        @Override
041        public void writeNext(String... nextLine) {
042                super.writeNext(nextLine);
043        }
044
045        public void writeNext(String first, String[] tail) {
046                writeHeadAndTail(tail, first);
047        }
048
049        public void writeNext(String first, String second, String[] tail) {
050                writeHeadAndTail(tail, first, second);
051        }
052
053        public void writeNext(String first, String second, String third, String[] tail) {
054                writeHeadAndTail(tail, first, second, third);
055        }
056
057        public void writeNext(String first, String second, String third, String fourth, String[] tail) {
058                writeHeadAndTail(tail, first, second, third, fourth);
059        }
060
061        private void writeHeadAndTail(String[] tail, String... head) {
062                String[] nextLine = new String[head.length + tail.length];
063                System.arraycopy(head, 0, nextLine, 0, head.length);
064                System.arraycopy(tail, 0, nextLine, head.length, tail.length);
065                writeNext(nextLine);
066        }
067
068        public void writeNextEmpty() {
069                writeNext(EMPTY_LINE);
070        }
071
072        public void writeNext(CSVLineBuilder lineBuilder) {
073                writeNext(lineBuilder.build());
074        }
075
076        @Override
077        public void flush() {
078                try {
079                        super.flush();
080                } catch (IOException e) {
081                        throw new UncheckedIOException(e);
082                }
083        }
084
085        @Override
086        public void close() {
087                try {
088                        super.close();
089                } catch (IOException e) {
090                        throw new UncheckedIOException(e);
091                }
092        }
093}