task4/solution07/src/org/apidesign/apifest08/currency/MonetaryAmount.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 45 task3/solution07/src/org/apidesign/apifest08/currency/MonetaryAmount.java@251d0ed461fb
permissions -rw-r--r--
Copying structure for task4
japod@6
     1
package org.apidesign.apifest08.currency;
japod@6
     2
japod@6
     3
import java.math.BigDecimal;
japod@6
     4
import java.util.Currency;
japod@6
     5
japod@6
     6
/**
japod@6
     7
 * An amount of a currency.
japod@6
     8
 * Immutable.
japod@6
     9
 * @author jdvorak
japod@6
    10
 */
japod@6
    11
public class MonetaryAmount {
japod@6
    12
japod@6
    13
    private final BigDecimal amount;
japod@6
    14
    private final Currency currency;
japod@6
    15
    
japod@6
    16
    /**
japod@6
    17
     * A new amount.
japod@6
    18
     * @param amount the quantity of the currency; must not be null
japod@6
    19
     * @param currency the currency; must not be null
japod@6
    20
     */
japod@6
    21
    public MonetaryAmount( final BigDecimal amount, final Currency currency ) {
japod@6
    22
        this.amount = amount;
japod@6
    23
        this.currency = currency;
japod@6
    24
        if ( amount == null ) {
japod@6
    25
            throw new NullPointerException( "The amount" );
japod@6
    26
        }
japod@6
    27
        if ( currency == null ) {
japod@6
    28
            throw new NullPointerException( "The currency" );
japod@6
    29
        }
japod@6
    30
    }
japod@6
    31
    
japod@6
    32
    /**
japod@6
    33
     * A new amount.
japod@6
    34
     * @param amount the quantity of the currency; must not be null
japod@6
    35
     * @param currency the currency; must not be null
japod@6
    36
     */
japod@6
    37
    public MonetaryAmount( final double amount, final Currency currency ) {
japod@6
    38
        this( new BigDecimal( amount ), currency );
japod@6
    39
    }
japod@6
    40
    
japod@6
    41
    /**
japod@6
    42
     * The amount.
japod@6
    43
     * @return the amount
japod@6
    44
     */
japod@6
    45
    public BigDecimal getAmount() {
japod@6
    46
        return amount;
japod@6
    47
    }
japod@6
    48
    
japod@6
    49
    /**
japod@6
    50
     * The currency.
japod@6
    51
     * @return the currency
japod@6
    52
     */
japod@6
    53
    public Currency getCurrency() {
japod@6
    54
        return currency;
japod@6
    55
    }
japod@6
    56
japod@6
    57
    /**
japod@6
    58
     * The string representation of the monetary amount.
japod@6
    59
     * @return the amount, a non-breakable space, the currency
japod@6
    60
     */
japod@6
    61
    @Override
japod@6
    62
    public String toString() {
japod@6
    63
        return amount.toPlainString() + "\u00a0" + currency.toString();
japod@6
    64
    }
japod@6
    65
    
japod@6
    66
    /**
japod@6
    67
     * Two monetary amounts are equal to each other iff they have equal amounts of equal currencies.
japod@6
    68
     * @param other the other object
japod@6
    69
     * @return equality
japod@6
    70
     */
japod@6
    71
    @Override
japod@6
    72
    public boolean equals( final Object other ) {
japod@6
    73
        if ( other instanceof MonetaryAmount ) {
japod@6
    74
            final MonetaryAmount otherMonetaryAmount = (MonetaryAmount) other;
japod@6
    75
            return getAmount().equals( otherMonetaryAmount.getAmount() ) && getCurrency().equals( otherMonetaryAmount.getCurrency() );
japod@6
    76
        }
japod@6
    77
        return false;
japod@6
    78
    }
japod@6
    79
    
japod@6
    80
    /**
japod@6
    81
     * The hash code combines the hash codes of the amount and of the currency.
japod@6
    82
     * @return hash code
japod@6
    83
     */
japod@6
    84
    @Override
japod@6
    85
    public int hashCode() {
japod@6
    86
        return amount.hashCode() * 37 + currency.hashCode();
japod@6
    87
    }
japod@6
    88
    
japod@6
    89
}