/** * Ensure singleton on deserialization. * * @return the singleton */ private Object readResolve() { return Currency.of(code); }
/** * 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); }
/** * 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); }
/** * Parses a string to obtain a {@code Currency}. * <p> * The parse is identical to {@link #of(String)} except that it will convert * letters to upper case first. * If the requested currency is not defined in configuration, it will still be created, * however it will have the default value of zero for the minor units and 'USD' for * the triangulation currency. * * @param currencyCode the three letter currency code, ASCII * @return the singleton instance * @throws IllegalArgumentException if the currency code is invalid */ public static Currency parse(String currencyCode) { ArgChecker.notNull(currencyCode, "currencyCode"); return of(currencyCode.toUpperCase(Locale.ENGLISH)); }
public void test_constants() { assertEquals(Currency.of("USD"), Currency.USD); assertEquals(Currency.of("EUR"), Currency.EUR); assertEquals(Currency.of("JPY"), Currency.JPY); assertEquals(Currency.of("GBP"), Currency.GBP); assertEquals(Currency.of("CHF"), Currency.CHF); assertEquals(Currency.of("AUD"), Currency.AUD); assertEquals(Currency.of("CAD"), Currency.CAD); }
private static ImmutableMap<Currency, FloatingRateName> parseIborDefaults( IniFile ini, ImmutableMap<String, FloatingRateName> names) { ImmutableMap.Builder<Currency, FloatingRateName> map = ImmutableMap.builder(); PropertySet section = ini.section("currencyDefaultIbor"); for (String key : section.keys()) { FloatingRateName frname = names.get(section.value(key)); if (frname == null) { throw new IllegalArgumentException("Invalid default Ibor index for currency " + key); } map.put(Currency.of(key), frname); } return map.build(); }
@Test(expectedExceptions = IllegalArgumentException.class) public void test_of_String_lowerCase() { Currency.of("gbp"); }
@Test(dataProvider = "ofBad", expectedExceptions = IllegalArgumentException.class) public void test_of_String_bad(String input) { Currency.of(input); }
private static ImmutableMap<Currency, FloatingRateName> parseOvernightDefaults( IniFile ini, ImmutableMap<String, FloatingRateName> names) { ImmutableMap.Builder<Currency, FloatingRateName> map = ImmutableMap.builder(); PropertySet section = ini.section("currencyDefaultOvernight"); for (String key : section.keys()) { FloatingRateName frname = names.get(section.value(key)); if (frname == null) { throw new IllegalArgumentException("Invalid default Overnight index for currency " + key); } map.put(Currency.of(key), frname); } return map.build(); } }
private static GroupAndReference createKey( CurveGroupName curveGroup, String curveTypeStr, String referenceStr) { // discount and forward curves are supported if (FORWARD.equalsIgnoreCase(curveTypeStr.toLowerCase(Locale.ENGLISH))) { Index index = LoaderUtils.findIndex(referenceStr); return new GroupAndReference(curveGroup, index); } else if (DISCOUNT.equalsIgnoreCase(curveTypeStr.toLowerCase(Locale.ENGLISH))) { Currency ccy = Currency.of(referenceStr); return new GroupAndReference(curveGroup, ccy); } else { throw new IllegalArgumentException(Messages.format("Unsupported curve type: {}", curveTypeStr)); } }
public void test_equals_hashCode() { Object a1 = Currency.GBP; Object a2 = Currency.of("GBP"); Object b = Currency.EUR; assertEquals(a1.equals(a1), true); assertEquals(a1.equals(b), false); assertEquals(a1.equals(a2), true); assertEquals(a2.equals(a1), true); assertEquals(a2.equals(a2), true); assertEquals(a2.equals(b), false); assertEquals(b.equals(a1), false); assertEquals(b.equals(a2), false); assertEquals(b.equals(b), true); assertEquals(a1.hashCode(), a2.hashCode()); }
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); }
@VisibleForTesting static ImmutableMap<Currency, HolidayCalendarId> loadDefaultsFromIni(String filename) { List<ResourceLocator> resources = ResourceConfig.orderedResources(filename); Map<Currency, HolidayCalendarId> map = new HashMap<>(); for (ResourceLocator resource : resources) { try { IniFile ini = IniFile.of(resource.getCharSource()); PropertySet section = ini.section("defaultByCurrency"); for (String currencyCode : section.keys()) { map.put(Currency.of(currencyCode), HolidayCalendarId.of(section.value(currencyCode))); } } catch (RuntimeException ex) { log.log(Level.SEVERE, "Error processing resource as Holiday Calendar Defaults INI file: " + resource, ex); return ImmutableMap.of(); } } return ImmutableMap.copyOf(map); }
static Position parseNonEtdPosition(CsvRow row, PositionInfo info, PositionCsvInfoResolver resolver) { SecurityPosition base = parseSecurityPosition(row, info, resolver); Optional<Double> tickSizeOpt = row.findValue(TICK_SIZE).map(str -> LoaderUtils.parseDouble(str)); Optional<Currency> currencyOpt = row.findValue(CURRENCY).map(str -> Currency.of(str)); Optional<Double> tickValueOpt = row.findValue(TICK_VALUE).map(str -> LoaderUtils.parseDouble(str)); double contractSize = row.findValue(CONTRACT_SIZE).map(str -> LoaderUtils.parseDouble(str)).orElse(1d); if (tickSizeOpt.isPresent() && currencyOpt.isPresent() && tickValueOpt.isPresent()) { SecurityPriceInfo priceInfo = SecurityPriceInfo.of(tickSizeOpt.get(), CurrencyAmount.of(currencyOpt.get(), tickValueOpt.get()), contractSize); GenericSecurity sec = GenericSecurity.of(SecurityInfo.of(base.getSecurityId(), priceInfo)); return GenericSecurityPosition.ofLongShort(base.getInfo(), sec, base.getLongQuantity(), base.getShortQuantity()); } return base; }
static SecurityQuantityTrade parseTrade(CsvRow row, TradeInfo info, TradeCsvInfoResolver resolver) { SecurityTrade trade = parseSecurityTrade(row, info, resolver); SecurityTrade base = resolver.completeTrade(row, trade); Optional<Double> tickSizeOpt = row.findValue(TICK_SIZE).map(str -> LoaderUtils.parseDouble(str)); Optional<Currency> currencyOpt = row.findValue(CURRENCY).map(str -> Currency.of(str)); Optional<Double> tickValueOpt = row.findValue(TICK_VALUE).map(str -> LoaderUtils.parseDouble(str)); double contractSize = row.findValue(CONTRACT_SIZE).map(str -> LoaderUtils.parseDouble(str)).orElse(1d); if (tickSizeOpt.isPresent() && currencyOpt.isPresent() && tickValueOpt.isPresent()) { SecurityPriceInfo priceInfo = SecurityPriceInfo.of(tickSizeOpt.get(), CurrencyAmount.of(currencyOpt.get(), tickValueOpt.get()), contractSize); GenericSecurity sec = GenericSecurity.of(SecurityInfo.of(base.getSecurityId(), priceInfo)); return GenericSecurityTrade.of(base.getInfo(), sec, base.getQuantity(), base.getPrice()); } return base; }
public void test_parse_String_unknownCurrencyCreated() { Currency test = Currency.parse("zyx"); assertEquals(test.getCode(), "ZYX"); assertEquals(test.getMinorUnitDigits(), 0); assertSame(test, Currency.of("ZYX")); }