javax.money.MonetaryOperator Java Examples

The following examples show how to use javax.money.MonetaryOperator. 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: MonetaryRoundedFactoryBuilderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnMathContextOperator() {
	int precision = 6;
	RoundingMode roundingMode = RoundingMode.HALF_EVEN;
	MonetaryRoundedFactory factory = MonetaryRoundedFactory
			.withRoundingMode(roundingMode).withPrecision(precision)
			.build();

	assertNotNull(factory);
	MonetaryOperator roundingOperator = factory.getRoundingOperator();
	assertNotNull(roundingOperator);
	assertTrue(PrecisionContextRoundedOperator.class.isInstance(roundingOperator));
	MathContext result = PrecisionContextRoundedOperator.class.cast(roundingOperator).getMathContext();

	assertEquals(precision, result.getPrecision());
	assertEquals(roundingMode, result.getRoundingMode());

}
 
Example #2
Source File: MonetaryRoundedFactoryBuilderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnScaleRoundingOperator() {
	int scale = 6;
	RoundingMode roundingMode = RoundingMode.HALF_EVEN;
	MonetaryRoundedFactory factory = MonetaryRoundedFactory
			.withRoundingMode(roundingMode).withScale(scale)
			.build();

	assertNotNull(factory);
	MonetaryOperator roundingOperator = factory.getRoundingOperator();
	assertNotNull(roundingOperator);
	assertTrue(ScaleRoundedOperator.class.isInstance(roundingOperator));
	ScaleRoundedOperator result = ScaleRoundedOperator.class.cast(roundingOperator);

	assertEquals(scale, result.getScale());
	assertEquals(roundingMode, result.getRoundingMode());

}
 
Example #3
Source File: MonetaryRoundedFactoryBuilderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnMathContextScaleOperator() {
	int precision = 6;
	int scale = 3;
	RoundingMode roundingMode = RoundingMode.HALF_EVEN;
	MonetaryRoundedFactory factory = MonetaryRoundedFactory
			.withRoundingMode(roundingMode).withPrecision(precision).withScale(scale)
			.build();

	assertNotNull(factory);
	MonetaryOperator roundingOperator = factory.getRoundingOperator();
	assertNotNull(roundingOperator);
	assertTrue(PrecisionScaleRoundedOperator.class.isInstance(roundingOperator));
	PrecisionScaleRoundedOperator result = PrecisionScaleRoundedOperator.class.cast(roundingOperator);

	assertEquals(precision, result.getMathContext().getPrecision());
	assertEquals(scale, result.getScale());
	assertEquals(roundingMode, result.getMathContext().getRoundingMode());

}
 
Example #4
Source File: MonetaryRoundedFactoryBuilderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnScaleMathContextOperator() {
	int precision = 6;
	int scale = 3;
	RoundingMode roundingMode = RoundingMode.HALF_EVEN;
	MonetaryRoundedFactory factory = MonetaryRoundedFactory
			.withRoundingMode(roundingMode).withScale(scale).withPrecision(precision)
			.build();

	assertNotNull(factory);
	MonetaryOperator roundingOperator = factory.getRoundingOperator();
	assertNotNull(roundingOperator);
	assertTrue(PrecisionScaleRoundedOperator.class.isInstance(roundingOperator));
	PrecisionScaleRoundedOperator result = PrecisionScaleRoundedOperator.class.cast(roundingOperator);

	assertEquals(precision, result.getMathContext().getPrecision());
	assertEquals(scale, result.getScale());
	assertEquals(roundingMode, result.getMathContext().getRoundingMode());

}
 
Example #5
Source File: PresentValueContinuousCompoundingTest.java    From javamoney-lib with Apache License 2.0 6 votes vote down vote up
/**
 * Method: of(Rate rate, int periods)
 *
 * @throws Exception the exception
 */
