org.javamoney.moneta.RoundedMoney Java Examples

The following examples show how to use org.javamoney.moneta.RoundedMoney. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MoneyModule.java    From jackson-datatype-money with MIT License 6 votes vote down vote up
private MoneyModule(final AmountWriter<?> writer,
        final FieldNames names,
        final MonetaryAmountFormatFactory formatFactory,
        final MonetaryAmountFactory<? extends MonetaryAmount> amountFactory,
        final MonetaryAmountFactory<FastMoney> fastMoneyFactory,
        final MonetaryAmountFactory<Money> moneyFactory,
        final MonetaryAmountFactory<RoundedMoney> roundedMoneyFactory) {

    this.writer = writer;
    this.names = names;
    this.formatFactory = formatFactory;
    this.amountFactory = amountFactory;
    this.fastMoneyFactory = fastMoneyFactory;
    this.moneyFactory = moneyFactory;
    this.roundedMoneyFactory = roundedMoneyFactory;
}
 
Example #2
Source File: MoneyModule.java    From jackson-datatype-money with MIT License 6 votes vote down vote up
@Override
public void setupModule(final SetupContext context) {
    final SimpleSerializers serializers = new SimpleSerializers();
    serializers.addSerializer(CurrencyUnit.class, new CurrencyUnitSerializer());
    serializers.addSerializer(MonetaryAmount.class, new MonetaryAmountSerializer(names, writer, formatFactory));
    context.addSerializers(serializers);

    final SimpleDeserializers deserializers = new SimpleDeserializers();
    deserializers.addDeserializer(CurrencyUnit.class, new CurrencyUnitDeserializer());
    deserializers.addDeserializer(MonetaryAmount.class, new MonetaryAmountDeserializer<>(amountFactory, names));
    // for reading into concrete implementation types
    deserializers.addDeserializer(Money.class, new MonetaryAmountDeserializer<>(moneyFactory, names));
    deserializers.addDeserializer(FastMoney.class, new MonetaryAmountDeserializer<>(fastMoneyFactory, names));
    deserializers.addDeserializer(RoundedMoney.class, new MonetaryAmountDeserializer<>(roundedMoneyFactory, names));
    context.addDeserializers(deserializers);
}
 
Example #3
Source File: MonetaryFormatsTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
/**
 * Tests formatting and parsing back the values using all available locales.
 */
@Test
public void testRoundRobinForAllLocales_RoundedMoney(){
    String report = "";
    Locale defaultLocale = Locale.getDefault();
    try {
        for (Locale locale : Locale.getAvailableLocales()) {
            Locale.setDefault(locale);
            try {
                RoundedMoney money = RoundedMoney.of(1.2, "EUR");
                if (!money.equals(RoundedMoney.parse(money.toString()))) {
                    report += "FAILED : " + locale + "(" + money.toString() + ")\n";
                } else {
                    report += "SUCCESS: " + locale + "\n";
                }
            }catch(Exception e){
                report += "ERROR: " + locale + " -> " + e + "\n";
            }
        }
        assertFalse(report.contains("FAILED"),"Formatting and parsing failed for some locales:\n\n"+report);
    }finally{
        Locale.setDefault(defaultLocale);
    }
}
 
Example #4
Source File: PrecisionContextRoundedOperatorTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney() {
	int scale = 4;
	MathContext mathContext = new MathContext(scale, RoundingMode.HALF_EVEN);

	CurrencyUnit real = Monetary.getCurrency("BRL");
	MonetaryAmount money = Money.of(BigDecimal.valueOf(35.34567), real);

	PrecisionContextRoundedOperator monetaryOperator = PrecisionContextRoundedOperator.of(mathContext);
	MonetaryAmount result = monetaryOperator.apply(money);
	assertTrue(RoundedMoney.class.isInstance(result));
	assertEquals(result.getCurrency(), real);
	assertEquals(result.getNumber().getPrecision(), scale);
	assertEquals(BigDecimal.valueOf(35.35), result.getNumber().numberValue(BigDecimal.class));



}
 
