/** * 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); }
/** * 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); } }
/** * 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); } }
/** * 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); } }
private static void writeCurveGroup(CsvOutput csv, RatesCurveGroup group) { String groupName = group.getName().getName(); Map<Currency, Curve> discountingCurves = group.getDiscountCurves(); for (Entry<Currency, Curve> entry : discountingCurves.entrySet()) { List<String> line = new ArrayList<>(4); line.add(groupName); line.add(DISCOUNT); line.add(entry.getKey().toString()); line.add(entry.getValue().getName().getName()); csv.writeLine(line); } Map<Index, Curve> forwardCurves = group.getForwardCurves(); for (Entry<Index, Curve> entry : forwardCurves.entrySet()) { List<String> line = new ArrayList<>(4); line.add(groupName); line.add(FORWARD); line.add(entry.getKey().toString()); line.add(entry.getValue().getName().getName()); csv.writeLine(line); } }
/** * 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); }
/** * Writes the curve settings in a CSV format to an appendable. * * @param underlying the underlying appendable destination * @param group the curve group */ public static void writeCurveSettings(Appendable underlying, RatesCurveGroup group) { CsvOutput csv = CsvOutput.standard(underlying); // header csv.writeLine(HEADERS_SETTINGS); // rows Map<Currency, Curve> discountingCurves = group.getDiscountCurves(); Set<CurveName> names = new HashSet<>(); for (Entry<Currency, Curve> entry : discountingCurves.entrySet()) { Curve curve = entry.getValue(); csv.writeLine(curveSettings(curve)); names.add(curve.getName()); } Map<Index, Curve> forwardCurves = group.getForwardCurves(); for (Entry<Index, Curve> entry : forwardCurves.entrySet()) { Curve curve = entry.getValue(); if (!names.contains(curve.getName())) { csv.writeLine(curveSettings(curve)); names.add(curve.getName()); } } }
public void test_standard_writeLines_tab_separated() { StringBuilder buf = new StringBuilder(); CsvOutput.standard(buf, "\n", LINE_ITEM_SEP_TAB).writeLine(Arrays.asList("a", "1,000")); assertEquals(buf.toString(), "a\t\"1,000\"\n"); }
public void test_standard_expressionPrefix() { StringBuilder buf = new StringBuilder(); CsvOutput.standard(buf, "\n").writeLine(Arrays.asList("=cmd", "+cmd", "-cmd", "@cmd", "")); assertEquals(buf.toString(), "\"=cmd\",+cmd,-cmd,\"@cmd\",\n"); }
public void test_standard_expressionPrefixNumbers() { StringBuilder buf = new StringBuilder(); CsvOutput.standard(buf, "\n").writeLine(Arrays.asList("+8", "-7", "+8-7", "-7+8", "NaN", "-Infinity")); assertEquals(buf.toString(), "+8,-7,+8-7,-7+8,NaN,-Infinity\n"); }
public void test_safe_expressionPrefixNumbers() { StringBuilder buf = new StringBuilder(); CsvOutput.safe(buf, "\n", LINE_ITEM_SEP_COMMA) .writeLine(Arrays.asList("+8", "-7", "+8-7", "-7+8", "NaN", "-Infinity")); assertEquals(buf.toString(), "+8,-7,=\"+8-7\",=\"-7+8\",NaN,=\"-Infinity\"\n"); }
public void test_standard_writeLine_selectiveQuote() { StringBuilder buf = new StringBuilder(); CsvOutput.standard(buf, "\n", LINE_ITEM_SEP_COMMA).writeLine(Arrays.asList("a", "1,000")); assertEquals(buf.toString(), "a,\"1,000\"\n"); }
public void test_safe_expressionPrefix() { StringBuilder buf = new StringBuilder(); CsvOutput.safe(buf, "\n").writeLine(Arrays.asList("=cmd", "+cmd", "-cmd", "@cmd")); assertEquals(buf.toString(), "=\"=cmd\",=\"+cmd\",=\"-cmd\",=\"@cmd\"\n"); }
private static void writeCurveGroupDefinition(CsvOutput csv, RatesCurveGroupDefinition group) { String groupName = group.getName().getName(); for (RatesCurveGroupEntry entry : group.getEntries()) { for (Currency currency : entry.getDiscountCurrencies()) { csv.writeLine( ImmutableList.of(groupName, DISCOUNT, currency.toString(), entry.getCurveName().getName())); } for (Index index : entry.getIndices()) { csv.writeLine( ImmutableList.of(groupName, FORWARD, index.toString(), entry.getCurveName().getName())); } } }
private static void nodeLines(String valuationDateStr, Curve curve, CsvOutput csv) { ArgChecker.isTrue(curve instanceof InterpolatedNodalCurve, "interpolated"); InterpolatedNodalCurve interpolatedCurve = (InterpolatedNodalCurve) curve; int nbPoints = interpolatedCurve.getXValues().size(); for (int i = 0; i < nbPoints; i++) { ArgChecker.isTrue( interpolatedCurve.getParameterMetadata(i) instanceof DatedParameterMetadata, "Curve metadata must contain a date, but was " + interpolatedCurve.getParameterMetadata(i).getClass().getSimpleName()); DatedParameterMetadata metadata = (DatedParameterMetadata) interpolatedCurve.getParameterMetadata(i); List<String> line = new ArrayList<>(); line.add(valuationDateStr); line.add(curve.getName().getName().toString()); line.add(metadata.getDate().toString()); line.add(BigDecimal.valueOf(interpolatedCurve.getYValues().get(i)).toPlainString()); line.add(metadata.getLabel()); csv.writeLine(line); } }
public void test_mixed() { List<String> row = Arrays.asList("x", "y"); StringBuilder buf = new StringBuilder(); CsvOutput.standard(buf, "\n") .writeCell("a") .writeCell("b") .writeLine(row); assertEquals(buf.toString(), "a,b,x,y\n"); }
/** * Writes the curve nodes in a CSV format to an appendable. * * @param underlying the underlying appendable destination * @param valuationDate the valuation date * @param group the curve group */ public static void writeCurveNodes(Appendable underlying, LocalDate valuationDate, RatesCurveGroup group) { CsvOutput csv = CsvOutput.standard(underlying); // header csv.writeLine(HEADERS_NODES); // rows String valuationDateStr = valuationDate.toString(); Map<Currency, Curve> discountingCurves = group.getDiscountCurves(); Set<CurveName> names = new HashSet<>(); for (Entry<Currency, Curve> entry : discountingCurves.entrySet()) { Curve curve = entry.getValue(); nodeLines(valuationDateStr, curve, csv); names.add(curve.getName()); } Map<Index, Curve> forwardCurves = group.getForwardCurves(); for (Entry<Index, Curve> entry : forwardCurves.entrySet()) { Curve curve = entry.getValue(); if (!names.contains(curve.getName())) { nodeLines(valuationDateStr, curve, csv); names.add(curve.getName()); } } }
csv.writeLine(additionalHeaders);
csv.writeLine(additionalCells);