@Test
public void testOfAndApply() throws Exception {
    Money money = Money.of(100, "CHF");
    MonetaryOperator rounding = Monetary.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
            .build());
    assertEquals(Money.of(BigDecimal.valueOf(95.12), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(0.05, 1))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(90.48), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(0.05, 2))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(46.3), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(0.07, 11))).with(rounding));

    assertEquals(Money.of(BigDecimal.valueOf(100.00), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(0.05, 0))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(100.00), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(-0.05, 0))).with(rounding));

    assertEquals(Money.of(BigDecimal.valueOf(105.13), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(-0.05, 1))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(110.52), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(-0.05, 2))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(215.98), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(-0.07, 11))).with(rounding));
}
 
Example #6
Source File: FastMoneyTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for {@link org.javamoney.moneta.FastMoney#with(javax.money.MonetaryOperator)} .
 */
@Test
public void testWithMonetaryOperator() {
    MonetaryOperator adj = amount -> FastMoney.of(-100, amount.getCurrency());
    FastMoney m = FastMoney.of(new BigDecimal("1.2345"), "XXX");
    FastMoney a = m.with(adj);
    assertNotNull(a);
    assertNotSame(m, a);
    assertEquals(m.getCurrency(), a.getCurrency());
    assertEquals(FastMoney.of(-100, m.getCurrency()), a);
    adj = amount -> amount.multiply(2).getFactory().setCurrency(Monetary.getCurrency("CHF")).create();
    a = m.with(adj);
    assertNotNull(a);
    assertNotSame(m, a);
    assertEquals(Monetary.getCurrency("CHF"), a.getCurrency());
    assertEquals(FastMoney.of(1.2345 * 2, a.getCurrency()), a);
}
 
Example #7
Source File: CompositeMonetaryOperator.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiates a new Composite monetary operator.
 *
 * @param operations the operations
 */
@SafeVarargs
public CompositeMonetaryOperator(Iterable<MonetaryOperator>... operations) {
    if (operations != null) {
        for (Iterable<MonetaryOperator> iterable : operations) {
            for (MonetaryOperator monetaryOperator : iterable) {
                functions.add(monetaryOperator);
            }
        }
    }
}
 
Example #8
Source File: MonetaryOperatorsTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#percent(java.lang.Number)}
 * .
 */
@Test
public void testPercentToString() {
	MonetaryOperator p = MonetaryOperators.percent((short) 25);
	assertTrue(p.toString().contains("25"));
	assertTrue(p.toString().contains("%"));
}
 
Example #9
Source File: MonetaryRoundedFactoryBuilderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnTheSameMonetaryOperator() {
	MonetaryOperator monetaryOperator = m -> m;
	MonetaryRoundedFactory factory = MonetaryRoundedFactory.of(monetaryOperator);
	assertNotNull(factory);
	assertEquals(monetaryOperator, factory.getRoundingOperator());

}
 
Example #10
Source File: PresentValueContinuousCompoundingTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Method: calculate(MonetaryAmount amount, Rate rate, int periods)
 *
 * @throws Exception the exception
 */
@Test
public void testCalculate() throws Exception {
    Money money = Money.of(100, "CHF");
    MonetaryOperator rounding = Monetary.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
            .build());
    assertEquals(Money.of(BigDecimal.valueOf(95.12), "CHF"), PresentValueContinuousCompounding.calculate(money, RateAndPeriods.of(0.05, 1)).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(90.48), "CHF"), PresentValueContinuousCompounding.calculate(money, RateAndPeriods.of(0.05, 2)).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(46.3), "CHF"), PresentValueContinuousCompounding.calculate(money, RateAndPeriods.of(0.07, 11)).with(rounding));
}
 
Example #11
Source File: FutureValueTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test formula.
 */
@Test
public void testFormula() {
    Money money = Money.of(100, "CHF");
    MonetaryOperator rounding = Monetary.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
            .build());
    assertEquals(Money.of(BigDecimal.valueOf(105.00), "CHF"), FutureValue.calculate(money, RateAndPeriods.of(0.05, 1)).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(110.25), "CHF"), FutureValue.calculate(money, RateAndPeriods.of(0.05, 2)).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(210.49), "CHF"), FutureValue.calculate(money, RateAndPeriods.of(0.07, 11)).with(rounding));
}
 
