/** * Derives a set of FX rates from these rates and another set of rates. * <p> * For example, given rates for EUR/GBP and EUR/CHF it is possible to derive rates for GBP/CHF. * <p> * There must be exactly one currency in common between the two currency pairs and * each pair must contain two different currencies. The other rates must have the same scenario count * as these rates. * <p> * The returned object contains rates for converting between the two currencies which only appear in * one set of rates. * * @param other the other rates * @return a set of FX rates derived from these rates and the other rates */ public FxRateScenarioArray crossRates(FxRateScenarioArray other) { return pair.cross(other.pair).map(cross -> computeCross(other, cross)) .orElseThrow(() -> new IllegalArgumentException(Messages.format( "Unable to cross when no unique common currency: {} and {}", pair, other.pair))); }
/** * Derives an FX rate from two related FX rates. * <p> * Given two FX rates it is possible to derive another rate if they have a currency in common. * For example, given rates for EUR/GBP and EUR/CHF it is possible to derive rates for GBP/CHF. * The result will always have a currency pair in the conventional order. * <p> * The cross is only returned if the two pairs contains three currencies in total. * If the inputs are invalid, an exception is thrown. * <ul> * <li>AAA/BBB and BBB/CCC - valid, producing AAA/CCC * <li>AAA/BBB and CCC/BBB - valid, producing AAA/CCC * <li>AAA/BBB and BBB/AAA - invalid, exception thrown * <li>AAA/BBB and BBB/BBB - invalid, exception thrown * <li>AAA/BBB and CCC/DDD - invalid, exception thrown * </ul> * * @param other the other rates * @return a set of FX rates derived from these rates and the other rates * @throws IllegalArgumentException if the cross rate cannot be calculated */ public FxRate crossRate(FxRate other) { return pair.cross(other.pair).map(cross -> computeCross(this, other, cross)) .orElseThrow(() -> new IllegalArgumentException(Messages.format( "Unable to cross when no unique common currency: {} and {}", pair, other.pair))); }
public void test_cross_CurrencyPair() { CurrencyPair gbpGbp = CurrencyPair.of(GBP, GBP); CurrencyPair gbpUsd = CurrencyPair.of(GBP, USD); CurrencyPair usdGbp = CurrencyPair.of(USD, GBP); CurrencyPair eurGbp = CurrencyPair.of(EUR, GBP); CurrencyPair eurUsd = CurrencyPair.of(EUR, USD); CurrencyPair usdEur = CurrencyPair.of(USD, EUR); assertEquals(gbpUsd.cross(gbpUsd), Optional.empty()); assertEquals(gbpUsd.cross(usdGbp), Optional.empty()); assertEquals(gbpGbp.cross(gbpUsd), Optional.empty()); assertEquals(gbpUsd.cross(gbpGbp), Optional.empty()); assertEquals(gbpUsd.cross(usdEur), Optional.of(eurGbp)); assertEquals(gbpUsd.cross(eurUsd), Optional.of(eurGbp)); assertEquals(usdGbp.cross(usdEur), Optional.of(eurGbp)); assertEquals(usdGbp.cross(eurUsd), Optional.of(eurGbp)); assertEquals(usdEur.cross(gbpUsd), Optional.of(eurGbp)); assertEquals(usdEur.cross(usdGbp), Optional.of(eurGbp)); assertEquals(eurUsd.cross(gbpUsd), Optional.of(eurGbp)); assertEquals(eurUsd.cross(usdGbp), Optional.of(eurGbp)); }
public void test_cross_CurrencyPair_null() { CurrencyPair test = CurrencyPair.of(GBP, USD); assertThrowsIllegalArg(() -> test.cross(null)); }