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 java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025import java.util.stream.Stream;
026
027public class CSVLineBuilder {
028        private final List<String> line = new ArrayList<>();
029
030        public CSVLineBuilder add(String cell) {
031                line.add(cell);
032                return this;
033        }
034
035        public CSVLineBuilder addf(String format, Object cell) {
036                line.add(String.format(format, cell));
037                return this;
038        }
039
040        public CSVLineBuilder addEmpty() {
041                line.add(null);
042                return this;
043        }
044
045        public CSVLineBuilder addAll(Stream<String> cells) {
046                cells.forEach(line::add);
047                return this;
048        }
049
050        public CSVLineBuilder addAll(String... cells) {
051                Collections.addAll(line, cells);
052                return this;
053        }
054
055        public CSVLineBuilder addBuilder(CSVLineBuilder builder) {
056                line.addAll(builder.line);
057                return this;
058        }
059
060        public String[] build() {
061                return line.toArray(new String[line.size()]);
062        }
063}