Example #12
Source File: FutureValueTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Method: calculate(MonetaryAmount amount, Rate rate, int periods)
 *
 * @throws Exception the exception
 */
@Test
public void testCalculate() throws Exception {
    Money money = Money.of(100, "CHF");
    MonetaryOperator rounding = Monetary.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
            .build());
    assertEquals(Money.of(BigDecimal.valueOf(105.00), "CHF"), FutureValue.calculate(money, RateAndPeriods.of(0.05, 1)).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(110.25), "CHF"), FutureValue.calculate(money, RateAndPeriods.of(0.05, 2)).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(210.49), "CHF"), FutureValue.calculate(money, RateAndPeriods.of(0.07, 11)).with(rounding));
}
 
Example #13
Source File: FutureValueTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Method: of(Rate ratePerPeriod, int periods).
 *
 * @throws Exception the exception
 */
@Test
public void testOfAndApply() throws Exception {
    Money money = Money.of(100, "CHF");
    MonetaryOperator rounding = Monetary.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
            .build());
    assertEquals(Money.of(BigDecimal.valueOf(105.00), "CHF"), money.with(FutureValue .of(RateAndPeriods.of(0.05, 1))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(110.25), "CHF"), money.with(FutureValue.of(RateAndPeriods.of(0.05, 2))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(210.49), "CHF"), money.with(FutureValue.of(RateAndPeriods.of(0.07, 11))).with(rounding));
}
 
Example #14
Source File: CompositeMonetaryOperator.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Override
public MonetaryAmount apply(MonetaryAmount value) {
    MonetaryAmount amount = value;
    for (MonetaryOperator op : functions) {
        amount = op.apply(amount);
    }
    return amount;
}
 
Example #15
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 #16
Source File: MonetaryRoundedFactoryBuilderTest.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void shouldReturnErrorWhenMonetaryOperatorIsNull() {
	MonetaryOperator roundingOperator = null;
	MonetaryRoundedFactory.of(roundingOperator);
	fail();
}
 
Example #17
Source File: MoneyAlgorithmics.java    From javamoney-examples with Apache License 2.0 4 votes vote down vote up
/**
 * Implement a {@link javax.money.MonetaryOperator} that calculates the total of all amounts operated on.
 */
public MonetaryOperator getTotalOperator() {
    throw new UnsupportedOperationException();
}
 
Example #18
Source File: DefaultMonetaryRoundedFactoryTest.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldReturnGetRoundingOperator() {
	MonetaryOperator roundingOperator = factory.getRoundingOperator();
	assertEquals(identical, roundingOperator);
}
 
Example #19
Source File: RoundedMoneyProducer.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
public MonetaryOperator getOperator() {
	return operator;
}
 
Example #20
Source File: DefaultMonetaryRoundedFactory.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public MonetaryOperator getRoundingOperator() {
	return roundingOperator;
}
 
Example #21
Source File: DefaultMonetaryRoundedFactory.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
public DefaultMonetaryRoundedFactory(MonetaryOperator roundingOperator) {
	this.roundingOperator = roundingOperator;
}
 
Example #22
Source File: RoundedMoneyProducer.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
public MonetaryOperator getOperator() {
	return operator;
}
 
Example #23
Source File: MonetaryOperators.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
* Gets the percentage of the amount.
* <p>
* This returns the monetary amount in percent. For example, for 10% 'EUR
* 2.35' will return 0.235.
* <p>
*<pre>
*{@code
*MonetaryAmount money = Money.parse("EUR 200.0");
*MonetaryAmount result = ConversionOperators.percent(BigDecimal.TEN).apply(money);//EUR 20.0
*}
*</pre>
* @param decimal the value to percent
* @return the percent of {@link MonetaryOperator}
*/
  public static MonetaryOperator percent(BigDecimal decimal) {
      return new PercentOperator(decimal);
  }
 
