task4/solution07/src/org/apidesign/apifest08/currency/Convertor.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/Convertor.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.util.Currency;
japod@6
     4
japod@6
     5
/** This is the skeleton class for your API. You need to make it public, so
japod@6
     6
 * it is accessible to your client code (currently in Task1Test.java) file.
japod@6
     7
 * <p>
japod@6
     8
 * Feel free to create additional classes or rename this one, just keep all
japod@6
     9
 * the API and its implementation in this package. Do not spread it outside
japod@6
    10
 * to other packages.
japod@6
    11
 */
japod@6
    12
public interface Convertor {
japod@6
    13
    
japod@6
    14
    /**
japod@6
    15
     * Converts by taking a request and producing a response.
japod@6
    16
     * If a convertor finds it cannot perform the requested conversion,
japod@6
    17
     * it should return a non-null {@link ConversionResult} that has null {@link ConversionResult#getNetAmount()}.
japod@6
    18
     * A convertor must not convert to a different currency than the one specified in the request.
japod@6
    19
     * <p>
japod@6
    20
     * When the need comes to extend the semantics, one subclasses the ConversionRequest and/or ConversionResult classes.
japod@6
    21
     * <p>
japod@6
    22
     * This method can be called as many times as you like.
japod@18
    23
     * A {@link Convertor} shall be considered immutable wrt calls to {@link #convert(org.apidesign.apifest08.currency.Convertor.ConversionRequest).
japod@6
    24
     * This method of a single {@link Convertor} can be called from many threads concurrently.
japod@6
    25
     * @param req the conversion request; mustn't be null
japod@6
    26
     * @return the result of carrying out the conversion request; never null
japod@6
    27
     * @throws IllegalRequestSubtypeException when the particular implementation cannot handle a specific ConversionRequest type
japod@6
    28
     */
japod@6
    29
    public ConversionResult convert( final ConversionRequest req ) throws IllegalRequestSubtypeException;
japod@6
    30
japod@6
    31
    /**
japod@6
    32
     * The request for converting a monetary amout into another currency.
japod@6
    33
     * Immutable.
japod@6
    34
     */
japod@6
    35
    public class ConversionRequest {
japod@6
    36
        
japod@6
    37
        private final MonetaryAmount srcAmount;
japod@6
    38
        private final Currency tgtCurrency;
japod@6
    39
japod@6
    40
        /**
japod@6
    41
         * A request to convert srcAmount into tgtCurrency.
japod@6
    42
         * @param srcAmount the source amount; must not be null
japod@6
    43
         * @param tgtCurrency the currency we want it in afterwards; must not be null
japod@6
    44
         */
japod@6
    45
        public ConversionRequest( final MonetaryAmount srcAmount, final Currency tgtCurrency ) {
japod@6
    46
            this.srcAmount = srcAmount;
japod@6
    47
            this.tgtCurrency = tgtCurrency;
japod@6
    48
            if ( srcAmount == null ) {
japod@6
    49
                throw new NullPointerException( "The source amount" );
japod@6
    50
            }
japod@6
    51
            if ( tgtCurrency == null ) {
japod@6
    52
                throw new NullPointerException( "The target currency" );
japod@6
    53
            }
japod@6
    54
            if ( srcAmount.getCurrency().equals( tgtCurrency ) ) {
japod@6
    55
                throw new IllegalArgumentException( "Cannot request conversion from " + srcAmount.getCurrency() + " to " + tgtCurrency );
japod@6
    56
            }
japod@6
    57
        }
japod@6
    58
japod@6
    59
        /**
japod@6
    60
         * The source amount.
japod@6
    61
         */
japod@6
    62
        public MonetaryAmount getSrcAmount() {
japod@6
    63
            return srcAmount;
japod@6
    64
        }
japod@6
    65
japod@6
    66
        /**
japod@6
    67
         * The target currency.
japod@6
    68
         */
japod@6
    69
        public Currency getTgtCurrency() {
japod@6
    70
            return tgtCurrency;
japod@6
    71
        }
japod@6
    72
        
japod@6
    73
    }
japod@6
    74
    
japod@6
    75
    /**
japod@6
    76
     * The result of converting a monetary amount into another currency.
japod@6
    77
     * For now it records just the net amount one recieves from the conversion.
japod@6
    78
     * Immutable.
japod@6
    79
     * <p>
japod@6
    80
     * <b>Extension note:</b> 
japod@6
    81
     * Other items can be added further down the road, as the need for them arises.
japod@6
    82
     * These items might provide info on other aspects of the conversion,
japod@6
    83
     * such as the fee or a reason why the conversion might not be admissible.
japod@6
    84
     */
japod@6
    85
    public class ConversionResult {
japod@6
    86
        
japod@6
    87
        private final MonetaryAmount netAmount;
japod@6
    88
        
japod@6
    89
        /**
japod@6
    90
         * A new conversion result.
japod@6
    91
         * @param netAmount the amount one recieves from the conversion; 
japod@6
    92
         * null means the conversion was not admissible
japod@6
    93
         */
japod@6
    94
        public ConversionResult( final MonetaryAmount netAmount ) {
japod@6
    95
            this.netAmount = netAmount;
japod@6
    96
        }
japod@6
    97
        
japod@6
    98
        /**
japod@6
    99
         * The amount one recieves from the conversion.
japod@6
   100
         * If null, the conversion is not admissible.
japod@6
   101
         * @return the amount
japod@6
   102
         */
japod@6
   103
        public MonetaryAmount getNetAmount() {
japod@6
   104
            return netAmount;
japod@6
   105
        }
japod@6
   106
        
japod@6
   107
    }
japod@6
   108
    
japod@6
   109
}