[#4239] Add Result.formatCSV(boolean) (and related convenience methods) to avoid header

This commit is contained in:
lukaseder 2015-07-27 15:32:36 +02:00
parent d8c44c7f57
commit 227254cf76
2 changed files with 135 additions and 16 deletions

View File

@ -490,12 +490,42 @@ public interface Result<R extends Record> extends List<R>, Attachable {
/**
* Get a simple formatted representation of this result as CSV.
* <p>
* This is the same as calling <code>formatCSV(',', "")</code>
* This is the same as calling <code>formatCSV(true, ',', "")</code>
*
* @return The formatted result
*/
String formatCSV();
/**
* Get a simple formatted representation of this result as CSV.
* <p>
* This is the same as calling <code>formatCSV(true, delimiter, "")</code>
*
* @param delimiter The delimiter to use between records
* @return The formatted result
*/
String formatCSV(char delimiter);
/**
* Get a simple formatted representation of this result as CSV.
* <p>
* This is the same as calling <code>formatCSV(true, delimiter, nullString)</code>
*
* @param delimiter The delimiter to use between records
* @param nullString A special string for encoding <code>NULL</code> values.
* @return The formatted result
*/
String formatCSV(char delimiter, String nullString);
/**
* Get a simple formatted representation of this result as CSV.
* <p>
* This is the same as calling <code>formatCSV(',', "")</code>
*
* @return The formatted result
*/
String formatCSV(boolean header);
/**
* Get a simple formatted representation of this result as CSV.
* <p>
@ -504,7 +534,7 @@ public interface Result<R extends Record> extends List<R>, Attachable {
* @param delimiter The delimiter to use between records
* @return The formatted result
*/
String formatCSV(char delimiter);
String formatCSV(boolean header, char delimiter);
/**
* Get a simple formatted representation of this result as CSV.
@ -513,7 +543,7 @@ public interface Result<R extends Record> extends List<R>, Attachable {
* @param nullString A special string for encoding <code>NULL</code> values.
* @return The formatted result
*/
String formatCSV(char delimiter, String nullString);
String formatCSV(boolean header, char delimiter, String nullString);
/**
* Get a simple formatted representation of this result as a JSON array of
@ -601,6 +631,27 @@ public interface Result<R extends Record> extends List<R>, Attachable {
*/
void formatCSV(OutputStream stream, char delimiter, String nullString) throws IOException;
/**
* Like {@link #formatCSV(boolean)}, but the data is output onto an {@link OutputStream}.
*
* @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong.
*/
void formatCSV(OutputStream stream, boolean header) throws IOException;
/**
* Like {@link #formatCSV(boolean, char)}, but the data is output onto an {@link OutputStream}.
*
* @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong.
*/
void formatCSV(OutputStream stream, boolean header, char delimiter) throws IOException;
/**
* Like {@link #formatCSV(boolean, char, String)}, but the data is output onto an {@link OutputStream}.
*
* @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong.
*/
void formatCSV(OutputStream stream, boolean header, char delimiter, String nullString) throws IOException;
/**
* Like {@link #formatJSON()}, but the data is output onto an {@link OutputStream}.
*
@ -671,6 +722,27 @@ public interface Result<R extends Record> extends List<R>, Attachable {
*/
void formatCSV(Writer writer, char delimiter, String nullString) throws IOException;
/**
* Like {@link #formatCSV(boolean)}, but the data is output onto a {@link Writer}.
*
* @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong.
*/
void formatCSV(Writer writer, boolean header) throws IOException;
/**
* Like {@link #formatCSV(boolean, char)}, but the data is output onto a {@link Writer}.
*
* @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong.
*/
void formatCSV(Writer writer, boolean header, char delimiter) throws IOException;
/**
* Like {@link #formatCSV(boolean, char, String)}, but the data is output onto a {@link Writer}.
*
* @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong.
*/
void formatCSV(Writer writer, boolean header, char delimiter, String nullString) throws IOException;
/**
* Like {@link #formatJSON()}, but the data is output onto a {@link Writer}.
*

View File

@ -660,23 +660,43 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
@Override
public final String formatCSV() {
return formatCSV(true);
}
@Override
public final String formatCSV(boolean header) {
StringWriter writer = new StringWriter();
formatCSV(writer);
formatCSV(writer, header);
return writer.toString();
}
@Override
public final void formatCSV(OutputStream stream) {
formatCSV(new OutputStreamWriter(stream));
formatCSV(stream, true);
}
@Override
public final void formatCSV(OutputStream stream, boolean header) {
formatCSV(new OutputStreamWriter(stream), header);
}
@Override
public final void formatCSV(Writer writer) {
formatCSV(writer, ',', "");
formatCSV(writer, true);
}
@Override
public final void formatCSV(Writer writer, boolean header) {
formatCSV(writer, header, ',', "");
}
@Override
public final String formatCSV(char delimiter) {
return formatCSV(true, delimiter);
}
@Override
public final String formatCSV(boolean header, char delimiter) {
StringWriter writer = new StringWriter();
formatCSV(writer, delimiter);
return writer.toString();
@ -684,39 +704,66 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
@Override
public final void formatCSV(OutputStream stream, char delimiter) {
formatCSV(stream, true, delimiter);
}
@Override
public final void formatCSV(OutputStream stream, boolean header, char delimiter) {
formatCSV(new OutputStreamWriter(stream), delimiter);
}
@Override
public final void formatCSV(Writer writer, char delimiter) {
formatCSV(writer, delimiter, "");
formatCSV(writer, true, delimiter);
}
@Override
public final void formatCSV(Writer writer, boolean header, char delimiter) {
formatCSV(writer, header, delimiter, "");
}
@Override
public final String formatCSV(char delimiter, String nullString) {
return formatCSV(true, delimiter, nullString);
}
@Override
public final String formatCSV(boolean header, char delimiter, String nullString) {
StringWriter writer = new StringWriter();
formatCSV(writer, delimiter, nullString);
formatCSV(writer, header, delimiter, nullString);
return writer.toString();
}
@Override
public final void formatCSV(OutputStream stream, char delimiter, String nullString) {
formatCSV(new OutputStreamWriter(stream), delimiter, nullString);
formatCSV(stream, true, delimiter, nullString);
}
@Override
public final void formatCSV(OutputStream stream, boolean header, char delimiter, String nullString) {
formatCSV(new OutputStreamWriter(stream), header, delimiter, nullString);
}
@Override
public final void formatCSV(Writer writer, char delimiter, String nullString) {
formatCSV(writer, true, delimiter, nullString);
}
@Override
public final void formatCSV(Writer writer, boolean header, char delimiter, String nullString) {
try {
String sep1 = "";
for (Field<?> field : fields.fields) {
writer.append(sep1);
writer.append(formatCSV0(field.getName(), ""));
if (header) {
String sep1 = "";
for (Field<?> field : fields.fields) {
writer.append(sep1);
writer.append(formatCSV0(field.getName(), ""));
sep1 = Character.toString(delimiter);
sep1 = Character.toString(delimiter);
}
writer.append("\n");
}
writer.append("\n");
for (Record record : this) {
String sep2 = "";