task4/solution06/src/org/apidesign/apifest08/currency/Amount.java
changeset 61 58ec6da75f6f
parent 45 251d0ed461fb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution06/src/org/apidesign/apifest08/currency/Amount.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,81 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +import static org.apidesign.apifest08.currency.Assert.notNull;
     1.7 +
     1.8 +import java.math.BigDecimal;
     1.9 +import java.math.RoundingMode;
    1.10 +import java.util.Currency;
    1.11 +
    1.12 +/**
    1.13 + * An amount representation. Amount is represented as composition of a value and 
    1.14 + * a currency.
    1.15 + */
    1.16 +public final class Amount {
    1.17 +	
    1.18 +	private final BigDecimal value;
    1.19 +	private final Currency currency;
    1.20 +	private final int scale;
    1.21 +	private final RoundingMode roundingMode;
    1.22 +	
    1.23 +	public static final RoundingMode DEFAULT_ROUNDING = RoundingMode.HALF_EVEN; 
    1.24 +	
    1.25 +	public Amount(final BigDecimal value, final Currency currency) {
    1.26 +		notNull(value, "value");
    1.27 +		notNull(currency, "currency");
    1.28 +		this.value = value;
    1.29 +		this.currency = currency;
    1.30 +		this.scale = currency.getDefaultFractionDigits();
    1.31 +		this.roundingMode = DEFAULT_ROUNDING;
    1.32 +	}
    1.33 +	
    1.34 +	public Amount(final BigDecimal value, final Currency currency, final RoundingMode roundingMode) {
    1.35 +		notNull(value, "value");
    1.36 +		notNull(currency, "currency");
    1.37 +		notNull(roundingMode, "roundingMode");
    1.38 +		
    1.39 +		this.value = value;
    1.40 +		this.currency = currency;
    1.41 +		this.scale = currency.getDefaultFractionDigits();
    1.42 +		this.roundingMode = roundingMode;
    1.43 +	}
    1.44 +	
    1.45 +	public Amount(final long value, final Currency currency) {
    1.46 +		this(BigDecimal.valueOf(value), currency);
    1.47 +	}
    1.48 +	
    1.49 +	public Amount(final String value, final Currency currency) {
    1.50 +		this(new BigDecimal(value), currency);
    1.51 +	}
    1.52 +	
    1.53 +	/**
    1.54 +	 * @return the value with scale of the associated currency and rounded by 
    1.55 +	 * the rounding mode. 
    1.56 +	 */
    1.57 +	public BigDecimal getValue() {
    1.58 +		return value.setScale(scale, roundingMode);
    1.59 +	}
    1.60 +	
    1.61 +	/**
    1.62 +	 * @return the raw (no explicit scale, no explicit rounding) value 
    1.63 +	 */
    1.64 +	public BigDecimal getRawValue() {
    1.65 +		return value;
    1.66 +	}
    1.67 +	
    1.68 +	public Currency getCurrency() {
    1.69 +		return currency;
    1.70 +	}
    1.71 +
    1.72 +	public int getScale() {
    1.73 +		return scale;
    1.74 +	}
    1.75 +
    1.76 +	public RoundingMode getRoundingMode() {
    1.77 +		return roundingMode;
    1.78 +	}
    1.79 +
    1.80 +	@Override
    1.81 +	public String toString() {
    1.82 +		return value + ",- " + currency.toString();
    1.83 +	}	
    1.84 +}