FXMatrix implements Serializable { public void addCurrency(final Currency ccyToAdd, final Currency ccyReference, final double fxRate) { ArgumentChecker.notNull(ccyToAdd, "Currency to add to the FX matrix should not be null"); ArgumentChecker.notNull(ccyReference, "Reference currency should not be null"); ArgumentChecker.isTrue(!ccyToAdd.equals(ccyReference), "Currencies should be different"); if (_nbCurrencies == 0) { _currencies.put(ccyReference, 0); _currencies.put(ccyToAdd, 1); _fxRates = new double[2][2]; _fxRates[0][0] = 1.0; _fxRates[1][1] = 1.0; _fxRates[1][0] = fxRate; _fxRates[0][1] = 1.0 / fxRate; _nbCurrencies = 2; } else { ArgumentChecker.isTrue(_currencies.containsKey(ccyReference), "Reference currency {} not in the FX matrix", ccyReference); ArgumentChecker.isTrue(!_currencies.containsKey(ccyToAdd), "New currency {} already in the FX matrix", ccyToAdd); _currencies.put(ccyToAdd, _nbCurrencies); _nbCurrencies++; final double[][] fxRatesNew = new double[_nbCurrencies][_nbCurrencies]; for (int loopccy = 0; loopccy < _nbCurrencies - 1; loopccy++) { System.arraycopy(_fxRates[loopccy], 0, fxRatesNew[loopccy], 0, _nbCurrencies - 1); } fxRatesNew[_nbCurrencies - 1][_nbCurrencies - 1] = 1.0; final int indexRef = _currencies.get(ccyReference); for (int loopccy = 0; loopccy < _nbCurrencies - 1; loopccy++) { fxRatesNew[_nbCurrencies - 1][loopccy] = fxRate * _fxRates[indexRef][loopccy]; fxRatesNew[loopccy][_nbCurrencies - 1] = 1.0 / fxRatesNew[_nbCurrencies - 1][loopccy]; } _fxRates = fxRatesNew; } } FXMatrix();  FXMatrix(final Currency ccy);  FXMatrix(final Currency ccy1, final Currency ccy2, final double fxRate);  FXMatrix(final Map<Currency, Integer> currencies, final double[][] fxRates);  FXMatrix(final FXMatrix fxMatrix); void addCurrency(final Currency ccyToAdd, final Currency ccyReference, final double fxRate); double getFxRate(final Currency ccy1, final Currency ccy2); boolean containsPair(final Currency ccy1, final Currency ccy2); CurrencyAmount convert(final MultipleCurrencyAmount amount, final Currency ccy); void updateRates(final Currency ccyToUpdate, final Currency ccyReference, final double fxRate); Map<Currency, Integer> getCurrencies(); double[][] getRates(); int getNumberOfCurrencies(); @Override String toString(); @Override int hashCode(); @Override boolean equals(final Object obj);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullCurrency1() { final FXMatrix fxMatrix = new FXMatrix(); fxMatrix.addCurrency(null, USD, EUR_USD); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullCurrency2() { final FXMatrix fxMatrix = new FXMatrix(); fxMatrix.addCurrency(EUR, null, EUR_USD); }
@Test(expectedExceptions = IllegalArgumentException.class) public void sameCurrency() { final FXMatrix fxMatrix = new FXMatrix(); fxMatrix.addCurrency(EUR, EUR, EUR_USD); }
@Test(expectedExceptions = IllegalArgumentException.class) public void currencyAlreadyPresent() { final FXMatrix fxMatrix = new FXMatrix(); fxMatrix.addCurrency(EUR, USD, EUR_USD); fxMatrix.addCurrency(GBP, EUR, GBP_EUR); fxMatrix.addCurrency(GBP, USD, 1.234); }
@Test public void mergeWithItself() { final FXMatrix fxMatrix = new FXMatrix(); fxMatrix.addCurrency(EUR, USD, EUR_USD); fxMatrix.addCurrency(GBP, EUR, GBP_EUR); final FXMatrix merged = FXMatrixUtils.merge(fxMatrix, fxMatrix); assertEquals("FXMatrixUtils - merge", fxMatrix, merged); }
@Test public void merge1() { final FXMatrix fxMatrixEURUSD = new FXMatrix(); fxMatrixEURUSD.addCurrency(EUR, USD, EUR_USD); assertTrue("FXMatrixUtils - merge", FXMatrixUtils.compare(fxMatrixEURUSD, FXMatrixUtils.merge(fxMatrixEURUSD, new FXMatrix(USD)), TOLERANCE_RATE)); assertTrue("FXMatrixUtils - merge", FXMatrixUtils.compare(fxMatrixEURUSD, FXMatrixUtils.merge(fxMatrixEURUSD, new FXMatrix(EUR)), TOLERANCE_RATE)); assertTrue("FXMatrixUtils - merge", FXMatrixUtils.compare(fxMatrixEURUSD, FXMatrixUtils.merge(new FXMatrix(USD), fxMatrixEURUSD), TOLERANCE_RATE)); assertTrue("FXMatrixUtils - merge", FXMatrixUtils.compare(fxMatrixEURUSD, FXMatrixUtils.merge(new FXMatrix(EUR), fxMatrixEURUSD), TOLERANCE_RATE)); final FXMatrix fxMatrixGBPEUR = new FXMatrix(); fxMatrixGBPEUR.addCurrency(GBP, EUR, GBP_EUR); final FXMatrix fxMatrixEURUSDGBP = new FXMatrix(); fxMatrixEURUSDGBP.addCurrency(EUR, USD, EUR_USD); fxMatrixEURUSDGBP.addCurrency(GBP, EUR, GBP_EUR); final FXMatrix merged = FXMatrixUtils.merge(fxMatrixEURUSD, fxMatrixGBPEUR); assertTrue("FXMatrixUtils - merge", FXMatrixUtils.compare(merged, fxMatrixEURUSDGBP, TOLERANCE_RATE)); }
@Test public void merge3() { final FXMatrix fxMatrix1 = new FXMatrix(); fxMatrix1.addCurrency(EUR, USD, EUR_USD); fxMatrix1.addCurrency(KRW, USD, 1.0 / USD_KRW); assertTrue("FXMatrixUtils - merge", FXMatrixUtils.compare(fxMatrix1, FXMatrixUtils.merge(fxMatrix1, new FXMatrix()), TOLERANCE_RATE)); assertTrue("FXMatrixUtils - merge", FXMatrixUtils.compare(fxMatrix1, FXMatrixUtils.merge(new FXMatrix(), fxMatrix1), TOLERANCE_RATE)); assertTrue("FXMatrixUtils - merge", FXMatrixUtils.compare(fxMatrix1, FXMatrixUtils.merge(new FXMatrix(USD), fxMatrix1), TOLERANCE_RATE)); assertTrue("FXMatrixUtils - merge", FXMatrixUtils.compare(new FXMatrix(), FXMatrixUtils.merge(new FXMatrix(), new FXMatrix()), TOLERANCE_RATE)); }
@Test public void compare() { final FXMatrix fxMatrix1 = new FXMatrix(); fxMatrix1.addCurrency(GBP, EUR, GBP_EUR); fxMatrix1.addCurrency(USD, EUR, 1.0d / EUR_USD); assertTrue("FXMatrixUtils - compare", FXMatrixUtils.compare(fxMatrix1, fxMatrix1, TOLERANCE_RATE)); final FXMatrix fxMatrix2 = new FXMatrix(); fxMatrix2.addCurrency(EUR, USD, EUR_USD); fxMatrix2.addCurrency(GBP, EUR, GBP_EUR); assertTrue("FXMatrixUtils - compare", FXMatrixUtils.compare(fxMatrix1, fxMatrix2, TOLERANCE_RATE)); final FXMatrix fxMatrix3 = new FXMatrix(); fxMatrix3.addCurrency(EUR, USD, EUR_USD + 1.0E-5); fxMatrix3.addCurrency(GBP, EUR, GBP_EUR); assertFalse("FXMatrixUtils - compare", FXMatrixUtils.compare(fxMatrix1, fxMatrix3, TOLERANCE_RATE)); final FXMatrix fxMatrix4 = new FXMatrix(); fxMatrix4.addCurrency(GBP, EUR, GBP_EUR); fxMatrix4.addCurrency(USD, EUR, 1.0d / EUR_USD); fxMatrix4.addCurrency(KRW, USD, 1.0 / USD_KRW); assertFalse("FXMatrixUtils - compare", FXMatrixUtils.compare(fxMatrix1, fxMatrix4, TOLERANCE_RATE)); }