public void test_eventsPerYearEstimate_bad() { assertEquals(Frequency.ofDays(3).eventsPerYearEstimate(), 364d / 3, 1e-8); assertEquals(Frequency.ofWeeks(3).eventsPerYearEstimate(), 364d / 21, 1e-8); assertEquals(Frequency.ofWeeks(104).eventsPerYearEstimate(), 364d / 728, 1e-8); assertEquals(Frequency.ofMonths(5).eventsPerYearEstimate(), 12d / 5, 1e-8); assertEquals(Frequency.ofMonths(22).eventsPerYearEstimate(), 12d / 22, 1e-8); assertEquals(Frequency.ofMonths(24).eventsPerYearEstimate(), 12d / 24, 1e-8); assertEquals(Frequency.ofYears(2).eventsPerYearEstimate(), 0.5d, 1e-8); assertEquals(Frequency.of(Period.of(10, 0, 1)).eventsPerYearEstimate(), 0.1d, 1e-3); assertEquals(Frequency.of(Period.of(5, 0, 95)).eventsPerYearEstimate(), 0.19d, 1e-3); assertEquals(Frequency.of(Period.of(5, 0, 97)).eventsPerYearEstimate(), 0.19d, 1e-3); }
/** * Obtains an instance backed by a period of days. * <p> * If the number of days is an exact multiple of 7 it will be converted to weeks. * * @param days the number of days * @return the periodic frequency * @throws IllegalArgumentException if days is negative or zero */ public static Frequency ofDays(int days) { if (days % 7 == 0) { return ofWeeks(days / 7); } return new Frequency(Period.ofDays(days)); }
/** * Obtains an instance backed by a period of years. * * @param years the number of years * @return the periodic frequency * @throws IllegalArgumentException if years is negative, zero or over 1,000 */ public static Frequency ofYears(int years) { if (years > MAX_YEARS) { throw new IllegalArgumentException(maxYearMsg()); } return new Frequency(Period.ofYears(years)); }
/** * Normalizes the months and years of this tenor. * <p> * This method returns a tenor of an equivalent length but with any number * of months greater than 12 normalized into a combination of months and years. * * @return the normalized tenor */ public Frequency normalized() { Period norm = period.normalized(); return (norm != period ? Frequency.of(norm) : this); }
public void test_eventsPerYear_bad() { assertThrowsIllegalArg(() -> Frequency.ofDays(3).eventsPerYear()); assertThrowsIllegalArg(() -> Frequency.ofWeeks(3).eventsPerYear()); assertThrowsIllegalArg(() -> Frequency.ofWeeks(104).eventsPerYear()); assertThrowsIllegalArg(() -> Frequency.ofMonths(5).eventsPerYear()); assertThrowsIllegalArg(() -> Frequency.ofMonths(24).eventsPerYear()); assertThrowsIllegalArg(() -> Frequency.of(Period.of(2, 2, 2)).eventsPerYear()); }
@Test(dataProvider = "events") public void test_eventsPerYear(Frequency test, int expected) { assertEquals(test.eventsPerYear(), expected); }
@Test(dataProvider = "normalized") public void test_normalized(Period period, Period normalized) { assertEquals(Frequency.of(period).normalized().getPeriod(), normalized); }
private static FloatingRateIndex parseIndex(CsvRow row, String leg) { Optional<String> fixedRateOpt = findValue(row, leg, FIXED_RATE_FIELD); Optional<String> indexOpt = findValue(row, leg, INDEX_FIELD); if (fixedRateOpt.isPresent()) { if (indexOpt.isPresent()) { throw new IllegalArgumentException( "Swap leg must not define both '" + leg + FIXED_RATE_FIELD + "' and '" + leg + INDEX_FIELD + "'"); } return null; } if (!indexOpt.isPresent()) { throw new IllegalArgumentException( "Swap leg must define either '" + leg + FIXED_RATE_FIELD + "' or '" + leg + INDEX_FIELD + "'"); } // use FloatingRateName to identify Ibor vs other String indexStr = indexOpt.get(); FloatingRateName frn = FloatingRateName.parse(indexStr); if (frn.getType() == FloatingRateType.IBOR) { // re-parse Ibor using tenor, which ensures tenor picked up from indexStr if present Frequency freq = Frequency.parse(getValue(row, leg, FREQUENCY_FIELD)); Tenor iborTenor = freq.isTerm() ? frn.getDefaultTenor() : Tenor.of(freq.getPeriod()); return FloatingRateIndex.parse(indexStr, iborTenor); } return frn.toFloatingRateIndex(); }
public void test_of_tooBig() { assertThrowsIllegalArg(() -> Frequency.of(Period.ofMonths(12001))); assertThrowsIllegalArg(() -> Frequency.of(Period.ofMonths(Integer.MAX_VALUE))); assertThrowsIllegalArg(() -> Frequency.of(Period.ofYears(1001))); assertThrowsIllegalArg(() -> Frequency.of(Period.ofYears(Integer.MAX_VALUE))); assertThrowsIllegalArg(() -> Frequency.ofMonths(12001), "Months must not exceed 12,000"); assertThrowsIllegalArg(() -> Frequency.ofMonths(Integer.MAX_VALUE)); assertThrowsIllegalArg(() -> Frequency.ofYears(1001), "Years must not exceed 1,000"); assertThrowsIllegalArg(() -> Frequency.ofYears(Integer.MAX_VALUE)); assertThrowsIllegalArg(() -> Frequency.of(Period.of(10000, 0, 1))); }
public void test_exactDivide_bad() { assertThrowsIllegalArg(() -> Frequency.ofDays(5).exactDivide(Frequency.ofDays(2))); assertThrowsIllegalArg(() -> Frequency.ofMonths(5).exactDivide(Frequency.ofMonths(2))); assertThrowsIllegalArg(() -> Frequency.P1M.exactDivide(Frequency.P1W)); assertThrowsIllegalArg(() -> Frequency.P1W.exactDivide(Frequency.P1M)); assertThrowsIllegalArg(() -> Frequency.TERM.exactDivide(Frequency.P1W)); assertThrowsIllegalArg(() -> Frequency.P12M.exactDivide(Frequency.TERM)); assertThrowsIllegalArg(() -> Frequency.ofYears(1).exactDivide(Frequency.P1W)); }
/** * Converts an FpML frequency string to a {@code Frequency}. * * @param multiplier the multiplier * @param unit the unit * @return the frequency * @throws IllegalArgumentException if the frequency is not known */ public Frequency convertFrequency(String multiplier, String unit) { String periodStr = multiplier + unit; Frequency frequency = FREQUENCY_MAP.get(periodStr); return frequency != null ? frequency : Frequency.parse(periodStr); }
@Test(dataProvider = "exactDivide") public void test_exactDivide_reverse(Frequency test, Frequency other, int expected) { if (!test.equals(other)) { assertThrowsIllegalArg(() -> other.exactDivide(test)); } }
public void test_equals_hashCode() { Frequency a1 = P1D; Frequency a2 = Frequency.ofDays(1); Frequency b = P3M; assertEquals(a1.equals(a1), true); assertEquals(a1.equals(b), false); assertEquals(a1.equals(a2), true); assertEquals(a2.equals(a1), true); assertEquals(a2.equals(a2), true); assertEquals(a2.equals(b), false); assertEquals(b.equals(a1), false); assertEquals(b.equals(a2), false); assertEquals(b.equals(b), true); assertEquals(a1.hashCode(), a2.hashCode()); }
if (isMonthBased() && other.isMonthBased()) { long paymentMonths = getPeriod().toTotalMonths(); long accrualMonths = other.getPeriod().toTotalMonths(); if ((paymentMonths % accrualMonths) == 0) { return Math.toIntExact(paymentMonths / accrualMonths); long paymentDays = getPeriod().getDays(); long accrualDays = other.getPeriod().getDays(); if ((paymentDays % accrualDays) == 0) { return Math.toIntExact(paymentDays / accrualDays);