
net.alloyggp.escaperope.CsvLineDelimiter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of escape-rope Show documentation
Show all versions of escape-rope Show documentation
A library for turning structured data into strings and back.
package net.alloyggp.escaperope;
import java.util.ArrayList;
import java.util.List;
//This attempts to comply with a subset of RFC 4180. Note that this does NOT
//mean that undelimit() will work with arbitrary valid CSV outputs
//from sources other than this class.
public class CsvLineDelimiter implements Delimiter {
private final Escaper escaper;
private CsvLineDelimiter(Escaper escaper) {
this.escaper = escaper;
}
public static Delimiter create(NullBehavior behavior) {
return new CsvLineDelimiter(CsvFieldEscaper.create(behavior));
}
@Override
public String delimit(Iterable inputs) {
if (inputs == null) {
throw new NullPointerException();
}
StringBuilder sb = new StringBuilder();
boolean isFirst = true;
for (String input : inputs) {
if (!isFirst) {
sb.append(",");
}
sb.append(escaper.escape(input));
isFirst = false;
}
return sb.toString();
}
@Override
public List undelimit(String input) {
if (input == null) {
throw new NullPointerException();
}
List strings = new ArrayList<>();
for (UnescapeResult result : escaper.unescape(input)) {
if (!result.isControlCharacter()) {
strings.add(result.getNonControlText());
}
}
return strings;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy