public final class Money {
// métodos de criação
public static Money valueOf(String value,String currencyIsoCode){
return valueOf(new BigDecimal(value),currency)
}
public static Money valueOf(BigDecimal value,String currencyIsoCode){
return valueOf(new BigDecimal(value),Currency.getInstance(currencyIsoCode))
}
public static Money valueOf(BigDecimal value,Currency currency){
return new Money(toLongRepresentation(value,currency),currency);
}
// métodos para redução de, e para, long
private static long toLongRepresentation(BigDecimal value,Currency currency){
return value.movePointRight(currency.getDefaultFractionDigits()).longValue();
}
private static BigDecimal fromLongRepresentation(long amount,Currency currency){
BigDecimal value = new BigDecimal(amount);
return value.movePointLeft(currency.getDefaultFractionDigits());
}
// contrutor privado
private Money (long value,Currency currency){
this.value = value;
this.currency = currency;
}
private long value;
private Currency currency;
public BigDecimal getAmount(){
return fromLongRepresentation(value, currency);
}
public Currency getCurrency(){
return currency;
}
}