/** * 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; } }
/** * 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; } }
private void parse( CharSource charSource, ListMultimap<String, CurveSensitivities> parsed, List<FailureItem> failures) { try (CsvIterator csv = CsvIterator.of(charSource, true)) { if (!csv.containsHeader(TENOR_HEADER) && !csv.containsHeader(DATE_HEADER)) { failures.add(FailureItem.of( FailureReason.PARSING, "CSV file could not be parsed as sensitivities, invalid format")); } else if (csv.containsHeader(REFERENCE_HEADER) && csv.containsHeader(TYPE_HEADER) && csv.containsHeader(VALUE_HEADER)) { parseStandardFormat(csv, parsed, failures); } else if (csv.containsHeader(REFERENCE_HEADER)) { parseListFormat(csv, parsed, failures); } else { parseGridFormat(csv, parsed, failures); } } catch (RuntimeException ex) { failures.add(FailureItem.of(FailureReason.PARSING, ex, "CSV file could not be parsed: {}", ex.getMessage())); } }
ImmutableList<String> headers = csvFile.headers(); assertEquals(headers.size(), 2); assertEquals(csvFile.containsHeader("h1"), true); assertEquals(csvFile.containsHeader("h2"), true); assertEquals(csvFile.containsHeader("a"), false); assertEquals(csvFile.containsHeader(Pattern.compile("h.")), true); assertEquals(csvFile.containsHeader(Pattern.compile("a")), false); assertEquals(headers.get(0), "h1"); assertEquals(headers.get(1), "h2");
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); } }