Example #5
Source File: PrecisionScaleRoundedOperatorTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney() {
	int scale = 3;
	int precision = 5;

	CurrencyUnit real = Monetary.getCurrency("BRL");
	BigDecimal valueOf = BigDecimal.valueOf(35.34567);
	MonetaryAmount money = Money.of(valueOf, real);

	MathContext mathContext = new MathContext(precision, RoundingMode.HALF_EVEN);
	PrecisionScaleRoundedOperator monetaryOperator = PrecisionScaleRoundedOperator.of(scale, mathContext);

	MonetaryAmount result = monetaryOperator.apply(money);
	assertTrue(RoundedMoney.class.isInstance(result));
	assertEquals(result.getCurrency(), real);
	assertEquals(result.getNumber().getScale(), scale);
	assertEquals(result.getNumber().getPrecision(), precision);
	assertEquals(BigDecimal.valueOf(35.346), result.getNumber().numberValue(BigDecimal.class));



}
 
Example #6
Source File: BasicsTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMonetaryAmountTypes() throws Exception {
    Collection<Class<? extends MonetaryAmount>> types = basics.getMonetaryAmountTypes();
    assertNotNull(types);
    assertTrue(types.contains(Money.class));
    assertTrue(types.contains(FastMoney.class));
    assertTrue(types.contains(RoundedMoney.class));
}
 
Example #7
Source File: ScaleRoundedOperatorTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney() {
	int scale = 4;

	CurrencyUnit real = Monetary.getCurrency("BRL");
	MonetaryAmount money = Money.of(BigDecimal.valueOf(35.34567), real);

	ScaleRoundedOperator monetaryOperator = ScaleRoundedOperator.of(scale, RoundingMode.HALF_EVEN);
	MonetaryAmount result = monetaryOperator.apply(money);
	assertTrue(RoundedMoney.class.isInstance(result));
	assertEquals(result.getCurrency(), real);
	assertEquals(result.getNumber().getScale(), scale);
	assertEquals(BigDecimal.valueOf(35.3457), result.getNumber().numberValue(BigDecimal.class));

}
 
Example #8
Source File: PrecisionContextRoundedOperator.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
	RoundedMoney roundedMoney = RoundedMoney.from(Objects.requireNonNull(amount));
	BigDecimal numberValue = roundedMoney.getNumber().numberValue(BigDecimal.class);
	BigDecimal numberRounded = numberValue.round(mathContext);
	return RoundedMoney.of(numberRounded, roundedMoney.getCurrency(), this);
}
 
Example #9
Source File: ScaleRoundedOperator.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
	RoundedMoney roundedMoney = RoundedMoney.from(Objects.requireNonNull(amount));
	BigDecimal numberValue = roundedMoney.getNumber().numberValue(BigDecimal.class);
	BigDecimal numberRounded = numberValue.setScale(scale, roundingMode);
	return RoundedMoney.of(numberRounded, roundedMoney.getCurrency(), this);
}
 
Example #10
Source File: MonetaryAmountDeserializerTest.java    From jackson-datatype-money with MIT License 5 votes vote down vote up
static Iterable<Arguments> data() {
    return Arrays.asList(
            of(MonetaryAmount.class, module -> module),
            of(FastMoney.class, MoneyModule::withFastMoney),
            of(Money.class, MoneyModule::withMoney),
            of(RoundedMoney.class, MoneyModule::withRoundedMoney),
            of(RoundedMoney.class, module -> module.withRoundedMoney(Monetary.getDefaultRounding()))
    );
}
 
Example #11
Source File: MoneyModule.java    From jackson-datatype-money with MIT License 5 votes vote down vote up
/**
 * @see RoundedMoney
 * @param rounding the rounding operator
 * @return new {@link MoneyModule} using {@link RoundedMoney} with the given {@link MonetaryRounding}
 */
public MoneyModule withRoundedMoney(final MonetaryOperator rounding) {
    final MonetaryAmountFactory<RoundedMoney> factory = (amount, currency) ->
            RoundedMoney.of(amount, currency, rounding);

    return new MoneyModule(writer, names, formatFactory, factory,
            fastMoneyFactory, moneyFactory, factory);
}
 
