//------------------------------------------------------------------------- // parses a position from the CSV row internal static Position parsePosition(CsvRow row, PositionInfo info, PositionCsvInfoResolver resolver) { if (row.findValue(EXPIRY_FIELD).Present) { // etd if (row.findValue(PUT_CALL_FIELD).Present || row.findValue(EXERCISE_PRICE_FIELD).Present) { return(resolver.parseEtdOptionPosition(row, info)); } else { return(resolver.parseEtdFuturePosition(row, info)); } } else { return(parseNonEtdPosition(row, info, resolver)); } }
// loads a single CSV file private ValueWithFailures <IList <T> > parseFile <T>(CsvIterator csv, Type <T> posType) where T : com.opengamma.strata.product.Position { IList <T> positions = new List <T>(); IList <FailureItem> failures = new List <FailureItem>(); int line = 2; foreach (CsvRow row in (IEnumerable <CsvRow>)() => csv) { try { PositionInfo info = parsePositionInfo(row); Optional <string> typeRawOpt = row.findValue(TYPE_FIELD); if (typeRawOpt.Present) { // type specified string type = typeRawOpt.get().ToUpper(Locale.ENGLISH); switch (type.ToUpper(Locale.ENGLISH)) { case "SEC": case "SECURITY": if (posType == typeof(SecurityPosition) || posType == typeof(ResolvableSecurityPosition)) { positions.Add(posType.cast(SecurityCsvLoader.parseSecurityPosition(row, info, resolver))); } else if (posType == typeof(GenericSecurityPosition) || posType == typeof(Position)) { Position parsed = SecurityCsvLoader.parseNonEtdPosition(row, info, resolver); if (posType.IsInstanceOfType(parsed)) { positions.Add(posType.cast(parsed)); } } break; case "FUT": case "FUTURE": if (posType == typeof(EtdPosition) || posType == typeof(EtdFuturePosition) || posType == typeof(ResolvableSecurityPosition) || posType == typeof(Position)) { positions.Add(posType.cast((Position)resolver.parseEtdFuturePosition(row, info))); } else if (posType == typeof(SecurityPosition)) { positions.Add(posType.cast(resolver.parseEtdFutureSecurityPosition(row, info))); } break; case "OPT": case "OPTION": if (posType == typeof(EtdPosition) || posType == typeof(EtdOptionPosition) || posType == typeof(ResolvableSecurityPosition) || posType == typeof(Position)) { positions.Add(posType.cast(resolver.parseEtdOptionPosition(row, info))); } else if (posType == typeof(SecurityPosition)) { positions.Add(posType.cast(resolver.parseEtdOptionSecurityPosition(row, info))); } break; default: failures.Add(FailureItem.of(FailureReason.PARSING, "CSV file position type '{positionType}' is not known at line {lineNumber}", typeRawOpt.get(), line)); break; } } else { // infer type if (posType == typeof(SecurityPosition)) { positions.Add(posType.cast(SecurityCsvLoader.parsePositionLightweight(row, info, resolver))); } else { Position position = SecurityCsvLoader.parsePosition(row, info, resolver); if (posType.IsInstanceOfType(position)) { positions.Add(posType.cast(position)); } } } } catch (Exception ex) { failures.Add(FailureItem.of(FailureReason.PARSING, ex, "CSV file position could not be parsed at line {lineNumber}: {exceptionMessage}", line, ex.Message)); } line++; } return(ValueWithFailures.of(positions, failures)); }