/** * Checks if this currency pair is an identity pair. * <p> * The identity pair is one where the base and counter currency are the same.. * * @return true if this pair is an identity pair */ public boolean isIdentity() { return base.equals(counter); }
public void test_parse_String_unknownCurrencyCreated() { Currency test = Currency.parse("zyx"); assertEquals(test.getCode(), "ZYX"); assertEquals(test.getMinorUnitDigits(), 0); assertSame(test, Currency.of("ZYX")); }
@Test(expectedExceptions = NullPointerException.class) public void test_compareTo_null() { Currency.EUR.compareTo(null); }
public void test_minorUnits() { assertEquals(Currency.of("USD").getMinorUnitDigits(), 2); assertEquals(Currency.of("EUR").getMinorUnitDigits(), 2); assertEquals(Currency.of("JPY").getMinorUnitDigits(), 0); assertEquals(Currency.of("GBP").getMinorUnitDigits(), 2); assertEquals(Currency.of("CHF").getMinorUnitDigits(), 2); assertEquals(Currency.of("AUD").getMinorUnitDigits(), 2); assertEquals(Currency.of("CAD").getMinorUnitDigits(), 2); }
public void test_triangulatonCurrency() { assertEquals(Currency.of("USD").getTriangulationCurrency(), Currency.USD); assertEquals(Currency.of("EUR").getTriangulationCurrency(), Currency.USD); assertEquals(Currency.of("JPY").getTriangulationCurrency(), Currency.USD); assertEquals(Currency.of("GBP").getTriangulationCurrency(), Currency.USD); assertEquals(Currency.of("CHF").getTriangulationCurrency(), Currency.USD); assertEquals(Currency.of("AUD").getTriangulationCurrency(), Currency.USD); assertEquals(Currency.of("CAD").getTriangulationCurrency(), Currency.USD); }
public void test_of_String_historicCurrency() { Currency test = Currency.of("BEF"); assertEquals(test.getCode(), "BEF"); assertEquals(test.getMinorUnitDigits(), 2); assertEquals(test.getTriangulationCurrency(), Currency.EUR); assertSame(test, Currency.of("BEF")); }
public void test_of_String_unknownCurrencyCreated() { Currency test = Currency.of("AAA"); assertEquals(test.getCode(), "AAA"); assertEquals(test.getMinorUnitDigits(), 0); assertSame(test, Currency.of("AAA")); }
/** * Ensure singleton on deserialization. * * @return the singleton */ private Object readResolve() { return Currency.of(code); }
/** * Parses currency from the input string. * <p> * Parsing is case insensitive. * * @param str the string to parse * @return the parsed currency * @throws IllegalArgumentException if the string cannot be parsed */ public static Currency parseCurrency(String str) { try { return Currency.parse(str); } catch (RuntimeException ex) { throw new IllegalArgumentException( "Unknown Currency, must be 3 letter ISO-4217 format but was '" + str + "'"); } }
/** * Obtains an instance that rounds to the number of minor units in the currency. * <p> * This returns a convention that rounds for the specified currency. * Rounding follows the normal {@link RoundingMode#HALF_UP} convention. * * @param currency the currency * @return the rounding convention */ public static Rounding of(Currency currency) { return HalfUpRounding.ofDecimalPlaces(currency.getMinorUnitDigits()); }
/** * Gets the preferred triangulation currency. * <p> * When obtaining a market quote for a currency, the triangulation currency * is used if no direct rate can be found. * For example, there is no direct rate for 'CZK/SGD'. Instead 'CZK' might be defined to * triangulate via 'EUR' and 'SGD' with 'USD'. Since the three rates, 'CZK/EUR', 'EUR/USD' * and 'USD/SGD' can be obtained, a rate can be determined for 'CZK/SGD'. * Note that most currencies triangulate via 'USD'. * * @return the triangulation currency */ public Currency getTriangulationCurrency() { return Currency.of(triangulationCurrency); }
@Override public Set<String> tokens(CurrencyParameterSensitivity sensitivity) { return ImmutableSet.of( sensitivity.getCurrency().getCode().toLowerCase(Locale.ENGLISH), sensitivity.getMarketDataName().getName().toLowerCase(Locale.ENGLISH)); }
/** * Tries to parse a currency from the input string. * <p> * Parsing is case insensitive. * * @param str the string to parse, may be null * @return the parsed currency, empty if unable to parse */ public static Optional<Currency> tryParseCurrency(String str) { if (str != null && str.length() == 3 && CURRENCY_MATCHER.matchesAllOf(str)) { try { return Optional.of(Currency.parse(str)); } catch (RuntimeException ex) { // ignore } } return Optional.empty(); }
/** * Gets the number of digits in the rate. * <p> * If this rate is a conventional currency pair defined in configuration, * then the number of digits in a market FX rate quote is returned. * <p> * If the currency pair is not defined in configuration the sum of the * {@link Currency#getMinorUnitDigits() minor unit digits} from the two currencies is returned. * * @return the number of digits in the FX rate */ public int getRateDigits() { Integer digits = CONFIGURED.get(this); if (digits != null) { return digits; } Integer inverseDigits = CONFIGURED.get(inverse()); if (inverseDigits != null) { return inverseDigits; } return base.getMinorUnitDigits() + counter.getMinorUnitDigits(); }
/** * Checks if this currency pair is the inverse of the specified pair. * <p> * This could be used to check if an FX rate specified in one currency pair needs inverting. * * @param other the other currency pair * @return true if the currency is the inverse of the specified pair */ public boolean isInverse(CurrencyPair other) { ArgChecker.notNull(other, "currencyPair"); return base.equals(other.counter) && counter.equals(other.base); }
/** * Obtains an instance of {@code CurrencyAmount} for the specified ISO-4217 * three letter currency code and amount. * <p> * A currency is uniquely identified by ISO-4217 three letter code. * This method creates the currency if it is not known. * * @param currencyCode the three letter currency code, ASCII and upper case * @param amount the amount of the currency to represent * @return the currency amount * @throws IllegalArgumentException if the currency code is invalid */ public static CurrencyAmount of(String currencyCode, double amount) { return of(Currency.of(currencyCode), amount); }