1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jadira.usertype.moneyandcurrency.moneta;
17
18 import java.math.BigDecimal;
19
20 import javax.money.CurrencyUnit;
21 import javax.money.MonetaryAmount;
22
23 import org.jadira.usertype.moneyandcurrency.legacyjdk.columnmapper.BigDecimalBigDecimalColumnMapper;
24 import org.jadira.usertype.moneyandcurrency.moneta.columnmapper.StringColumnCurrencyUnitMapper;
25 import org.jadira.usertype.spi.shared.AbstractMultiColumnUserType;
26 import org.jadira.usertype.spi.shared.ColumnMapper;
27 import org.javamoney.moneta.FastMoney;
28
29
30
31
32 public class PersistentFastMoneyAmountAndCurrency extends AbstractMultiColumnUserType<MonetaryAmount> {
33
34 private static final long serialVersionUID = 3735995469031558183L;
35
36 private static final ColumnMapper<?, ?>[] COLUMN_MAPPERS = new ColumnMapper<?, ?>[] { new StringColumnCurrencyUnitMapper(), new BigDecimalBigDecimalColumnMapper() };
37
38 private static final String[] PROPERTY_NAMES = new String[]{ "currencyUnit", "amount" };
39
40 @Override
41 protected ColumnMapper<?, ?>[] getColumnMappers() {
42 return COLUMN_MAPPERS;
43 }
44
45 @Override
46 protected FastMoney fromConvertedColumns(Object[] convertedColumns) {
47
48 CurrencyUnit currencyUnitPart = (CurrencyUnit) convertedColumns[0];
49 BigDecimal amountPart = (BigDecimal) convertedColumns[1];
50 FastMoney money = FastMoney.of(amountPart, currencyUnitPart);
51
52 return money;
53 }
54
55 @Override
56 protected Object[] toConvertedColumns(MonetaryAmount value) {
57
58 return new Object[] { value.getCurrency(), value.getNumber().numberValue(BigDecimal.class) };
59 }
60
61 @Override
62 public String[] getPropertyNames() {
63 return PROPERTY_NAMES;
64 }
65 }