@Test public void shouldReturnPositiveValue() { CurrencyUnit currency = Monetary.getCurrency("EUR"); MonetaryAmount money = Money.parse("EUR 2.0"); MonetaryAmount result = operator.apply(money); assertEquals(result.getCurrency(), currency); assertEquals(result.getNumber().doubleValue(), 0.5); }
@Override public MonetaryAmount apply(MonetaryAmount amount) { requireNonNull(amount, "Amount required."); NumberValue num = amount.getNumber(); BigDecimal one = ONE.setScale(num.getScale() < 5 ? 5 : num.getScale(), HALF_EVEN); BigDecimal reciprocal = one.divide(num.numberValue(BigDecimal.class), HALF_EVEN); return amount.getFactory().setNumber(reciprocal).create(); }
ReciprocalOperator implements MonetaryOperator { @Override public MonetaryAmount apply(MonetaryAmount amount) { requireNonNull(amount, "Amount required."); NumberValue num = amount.getNumber(); BigDecimal one = ONE.setScale(num.getScale() < 5 ? 5 : num.getScale(), HALF_EVEN); BigDecimal reciprocal = one.divide(num.numberValue(BigDecimal.class), HALF_EVEN); return amount.getFactory().setNumber(reciprocal).create(); } }
ReciprocalOperator implements MonetaryOperator { @Override public MonetaryAmount apply(MonetaryAmount amount) { requireNonNull(amount, "Amount required."); NumberValue num = amount.getNumber(); BigDecimal one = ONE.setScale(num.getScale() < 5 ? 5 : num.getScale(), HALF_EVEN); BigDecimal reciprocal = one.divide(num.numberValue(BigDecimal.class), HALF_EVEN); return amount.getFactory().setNumber(reciprocal).create(); } ReciprocalOperator(); }
ReciprocalOperator implements MonetaryOperator { @Override public MonetaryAmount apply(MonetaryAmount amount) { requireNonNull(amount, "Amount required."); NumberValue num = amount.getNumber(); BigDecimal one = ONE.setScale(num.getScale() < 5 ? 5 : num.getScale(), HALF_EVEN); BigDecimal reciprocal = one.divide(num.numberValue(BigDecimal.class), HALF_EVEN); return amount.getFactory().setNumber(reciprocal).create(); } ReciprocalOperator(); @Override MonetaryAmount apply(MonetaryAmount amount); }
ReciprocalOperator implements MonetaryOperator { @Override public MonetaryAmount apply(MonetaryAmount amount) { requireNonNull(amount, "Amount required."); NumberValue num = amount.getNumber(); BigDecimal one = ONE.setScale(num.getScale() < 5 ? 5 : num.getScale(), HALF_EVEN); BigDecimal reciprocal = one.divide(num.numberValue(BigDecimal.class), HALF_EVEN); return amount.getFactory().setNumber(reciprocal).create(); } ReciprocalOperator(); @Override MonetaryAmount apply(MonetaryAmount amount);  }