/** * Writes the curve group in a CSV format to an appendable. * * @param underlying the underlying appendable destination * @param groups the curve groups */ public static void writeCurveGroup(Appendable underlying, RatesCurveGroup... groups) { CsvOutput csv = CsvOutput.standard(underlying); csv.writeLine(HEADERS); for (RatesCurveGroup group : groups) { writeCurveGroup(csv, group); } }
/** * Writes a single CSV line to the underlying. * <p> * The boolean flag controls whether each entry is always quoted or only quoted when necessary. * <p> * This method writes each cell in the specified list to the underlying, followed by * a new line character. * * @param line the line to write * @param alwaysQuote when true, each column will be quoted, when false, quoting is selective * @throws UncheckedIOException if an IO exception occurs */ public void writeLine(List<String> line, boolean alwaysQuote) { ArgChecker.notNull(line, "line"); for (String cell : line) { writeCell(cell, alwaysQuote); } writeNewLine(); }
/** * Creates an instance, allowing the new line character to be controlled and using a comma separator. * <p> * See the standard quoting rules in the class-level documentation. * * @param underlying the destination to write to * @param newLine the new line string * @return the CSV outputter */ public static CsvOutput standard(Appendable underlying, String newLine) { return new CsvOutput(underlying, newLine, COMMA, false); }
/** * Outputs the report table in CSV format. * * @param report the report * @param out the output stream to write to */ @SuppressWarnings("resource") public void writeCsv(R report, OutputStream out) { OutputStreamWriter outputWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8); CsvOutput csvOut = CsvOutput.safe(outputWriter); csvOut.writeLine(report.getColumnHeaders()); IntStream.range(0, report.getRowCount()) .mapToObj(rowIdx -> formatRow(report, rowIdx, ReportOutputFormat.CSV)) .forEach(csvOut::writeLine); Unchecked.wrap(outputWriter::flush); }
public void test_writeCell() { StringBuilder buf = new StringBuilder(); CsvOutput.standard(buf, "\n") .writeCell("a") .writeCell("x") .writeNewLine() .writeCell("b", true) .writeCell("y", true) .writeNewLine(); assertEquals(buf.toString(), "a,x\n\"b\",\"y\"\n"); }
/** * Writes a single CSV line to the underlying, only quoting if needed. * <p> * This can be used as a method reference from a {@code Stream} pipeline from * {@link Stream#forEachOrdered(Consumer)}. * <p> * This method writes each cell in the specified list to the underlying, followed by * a new line character. * * @param line the line to write * @throws UncheckedIOException if an IO exception occurs */ public void writeLine(List<String> line) { writeLine(line, false); }
Tenor tenor = ((TenoredParameterMetadata) pmd).getTenor(); double value = paramSens.getSensitivity().get(i); csv.writeCell(reference); csv.writeCell(type.getName()); csv.writeCell(tenor.toString()); if (containsDates) { csv.writeCell(pmd instanceof DatedParameterMetadata ? ((DatedParameterMetadata) pmd).getDate().toString() : ""); csv.writeCell(paramSens.getCurrency().getCode()); csv.writeCell(BigDecimal.valueOf(value).toPlainString()); csv.writeLine(additionalCells);
public void test_standard_writeLines_selectiveQuote_trimmable() { List<List<String>> rows = Arrays.asList(Arrays.asList("a", " x"), Arrays.asList("b ", "y")); StringBuilder buf = new StringBuilder(); CsvOutput.standard(buf, "\n", LINE_ITEM_SEP_COMMA).writeLines(rows, false); assertEquals(buf.toString(), "a,\" x\"\n\"b \",y\n"); }
/** * Writes a single cell to the current line, only quoting if needed. * <p> * When using this method, either {@link #writeNewLine()} or one of the {@code writeLine} * methods must be called at the end of the line. * * @param cell the cell to write * @return this, for method chaining * @throws UncheckedIOException if an IO exception occurs */ public CsvOutput writeCell(String cell) { writeCell(cell, false); return this; }
public void test_safe_writeLines_systemNewLine() { List<List<String>> rows = Arrays.asList(Arrays.asList("a", "=x"), Arrays.asList("b", "y")); StringBuilder buf = new StringBuilder(); CsvOutput.safe(buf).writeLines(rows, false); assertEquals(buf.toString(), "a,=\"=x\"" + LINE_SEP + "b,y" + LINE_SEP); }
private boolean isQuotingRequired(String cell) { return cell.indexOf('"') >= 0 || cell.indexOf(',') >= 0 || cell.trim().length() != cell.length() || isExpressionPrefix(cell); }
CsvOutput csv = CsvOutput.standard(output, "\n"); List<String> additionalHeaders = supplier.headers(curveSens); csv.writeCell(SensitivityCsvLoader.REFERENCE_HEADER); csv.writeCell(SensitivityCsvLoader.TYPE_HEADER); csv.writeCell(SensitivityCsvLoader.TENOR_HEADER); if (containsDates) { csv.writeCell(SensitivityCsvLoader.DATE_HEADER); csv.writeCell(SensitivityCsvLoader.CURRENCY_HEADER); csv.writeCell(SensitivityCsvLoader.VALUE_HEADER); csv.writeLine(additionalHeaders);
public void test_exception() { Appendable throwingAppendable = new Appendable() { @Override public Appendable append(CharSequence csq, int start, int end) throws IOException { throw new IOException(); } @Override public Appendable append(char c) throws IOException { throw new IOException(); } @Override public Appendable append(CharSequence csq) throws IOException { throw new IOException(); } }; CsvOutput output = CsvOutput.standard(throwingAppendable, "\n"); assertThrows(UncheckedIOException.class, () -> output.writeCell("a")); assertThrows(UncheckedIOException.class, () -> output.writeNewLine()); }
/** * Writes multiple CSV lines to the underlying. * <p> * The boolean flag controls whether each entry is always quoted or only quoted when necessary. * * @param lines the lines to write * @param alwaysQuote when true, each column will be quoted, when false, quoting is selective * @throws UncheckedIOException if an IO exception occurs */ public void writeLines(Iterable<? extends List<String>> lines, boolean alwaysQuote) { ArgChecker.notNull(lines, "lines"); for (List<String> line : lines) { writeLine(line, alwaysQuote); } }
public void test_standard_writeLines_systemNewLine() { List<List<String>> rows = Arrays.asList(Arrays.asList("a", "x"), Arrays.asList("b", "y")); StringBuilder buf = new StringBuilder(); CsvOutput.standard(buf).writeLines(rows, false); assertEquals(buf.toString(), "a,x" + LINE_SEP + "b,y" + LINE_SEP); }
private void outputQuotedCell(String cell) throws IOException { if (safeExpressions && isExpressionPrefix(cell)) { underlying.append('='); } underlying.append('"'); underlying.append(cell.replace("\"", "\"\"")); underlying.append('"'); }
/** * Writes the curve groups definition in a CSV format to an appendable. * * @param underlying the underlying appendable destination * @param groups the curve groups */ public static void writeCurveGroupDefinition(Appendable underlying, RatesCurveGroupDefinition... groups) { CsvOutput csv = CsvOutput.standard(underlying); csv.writeLine(HEADERS); for (RatesCurveGroupDefinition group : groups) { writeCurveGroupDefinition(csv, group); } }