Example #12
Source File: RoundedMoneyProducer.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public MonetaryAmount create(CurrencyUnit currency, Number number) {
	return RoundedMoney.of(Objects.requireNonNull(number), Objects.requireNonNull(currency), operator);
}
 
Example #13
Source File: DefaultMonetaryRoundedFactory.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public MonetaryAmount create(Number number, CurrencyUnit currencyUnit) {
	return RoundedMoney.of(requireNonNull(number), requireNonNull(currencyUnit), roundingOperator);
}
 
Example #14
Source File: MonetaryAmountSerializerTest.java    From jackson-datatype-money with MIT License 4 votes vote down vote up
static Iterable<MonetaryAmount> fractions() {
    return Arrays.asList(
            FastMoney.of(0.0001, "EUR"),
            Money.of(0.0001, "EUR"),
            RoundedMoney.of(0.0001, "EUR", getDefaultRounding()));
}
 
Example #15
Source File: RoundedMoneyProducer.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public MonetaryAmount create(CurrencyUnit currency, Number number) {
	return RoundedMoney.of(Objects.requireNonNull(number), Objects.requireNonNull(currency), operator);
}
 
Example #16
Source File: RoundedMoneyAmountFactory.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
protected RoundedMoney create(Number number, CurrencyUnit currency, MonetaryContext monetaryContext) {
    return RoundedMoney.of(number, currency, MonetaryContext.from(monetaryContext, RoundedMoney.class));
}
 
Example #17
Source File: RoundedMoneyAmountFactory.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public Class<RoundedMoney> getAmountType() {
    return RoundedMoney.class;
}
 
Example #18
Source File: RoundedMoneyAmountFactoryProvider.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public Class<RoundedMoney> getAmountType(){
    return RoundedMoney.class;
}
 
Example #19
Source File: RoundedMoneyAmountFactoryProvider.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public MonetaryAmountFactory<RoundedMoney> createMonetaryAmountFactory(){
    return new RoundedMoneyAmountFactory();
}
 
Example #20
Source File: RoundedMoneyProducerTest.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldCreateUsingRoundedMoneyImplementation() {
	Long value = 10L;
	MonetaryAmount amount = producer.create(currency, value);
	assertTrue(RoundedMoney.class.isInstance(amount));
}
 
Example #21
Source File: MonetaryAmountSerializerTest.java    From jackson-datatype-money with MIT License 4 votes vote down vote up
static Iterable<MonetaryAmount> hundreds() {
    return Arrays.asList(
            FastMoney.of(100, "EUR"),
            Money.of(100, "EUR"),
            RoundedMoney.of(100, "EUR", getDefaultRounding()));
}
 
Example #22
Source File: MonetaryAmountSerializerTest.java    From jackson-datatype-money with MIT License 4 votes vote down vote up
static Iterable<MonetaryAmount> amounts() {
    return Arrays.asList(
            FastMoney.of(29.95, "EUR"),
            Money.of(29.95, "EUR"),
            RoundedMoney.of(29.95, "EUR", getDefaultRounding()));
}
 
Example #23
Source File: MoneyModule.java    From jackson-datatype-money with MIT License 4 votes vote down vote up
public MoneyModule() {
    this(new DecimalAmountWriter(), FieldNames.defaults(), MonetaryAmountFormatFactory.NONE,
            Money::of, FastMoney::of, Money::of, RoundedMoney::of);
}
 
Example #24
Source File: DefaultMonetaryRoundedFactoryTest.java    From jsr354-ri with Apache License 2.0 3 votes vote down vote up
@Test
public void shouldCreateMonetaryAmount() {
	MonetaryAmount monetaryAmount = factory.create(BigDecimal.TEN, real);

	assertNotNull(monetaryAmount);
	assertTrue(RoundedMoney.class.isInstance(monetaryAmount));

	RoundedMoney roundedMoney = RoundedMoney.class.cast(monetaryAmount);
	assertEquals(real, roundedMoney.getCurrency());
	assertEquals(BigDecimal.TEN, roundedMoney.getNumber().numberValue(BigDecimal.class));


}