/** * Checks if a {@link MonetaryRounding} is available given a roundingId. * * @param roundingId The rounding identifier. * @param providers the providers and ordering to be used. By default providers and ordering as defined in * #getDefaultProviders is used. * @return true, if a corresponding {@link javax.money.MonetaryRounding} is available. * @throws IllegalArgumentException if no such rounding is registered using a * {@link RoundingProviderSpi} instance. */ default boolean isRoundingAvailable(String roundingId, String... providers) { return isRoundingAvailable( RoundingQueryBuilder.of().setProviderNames(providers).setRoundingName(roundingId).build()); }
/** * Sets the target scale. This allows to define the scale required. If not specified as additional * attribute, by default, RoundingMode.HALF_EVEN is used hereby. * * @param scale the target scale, >0. * @return this instance for chaining */ public RoundingQueryBuilder setScale(int scale) { set(RoundingQuery.KEY_QUERY_SCALE, scale); return this; }
/** * Checks if a {@link MonetaryRounding} is available given a {@link javax.money.CurrencyUnit}. * * @param currencyUnit The currency, which determines the required precision. As {@link java.math.RoundingMode}, * by default, {@link java.math.RoundingMode#HALF_UP} is used. * @param providers the providers and ordering to be used. By default providers and ordering as defined in * #getDefaultProviders is used. * @return true, if a corresponding {@link javax.money.MonetaryRounding} is available. * @throws IllegalArgumentException if no such rounding is registered using a * {@link RoundingProviderSpi} instance. */ default boolean isRoundingAvailable(CurrencyUnit currencyUnit, String... providers) { return isRoundingAvailable(RoundingQueryBuilder.of().setProviderNames(providers).setCurrency(currencyUnit).build()); } }
private MonetaryOperator createUsingMonetaryContext( MonetaryContext context, MonetaryContextBuilder monetaryContextBuilder) { MathContext mathContext = context.get(MathContext.class); if (Objects.isNull(mathContext)) { RoundingMode roundingMode = context.get(RoundingMode.class); if (Objects.nonNull(roundingMode)) { int scale = ofNullable(context.getInt("scale")).orElse(SCALE_DEFAULT); monetaryContextBuilder.set(roundingMode); monetaryContextBuilder.set("scale", scale); return Monetary.getRounding(RoundingQueryBuilder.of() .setScale(scale).set(roundingMode).build()); } else { return Monetary.getDefaultRounding(); } } else { monetaryContextBuilder.set(mathContext.getRoundingMode()); monetaryContextBuilder.set("scale", SCALE_DEFAULT); return Monetary.getRounding(RoundingQueryBuilder.of().set(mathContext) .setScale(SCALE_DEFAULT).build()); } } }
protected static long amountToCents(final MonetaryAmount monetaryAmount) { final MonetaryRounding ROUNDING = ROUNDING_PROVIDER.getRounding(RoundingQueryBuilder.of().setRoundingName("default").setCurrency(monetaryAmount.getCurrency()) .build()); return monetaryAmount .with(ROUNDING) .query(amount -> queryFrom(monetaryAmount).longValue()); }
@Deprecated public RoundedMoney(Number number, CurrencyUnit currency, MathContext mathContext) { Objects.requireNonNull(currency, "Currency is required."); this.currency = currency; this.rounding = Monetary.getRounding(RoundingQueryBuilder.of().set(mathContext).build()); this.monetaryContext = DEFAULT_MONETARY_CONTEXT.toBuilder().set("MonetaryRounding", rounding).set(mathContext) .build(); Objects.requireNonNull(number, "Number is required."); checkNumber(number); this.number = MoneyUtils.getBigDecimal(number, monetaryContext); }
/** * Creates a new builder instances, initialized with the data from this one. * * @return a new {@link javax.money.RoundingQueryBuilder} instance, never null. */ public RoundingQueryBuilder toBuilder() { return RoundingQueryBuilder.of(this); }
/** * Creates a new RoundingQueryBuilder. */ public static RoundingQueryBuilder of() { return new RoundingQueryBuilder(); }
/** * Access roundings using a MonetaryContext. Use different * MathContext/RoundingMode, as an attribute, when running * on the JDK. */ @Test(description = "4.2.7 Ensure correct MonetaryRounding returned for a mathematical RoundingQuery.") @SpecAssertion(section = "4.2.7", id = "427-C2") public void testAccessRoundingsWithRoundingContext() { RoundingQuery ctx = RoundingQueryBuilder.of().setScale(1).set(RoundingMode.UP).build(); MonetaryOperator r = Monetary.getRounding(ctx); AssertJUnit.assertNotNull("Section 4.2.7: No rounding provided for RoundingQuery: " + ctx, r); MonetaryAmount m = new TestMonetaryAmountBuilder().setNumber(new BigDecimal("12.123456789101222232323")).setCurrency("CHF") .create(); AssertJUnit.assertEquals("Section 4.2.7: Invalid rounding provided for RoundingQuery: " + ctx, "CHF 12.2", m.with(r).toString()); }
private static BigDecimal amountToPreciseAmount(final HighPrecisionMoneyImpl monetaryAmount) { final MonetaryRounding ROUNDING = ROUNDING_PROVIDER.getRounding(RoundingQueryBuilder.of().setRoundingName("default").setCurrency(monetaryAmount.getCurrency()) .build()); return monetaryAmount .with(ROUNDING) .query(amount -> queryFrom(monetaryAmount, monetaryAmount.getFractionDigits())); }
/** * Creates a new RoundingQueryBuilder. * * @param roundingQuery the rounding query, used as a template, not null. * @return a new {@link javax.money.RoundingQueryBuilder} instance, never null. */ public static RoundingQueryBuilder of(RoundingQuery roundingQuery) { return new RoundingQueryBuilder(roundingQuery); }
/** * Access a {@link javax.money.MonetaryRounding} using the rounding name. * * @param roundingName The rounding name, not null. * @param providers the optional provider list and ordering to be used * @return the corresponding {@link javax.money.MonetaryOperator} implementing the * rounding, never {@code null}. * @throws IllegalArgumentException if no such rounding is registered using a * {@link javax.money.spi.RoundingProviderSpi} instance. */ default MonetaryRounding getRounding(String roundingName, String... providers) { MonetaryRounding op = getRounding(RoundingQueryBuilder.of().setProviderNames(providers).setRoundingName(roundingName).build()); return Optional.ofNullable(op) .orElseThrow(() -> new MonetaryException("No rounding provided with rounding name: " + roundingName)); }
/** * Access a {@link javax.money.MonetaryRounding} for rounding {@link javax.money.MonetaryAmount} * instances given a currency. * * @param currencyUnit The currency, which determines the required precision. As * {@link java.math.RoundingMode}, by default, {@link java.math.RoundingMode#HALF_UP} * is sued. * @param providers the optional provider list and ordering to be used * @return a new instance {@link javax.money.MonetaryOperator} implementing the * rounding, never {@code null}. * @throws MonetaryException if no such rounding could be provided. */ default MonetaryRounding getRounding(CurrencyUnit currencyUnit, String... providers) { MonetaryRounding op = getRounding(RoundingQueryBuilder.of().setProviderNames(providers).setCurrency(currencyUnit).build()); return Optional.ofNullable(op).orElseThrow(() -> new MonetaryException( "No rounding provided for CurrencyUnit: " + currencyUnit.getCurrencyCode())); }
/** * Sets the target {@link CurrencyUnit}, which defines a rounding targeting a concrete {@link * CurrencyUnit}. * Typically this determines all other properties, such as scale and the concrete rounding algorithm. With * rounding names, depending on the implementation, additional subselections are possible. Similarly * additional attributes can be used to select special rounding instances, e.g. for cash rounding. * * @param currencyUnit the target {@link CurrencyUnit} not null. * @return this instance for chaining */ public RoundingQueryBuilder setCurrency(CurrencyUnit currencyUnit) { Objects.requireNonNull(currencyUnit); set(CurrencyUnit.class, currencyUnit); return this; }
/** * Sets the rounding names of the {@link MonetaryRounding} instances. This method allows to * access the {@link MonetaryRounding} instances by passing a name, which most of the time * identifies a certain rounding instance. Each entry is first matched as name on equality. If no instance * with such a name exists, the value passed is interpreted as a regular * expression to lookup roundings. * * @param roundingName the (custom) rounding name expression, not {@code null}. * @return this instance for chaining */ public RoundingQueryBuilder setRoundingName(String roundingName) { Objects.requireNonNull(roundingName); set(RoundingQuery.KEY_QUERY_ROUNDING_NAME, roundingName); return this; }