/** * Checks whether the source is a CSV format sensitivities file. * <p> * This parses the headers as CSV and checks that mandatory headers are present. * * @param charSource the CSV character source to check * @return true if the source is a CSV file with known headers, false otherwise */ public boolean isKnownFormat(CharSource charSource) { try (CsvIterator csv = CsvIterator.of(charSource, true)) { if (!csv.containsHeader(TENOR_HEADER) && !csv.containsHeader(DATE_HEADER)) { return false; } if (csv.containsHeader(REFERENCE_HEADER) && csv.containsHeader(TYPE_HEADER) && csv.containsHeader(VALUE_HEADER)) { return true; // standard format } else if (csv.containsHeader(REFERENCE_HEADER) || csv.containsHeader(TYPE_HEADER)) { return true; // list or grid format } else { return csv.headers().stream().anyMatch(SensitivityCsvLoader::knownReference); // implied grid format } } catch (RuntimeException ex) { return false; } }
/** * Checks whether the source is a CSV format trade file. * <p> * This parses the headers as CSV and checks that mandatory headers are present. * This is determined entirely from the 'Strata Trade Type' column. * * @param charSource the CSV character source to check * @return true if the source is a CSV file with known headers, false otherwise */ public boolean isKnownFormat(CharSource charSource) { try (CsvIterator csv = CsvIterator.of(charSource, true)) { return csv.containsHeader(TYPE_FIELD); } catch (RuntimeException ex) { return false; } }
/** * Returns the next batch of rows from the CSV file. * <p> * This will return up to the specified number of rows from the file at the current iteration point. * An empty list is returned if there are no more rows. * * @param count the number of rows to try and get, negative returns an empty list * @return the next batch of rows, up to the number requested * @throws UncheckedIOException if an IO exception occurs * @throws IllegalArgumentException if the file cannot be parsed */ public List<CsvRow> nextBatch(int count) { List<CsvRow> rows = new ArrayList<>(); for (int i = 0; i < count; i++) { if (hasNext()) { rows.add(next()); } } return rows; }
public void test_of_empty_no_header() { try (CsvIterator csvFile = CsvIterator.of(CharSource.wrap(""), false)) { assertEquals(csvFile.headers().size(), 0); assertEquals(csvFile.containsHeader("a"), false); assertEquals(csvFile.hasNext(), false); assertEquals(csvFile.hasNext(), false); assertThrows(() -> csvFile.peek(), NoSuchElementException.class); assertThrows(() -> csvFile.next(), NoSuchElementException.class); assertThrows(() -> csvFile.next(), NoSuchElementException.class); assertThrows(() -> csvFile.remove(), UnsupportedOperationException.class); } }
public void nextBatch_predicate() { try (CsvIterator csvFile = CsvIterator.of(CharSource.wrap(CSV5GROUPED), true)) { ImmutableList<String> headers = csvFile.headers(); assertEquals(headers.size(), 2); assertEquals(headers.get(0), "id"); assertEquals(headers.get(1), "value"); int batches = 0; int total = 0; while (csvFile.hasNext()) { CsvRow first = csvFile.peek(); String id = first.getValue("id"); List<CsvRow> batch = csvFile.nextBatch(row -> row.getValue("id").equals(id)); assertEquals(batch.stream().map(row -> row.getValue("id")).distinct().count(), 1); batches++; total += batch.size(); } assertEquals(batches, 3); assertEquals(total, 6); } }
public void test_asStream_simple_with_header() { try (CsvIterator csvFile = CsvIterator.of(CharSource.wrap(CSV1), true)) { ImmutableList<String> headers = csvFile.headers(); assertEquals(headers.size(), 2); assertEquals(headers.get(0), "h1"); assertEquals(headers.get(1), "h2"); List<CsvRow> rows = csvFile.asStream().collect(toList()); assertEquals(csvFile.hasNext(), false); assertEquals(rows.size(), 2); CsvRow row0 = rows.get(0); assertEquals(row0.headers(), headers); assertEquals(row0.fieldCount(), 2); assertEquals(row0.field(0), "r11"); assertEquals(row0.field(1), "r12"); CsvRow row1 = rows.get(1); assertEquals(row1.headers(), headers); assertEquals(row1.fieldCount(), 2); assertEquals(row1.field(0), "r21"); assertEquals(row1.field(1), "r22"); } }
public void test_of_blank_row() { try (CsvIterator csvFile = CsvIterator.of(CharSource.wrap(CSV3), false)) { assertEquals(csvFile.hasNext(), true); CsvRow row0 = csvFile.next(); assertEquals(row0.fieldCount(), 2); assertEquals(row0.field(0), "r11"); assertEquals(row0.field(1), "r12"); CsvRow row1 = csvFile.next(); assertEquals(row1.fieldCount(), 2); assertEquals(row1.field(0), "r21"); assertEquals(row1.field(1), "r22"); assertEquals(csvFile.hasNext(), false); } }
private <T extends Trade> ValueWithFailures<List<T>> parseFile(CharSource charSource, Class<T> tradeType) { try (CsvIterator csv = CsvIterator.of(charSource, true)) { if (!csv.headers().contains(TYPE_FIELD)) { return ValueWithFailures.of( ImmutableList.of(), FailureItem.of(FailureReason.PARSING, "CSV file does not contain '{header}' header: {}", TYPE_FIELD, charSource)); } return parseFile(csv, tradeType); } catch (RuntimeException ex) { return ValueWithFailures.of( ImmutableList.of(), FailureItem.of( FailureReason.PARSING, ex, "CSV file could not be parsed: {exceptionMessage}: {}", ex.getMessage(), charSource)); } }
/** * Parses the specified source as a CSV file, using a comma as the separator. * <p> * This method opens the CSV file for reading. * The caller is responsible for closing it by calling {@link #close()}. * * @param source the CSV file resource * @param headerRow whether the source has a header row, an empty source must still contain the header * @return the CSV file * @throws UncheckedIOException if an IO exception occurs * @throws IllegalArgumentException if the file cannot be parsed */ public static CsvIterator of(CharSource source, boolean headerRow) { return of(source, headerRow, ','); }
/** * Peeks the next row from the CSV file without changing the iteration position. * * @return the peeked row * @throws UncheckedIOException if an IO exception occurs * @throws IllegalArgumentException if the file cannot be parsed * @throws NoSuchElementException if the end of file has been reached */ @Override public CsvRow peek() { if (nextRow != null || hasNext()) { return nextRow; } else { throw new NoSuchElementException("CsvIterator has reached the end of the file"); } }
private static CsvIterator create(BufferedReader breader, boolean headerRow, char separator) { try { if (!headerRow) { return new CsvIterator(breader, separator, ImmutableList.of(), ImmutableMap.of(), 0); ImmutableList<String> headers = CsvFile.parseLine(line, lineNumber, separator); if (!headers.isEmpty()) { return new CsvIterator(breader, separator, headers, CsvFile.buildSearchHeaders(headers), lineNumber);
public void test_of_headerComment() { try (CsvIterator csvFile = CsvIterator.of(CharSource.wrap(CSV4), true)) { assertEquals(csvFile.hasNext(), true); CsvRow row0 = csvFile.next(); assertEquals(row0.lineNumber(), 3); assertEquals(csvFile.headers().size(), 2); assertEquals(csvFile.headers().get(0), "h1"); assertEquals(csvFile.headers().get(1), "h2"); assertEquals(row0.fieldCount(), 2); assertEquals(row0.field(0), "r1"); assertEquals(row0.field(1), "r2"); } }
for (String header : csv.headers()) { String headerLowerCase = header.toLowerCase(Locale.ENGLISH); if (!REF_HEADERS.contains(headerLowerCase) && !resolver.isInfoColumn(headerLowerCase)) { while (csv.hasNext()) { CsvRow peekedRow = csv.peek(); PortfolioItemInfo info = parseInfo(peekedRow); String id = info.getId().map(StandardId::toString).orElse(""); List<CsvRow> batchRows = csv.nextBatch(r -> matchId(r, id)); for (CsvRow batchRow : batchRows) { try {
public void test_asStream_simple_no_header() { try (CsvIterator csvFile = CsvIterator.of(CharSource.wrap(CSV1), false)) { assertEquals(csvFile.headers().size(), 0); List<CsvRow> rows = csvFile.asStream().collect(toList()); assertEquals(csvFile.hasNext(), false); assertEquals(rows.size(), 3); CsvRow row0 = rows.get(0); assertEquals(row0.headers().size(), 0); assertEquals(row0.fieldCount(), 2); assertEquals(row0.field(0), "h1"); assertEquals(row0.field(1), "h2"); CsvRow row1 = rows.get(1); assertEquals(row1.headers().size(), 0); assertEquals(row1.fieldCount(), 2); assertEquals(row1.field(0), "r11"); assertEquals(row1.field(1), "r12"); CsvRow row2 = rows.get(2); assertEquals(row2.headers().size(), 0); assertEquals(row2.fieldCount(), 2); assertEquals(row2.field(0), "r21"); assertEquals(row2.field(1), "r22"); } }
/** * Checks whether the source is a CSV format position file. * <p> * This parses the headers as CSV and checks that mandatory headers are present. * This is determined entirely from the 'Strata Position Type' column. * * @param charSource the CSV character source to check * @return true if the source is a CSV file with known headers, false otherwise */ public boolean isKnownFormat(CharSource charSource) { try (CsvIterator csv = CsvIterator.of(charSource, true)) { return csv.containsHeader(TYPE_FIELD); } catch (RuntimeException ex) { return false; } }