//------------------------------------------------------------------------- public virtual void test_crossRate() { FxRate gbpUsd = FxRate.of(GBP, USD, 5d / 4d); FxRate usdGbp = FxRate.of(USD, GBP, 4d / 5d); FxRate eurUsd = FxRate.of(EUR, USD, 8d / 7d); FxRate usdEur = FxRate.of(USD, EUR, 7d / 8d); FxRate eurGbp = FxRate.of(EUR, GBP, (8d / 7d) * (4d / 5d)); FxRate gbpGbp = FxRate.of(GBP, GBP, 1d); FxRate usdUsd = FxRate.of(USD, USD, 1d); assertEquals(eurUsd.crossRate(usdGbp), eurGbp); assertEquals(eurUsd.crossRate(gbpUsd), eurGbp); assertEquals(usdEur.crossRate(usdGbp), eurGbp); assertEquals(usdEur.crossRate(gbpUsd), eurGbp); assertEquals(gbpUsd.crossRate(usdEur), eurGbp); assertEquals(gbpUsd.crossRate(eurUsd), eurGbp); assertEquals(usdGbp.crossRate(usdEur), eurGbp); assertEquals(usdGbp.crossRate(eurUsd), eurGbp); assertThrowsIllegalArg(() => gbpGbp.crossRate(gbpUsd)); // identity assertThrowsIllegalArg(() => usdUsd.crossRate(gbpUsd)); // identity assertThrowsIllegalArg(() => gbpUsd.crossRate(gbpUsd)); // same currencies assertThrowsIllegalArg(() => gbpUsd.crossRate(usdGbp)); // same currencies assertThrowsIllegalArg(() => gbpUsd.crossRate(FxRate.of(EUR, CAD, 12d / 5d))); // no common currency }
public virtual void test_equals_bad() { FxRate test = FxRate.of(AUD, GBP, 1.25d); assertFalse(test.Equals(ANOTHER_TYPE)); assertFalse(test.Equals(null)); }
//------------------------------------------------------------------------- public virtual void test_fxRate_forBase() { FxRate test = FxRate.of(GBP, USD, 1.25d); assertEquals(test.fxRate(GBP, USD), 1.25d); assertEquals(test.fxRate(USD, GBP), 1d / 1.25d); assertThrowsIllegalArg(() => test.fxRate(GBP, AUD)); }
//------------------------------------------------------------------------- public virtual void test_of_CurrencyPairDouble() { FxRate test = FxRate.of(CurrencyPair.of(GBP, USD), 1.5d); assertEquals(test.Pair, CurrencyPair.of(GBP, USD)); assertEquals(test.fxRate(GBP, USD), 1.5d, 0); assertEquals(test.ToString(), "GBP/USD 1.5"); }
public virtual void test_of_CurrencyPairDouble_reverseStandardOrder() { FxRate test = FxRate.of(CurrencyPair.of(USD, GBP), 0.8d); assertEquals(test.Pair, CurrencyPair.of(USD, GBP)); assertEquals(test.fxRate(USD, GBP), 0.8d, 0); assertEquals(test.ToString(), "USD/GBP 0.8"); }
public virtual void test_of_CurrencyPairDouble_same() { FxRate test = FxRate.of(CurrencyPair.of(USD, USD), 1d); assertEquals(test.Pair, CurrencyPair.of(USD, USD)); assertEquals(test.fxRate(USD, USD), 1d, 0); assertEquals(test.ToString(), "USD/USD 1"); }
public virtual void test_convertedTo_missingFxRate() { DoubleArray values = DoubleArray.of(1, 2, 3); CurrencyAmountArray test = CurrencyAmountArray.of(GBP, values); FxRate fxRate = FxRate.of(EUR, USD, 1.61); assertThrows(() => test.convertedTo(USD, fxRate), typeof(System.ArgumentException)); }
//------------------------------------------------------------------------- public virtual void test_convert() { FxRate test = FxRate.of(GBP, USD, 1.25d); assertEquals(test.convert(100, GBP, USD), 125d); assertEquals(test.convert(100, USD, GBP), 100d / 1.25d); assertThrowsIllegalArg(() => test.convert(100, GBP, AUD)); }
public virtual void test_convertedTo_noConversionNecessary() { DoubleArray values = DoubleArray.of(1, 2, 3); CurrencyAmountArray test = CurrencyAmountArray.of(GBP, values); FxRate fxRate = FxRate.of(GBP, USD, 1.61); CurrencyAmountArray convertedList = test.convertedTo(GBP, fxRate); assertThat(convertedList).isEqualTo(test); }
//------------------------------------------------------------------------- public virtual void test_convertedTo() { DoubleArray values = DoubleArray.of(1, 2, 3); CurrencyAmountArray test = CurrencyAmountArray.of(GBP, values); FxRate fxRate = FxRate.of(GBP, USD, 1.61); CurrencyAmountArray convertedList = test.convertedTo(USD, fxRate); DoubleArray expectedValues = DoubleArray.of(1 * 1.61, 2 * 1.61, 3 * 1.61); CurrencyAmountArray expectedList = CurrencyAmountArray.of(USD, expectedValues); assertThat(convertedList).isEqualTo(expectedList); }
//----------------------------------------------------------------------- public override bool Equals(object obj) { if (obj == this) { return(true); } if (obj != null && obj.GetType() == this.GetType()) { FxRate other = (FxRate)obj; return(JodaBeanUtils.equal(pair, other.pair) && JodaBeanUtils.equal(rate, other.rate)); } return(false); }
// computes the cross rate private static FxRate computeCross(FxRate fx1, FxRate fx2, CurrencyPair crossPairAC) { // aim is to convert AAA/BBB and BBB/CCC to AAA/CCC Currency currA = crossPairAC.Base; Currency currC = crossPairAC.Counter; // given the conventional cross rate pair, order the two rates to match bool crossBaseCurrencyInFx1 = fx1.pair.contains(currA); FxRate fxABorBA = crossBaseCurrencyInFx1 ? fx1 : fx2; FxRate fxBCorCB = crossBaseCurrencyInFx1 ? fx2 : fx1; // extract the rates, taking the inverse if the pair is in the inverse order double rateAB = fxABorBA.Pair.Base.Equals(currA) ? fxABorBA.rate : 1d / fxABorBA.rate; double rateBC = fxBCorCB.Pair.Counter.Equals(currC) ? fxBCorCB.rate : 1d / fxBCorCB.rate; return(FxRate.of(crossPairAC, rateAB * rateBC)); }
//------------------------------------------------------------------------- public virtual void test_equals_hashCode() { FxRate a1 = FxRate.of(AUD, GBP, 1.25d); FxRate a2 = FxRate.of(AUD, GBP, 1.25d); FxRate b = FxRate.of(USD, GBP, 1.25d); FxRate c = FxRate.of(USD, GBP, 1.35d); assertEquals(a1.Equals(a1), true); assertEquals(a1.Equals(a2), true); assertEquals(a1.Equals(b), false); assertEquals(a1.Equals(c), false); assertEquals(b.Equals(a1), false); assertEquals(b.Equals(a2), false); assertEquals(b.Equals(b), true); assertEquals(b.Equals(c), false); assertEquals(c.Equals(a1), false); assertEquals(c.Equals(a2), false); assertEquals(c.Equals(b), false); assertEquals(c.Equals(c), true); assertEquals(a1.GetHashCode(), a2.GetHashCode()); }
/// <summary> /// Returns an FX rate object representing the market convention rate between the two currencies. /// <para> /// If the currency pair is the market convention pair, this method returns {@code this}, otherwise /// it returns an {@code FxRate} with the inverse currency pair and reciprocal rate. /// /// </para> /// </summary> /// <returns> an FX rate object representing the market convention rate between the two currencies </returns> public FxRate toConventional() { return(pair.Conventional ? this : FxRate.of(pair.toConventional(), 1 / rate)); }
/// <summary> /// Derives an FX rate from two related FX rates. /// <para> /// 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. /// </para> /// <para> /// 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> /// /// </para> /// </summary> /// <param name="other"> the other rates </param> /// <returns> a set of FX rates derived from these rates and the other rates </returns> /// <exception cref="IllegalArgumentException"> if the cross rate cannot be calculated </exception> public FxRate crossRate(FxRate other) { return(pair.cross(other.pair).map(cross => computeCross(this, other, cross)).orElseThrow(() => new System.ArgumentException(Messages.format("Unable to cross when no unique common currency: {} and {}", pair, other.pair)))); }
public virtual void test_toConventional() { assertEquals(FxRate.of(GBP, USD, 1.25), FxRate.of(USD, GBP, 0.8).toConventional()); assertEquals(FxRate.of(GBP, USD, 1.25), FxRate.of(GBP, USD, 1.25).toConventional()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test(dataProvider = "parseGood") public void test_parse_String_good(String input, Currency super, Currency counter, double rate) public virtual void test_parse_String_good(string input, Currency @base, Currency counter, double rate) { assertEquals(FxRate.parse(input), FxRate.of(@base, counter, rate)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test(dataProvider = "parseBad", expectedExceptions = IllegalArgumentException.class) public void test_parse_String_bad(String input) public virtual void test_parse_String_bad(string input) { FxRate.parse(input); }
public virtual void test_of_CurrencyCurrencyDouble_null() { assertThrowsIllegalArg(() => FxRate.of(null, USD, 1.5d)); assertThrowsIllegalArg(() => FxRate.of(USD, null, 1.5d)); assertThrowsIllegalArg(() => FxRate.of(null, null, 1.5d)); }
public virtual void test_of_CurrencyCurrencyDouble_invalid() { assertThrowsIllegalArg(() => FxRate.of(GBP, USD, -1.5d)); assertThrowsIllegalArg(() => FxRate.of(GBP, GBP, 2d)); }
//----------------------------------------------------------------------- public virtual void coverage() { coverImmutableBean(FxRate.of(GBP, USD, 1.25d)); }
//----------------------------------------------------------------------- public virtual void test_serialization() { assertSerialization(FxRate.of(GBP, USD, 1.25d)); assertSerialization(FxRate.of(GBP, GBP, 1d)); }
//------------------------------------------------------------------------- public virtual void test_inverse() { FxRate test = FxRate.of(GBP, USD, 1.25d); assertEquals(test.inverse(), FxRate.of(USD, GBP, 0.8d)); }
public virtual void test_inverse_same() { FxRate test = FxRate.of(GBP, GBP, 1d); assertEquals(test.inverse(), FxRate.of(GBP, GBP, 1d)); }