本文整理汇总了Java中javax.money.MonetaryContext类的典型用法代码示例。如果您正苦于以下问题:Java MonetaryContext类的具体用法?Java MonetaryContext怎么用?Java MonetaryContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MonetaryContext类属于javax.money包,在下文中一共展示了MonetaryContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: RoundedMoney
import javax.money.MonetaryContext; //导入依赖的package包/类
@Deprecated
public RoundedMoney(Number number, CurrencyUnit currency, MonetaryContext context, MonetaryOperator rounding) {
Objects.requireNonNull(currency, "Currency is required.");
this.currency = currency;
Objects.requireNonNull(number, "Number is required.");
checkNumber(number);
MonetaryContextBuilder monetaryContextBuilder = DEFAULT_MONETARY_CONTEXT.toBuilder();
if (context != null) {
monetaryContextBuilder.importContext(context);
}
this.rounding = RoundedMoneyMonetaryOperatorFactory.INSTANCE.getDefaultMonetaryOperator(rounding, monetaryContextBuilder.build());
monetaryContextBuilder.set(MONETARY_ROUNDING_KEY, this.rounding);
this.monetaryContext = monetaryContextBuilder.build();
this.number = MoneyUtils.getBigDecimal(number, monetaryContext);
}
示例2: setContext
import javax.money.MonetaryContext; //导入依赖的package包/类
@Override
public MonetaryAmountFactory<T> setContext(MonetaryContext monetaryContext) {
Objects.requireNonNull(monetaryContext);
int maxScale = getMaximalMonetaryContext().getMaxScale();
if (maxScale != -1 && maxScale < monetaryContext.getMaxScale()) {
throw new MonetaryException(
"Context exceeds maximal capabilities (scale) of this type: " + monetaryContext);
}
int precision = getMaximalMonetaryContext().getPrecision();
if (precision != 0 && precision < monetaryContext.getPrecision()) {
throw new MonetaryException(
"Contexts exceeds maximal capabilities (precision) of this type: " + monetaryContext);
}
this.monetaryContext = monetaryContext.toBuilder()
.setAmountType(getAmountType()).build();
return this;
}
示例3: Money
import javax.money.MonetaryContext; //导入依赖的package包/类
/**
* Creates a new instance of {@link Money}.
*
* @param currency the currency, not {@code null}.
* @param number the amount, not {@code null}.
* @param monetaryContext the {@link MonetaryContext}, if {@code null}, the default is
* used.
* @throws ArithmeticException If the number exceeds the capabilities of the
* {@link MonetaryContext} used.
*/
private Money(BigDecimal number, CurrencyUnit currency, MonetaryContext monetaryContext) {
Objects.requireNonNull(currency, "Currency is required.");
this.currency = currency;
if (monetaryContext!=null) {
this.monetaryContext = monetaryContext;
} else {
this.monetaryContext = DEFAULT_MONETARY_CONTEXT;
}
Objects.requireNonNull(number, "Number is required.");
this.number = MoneyUtils.getBigDecimal(number, monetaryContext);
}
示例4: getBigDecimal
import javax.money.MonetaryContext; //导入依赖的package包/类
/**
* Creates a {@link BigDecimal} from the given {@link Number} doing the
* valid conversion depending the type given, if a {@link MonetaryContext}
* is given, it is applied to the number returned.
*
* @param num the number type
* @return the corresponding {@link BigDecimal}
*/
public static BigDecimal getBigDecimal(Number num, MonetaryContext moneyContext) {
BigDecimal bd = getBigDecimal(num);
if (moneyContext!=null) {
MathContext mc = getMathContext(moneyContext, RoundingMode.HALF_EVEN);
bd = new BigDecimal(bd.toString(), mc);
if (moneyContext.getMaxScale() > 0) {
LOG.fine(String.format("Got Max Scale %s", moneyContext.getMaxScale()));
bd = bd.setScale(moneyContext.getMaxScale(), mc.getRoundingMode());
}
}
return bd;
}
示例5: getMathContext
import javax.money.MonetaryContext; //导入依赖的package包/类
/**
* Evaluates the {@link MathContext} from the given {@link MonetaryContext}.
*
* @param monetaryContext the {@link MonetaryContext}
* @param defaultMode the default {@link RoundingMode}, to be used if no one is set
* in {@link MonetaryContext}.
* @return the corresponding {@link MathContext}
*/
public static MathContext getMathContext(MonetaryContext monetaryContext, RoundingMode defaultMode) {
MathContext ctx = monetaryContext.get(MathContext.class);
if (ctx!=null) {
return ctx;
}
RoundingMode roundingMode = monetaryContext.get(RoundingMode.class);
if (roundingMode == null) {
roundingMode = defaultMode;
}
if (roundingMode == null) {
roundingMode = RoundingMode.HALF_EVEN;
}
return new MathContext(monetaryContext.getPrecision(), roundingMode);
}
示例6: getContext
import javax.money.MonetaryContext; //导入依赖的package包/类
public MonetaryContext getContext() {
try {
Map<String, String> config = MonetaryConfig.getConfig();
String value = config.get("org.javamoney.moneta.Money.defaults.precision");
if (value!=null) {
return createMonetaryContextNonNullConfig(config, Integer.parseInt(value));
} else {
return createContextWithConfig(config);
}
} catch (Exception e) {
Logger.getLogger(DefaultMonetaryContextFactory.class.getName())
.log(Level.SEVERE, "Error evaluating default NumericContext, using default (NumericContext.NUM64).", e);
return MonetaryContextBuilder.of(Money.class).set(MathContext.DECIMAL64).build();
}
}
示例7: createContextWithConfig
import javax.money.MonetaryContext; //导入依赖的package包/类
private MonetaryContext createContextWithConfig(Map<String, String> config) {
MonetaryContextBuilder builder = MonetaryContextBuilder.of(Money.class);
String value = config.get("org.javamoney.moneta.Money.defaults.mathContext");
if (value!=null) {
switch (value.toUpperCase(Locale.ENGLISH)) {
case "DECIMAL32":
Logger.getLogger(Money.class.getName()).info(
"Using MathContext.DECIMAL32");
builder.set(MathContext.DECIMAL32);
break;
case "DECIMAL64":
Logger.getLogger(Money.class.getName()).info(
"Using MathContext.DECIMAL64");
builder.set(MathContext.DECIMAL64);
break;
case "DECIMAL128":
Logger.getLogger(Money.class.getName()).info(
"Using MathContext.DECIMAL128");
builder.set(MathContext.DECIMAL128);
break;
case "UNLIMITED":
Logger.getLogger(Money.class.getName()).info(
"Using MathContext.UNLIMITED");
builder.set(MathContext.UNLIMITED);
break;
default:
Logger.getLogger(Money.class.getName()).warning(
"Found invalid MathContext: " + value + ", using default MathContext.DECIMAL64");
builder.set(MathContext.DECIMAL64);
}
} else {
Logger.getLogger(Money.class.getName()).info(
"Using default MathContext.DECIMAL64");
builder.set(MathContext.DECIMAL64);
}
return builder.build();
}
示例8: createMonetaryContextNonNullConfig
import javax.money.MonetaryContext; //导入依赖的package包/类
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;
}
示例9: testGetMonetaryContext
import javax.money.MonetaryContext; //导入依赖的package包/类
/**
* Test method for
* {@link RoundedMoney#getContext()} .
*/
@Test
public void testGetMonetaryContext() {
RoundedMoney m = RoundedMoney.of(10, "CHF");
assertEquals(RoundedMoney.DEFAULT_MONETARY_CONTEXT, m.getContext());
MonetaryContext mc =
MonetaryContextBuilder.of(RoundedMoney.class).setPrecision(2345).set(RoundingMode.CEILING).build();
m = RoundedMoney.of(10, "CHF", mc);
assertEquals(m.getContext().get(RoundingMode.class), RoundingMode.CEILING);
assertEquals((int) m.getContext().getInt("precision"), 2345);
}
示例10: testWithMonetaryContext
import javax.money.MonetaryContext; //导入依赖的package包/类
/**
* 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());
}
示例11: getBigDecimal
import javax.money.MonetaryContext; //导入依赖的package包/类
/**
* Creates a {@link BigDecimal} from the given {@link Number} doing the
* valid conversion depending the type given, if a {@link MonetaryContext}
* is given, it is applied to the number returned.
*
* @param num the number type
* @return the corresponding {@link BigDecimal}
*/
public static BigDecimal getBigDecimal(Number num, MonetaryContext moneyContext) {
BigDecimal bd = getBigDecimal(num);
if (Objects.nonNull(moneyContext)) {
MathContext mc = getMathContext(moneyContext, RoundingMode.HALF_EVEN);
bd = new BigDecimal(bd.toString(), mc);
if (moneyContext.getMaxScale() > 0) {
LOG.log(Level.FINE, "Got Max Scale %n", moneyContext.getMaxScale());
bd = bd.setScale(moneyContext.getMaxScale(), mc.getRoundingMode());
}
}
return bd;
}
示例12: getMathContext
import javax.money.MonetaryContext; //导入依赖的package包/类
/**
* Evaluates the {@link MathContext} from the given {@link MonetaryContext}.
*
* @param monetaryContext the {@link MonetaryContext}
* @param defaultMode the default {@link RoundingMode}, to be used if no one is set
* in {@link MonetaryContext}.
* @return the corresponding {@link MathContext}
*/
public static MathContext getMathContext(MonetaryContext monetaryContext, RoundingMode defaultMode) {
MathContext ctx = monetaryContext.get(MathContext.class);
if (Objects.nonNull(ctx)) {
return ctx;
}
RoundingMode roundingMode = monetaryContext.get(RoundingMode.class);
if (roundingMode == null) {
roundingMode = Optional.ofNullable(defaultMode).orElse(RoundingMode.HALF_EVEN);
}
return new MathContext(monetaryContext.getPrecision(), roundingMode);
}
示例13: createContext
import javax.money.MonetaryContext; //导入依赖的package包/类
private MonetaryContext createContext(MonetaryAmountFactoryQuery factoryQuery) {
MonetaryContextBuilder contextBuilder = MonetaryContextBuilder.of();
if (Objects.nonNull(factoryQuery.getPrecision())) {
contextBuilder.setPrecision(factoryQuery.getPrecision());
}
if (Objects.nonNull(factoryQuery.get(RoundingMode.class))) {
contextBuilder.set(factoryQuery.get(RoundingMode.class));
}
return contextBuilder.build();
}
示例14: isPrecisionOK
import javax.money.MonetaryContext; //导入依赖的package包/类
private boolean isPrecisionOK(MonetaryAmountFactoryQuery requiredContext, MonetaryContext maxMonetaryContext){
if(maxMonetaryContext.getPrecision() == 0){
return true;
}
if(requiredContext.getPrecision() != null){
if(requiredContext.getPrecision() == 0){
return false;
}
if(requiredContext.getPrecision() > maxMonetaryContext.getPrecision()){
return false;
}
}
return null == requiredContext.getMaxScale() ||
requiredContext.getMaxScale() <= maxMonetaryContext.getMaxScale();
}
示例15: getContext
import javax.money.MonetaryContext; //导入依赖的package包/类
public MonetaryContext getContext() {
try {
Map<String, String> config = MonetaryConfig.getConfig();
String value = config.get("org.javamoney.moneta.Money.defaults.precision");
if (Objects.nonNull(value)) {
return createMonetaryContextNonNullConfig(config, Integer.parseInt(value));
} else {
return createContextWithConfig(config);
}
} catch (Exception e) {
Logger.getLogger(DefaultMonetaryContextFactory.class.getName())
.log(Level.SEVERE, "Error evaluating default NumericContext, using default (NumericContext.NUM64).", e);
return MonetaryContextBuilder.of(Money.class).set(MathContext.DECIMAL64).build();
}
}