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