/** * Creates a new builder instances, initialized with the data from this one. * * @return a new {@link MonetaryAmountFactoryQueryBuilder} instance, never null. */ public MonetaryAmountFactoryQueryBuilder toBuilder() { return MonetaryAmountFactoryQueryBuilder.of(this); }
/** * Creates a new instance of {@link javax.money.CurrencyQueryBuilder}. * * @return a new {@link javax.money.CurrencyQueryBuilder} instance, never null. */ public static MonetaryAmountFactoryQueryBuilder of(){ return new MonetaryAmountFactoryQueryBuilder(); }
/** * Creates a new instance of {@link javax.money.CurrencyQueryBuilder}. * * @param monetaryAmountFactoryQuery {@link MonetaryAmountFactoryQuery} used for initializing this */ private MonetaryAmountFactoryQueryBuilder(MonetaryAmountFactoryQuery monetaryAmountFactoryQuery){ Objects.requireNonNull(monetaryAmountFactoryQuery); importContext(monetaryAmountFactoryQuery); }
/** * @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); }
/** * Creates an amount with the given scale. * @param scale the target scale * @return the new amount. */ public static MonetaryAmount createAmountWithScale(int scale) { MonetaryAmountFactoryQuery tgtContext = MonetaryAmountFactoryQueryBuilder.of().setMaxScale(scale).build(); MonetaryAmountFactory<?> exceedingFactory; try { exceedingFactory = Monetary.getAmountFactory(tgtContext); AssertJUnit.assertNotNull(exceedingFactory); MonetaryAmountFactory<? extends MonetaryAmount> bigFactory = Monetary.getAmountFactory(exceedingFactory.getAmountType()); return bigFactory.setCurrency("CHF").setNumber(createNumberWithScale(scale)).create(); } catch (MonetaryException e) { return null; } }
/** * Creates an amount with the given precision. * @param precision the target precision * @return a corresponding amount. */ public static MonetaryAmount createAmountWithPrecision(int precision) { MonetaryAmountFactoryQuery tgtContext = MonetaryAmountFactoryQueryBuilder.of().setPrecision(precision).build(); MonetaryAmountFactory<?> exceedingFactory; try { exceedingFactory = Monetary.getAmountFactory(tgtContext); AssertJUnit.assertNotNull(exceedingFactory); MonetaryAmountFactory<? extends MonetaryAmount> bigFactory = Monetary.getAmountFactory(exceedingFactory.getAmountType()); return bigFactory.setCurrency("CHF").setNumber(createNumberWithPrecision(precision)).create(); } catch (MonetaryException e) { return null; } } }
/** * Ensure correct query function implementations, providing also * the some test implementations with the TCK. */ @Test(description = "4.2.7 Ensure correct query function, Monetary.getAmountFactories should return factory" + "for explicit acquired amount types.") @SpecAssertion(section = "4.2.7", id = "427-B4") public void testAmountQueryType() { MonetaryAmountFactoryQuery ctx = MonetaryAmountFactoryQueryBuilder.of().setTargetType(TestAmount.class).build(); Collection<MonetaryAmountFactory<?>> factories = Monetary.getAmountFactories(ctx); AssertJUnit.assertNotNull("Section 4.2.7: Amount factory query should return explicitly queried factories", factories); boolean found = false; for (MonetaryAmountFactory<?> f : factories) { if (f.getAmountType().equals(TestAmount.class)) { found = true; break; } } AssertJUnit.assertTrue("Section 4.2.7: Amount type query should return same explicitly queried factory", found); ctx = MonetaryAmountFactoryQueryBuilder.of().build(); MonetaryAmountFactory<?> factory = Monetary.getAmountFactory(ctx); AssertJUnit.assertNotNull("Section 4.2.7: Amount type must be provided", factory); }
/** * Sets the flag if the scale should fixed, meaning minimal scale and maximal scale are always equally sized. * * @param fixedScale the fixed scale flag. * @return this Builder for chaining. */ public MonetaryAmountFactoryQueryBuilder setFixedScale(boolean fixedScale){ return set("fixedScale", fixedScale); }
/** * Sets the maximal scale to be supported. * * @param maxScale the max scale, >= 0. * @return this Builder for chaining. */ public MonetaryAmountFactoryQueryBuilder setMaxScale(int maxScale){ return set("maxScale", maxScale); }
/** * Sets the required precision, the value 0 models unlimited precision. * * @param precision the precision, >= 0, 0 meaning unlimited. * @return this Builder for chaining. */ public MonetaryAmountFactoryQueryBuilder setPrecision(int precision){ return set("precision", precision); }
/** * Creates a new instance of {@link javax.money.CurrencyQueryBuilder}. * * @param monetaryAmountFactoryQuery {@link MonetaryAmountFactoryQuery} used for initializing this * builder. * @return a new {@link MonetaryAmountFactoryQueryBuilder} instance, never null. */ public static MonetaryAmountFactoryQueryBuilder of(MonetaryAmountFactoryQuery monetaryAmountFactoryQuery){ return new MonetaryAmountFactoryQueryBuilder(monetaryAmountFactoryQuery); }