private void assertSameCurrency(final Money money) { if (!money.getCurrency().equals(getCurrency())) { throw new IllegalArgumentException("Not the same currency: " + getCurrency() + " & " + money.getCurrency()); } }
private void assertSameCurrency(final Money money) { if (!money.getCurrency().equals(getCurrency())) { throw new IllegalArgumentException("Not the same currency: " + getCurrency() + " & " + money.getCurrency()); } }
@Override public Object getValueForDatastoreMapping(final NucleusContext nucleusCtx, final int index, final Object value) { final Money m = ((Money) value); switch (index) { case 0: return m.longValue(); case 1: return m.getCurrency(); } throw new IndexOutOfBoundsException("Wrong index: " + index); }
@Override public String getCurrencyCode(final ObjectAdapter object) { final Money money = (Money) object.getObject(); if (money == null) { return ""; } else { return money.getCurrency(); } }
@Override protected String doEncode(final Object object) { final Money money = (Money) object; final String value = String.valueOf(money.doubleValue()) + " " + money.getCurrency(); return value; }
@Override public String titleString(final Object object, final Localization localization) { if (object == null) { return ""; } final Money money = (Money) object; final boolean localCurrency = LOCAL_CURRENCY_CODE.equals(money.getCurrency()); if (localCurrency) { return DEFAULT_CURRENCY_FORMAT.format(money.doubleValue()); } else { return DEFAULT_NUMBER_FORMAT.format(money.doubleValue()) + " " + money.getCurrency(); } }
@Override protected Object preparedStatementObject(final int index, final Object o) { if (o instanceof Money) { if (index == 0) { return ((Money) o).doubleValue(); } else { return ((Money) o).getCurrency(); } } else { throw new PersistFailedException("Invalid object type " + o.getClass().getCanonicalName() + " for MoneyValueMapper"); } }
Money m1 = new Money(money1.getCurrency(),new Value(22,70)); Money m2 = new Money(money1.getCurrency(),new Value(22,70)).add(money2); assertEquals("currencies differ", m1.getCurrency(), m2.getCurrency()); assertEquals("values differ", m1.getValue(), m2.getValue());
private Money parseDerivedValue(final Object original, final String entry) { Money money = (Money) original; if (money == null || money.getCurrency().equals(LOCAL_CURRENCY_CODE)) { try { final double value = DEFAULT_CURRENCY_FORMAT.parse(entry).doubleValue(); money = new Money(value, LOCAL_CURRENCY_CODE); return money; } catch (final ParseException ignore) { } } try { final double value = DEFAULT_NUMBER_FORMAT.parse(entry).doubleValue(); final String currencyCode = money == null ? defaultCurrencyCode : money.getCurrency(); money = new Money(value, currencyCode); return money; } catch (final ParseException ex) { throw new TextEntryParseException("Not a distinguishable money value " + entry, ex); } }
@Test public void thatMoneyAddedIsNotAltered(){ Money mockMoney = EasyMock.createMock(Money.class); EasyMock.expect( mockMoney.getCurrency() ).andReturn( "EUR" ); EasyMock.expect( mockMoney.getAmount() ).andReturn( 3.0 ); EasyMock.replay(mockMoney); Money realMoney = new Money(); realMoney.addMoney( mockMoney ); EasyMock.verify( mockMoney ); }
@Override public void setObject(final ExecutionContext ec, final PreparedStatement preparedStmt, final int[] exprIndex, final Object value) { if (value instanceof Money) { final Money m = ((Money) value); getDatastoreMapping(0).setLong(preparedStmt, exprIndex[0], m.longValue()); getDatastoreMapping(1).setString(preparedStmt, exprIndex[1], m.getCurrency()); } else { getDatastoreMapping(0).setLong(preparedStmt, exprIndex[0], 0l); getDatastoreMapping(1).setString(preparedStmt, exprIndex[1], null); } }