/** * Gets a single value from the row by header. * <p> * This returns the value of the first column where the header matches the specified header pattern. * If the value is an empty string, then an empty optional is returned. * * @param header the column header * @return the trimmed field value, empty */ public Optional<String> findValue(String header) { return findField(header).filter(str -> !str.isEmpty()); }
/** * Gets a single value from the row by header pattern. * <p> * This returns the value of the first column where the header matches the specified header pattern. * If the value is an empty string, then an empty optional is returned. * * @param headerPattern the header pattern to match * @return the trimmed field value, empty */ public Optional<String> findValue(Pattern headerPattern) { return findField(headerPattern).filter(str -> !str.isEmpty()); }
private PositionInfo parsePositionInfo(CsvRow row) { PositionInfoBuilder infoBuilder = PositionInfo.builder(); String scheme = row.findField(ID_SCHEME_FIELD).orElse(DEFAULT_POSITION_SCHEME); row.findValue(ID_FIELD).ifPresent(id -> infoBuilder.id(StandardId.of(scheme, id))); resolver.parsePositionInfo(row, infoBuilder); return infoBuilder.build(); }
private static List<CurveDefinition> parseSingle( CharSource resource, Map<CurveName, LoadedCurveSettings> settingsMap) { CsvFile csv = CsvFile.of(resource, true); Map<CurveName, List<CurveNode>> allNodes = new HashMap<>(); for (CsvRow row : csv.rows()) { String curveNameStr = row.getField(CURVE_NAME); String label = row.getField(CURVE_LABEL); String symbologyQuoteStr = row.getField(CURVE_SYMBOLOGY_QUOTE); String tickerQuoteStr = row.getField(CURVE_TICKER_QUOTE); String fieldQuoteStr = row.getField(CURVE_FIELD_QUOTE); String typeStr = row.getField(CURVE_TYPE); String conventionStr = row.getField(CURVE_CONVENTION); String timeStr = row.getField(CURVE_TIME); String dateStr = row.findField(CURVE_DATE).orElse(""); String minGapStr = row.findField(CURVE_MIN_GAP).orElse(""); String clashActionStr = row.findField(CURVE_CLASH_ACTION).orElse(""); String spreadStr = row.findField(CURVE_SPREAD).orElse(""); CurveName curveName = CurveName.of(curveNameStr); StandardId quoteStandardId = StandardId.of(symbologyQuoteStr, tickerQuoteStr); FieldName quoteField = fieldQuoteStr.isEmpty() ? FieldName.MARKET_VALUE : FieldName.of(fieldQuoteStr); QuoteId quoteId = QuoteId.of(quoteStandardId, quoteField); double spread = spreadStr.isEmpty() ? 0d : Double.parseDouble(spreadStr); CurveNodeDate date = parseDate(dateStr); CurveNodeDateOrder order = parseDateOrder(minGapStr, clashActionStr); List<CurveNode> curveNodes = allNodes.computeIfAbsent(curveName, k -> new ArrayList<>()); curveNodes.add(createCurveNode(typeStr, conventionStr, timeStr, label, quoteId, spread, date, order)); } return buildCurveDefinition(settingsMap, allNodes); }
private TradeInfo parseTradeInfo(CsvRow row) { TradeInfoBuilder infoBuilder = TradeInfo.builder(); String scheme = row.findField(ID_SCHEME_FIELD).orElse(DEFAULT_TRADE_SCHEME); row.findValue(ID_FIELD).ifPresent(id -> infoBuilder.id(StandardId.of(scheme, id))); String schemeCpty = row.findValue(CPTY_SCHEME_FIELD).orElse(DEFAULT_CPTY_SCHEME); row.findValue(CPTY_FIELD).ifPresent(cpty -> infoBuilder.counterparty(StandardId.of(schemeCpty, cpty))); row.findValue(TRADE_DATE_FIELD).ifPresent(dateStr -> infoBuilder.tradeDate(LoaderUtils.parseDate(dateStr))); row.findValue(TRADE_TIME_FIELD).ifPresent(timeStr -> infoBuilder.tradeTime(LoaderUtils.parseTime(timeStr))); row.findValue(TRADE_ZONE_FIELD).ifPresent(zoneStr -> infoBuilder.zone(ZoneId.of(zoneStr))); row.findValue(SETTLEMENT_DATE_FIELD).ifPresent(dateStr -> infoBuilder.settlementDate(LoaderUtils.parseDate(dateStr))); resolver.parseTradeInfo(row, infoBuilder); return infoBuilder.build(); }
public void test_of_simple_with_header_access_by_invalid_field() { CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1), true); assertEquals(csvFile.row(0).findField("h3"), Optional.empty()); assertThrowsIllegalArg(() -> csvFile.row(0).getField("h3")); }
public void test_of_simple_no_header_access_by_field() { CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1), false); assertEquals(csvFile.row(0).findField("h1"), Optional.empty()); assertThrowsIllegalArg(() -> csvFile.row(0).getField("h1")); }
assertThrowsIllegalArg(() -> csvFile.row(2).getValue("h2")); assertEquals(csvFile.row(0).findField("h1"), Optional.of("r11")); assertEquals(csvFile.row(0).findField("h2"), Optional.of("r12")); assertEquals(csvFile.row(1).findField("h1"), Optional.of("r21")); assertEquals(csvFile.row(1).findField("h2"), Optional.of("r22")); assertEquals(csvFile.row(0).findField("zzz"), Optional.empty()); assertThrowsIllegalArg(() -> csvFile.row(2).getValue(Pattern.compile("h2"))); assertEquals(csvFile.row(0).findField(Pattern.compile("h[13]")), Optional.of("r11")); assertEquals(csvFile.row(0).findField(Pattern.compile("h[24]")), Optional.of("r12")); assertEquals(csvFile.row(0).findField(Pattern.compile("zzz")), Optional.empty());