Created
September 1, 2014 06:23
-
-
Save mlesikov/78e8eed0e05652f81dac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Mihail Lesikov ([email protected]) | |
*/ | |
public class CsvBuilder { | |
private final StringBuilder sb; | |
private String separator = ","; | |
private String end = "\n"; | |
private static final String NONE = "N/A"; | |
private static final String ZERO = "0"; | |
public static class RowBuilder { | |
private final StringBuilder sb; | |
private String separator = ","; | |
private String end = "\n"; | |
private RowBuilder(StringBuilder sb) { | |
this.sb = sb; | |
} | |
public static RowBuilder rowBuilder() { | |
return new RowBuilder(new StringBuilder()); | |
} | |
public RowBuilder string(String value) { | |
if (Strings.isNullOrEmpty(value)) { | |
addValue(NONE); | |
} else { | |
addValue(value); | |
} | |
return this; | |
} | |
public RowBuilder integer(Integer value) { | |
if (value == null) { | |
addValue(ZERO); | |
} else { | |
addValue(value.toString()); | |
} | |
return this; | |
} | |
public RowBuilder longValue(Long value) { | |
if (value == null) { | |
addValue(ZERO); | |
} else { | |
addValue(value.toString()); | |
} | |
return this; | |
} | |
public RowBuilder doubleValue(Double value) { | |
if (value == null) { | |
value = 0d; | |
} | |
addValue(value.toString()); | |
return this; | |
} | |
public RowBuilder onlyDate(Date value) { | |
if (value == null) { | |
addValue(NONE); | |
} else { | |
addValue(new SimpleDateFormat("yyyy-MM-dd").format(value)); | |
} | |
return this; | |
} | |
public RowBuilder onlyDate(DateTime value) { | |
if (value != null) { | |
return onlyDate(value.getDate()); | |
} | |
addValue(NONE); | |
return this; | |
} | |
public RowBuilder money(Double value) { | |
if (value == null) { | |
value = 0d; | |
} | |
addValue(new DecimalFormat("0.00").format(value)); | |
return this; | |
} | |
public RowBuilder year(Date value) { | |
if (value != null) { | |
return year(DateTime.from(value)); | |
} else { | |
addValue(ZERO); | |
} | |
return this; | |
} | |
public RowBuilder year(DateTime value) { | |
if (value != null && value.getDate() != null) { | |
addValue(value.getYear() + ""); | |
} else { | |
addValue(ZERO); | |
} | |
return this; | |
} | |
public RowBuilder quarterOfYear(Date value) { | |
if (value != null) { | |
return quarterOfYear(DateTime.from(value)); | |
} else { | |
addValue(ZERO); | |
} | |
return this; | |
} | |
public RowBuilder quarterOfYear(DateTime value) { | |
if (value != null && value.getDate() != null) { | |
addValue(value.quarterOfYear() + ""); | |
} else { | |
addValue(ZERO); | |
} | |
return this; | |
} | |
public RowBuilder weekYear(Date value) { | |
if (value != null) { | |
return weekYear(DateTime.from(value)); | |
} else { | |
addValue(ZERO); | |
} | |
return this; | |
} | |
public RowBuilder weekYear(DateTime value) { | |
if (value != null && value.getDate() != null) { | |
addValue(value.weekYear() + ""); | |
} else { | |
addValue(ZERO); | |
} | |
return this; | |
} | |
public RowBuilder weekOfWeekYear(Date value) { | |
if (value != null) { | |
return weekOfWeekYear(DateTime.from(value)); | |
} else { | |
addValue(ZERO); | |
} | |
return this; | |
} | |
public RowBuilder weekOfWeekYear(DateTime value) { | |
if (value != null && value.getDate() != null) { | |
addValue(value.weekOfWeekYear() + ""); | |
} else { | |
addValue(ZERO); | |
} | |
return this; | |
} | |
public RowBuilder monthOfYear(Date value) { | |
if (value != null) { | |
return monthOfYear(DateTime.from(value)); | |
} else { | |
addValue(ZERO); | |
} | |
return this; | |
} | |
public RowBuilder monthOfYear(DateTime value) { | |
if (value != null && value.getDate() != null) { | |
addValue(value.getMonthOfYear() + ""); | |
} else { | |
addValue(ZERO); | |
} | |
return this; | |
} | |
public RowBuilder timestamp(Date date) { | |
timestamp(new DateTime(date)); | |
return this; | |
} | |
public RowBuilder timestamp(Date date, String timeZoneId) { | |
timestamp(new DateTime(date), timeZoneId); | |
return this; | |
} | |
public RowBuilder timestamp(DateTime date) { | |
if (date != null && date.getDate() != null) { | |
addValue(DateUtil.formatByPattern("yyy-MM-dd HH:mm:ss XXX", date)); | |
} else { | |
addValue(""); | |
} | |
return this; | |
} | |
public RowBuilder timestamp(DateTime date, String timeZoneId) { | |
if (date != null && date.getDate() != null) { | |
addValue(DateUtil.formatByPattern("yyy-MM-dd HH:mm:ss XXX",date, timeZoneId)); | |
} else { | |
addValue(""); | |
} | |
return this; | |
} | |
public RowBuilder booleanValue(boolean b) { | |
addValue(Boolean.valueOf(b).toString()); | |
return this; | |
} | |
private void addValue(String param) { | |
if (param != null) { | |
sb.append(formatValue(param)); | |
sb.append(separator); | |
} | |
} | |
private String formatValue(String str) { | |
boolean mustQuote = (str.contains(",") || str.contains("\"") || str.contains("\r") || str.contains("\n")); | |
if (mustQuote) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append("\""); | |
for (char nextChar : str.toCharArray()) { | |
sb.append(nextChar); | |
if (nextChar == '"') | |
sb.append("\""); | |
} | |
sb.append("\""); | |
return sb.toString(); | |
} | |
return str; | |
} | |
public String build() { | |
//remove last separator | |
sb.deleteCharAt(sb.length() - 1); | |
sb.append(end); | |
return sb.toString(); | |
} | |
} | |
private CsvBuilder(StringBuilder sb) { | |
this.sb = sb; | |
} | |
public static CsvBuilder csvBuilder() { | |
return new CsvBuilder(new StringBuilder()); | |
} | |
public CsvBuilder row(String... params) { | |
if (params != null) { | |
// for (String param : params) { | |
for (int i = 0; i < params.length; i++) { | |
String param = params[i]; | |
if (param != null) { | |
String value = ""; | |
value = param.replaceAll("\n", ""); | |
value = value.replaceAll("\'", ""); | |
value = value.replaceAll("\"", ""); | |
value = value.replaceAll(",", ""); | |
sb.append(value); | |
if (i < params.length - 1) { | |
sb.append(separator); | |
} | |
} | |
} | |
} | |
sb.append(end); | |
return this; | |
} | |
public CsvBuilder row(RowBuilder rowBuilder) { | |
sb.append(rowBuilder.build()); | |
return this; | |
} | |
public String build() { | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment