Skip to content

Commit

Permalink
Parse zero length period to start from CSV (#1670)
Browse files Browse the repository at this point in the history
Zero is a valid period to start
  • Loading branch information
jodastephen committed Mar 23, 2018
1 parent bc53e68 commit b5a16fb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
Expand All @@ -24,6 +25,7 @@

import com.opengamma.strata.basics.date.BusinessDayConvention;
import com.opengamma.strata.basics.date.DayCount;
import com.opengamma.strata.basics.date.Tenor;
import com.opengamma.strata.basics.index.FxIndex;
import com.opengamma.strata.basics.index.IborIndex;
import com.opengamma.strata.basics.index.Index;
Expand Down Expand Up @@ -299,6 +301,44 @@ public static LocalTime parseTime(String str) {
}
}

//-------------------------------------------------------------------------
/**
* Parses a period from the input string.
* <p>
* It accepts the same formats as {@link Period}, but the "P" at the start is optional.
*
* @param str the string to parse
* @return the parsed value
* @throws IllegalArgumentException if the string cannot be parsed
*/
public static Period parsePeriod(String str) {
try {
String prefixed = str.startsWith("P") ? str : "P" + str;
return Period.parse(prefixed);

} catch (DateTimeParseException ex) {
throw new IllegalArgumentException("Unknown period format: " + str);
}
}

/**
* Parses a tenor from the input string.
* <p>
* A tenor cannot be zero or negative.
*
* @param str the string to parse
* @return the parsed value
* @throws IllegalArgumentException if the string cannot be parsed
*/
public static Tenor parseTenor(String str) {
try {
return Tenor.parse(str);

} catch (DateTimeParseException ex) {
throw new IllegalArgumentException("Unknown tenor format: " + str);
}
}

