public virtual void test_other_Currency_same()
        {
            CurrencyPair test = CurrencyPair.of(GBP, GBP);

            assertEquals(test.other(GBP), GBP);
            assertThrows(typeof(System.ArgumentException), () => test.other(EUR));
        }
Exemple #2
0
        private void addCurrencyPair(Currency ccy1, Currency ccy2, double rate)
        {
            // Only resize if there's a danger we can't fit a new currency in
            if (rates.Length < currencies.size() + 1)
            {
                ensureCapacity(Stream.of(ccy1, ccy2));
            }

            if (!currencies.containsKey(ccy1) && !currencies.containsKey(ccy2))
            {
                // Neither currency present - add to disjoint set
                disjointRates[CurrencyPair.of(ccy1, ccy2)] = rate;
            }
            else if (currencies.containsKey(ccy1) && currencies.containsKey(ccy2))
            {
                // We already have a rate for this currency pair
                updateRate(ccy1, ccy2, rate);
            }
            else
            {
                // We have exactly one of the currencies already
                addNewRate(ccy1, ccy2, rate);

                // With a new rate added we may be able to handle the disjoint
                retryDisjoints();
            }
        }
        public virtual void test_equals_bad()
        {
            CurrencyPair test = CurrencyPair.of(AUD, GBP);

            assertFalse(test.Equals(ANOTHER_TYPE));
            assertFalse(test.Equals(null));
        }
        public virtual void addMultipleRates()
        {
            // Use linked map to force the order of evaluation
            // want to see that builder recovers when
            // encountering a currency pair for 2 unknown
            // currencies but which will appear later
            LinkedHashMap <CurrencyPair, double> rates = new LinkedHashMap <CurrencyPair, double>();

            rates.put(CurrencyPair.of(GBP, USD), 1.6);
            rates.put(CurrencyPair.of(EUR, USD), 1.4);
            rates.put(CurrencyPair.of(CHF, AUD), 1.2);     // Neither currency seen before
            rates.put(CurrencyPair.of(SEK, AUD), 0.16);    // AUD seen before but not added yet
            rates.put(CurrencyPair.of(JPY, CAD), 0.01);    // Neither currency seen before
            rates.put(CurrencyPair.of(EUR, CHF), 1.2);
            rates.put(CurrencyPair.of(JPY, USD), 0.0084);

            FxMatrix matrix = FxMatrix.builder().addRates(rates).build();

            assertThat(matrix.fxRate(GBP, USD)).isEqualTo(1.6);
            assertThat(matrix.fxRate(USD, GBP)).isEqualTo(1 / 1.6);
            assertThat(matrix.fxRate(EUR, USD)).isEqualTo(1.4);
            assertThat(matrix.fxRate(USD, EUR)).isEqualTo(1 / 1.4);
            assertThat(matrix.fxRate(EUR, GBP)).isEqualTo(1.4 / 1.6, TOL);
            assertThat(matrix.fxRate(GBP, EUR)).isEqualTo(1.6 / 1.4, TOL);
            assertThat(matrix.fxRate(EUR, CHF)).isEqualTo(1.2);
        }
        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_getAvailable()
        {
            ISet <CurrencyPair> available = CurrencyPair.AvailablePairs;

            assertTrue(available.Contains(CurrencyPair.of(EUR, USD)));
            assertTrue(available.Contains(CurrencyPair.of(EUR, GBP)));
            assertTrue(available.Contains(CurrencyPair.of(GBP, USD)));
        }
        public virtual void test_contains_Currency_same()
        {
            CurrencyPair test = CurrencyPair.of(GBP, GBP);

            assertEquals(test.contains(GBP), true);
            assertEquals(test.contains(USD), false);
            assertEquals(test.contains(EUR), false);
        }
        public virtual void canAddRateUsingCurrencyPair()
        {
            FxMatrix matrix = FxMatrix.builder().addRate(CurrencyPair.of(GBP, USD), 1.6).build();

            assertThat(matrix.Currencies).containsOnly(GBP, USD);
            assertThat(matrix.fxRate(GBP, USD)).isEqualTo(1.6);
            assertThat(matrix.fxRate(USD, GBP)).isEqualTo(0.625);
        }
        //-------------------------------------------------------------------------
        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_rateDigits()
 {
     assertEquals(CurrencyPair.of(GBP, USD).RateDigits, 4);
     assertEquals(CurrencyPair.of(USD, GBP).RateDigits, 4);
     assertEquals(CurrencyPair.of(BRL, GBP).RateDigits, 4);
     assertEquals(CurrencyPair.of(GBP, BRL).RateDigits, 4);
     assertEquals(CurrencyPair.of(BRL, BHD).RateDigits, 5);
     assertEquals(CurrencyPair.of(BHD, BRL).RateDigits, 5);
 }
        public virtual void test_of_CurrencyCurrency_same()
        {
            CurrencyPair test = CurrencyPair.of(USD, USD);

            assertEquals(test.Base, USD);
            assertEquals(test.Counter, USD);
            assertEquals(test.Identity, true);
            assertEquals(test.ToString(), "USD/USD");
        }
        public virtual void singleRateMatrixByOfCurrencyPairFactory()
        {
            FxMatrix matrix = FxMatrix.of(CurrencyPair.of(GBP, USD), 1.6);

            assertThat(matrix.Currencies).containsOnly(GBP, USD);
            assertThat(matrix.fxRate(GBP, USD)).isEqualTo(1.6);
            assertThat(matrix.fxRate(USD, GBP)).isEqualTo(0.625);
            assertThat(matrix.ToString()).isEqualTo("FxMatrix[GBP, USD : [1.0, 1.6],[0.625, 1.0]]");
        }
        public virtual void emptyMatrixCanHandleTrivialRate()
        {
            FxRateProvider test = (ccy1, ccy2) =>
            {
                return(2.5d);
            };

            assertThat(test.fxRate(CurrencyPair.of(GBP, USD))).isEqualTo(2.5d);
        }
        public virtual void addMultipleRatesContainingEntryWithNoCommonCurrency()
        {
            LinkedHashMap <CurrencyPair, double> rates = new LinkedHashMap <CurrencyPair, double>();

            rates.put(CurrencyPair.of(GBP, USD), 1.6);
            rates.put(CurrencyPair.of(EUR, USD), 1.4);
            rates.put(CurrencyPair.of(JPY, CAD), 0.01);     // Neither currency linked to one of the others

            assertThrows(() => FxMatrix.builder().addRates(rates).build(), typeof(System.InvalidOperationException));
        }
        //-------------------------------------------------------------------------
        public virtual void test_of_CurrencyCurrency()
        {
            CurrencyPair test = CurrencyPair.of(GBP, USD);

            assertEquals(test.Base, GBP);
            assertEquals(test.Counter, USD);
            assertEquals(test.Identity, false);
            assertEquals(test.toSet(), ImmutableSet.of(GBP, USD));
            assertEquals(test.ToString(), "GBP/USD");
        }
        public virtual void test_of_CurrencyCurrency_reverseStandardOrder()
        {
            CurrencyPair test = CurrencyPair.of(USD, GBP);

            assertEquals(test.Base, USD);
            assertEquals(test.Counter, GBP);
            assertEquals(test.Identity, false);
            assertEquals(test.toSet(), ImmutableSet.of(GBP, USD));
            assertEquals(test.ToString(), "USD/GBP");
        }
        public virtual void test_toConventional()
        {
            assertEquals(CurrencyPair.of(GBP, USD).toConventional(), CurrencyPair.of(GBP, USD));
            assertEquals(CurrencyPair.of(USD, GBP).toConventional(), CurrencyPair.of(GBP, USD));

            assertEquals(CurrencyPair.of(GBP, BRL).toConventional(), CurrencyPair.of(GBP, BRL));
            assertEquals(CurrencyPair.of(BRL, GBP).toConventional(), CurrencyPair.of(GBP, BRL));

            assertEquals(CurrencyPair.of(BHD, BRL).toConventional(), CurrencyPair.of(BHD, BRL));
            assertEquals(CurrencyPair.of(BRL, BHD).toConventional(), CurrencyPair.of(BHD, BRL));
        }
        //-------------------------------------------------------------------------
        public virtual void test_isInverse_CurrencyPair()
        {
            CurrencyPair test = CurrencyPair.of(GBP, USD);

            assertEquals(test.isInverse(test), false);
            assertEquals(test.isInverse(CurrencyPair.of(GBP, USD)), false);
            assertEquals(test.isInverse(CurrencyPair.of(USD, GBP)), true);
            assertEquals(test.isInverse(CurrencyPair.of(GBP, EUR)), false);
            assertEquals(test.isInverse(CurrencyPair.of(EUR, GBP)), false);
            assertEquals(test.isInverse(CurrencyPair.of(USD, EUR)), false);
            assertEquals(test.isInverse(CurrencyPair.of(EUR, USD)), false);
        }
 //-----------------------------------------------------------------------
 public virtual void test_isConventional()
 {
     assertEquals(CurrencyPair.of(GBP, USD).Conventional, true);
     assertEquals(CurrencyPair.of(USD, GBP).Conventional, false);
     // There is no configuration for GBP/BRL or BRL/GBP so the ordering list is used to choose a convention pair
     // GBP is in the currency order list and BRL isn't so GBP is the base
     assertEquals(CurrencyPair.of(GBP, BRL).Conventional, true);
     assertEquals(CurrencyPair.of(BRL, GBP).Conventional, false);
     // There is no configuration for BHD/BRL or BRL/BHD and neither are in the list specifying currency priority order.
     // Lexicographical ordering is used
     assertEquals(CurrencyPair.of(BHD, BRL).Conventional, true);
     assertEquals(CurrencyPair.of(BRL, BHD).Conventional, false);
     assertEquals(CurrencyPair.of(GBP, GBP).Conventional, true);
 }
        public virtual void addSimpleMultipleRates()
        {
            // Use linked to force the order of evaluation
            // want to see that builder recovers when
            // encountering a currency pair for 2 unknown
            // currencies but which will appear later
            LinkedHashMap <CurrencyPair, double> rates = new LinkedHashMap <CurrencyPair, double>();

            rates.put(CurrencyPair.of(GBP, USD), 1.6);
            rates.put(CurrencyPair.of(EUR, USD), 1.4);

            FxMatrix matrix = FxMatrix.builder().addRates(rates).build();

            assertThat(matrix.fxRate(GBP, USD)).isEqualTo(1.6);
            assertThat(matrix.fxRate(USD, GBP)).isEqualTo(1 / 1.6);
            assertThat(matrix.fxRate(EUR, USD)).isEqualTo(1.4);
            assertThat(matrix.fxRate(USD, EUR)).isEqualTo(1 / 1.4);
            assertThat(matrix.fxRate(EUR, GBP)).isEqualTo(1.4 / 1.6, TOL);
            assertThat(matrix.fxRate(GBP, EUR)).isEqualTo(1.6 / 1.4, TOL);
        }
