task4/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 50 task3/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java@03c5c5dc94e7
child 67 bf7622ec1713
permissions -rw-r--r--
Copying structure for task4
     1 
     2 package org.apidesign.apifest08.currency;
     3 
     4 import java.util.ArrayList;
     5 import java.util.List;
     6 
     7 public final class ConvertorFactory {
     8 
     9     //Singleton
    10     private static ConvertorFactory thisFactory = new ConvertorFactory();    
    11     private ConvertorFactory() {};    
    12     public static ConvertorFactory newInstance() { //ehm, mistake - it should be named getInstance
    13         return thisFactory;
    14     }        
    15     
    16     public Convertor createConvertor(String currency1, String currency2, Rate rate) {
    17         return new Convertor(currency1, currency2, rate);
    18     }
    19     
    20     public Convertor createConvertor(String currency1, String currency2, int amount1, int amount2) {
    21         return new Convertor(currency1, currency2, new Rate(amount1, amount2));
    22     }
    23 
    24     public Convertor createConvertor(String currency1, String currency2, double amount1, double amount2) {
    25         return new Convertor(currency1, currency2, new Rate(amount1, amount2));
    26     }
    27     
    28     public Convertor createConvertor(String currency1, String currency2, double rate) {
    29         return new Convertor(currency1, currency2, new Rate(rate));
    30     }
    31 
    32     public Convertor createConvertor(CurrencyRate currencyRate) {
    33         return new Convertor(currencyRate);
    34     }
    35 
    36     public Convertor createConvertor(CurrencyRate ... currencyRates) {
    37         return new Convertor(currencyRates);
    38     }
    39 
    40     public Convertor mergeConvertors(Convertor ... convertors) {
    41         if (convertors == null) {
    42             throw new IllegalArgumentException("Parameter cannot be null.");
    43         }
    44         if (convertors.length == 0) {
    45             throw new IllegalArgumentException("Convertors cannot be empty.");
    46         }
    47         List<CurrencyRate> currRates = new ArrayList<CurrencyRate>();
    48         List<Pair<String,String>> currPairs = new ArrayList<Pair<String,String>>();
    49         for (Convertor convertor : convertors) {
    50             if (convertor == null) {
    51                 throw new IllegalArgumentException("Parameter cannot be null.");                
    52             }
    53             for (CurrencyRate currRate : convertor.getCurrencyRates()) {
    54                 Pair<String,String> currPair = new Pair<String,String>(currRate.getCurrency1(), currRate.getCurrency2());
    55                 if (currPairs.contains(currPair)) {
    56                     throw new IllegalArgumentException("Cannot merge - convertors contain same currency rates.");
    57                 }
    58                 currPairs.add(currPair);
    59                 currRates.add(currRate);
    60             }
    61         }
    62         
    63         return new Convertor(currRates.toArray(new CurrencyRate[0]));
    64     }
    65 
    66 }