//-------------------------------------------------------------------------
/**
* Parses day count from the input string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.opengamma.strata.basics.date.BusinessDayConventions;
import com.opengamma.strata.basics.date.DayCount;
import com.opengamma.strata.basics.date.HolidayCalendarId;
import com.opengamma.strata.basics.date.Tenor;
import com.opengamma.strata.basics.index.IborIndex;
import com.opengamma.strata.collect.io.CsvRow;
import com.opengamma.strata.loader.LoaderUtils;
Expand Down Expand Up @@ -63,7 +62,7 @@ private static FraTrade parseRow(CsvRow row, TradeInfo info, TradeCsvInfoResolve
double notional = LoaderUtils.parseDouble(row.getValue(NOTIONAL_FIELD));
double fixedRate = LoaderUtils.parseDoublePercent(row.getValue(FIXED_RATE_FIELD));
Optional<FraConvention> conventionOpt = row.findValue(CONVENTION_FIELD).map(s -> FraConvention.of(s));
Optional<Period> periodToStartOpt = row.findValue(PERIOD_TO_START_FIELD).map(s -> Tenor.parse(s).getPeriod());
Optional<Period> periodToStartOpt = row.findValue(PERIOD_TO_START_FIELD).map(s -> LoaderUtils.parsePeriod(s));
Optional<LocalDate> startDateOpt = row.findValue(START_DATE_FIELD).map(s -> LoaderUtils.parseDate(s));
Optional<LocalDate> endDateOpt = row.findValue(END_DATE_FIELD).map(s -> LoaderUtils.parseDate(s));
Optional<IborIndex> indexOpt = row.findValue(INDEX_FIELD).map(s -> IborIndex.of(s));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ static SwapTrade parseWithConvention(CsvRow row, TradeInfo info, TradeCsvInfoRes
BuySell buySell = LoaderUtils.parseBuySell(row.getValue(BUY_SELL_FIELD));
double notional = LoaderUtils.parseDouble(row.getValue(NOTIONAL_FIELD));
double fixedRate = LoaderUtils.parseDoublePercent(row.getValue(FIXED_RATE_FIELD));
Optional<Period> periodToStartOpt = row.findValue(PERIOD_TO_START_FIELD).map(s -> Tenor.parse(s).getPeriod());
Optional<Tenor> tenorOpt = row.findValue(TENOR_FIELD).map(s -> Tenor.parse(s));
Optional<Period> periodToStartOpt = row.findValue(PERIOD_TO_START_FIELD).map(s -> LoaderUtils.parsePeriod(s));
Optional<Tenor> tenorOpt = row.findValue(TENOR_FIELD).map(s -> LoaderUtils.parseTenor(s));
Optional<LocalDate> startDateOpt = row.findValue(START_DATE_FIELD).map(s -> LoaderUtils.parseDate(s));
Optional<LocalDate> endDateOpt = row.findValue(END_DATE_FIELD).map(s -> LoaderUtils.parseDate(s));
Optional<RollConvention> rollCnvOpt = row.findValue(ROLL_CONVENTION_FIELD).map(s -> LoaderUtils.parseRollConvention(s));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.opengamma.strata.basics.date.BusinessDayConventions;
import com.opengamma.strata.basics.date.DayCount;
import com.opengamma.strata.basics.date.HolidayCalendarId;
import com.opengamma.strata.basics.date.Tenor;
import com.opengamma.strata.collect.io.CsvRow;
import com.opengamma.strata.loader.LoaderUtils;
import com.opengamma.strata.product.TradeInfo;
Expand Down Expand Up @@ -62,7 +61,7 @@ private static TermDepositTrade parseRow(CsvRow row, TradeInfo info, TradeCsvInf
double notional = LoaderUtils.parseDouble(row.getValue(NOTIONAL_FIELD));
double fixedRate = LoaderUtils.parseDoublePercent(row.getValue(FIXED_RATE_FIELD));
Optional<TermDepositConvention> conventionOpt = row.findValue(CONVENTION_FIELD).map(s -> TermDepositConvention.of(s));
Optional<Period> tenorOpt = row.findValue(TENOR_FIELD).map(s -> Tenor.parse(s).getPeriod());
Optional<Period> tenorOpt = row.findValue(TENOR_FIELD).map(s -> LoaderUtils.parseTenor(s).getPeriod());
Optional<LocalDate> startDateOpt = row.findValue(START_DATE_FIELD).map(s -> LoaderUtils.parseDate(s));
Optional<LocalDate> endDateOpt = row.findValue(END_DATE_FIELD).map(s -> LoaderUtils.parseDate(s));
Optional<Currency> currencyOpt = row.findValue(CURRENCY_FIELD).map(s -> Currency.parse(s));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
import java.time.YearMonth;

import org.testng.annotations.Test;

import com.opengamma.strata.basics.date.Tenor;
import com.opengamma.strata.basics.index.FxIndices;
import com.opengamma.strata.basics.index.IborIndices;
import com.opengamma.strata.basics.index.OvernightIndices;
Expand Down Expand Up @@ -119,6 +121,18 @@ public void test_parseTime() {
assertThrowsIllegalArg(() -> LoaderUtils.parseTime("Rubbish"));
}

public void test_parsePeriod() {
assertEquals(LoaderUtils.parsePeriod("P2D"), Period.ofDays(2));
assertEquals(LoaderUtils.parsePeriod("2D"), Period.ofDays(2));
assertThrowsIllegalArg(() -> LoaderUtils.parsePeriod("2"));
}

public void test_parseTenor() {
assertEquals(LoaderUtils.parseTenor("P2D"), Tenor.ofDays(2));
assertEquals(LoaderUtils.parseTenor("2D"), Tenor.ofDays(2));
assertThrowsIllegalArg(() -> LoaderUtils.parseTenor("2"));
}

public void test_parseBuySell() {
assertEquals(LoaderUtils.parseBuySell("BUY"), BuySell.BUY);
assertEquals(LoaderUtils.parseBuySell("Buy"), BuySell.BUY);
Expand Down

0 comments on commit b5a16fb

Please sign in to comment.