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