diff -r 03c5c5dc94e7 -r 58ec6da75f6f task4/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task4/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java Sat Oct 11 23:38:46 2008 +0200 @@ -0,0 +1,66 @@ + +package org.apidesign.apifest08.currency; + +import java.util.ArrayList; +import java.util.List; + +public final class ConvertorFactory { + + //Singleton + private static ConvertorFactory thisFactory = new ConvertorFactory(); + private ConvertorFactory() {}; + public static ConvertorFactory newInstance() { //ehm, mistake - it should be named getInstance + return thisFactory; + } + + public Convertor createConvertor(String currency1, String currency2, Rate rate) { + return new Convertor(currency1, currency2, rate); + } + + public Convertor createConvertor(String currency1, String currency2, int amount1, int amount2) { + return new Convertor(currency1, currency2, new Rate(amount1, amount2)); + } + + public Convertor createConvertor(String currency1, String currency2, double amount1, double amount2) { + return new Convertor(currency1, currency2, new Rate(amount1, amount2)); + } + + public Convertor createConvertor(String currency1, String currency2, double rate) { + return new Convertor(currency1, currency2, new Rate(rate)); + } + + public Convertor createConvertor(CurrencyRate currencyRate) { + return new Convertor(currencyRate); + } + + public Convertor createConvertor(CurrencyRate ... currencyRates) { + return new Convertor(currencyRates); + } + + public Convertor mergeConvertors(Convertor ... convertors) { + if (convertors == null) { + throw new IllegalArgumentException("Parameter cannot be null."); + } + if (convertors.length == 0) { + throw new IllegalArgumentException("Convertors cannot be empty."); + } + List currRates = new ArrayList(); + List> currPairs = new ArrayList>(); + for (Convertor convertor : convertors) { + if (convertor == null) { + throw new IllegalArgumentException("Parameter cannot be null."); + } + for (CurrencyRate currRate : convertor.getCurrencyRates()) { + Pair currPair = new Pair(currRate.getCurrency1(), currRate.getCurrency2()); + if (currPairs.contains(currPair)) { + throw new IllegalArgumentException("Cannot merge - convertors contain same currency rates."); + } + currPairs.add(currPair); + currRates.add(currRate); + } + } + + return new Convertor(currRates.toArray(new CurrencyRate[0])); + } + +}