Example #24
Source File: CompositeMonetaryOperator.java    From javamoney-lib with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiates a new Composite monetary operator.
 *
 * @param name       the name
 * @param operations the operations
 */
public CompositeMonetaryOperator(String name, MonetaryOperator... operations) {
    Objects.requireNonNull(name);
    Collections.addAll(functions, operations);
}
 
Example #25
Source File: MoneyAlgorithmics.java    From javamoney-examples with Apache License 2.0 2 votes vote down vote up
/**
 * Implement a {@link javax.money.MonetaryOperator} that simply duplicates the amount given.
 *
 * @return the duplicating operator.
 */
public MonetaryOperator getDuplicateOperator() {
    throw new UnsupportedOperationException();
}
 
Example #26
Source File: ConversionOperators.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Do exchange of currency, in other words, create the monetary amount with the
 * same value but with currency different.
 * <p>
 * For example, 'EUR 2.35', using the currency 'USD' as exchange parameter, will return 'USD 2.35',
 * and 'BHD -1.345', using the currency 'USD' as exchange parameter, will return 'BHD -1.345'.
 * <p>
 *<pre>
 *{@code
 *Currency real = Monetary.getCurrency("BRL");
 *MonetaryAmount money = Money.parse("EUR 2.355");
 *MonetaryAmount result = ConversionOperators.exchangeCurrency(real).apply(money);//BRL 2.355
 *}
 *</pre>
 * @param currencyUnit the currency to be used
 * @return the major part as {@link MonetaryOperator}
 * @since 1.0.1
 */
public static MonetaryOperator exchange(CurrencyUnit currencyUnit){
	return new ExchangeCurrencyOperator(Objects.requireNonNull(currencyUnit));
}
 
Example #27
Source File: RoundedMoneyProducer.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Creates this producer using this operator
 * as rounding operator in all MonetaryAmount produced.
 * @param operator the rounding, not null.
 * @throws NullPointerException if operator is null
 */
public RoundedMoneyProducer(MonetaryOperator operator) {
	this.operator = Objects.requireNonNull(operator);
}
 
Example #28
Source File: MonetaryOperators.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
* Gets the reciprocal of {@link MonetaryAmount}
* <p>
* Gets the amount as reciprocal, multiplicative inverse value (1/n).
* <p>
*<pre>
*{@code
*MonetaryAmount money = Money.parse("EUR 2.0");
*MonetaryAmount result = ConversionOperators.reciprocal().apply(money);//EUR 0.5
*}
*</pre>
* @return the reciprocal part as {@link MonetaryOperator}
*/
  public static MonetaryOperator reciprocal() {
      return RECIPROCAL;
  }
 
Example #29
Source File: MonetaryOperators.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
* Gets the permil of the amount.
* <p>
* This returns the monetary amount in permil. For example, for 10% 'EUR
* 2.35' will return 0.235.
* <p>
*<pre>
*{@code
*MonetaryAmount money = Money.parse("EUR EUR 2.35");
*MonetaryAmount result = ConversionOperators.permil(BigDecimal.TEN).apply(money);//EUR 0.0235
*}
*</pre>
* @return the permil as {@link MonetaryOperator}
*/
  public static MonetaryOperator permil(BigDecimal decimal) {
      return new PermilOperator(decimal);
  }
 
Example #30
Source File: MonetaryOperators.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link #percent(BigDecimal)} converting
 * this number to {@link BigDecimal} and using the {@link #DEFAULT_MATH_CONTEXT}
 * @param number to be converted to {@link BigDecimal}
 * @see #permil(BigDecimal)
 * @return the permil {@link MonetaryOperator}
 */
public static MonetaryOperator permil(Number number) {
    return permil(number, DEFAULT_MATH_CONTEXT);
}