private static MonetaryAmount[] of(final MonetaryAmount[] array) { for (int i = 0; i < array.length; i++) { array[i] = of(array[i]); } return array; }
/** * Creates a {@link MonetaryAmount} from a cent amount as String and a currencyUnit as String. * * @param amount the amount in cents which consists just of numbers * @param currencyCode the currency code as String like "EUR" for EURO * @return amount */ public static MonetaryAmount ofCentsAndCurrencyCode(final String amount, final String currencyCode) { return ofCents(Long.parseLong(amount), currencyCode); }
public static MonetaryAmount of(final MonetaryAmount money) { return new MoneyImpl(money); }
public static MonetaryAmount of(final BigDecimal amount, final String currencyCode) { final CurrencyUnit currency = createCurrencyByCode(currencyCode); return MoneyImpl.of(amount, currency); }
public static MonetaryAmount ofCents(final long centAmount, final String currencyCode) { return ofCents(centAmount, createCurrencyByCode(currencyCode)); }
private Money asMoney() { return Money.of(getNumber(), getCurrency()); }
@Test public void ofCentsStringAndCurrencyCode() { final MonetaryAmount amount = MoneyImpl.ofCentsAndCurrencyCode("123456", "EUR"); assertThat(amount).isEqualTo(MoneyImpl.of(new BigDecimal("1234.56"), EUR)); }
@Override public int hashCode() { return asMoney().getNumberStripped().hashCode() + getCurrency().getCurrencyCode().hashCode(); }
@Override public int hashCode() { return asMoney().hashCode(); }
@Override public boolean equals(final Object obj) { return Optional.ofNullable(obj) .filter(other -> other != null) .filter(other -> other instanceof MonetaryAmount) .map(o -> (MonetaryAmount) o) .map(o -> o.getCurrency().equals(getCurrency()) && o.isEqualTo(money)) .orElse(false); }
private Money asMoney() { return Money.of(getNumber(), getCurrency()); }
@Override public boolean equals(final Object obj) { boolean isEqual = false; if (obj != null && obj instanceof MonetaryAmount) { final MonetaryAmount other = (MonetaryAmount) obj; final Money thisAsMoneyImpl = asMoney(); final String currencyCode = thisAsMoneyImpl.getCurrency().getCurrencyCode(); final String otherCurrencyCode = other.getCurrency().getCurrencyCode(); final BigDecimal numberStripped = thisAsMoneyImpl.getNumberStripped(); final BigDecimal otherNumberStripped = Money.of(other.getNumber(), other.getCurrency()).getNumberStripped(); isEqual = currencyCode.equals(otherCurrencyCode) && numberStripped.equals(otherNumberStripped); } return isEqual; }
@Override public MonetaryAmount zero() { return MoneyImpl.of(0, currency); }
@Test public void yen() throws Exception { assertThat(MoneyImpl.ofCents(123456, "JPY")).isEqualTo(MoneyImpl.of(123456, Monetary.getCurrency("JPY"))); assertThat(MoneyImpl.of(123456, Monetary.getCurrency("JPY")).query(MonetaryQueries.extractMajorPart())).isEqualTo(123456); assertThat(MoneyImpl.of(123456, Monetary.getCurrency("JPY")).query(MonetaryQueries.extractMajorPart())).isEqualTo(123456); assertThat(MoneyImpl.of(new BigDecimal("1234.56"), Monetary.getCurrency("JPY")).query(MonetaryQueries.extractMajorPart())).isEqualTo(1234); assertThat(MoneyImpl.of(new BigDecimal("1234.56"), Monetary.getCurrency("JPY")).query(MonetaryQueries.extractMinorPart())).isEqualTo(0); }
public static MonetaryAmount calculateSubTotal(final CartLike<?> cartLike) { final MonetaryAmount zeroAmount = MoneyImpl.ofCents(0, cartLike.getCurrency()); return cartLike.getLineItems().stream() .map(lineItem -> { final MonetaryAmount basePrice = calculateFinalPrice(lineItem.getPrice()); return basePrice.multiply(lineItem.getQuantity()); }).reduce(zeroAmount, (left, right) -> left.add(right)); }
public static MonetaryAmount of(final MonetaryAmount money) { return new MoneyImpl(money); }
public static MonetaryAmount zeroAmount(final CurrencyUnit currency) { return MoneyImpl.of(0, currency); }
@Test public void calculatesSubTotalPrice() throws Exception { assertThat(emptyCart.calculateSubTotalPrice()).isEqualTo(MoneyImpl.ofCents(0, EUR)); final MonetaryAmount sumLineItems = MoneyImpl.ofCents(188750 + 65000 + 8400, EUR); assertThat(cartWithoutTaxes.calculateSubTotalPrice()).isEqualTo(sumLineItems); assertThat(cartWithTaxes.calculateSubTotalPrice()).isEqualTo(sumLineItems); } }