Exemple #22
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// Parses a rate from a string with format AAA/BBB RATE.
        /// <para>
        /// The parsed format is '${baseCurrency}/${counterCurrency} ${rate}'.
        /// Currency parsing is case insensitive.
        ///
        /// </para>
        /// </summary>
        /// <param name="rateStr">  the rate as a string AAA/BBB RATE </param>
        /// <returns> the FX rate </returns>
        /// <exception cref="IllegalArgumentException"> if the FX rate cannot be parsed </exception>
        public static FxRate parse(string rateStr)
        {
            ArgChecker.notNull(rateStr, "rateStr");
            Matcher matcher = REGEX_FORMAT.matcher(rateStr.ToUpper(Locale.ENGLISH));

            if (!matcher.matches())
            {
                throw new System.ArgumentException("Invalid rate: " + rateStr);
            }
            try
            {
                Currency @base   = Currency.parse(matcher.group(1));
                Currency counter = Currency.parse(matcher.group(2));
                double   rate    = double.Parse(matcher.group(3));
                return(new FxRate(CurrencyPair.of(@base, counter), rate));
            }
            catch (Exception ex)
            {
                throw new System.ArgumentException("Unable to parse rate: " + rateStr, ex);
            }
        }
        //-------------------------------------------------------------------------
        public virtual void test_equals_hashCode()
        {
            CurrencyPair a1 = CurrencyPair.of(AUD, GBP);
            CurrencyPair a2 = CurrencyPair.of(AUD, GBP);
            CurrencyPair b  = CurrencyPair.of(USD, GBP);
            CurrencyPair c  = CurrencyPair.of(USD, EUR);

            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());
        }
        //-------------------------------------------------------------------------
        public virtual 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), null);
            assertEquals(gbpUsd.cross(usdGbp), null);
            assertEquals(gbpGbp.cross(gbpUsd), null);
            assertEquals(gbpUsd.cross(gbpGbp), null);

            assertEquals(gbpUsd.cross(usdEur), eurGbp);
            assertEquals(gbpUsd.cross(eurUsd), eurGbp);
            assertEquals(usdGbp.cross(usdEur), eurGbp);
            assertEquals(usdGbp.cross(eurUsd), eurGbp);

            assertEquals(usdEur.cross(gbpUsd), eurGbp);
            assertEquals(usdEur.cross(usdGbp), eurGbp);
            assertEquals(eurUsd.cross(gbpUsd), eurGbp);
            assertEquals(eurUsd.cross(usdGbp), eurGbp);
        }
 public virtual void test_of_CurrencyCurrency_null()
 {
     assertThrowsIllegalArg(() => CurrencyPair.of(null, USD));
     assertThrowsIllegalArg(() => CurrencyPair.of(USD, null));
     assertThrowsIllegalArg(() => CurrencyPair.of(null, null));
 }
 //-----------------------------------------------------------------------
 public virtual void test_serialization()
 {
     assertSerialization(CurrencyPair.of(GBP, USD));
     assertSerialization(CurrencyPair.of(GBP, GBP));
 }
        public virtual void test_isInverse_CurrencyPair_null()
        {
            CurrencyPair test = CurrencyPair.of(GBP, USD);

            assertThrowsIllegalArg(() => test.isInverse(null));
        }
 public virtual void test_jodaConvert()
 {
     assertJodaConvert(typeof(CurrencyPair), CurrencyPair.of(GBP, USD));
     assertJodaConvert(typeof(CurrencyPair), CurrencyPair.of(GBP, GBP));
 }
Exemple #29
0
 //-------------------------------------------------------------------------
 /// <summary>
 /// Obtains an instance from two currencies.
 /// <para>
 /// The first currency is the base and the second is the counter.
 /// The two currencies may be the same, but if they are then the rate must be one.
 ///
 /// </para>
 /// </summary>
 /// <param name="base">  the base currency </param>
 /// <param name="counter">  the counter currency </param>
 /// <param name="rate">  the conversion rate, greater than zero </param>
 /// <returns> the FX rate </returns>
 /// <exception cref="IllegalArgumentException"> if the rate is invalid </exception>
 public static FxRate of(Currency @base, Currency counter, double rate)
 {
     return(new FxRate(CurrencyPair.of(@base, counter), rate));
 }
        public virtual void test_cross_CurrencyPair_null()
        {
            CurrencyPair test = CurrencyPair.of(GBP, USD);

            assertThrowsIllegalArg(() => test.cross(null));
        }