@Override public MonetaryAmount parse(String text, Locale locale) throws ParseException { CurrencyUnit currencyUnit = Monetary.getCurrency(locale); Number numberValue = this.numberFormatter.parse(text, locale); return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); } }
@Override public MonetaryAmount parse(String text, Locale locale) throws ParseException { CurrencyStyleFormatter formatter = new CurrencyStyleFormatter(); Currency currency = determineCurrency(text, locale); CurrencyUnit currencyUnit = Monetary.getCurrency(currency.getCurrencyCode()); formatter.setCurrency(currency); formatter.setPattern(this.pattern); Number numberValue = formatter.parse(text, locale); return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); }
@Override public MonetaryAmount parse(String text, Locale locale) throws ParseException { CurrencyUnit currencyUnit = Monetary.getCurrency(locale); Number numberValue = this.numberFormatter.parse(text, locale); return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); } }
@Override public MonetaryAmount parse(String text, Locale locale) throws ParseException { CurrencyStyleFormatter formatter = new CurrencyStyleFormatter(); Currency currency = determineCurrency(text, locale); CurrencyUnit currencyUnit = Monetary.getCurrency(currency.getCurrencyCode()); formatter.setCurrency(currency); formatter.setPattern(this.pattern); Number numberValue = formatter.parse(text, locale); return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); }
/** * Gets the {@link javax.money.MonetaryAmountFactoryQuery} to be used for accessing {@link javax.money * .MonetaryAmountFactory}, when amount's are parsed. * * @return the monetary context, or {@code null}. */ @SuppressWarnings("rawtypes") public MonetaryAmountFactory getMonetaryAmountFactory() { MonetaryAmountFactory factory = get(MonetaryAmountFactory.class); if (factory == null) { return Monetary.getDefaultAmountFactory(); } return factory; }
/** * Ensure a default factory is returned. Test javamoney.config * for configuring default value. */ @Test(description = "4.2.7 Ensure a default MonetaryAmountFactory is available.") @SpecAssertion(section = "4.2.7", id = "427-B5") public void testAmountDefaultType() { AssertJUnit.assertNotNull("Section 4.2.7: No default MonetaryAmountFactory found.", Monetary.getDefaultAmountFactory()); }
@Override public MonetaryAmount decode(BsonReader reader, DecoderContext decoderContext) { reader.readStartDocument(); CurrencyUnit currency = Monetary.getCurrency(reader.readString(this.currencyKey)); BigDecimal number = new BigDecimal( BigInteger.valueOf(reader.readInt64(this.unscaledKey)), reader.readInt32(this.scaleKey)); reader.readEndDocument(); return Monetary.getDefaultAmountFactory().setNumber(number).setCurrency(currency) .create(); }
/** * For each MonetaryAmount implementation: Ensure getCurrencyCode * returns correct results. */ @SpecAssertion(section = "4.2.2", id = "422-A1") @Test(description = "4.2.2 Ensure amount can be created with all default currencies.") public void testCurrencyCode() { for (Class type : Monetary.getAmountTypes()) { for (Currency jdkCur : Currency.getAvailableCurrencies()) { MonetaryAmount amount = Monetary.getDefaultAmountFactory().setCurrency(jdkCur.getCurrencyCode()).setNumber(10.15) .create(); AssertJUnit.assertNotNull( "Section 4.2.2: Amount factory returned null for new amount type: " + type.getName(), amount); AssertJUnit.assertNotNull( "Section 4.2.2: Amount factory returned new amount with null currency, type: " + type.getName(), amount.getCurrency()); AssertJUnit.assertEquals( "Section 4.2.2: Amount factory returned new amount with invalid currency, type: " + type.getName(), jdkCur.getCurrencyCode(), amount.getCurrency().getCurrencyCode()); } } }
@Override public MonetaryAmount parse(String text, Locale locale) throws ParseException { CurrencyUnit currencyUnit = Monetary.getCurrency(locale); Number numberValue = this.numberFormatter.parse(text, locale); return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); } }
@Override public MonetaryAmount parse(String text, Locale locale) throws ParseException { CurrencyStyleFormatter formatter = new CurrencyStyleFormatter(); Currency currency = determineCurrency(text, locale); CurrencyUnit currencyUnit = Monetary.getCurrency(currency.getCurrencyCode()); formatter.setCurrency(currency); formatter.setPattern(this.pattern); Number numberValue = formatter.parse(text, locale); return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); }
factory = Monetary.getDefaultAmountFactory();
@Override public MonetaryAmount decode(BsonReader reader, DecoderContext decoderContext) { reader.readStartDocument(); CurrencyUnit currency = Monetary.getCurrency(reader.readString(this.currencyKey)); BigDecimal number = new BigDecimal(reader.readString(this.numberKey)); reader.readEndDocument(); return Monetary.getDefaultAmountFactory().setNumber(number).setCurrency(currency) .create(); }
/** * Bad case: try converting from/to an inconvertible (custom) currency, ensure CurrencyConversionException is * thrown. * * @see org.javamoney.tck.tests.internal.TestCurrencyUnit } for creating a custom currency, * with some fancy non-ISO currency code. */ @Test(description = "4.3.2 Bad case: Try CurrencyConversion to an inconvertible (custom) " + "currency (FOOANY), ensure CurrencyConversionException is thrown.") @SpecAssertion(id = "432-A3", section = "4.3.2") public void testUnsupportedConversion() { MonetaryAmount m = Monetary.getDefaultAmountFactory().setNumber(10).setCurrency("CHF").create(); CurrencyUnit cu = new TestCurrencyUnit("FOOANY"); try { CurrencyConversion conv = MonetaryConversions.getConversion(cu); m.with(conv); } catch (CurrencyConversionException e) { // expected } }
/** * Format several amounts, created using the default factory, * but * also a test instance, provided by the TCK, to ensure no * implementation * dependencies on the implementation. */ @SpecAssertion(section = "4.4.1", id = "441-A1") @Test(description = "4.4.1 Ensures the system.s default locale is supported for MonetaryAmountFormat.") public void testNoDepOnAmountImplementation() { final Locale defaultLocale = Locale.getDefault(); MonetaryAmountFormat amountFormat = MonetaryFormats.getAmountFormat(defaultLocale); final Number[] values = new Number[]{100, 10000000000000L}; for (CurrencyUnit currency : Monetary.getCurrencies()) { for (Number value : values) { MonetaryAmount amount = Monetary.getDefaultAmountFactory().setCurrency(currency.getCurrencyCode()) .setNumber(value).create(); String formattedAmount = amountFormat.format(amount); MonetaryAmount amountMock = TestMonetaryAmountBuilder.getAmount(value, currency); String formattedAmountMock = amountFormat.format(amountMock); AssertJUnit.assertNotNull(formattedAmountMock); assertEquals(formattedAmountMock, formattedAmount); } } }
/** * Test successful conversion for possible currency pairs.<p> * Hint: you may only check for rate factory, when using a hardcoded ExchangeRateProvider, such a provider * must be also implemented and registered as an SPI. */ @Test(description = "4.3.2 Test successful conversion for CHF -> FOO, using TestRateProvider.") @SpecAssertion(id = "432-A1", section = "4.3.2") public void testConversion() { CurrencyUnit cu = new TestCurrencyUnit("FOO"); CurrencyConversion conv = MonetaryConversions.getConversion(cu, "TestRateProvider"); MonetaryAmount m = Monetary.getDefaultAmountFactory().setNumber(10).setCurrency("CHF").create(); MonetaryAmount m2 = m.with(conv); AssertJUnit.assertEquals(m2.getCurrency().getCurrencyCode(), "FOO"); AssertJUnit.assertEquals(20L, m2.getNumber().longValueExact()); m2 = m.with(conv); AssertJUnit.assertEquals(m2.getCurrency().getCurrencyCode(), "FOO"); AssertJUnit.assertEquals(20L, m2.getNumber().longValueExact()); }
/** * @param args */ public static void main(String[] args) { MonetaryAmount amount = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(234).create(); ConsoleUtils.printDetails(amount); amount = Monetary.getAmountFactory(FastMoney.class).setCurrency("EUR").setNumber(234).create(); ConsoleUtils.printDetails(amount); amount = Monetary.getAmountFactory( MonetaryAmountFactoryQueryBuilder.of().setMaxScale(50).setPrecision(30).build()) .setCurrency("EUR").setNumber(234).create(); ConsoleUtils.printDetails(amount); Money amt1 = Money.of(10.1234556123456789, "USD"); FastMoney amt2 = FastMoney.of(123456789, "USD"); Money total = amt1.add(amt2).multiply(0.5) .remainder(1); ConsoleUtils.printDetails(total); }