task4/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java
changeset 61 58ec6da75f6f
parent 50 03c5c5dc94e7
child 67 bf7622ec1713
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,66 @@
     1.4 +
     1.5 +package org.apidesign.apifest08.currency;
     1.6 +
     1.7 +import java.util.ArrayList;
     1.8 +import java.util.List;
     1.9 +
    1.10 +public final class ConvertorFactory {
    1.11 +
    1.12 +    //Singleton
    1.13 +    private static ConvertorFactory thisFactory = new ConvertorFactory();    
    1.14 +    private ConvertorFactory() {};    
    1.15 +    public static ConvertorFactory newInstance() { //ehm, mistake - it should be named getInstance
    1.16 +        return thisFactory;
    1.17 +    }        
    1.18 +    
    1.19 +    public Convertor createConvertor(String currency1, String currency2, Rate rate) {
    1.20 +        return new Convertor(currency1, currency2, rate);
    1.21 +    }
    1.22 +    
    1.23 +    public Convertor createConvertor(String currency1, String currency2, int amount1, int amount2) {
    1.24 +        return new Convertor(currency1, currency2, new Rate(amount1, amount2));
    1.25 +    }
    1.26 +
    1.27 +    public Convertor createConvertor(String currency1, String currency2, double amount1, double amount2) {
    1.28 +        return new Convertor(currency1, currency2, new Rate(amount1, amount2));
    1.29 +    }
    1.30 +    
    1.31 +    public Convertor createConvertor(String currency1, String currency2, double rate) {
    1.32 +        return new Convertor(currency1, currency2, new Rate(rate));
    1.33 +    }
    1.34 +
    1.35 +    public Convertor createConvertor(CurrencyRate currencyRate) {
    1.36 +        return new Convertor(currencyRate);
    1.37 +    }
    1.38 +
    1.39 +    public Convertor createConvertor(CurrencyRate ... currencyRates) {
    1.40 +        return new Convertor(currencyRates);
    1.41 +    }
    1.42 +
    1.43 +    public Convertor mergeConvertors(Convertor ... convertors) {
    1.44 +        if (convertors == null) {
    1.45 +            throw new IllegalArgumentException("Parameter cannot be null.");
    1.46 +        }
    1.47 +        if (convertors.length == 0) {
    1.48 +            throw new IllegalArgumentException("Convertors cannot be empty.");
    1.49 +        }
    1.50 +        List<CurrencyRate> currRates = new ArrayList<CurrencyRate>();
    1.51 +        List<Pair<String,String>> currPairs = new ArrayList<Pair<String,String>>();
    1.52 +        for (Convertor convertor : convertors) {
    1.53 +            if (convertor == null) {
    1.54 +                throw new IllegalArgumentException("Parameter cannot be null.");                
    1.55 +            }
    1.56 +            for (CurrencyRate currRate : convertor.getCurrencyRates()) {
    1.57 +                Pair<String,String> currPair = new Pair<String,String>(currRate.getCurrency1(), currRate.getCurrency2());
    1.58 +                if (currPairs.contains(currPair)) {
    1.59 +                    throw new IllegalArgumentException("Cannot merge - convertors contain same currency rates.");
    1.60 +                }
    1.61 +                currPairs.add(currPair);
    1.62 +                currRates.add(currRate);
    1.63 +            }
    1.64 +        }
    1.65 +        
    1.66 +        return new Convertor(currRates.toArray(new CurrencyRate[0]));
    1.67 +    }
    1.68 +
    1.69 +}