@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(); }
/** * Sets the {@link CurrencyUnit} to be used. * * @param currencyCode the currencyCode of the currency to be used, not {@code null}. The currency code * will be resolved using {@link Monetary#getCurrency(String, String...)}. * @return This factory instance, for chaining. * @throws UnknownCurrencyException if the {@code currencyCode} is not resolvable. */ default MonetaryAmountFactory<T> setCurrency(String currencyCode) { return setCurrency(Monetary.getCurrency(currencyCode)); }
@Override public MonetaryAmount apply(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); return amount.getFactory().setCurrency(currency).create(); }
@Override public MonetaryAmount apply(MonetaryAmount value) { return value.getFactory().setCurrency(term).create(); }
@Override public MonetaryAmount apply(MonetaryAmount value) { return value.multiply(2).getFactory().setCurrency(TERM).create(); } };
@Override public MonetaryAmount apply(MonetaryAmount value) { return value.multiply(FACTOR).getFactory().setCurrency(term).create(); }
@Override public MonetaryAmount apply(MonetaryAmount value) { return value.multiply(FACTOR).getFactory().setCurrency(term).create(); }
@Override public MonetaryAmount apply(MonetaryAmount value) { return value.multiply(FACTOR).getFactory().setCurrency(term).create(); }
/** * Test divide(Double.NEGATIVE_INFINITY) function must return ZERO amount. */ @SpecAssertion(section = "4.2.2", id = "422-D16") @Test(description = "4.2.2 For each amount class, ensure divide(Double.NEGATIVE_INFINITY) return ZERO amount.") public void testDivideDoubleNEGATIVE_INFINITY() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount mActualResult = mAmount1.divide(Double.NEGATIVE_INFINITY); AssertJUnit.assertEquals("Section 4.2.2: ZERO amount expected on division by Double.POSITIVE_INFINITY, type was " + type.getName(), mActualResult, Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(0).create()); } }
/** * Test remainder(null) must throw a NullPointerException */ @SpecAssertion(section = "4.2.2", id = "422-D21") @Test(description = "4.2.2 For each amount class, ensure remainder(Double.POSITIVE_INFINITY), throws ArithmeticException.") public void testRemainder_DoublePOSITIVE_INFINITY() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount mActualResult = mAmount1.remainder(Double.POSITIVE_INFINITY); AssertJUnit.assertEquals(Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(0).create(), mActualResult); } }
/** * Test TCK custom roundings. */ @Test(description = "4.2.7 Access custom roundings and ensure correct functionality.") @SpecAssertion(section = "4.2.7", id = "427-C4") public void testCustomRoundings() { MonetaryOperator r = Monetary.getRounding("NOSCALE"); AssertJUnit.assertNotNull(r); MonetaryAmount m = new TestMonetaryAmountBuilder().setNumber(new BigDecimal("12.123456789101222232323")).setCurrency("CHF") .create(); AssertJUnit.assertEquals("Section 4.2.7 Expected CHF 12 for NOSCALE operator on " + m, "CHF 12", m.with(r).toString()); }
/** * Test remainder(null) must throw a NullPointerException */ @SpecAssertion(section = "4.2.2", id = "422-D21") @Test(description = "4.2.2 For each amount class, ensure remainder(Double.NEGATIVE_INFINITY), throws ArithmeticException.") public void testRemainder_DoubleNEGATIVE_INFINITY() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount mActualResult = mAmount1.remainder(Double.NEGATIVE_INFINITY); AssertJUnit.assertEquals(Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(0).create(), mActualResult); } }
@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(); }
@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 apply(MonetaryAmount amount) { return amount.getFactory().setCurrency(amount.getCurrency()).setNumber( amount.getNumber().numberValue(BigDecimal.class) .setScale(this.context.getInt(SCALE_KEY), this.context.get(RoundingMode.class))).create(); }
@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(); }
@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(); }