use of javax.money.MonetaryContext in project jsr354-ri by JavaMoney.
the class DefaultMonetaryContextFactory method createMonetaryContextNonNullConfig.
private MonetaryContext createMonetaryContextNonNullConfig(Map<String, String> config, int prec) {
String value = config.get("org.javamoney.moneta.Money.defaults.roundingMode");
RoundingMode rm = Objects.nonNull(value) ? RoundingMode.valueOf(value.toUpperCase(Locale.ENGLISH)) : RoundingMode.HALF_UP;
MonetaryContext mc = MonetaryContextBuilder.of(Money.class).setPrecision(prec).set(rm).set(Money.class).build();
Logger.getLogger(DefaultMonetaryContextFactory.class.getName()).info("Using custom MathContext: precision=" + prec + ", roundingMode=" + rm);
return mc;
}
use of javax.money.MonetaryContext in project jsr354-ri-bp by JavaMoney.
the class MoneyTest method testOfStringNumberMathContext.
/**
* Test method for
* {@link Money#of(Number, String, javax.money.MonetaryContext)} .
* .
*/
@Test
public void testOfStringNumberMathContext() {
MonetaryContext mc = MonetaryContextBuilder.of(Money.class).setMaxScale(2345).setFixedScale(true).set(RoundingMode.CEILING).build();
Money m = Money.of((byte) 2, "EUR", mc);
assertNotNull(m);
assertEquals(mc, m.getContext());
assertEquals(EURO, m.getCurrency());
assertEquals(Byte.valueOf((byte) 2), m.getNumber().numberValue(Byte.class));
m = Money.of((short) -2, "USD", mc);
assertNotNull(m);
assertEquals(mc, m.getContext());
assertEquals(DOLLAR, m.getCurrency());
assertEquals(Short.valueOf((short) -2), m.getNumber().numberValue(Short.class));
m = Money.of(-12, "EUR", mc);
assertNotNull(m);
assertEquals(mc, m.getContext());
assertEquals(EURO, m.getCurrency());
assertEquals(Integer.valueOf(-12), m.getNumber().numberValue(Integer.class));
m = Money.of((long) 12, "USD", mc);
assertEquals(mc, m.getContext());
assertNotNull(m);
assertEquals(DOLLAR, m.getCurrency());
assertEquals(Long.valueOf(12), m.getNumber().numberValue(Long.class));
m = Money.of((float) 12.23, "EUR", mc);
assertNotNull(m);
assertEquals(mc, m.getContext());
assertEquals(EURO, m.getCurrency());
assertEquals(m.getNumber().numberValue(Float.class), (float) 12.23);
m = Money.of(-12.23, "USD", mc);
assertNotNull(m);
assertEquals(mc, m.getContext());
assertEquals(DOLLAR, m.getCurrency());
assertEquals(mc, m.getContext());
assertEquals(m.getNumber().numberValue(Double.class), -12.23);
m = Money.of((Number) BigDecimal.valueOf(234.2345), "EUR", mc);
assertNotNull(m);
assertEquals(EURO, m.getCurrency());
assertEquals(mc, m.getContext());
assertEquals(m.getNumber().numberValue(BigDecimal.class), BigDecimal.valueOf(234.2345));
m = Money.of(BigInteger.valueOf(23232312321432432L), "USD", mc);
assertNotNull(m);
assertEquals(DOLLAR, m.getCurrency());
assertEquals(mc, m.getContext());
assertEquals(Long.valueOf(23232312321432432L), m.getNumber().numberValue(Long.class));
assertEquals(BigInteger.valueOf(23232312321432432L), m.getNumber().numberValue(BigInteger.class));
}
use of javax.money.MonetaryContext in project jsr354-ri-bp by JavaMoney.
the class MoneyTest method testWithMonetaryContext.
/**
* Test method for {@link Money#getFactory()#setContext(java.math.MathContext)}.
*/
@Test
public void testWithMonetaryContext() {
Money m = Money.of(10, "CHF");
assertEquals(Money.DEFAULT_MONETARY_CONTEXT, m.getContext());
MonetaryContext mc = MonetaryContextBuilder.of(Money.class).setPrecision(128).set(RoundingMode.HALF_EVEN).build();
MonetaryAmount m2 = m.getFactory().setContext(mc).create();
assertNotNull(m2);
assertTrue(m != m2);
assertEquals(Money.DEFAULT_MONETARY_CONTEXT, m.getContext());
assertEquals(mc, m2.getContext());
}
use of javax.money.MonetaryContext in project jsr354-ri by JavaMoney.
the class DefaultMonetaryAmountsSingletonQuerySpi method getAmountFactories.
/**
* (non-Javadoc)
*
* @see javax.money.spi.MonetaryAmountsSingletonQuerySpi#getAmountFactories(javax.money.MonetaryAmountFactoryQuery)
*/
@Override
public Collection<MonetaryAmountFactory<?>> getAmountFactories(MonetaryAmountFactoryQuery factoryQuery) {
Objects.requireNonNull(factoryQuery);
List<MonetaryAmountFactory<?>> factories = new ArrayList<>();
// first check for explicit type
for (@SuppressWarnings("unchecked") MonetaryAmountFactoryProviderSpi<? extends MonetaryAmount> factory : Bootstrap.getServices(MonetaryAmountFactoryProviderSpi.class)) {
if (factory.getQueryInclusionPolicy() == QueryInclusionPolicy.NEVER) {
continue;
}
if (factoryQuery.getTargetType() == factory.getAmountType()) {
if (isPrecisionOK(factoryQuery, factory.getMaximalMonetaryContext())) {
factories.add(factory.createMonetaryAmountFactory());
} else {
throw new MonetaryException("Incompatible context required=" + factoryQuery + ", maximal=" + factory.getMaximalMonetaryContext());
}
}
}
List<MonetaryAmountFactoryProviderSpi<? extends MonetaryAmount>> selection = new ArrayList<>();
for (@SuppressWarnings("unchecked") MonetaryAmountFactoryProviderSpi<? extends MonetaryAmount> factory : Bootstrap.getServices(MonetaryAmountFactoryProviderSpi.class)) {
if (factory.getQueryInclusionPolicy() == QueryInclusionPolicy.DIRECT_REFERENCE_ONLY || factory.getQueryInclusionPolicy() == QueryInclusionPolicy.NEVER) {
continue;
}
if (isPrecisionOK(factoryQuery, factory.getMaximalMonetaryContext())) {
selection.add(factory);
}
}
if (selection.isEmpty()) {
// fall back, add all selections, ignore flavor
for (@SuppressWarnings("unchecked") MonetaryAmountFactoryProviderSpi<? extends MonetaryAmount> factory : Bootstrap.getServices(MonetaryAmountFactoryProviderSpi.class)) {
if (factory.getQueryInclusionPolicy() == QueryInclusionPolicy.DIRECT_REFERENCE_ONLY || factory.getQueryInclusionPolicy() == QueryInclusionPolicy.NEVER) {
continue;
}
if (isPrecisionOK(factoryQuery, factory.getMaximalMonetaryContext())) {
selection.add(factory);
}
}
}
if (selection.size() == 1) {
factories.add(selection.get(0).createMonetaryAmountFactory());
}
MonetaryContext context = createContext(factoryQuery);
factories.forEach(f -> f.setContext(context));
selection.sort(CONTEXT_COMPARATOR);
factories.add(selection.get(0).createMonetaryAmountFactory());
return factories;
}
use of javax.money.MonetaryContext in project jsr354-ri-bp by JavaMoney.
the class DefaultMonetaryContextFactory method createMonetaryContextNonNullConfig.
private MonetaryContext createMonetaryContextNonNullConfig(Map<String, String> config, int prec) {
String value = config.get("org.javamoney.moneta.Money.defaults.roundingMode");
RoundingMode rm = value != null ? RoundingMode.valueOf(value.toUpperCase(Locale.ENGLISH)) : RoundingMode.HALF_UP;
MonetaryContext mc = MonetaryContextBuilder.of(Money.class).setPrecision(prec).set(rm).set(Money.class).build();
Logger.getLogger(DefaultMonetaryContextFactory.class.getName()).info("Using custom MathContext: precision=" + prec + ", roundingMode=" + rm);
return mc;
}
Aggregations