task2/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java
changeset 49 de033c457bed
parent 29 f6073056b9fe
     1.1 --- a/task2/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java	Wed Oct 01 10:43:05 2008 +0200
     1.2 +++ b/task2/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java	Wed Oct 08 12:51:52 2008 +0200
     1.3 @@ -1,12 +1,15 @@
     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() {
    1.16 +    public static ConvertorFactory newInstance() { //ehm, mistake - it should be named getInstance
    1.17          return thisFactory;
    1.18      }        
    1.19      
    1.20 @@ -25,4 +28,39 @@
    1.21      public Convertor createConvertor(String currency1, String currency2, double rate) {
    1.22          return new Convertor(currency1, currency2, new Rate(rate));
    1.23      }
    1.24 +
    1.25 +    public Convertor createConvertor(CurrencyRate currencyRate) {
    1.26 +        return new Convertor(currencyRate);
    1.27 +    }
    1.28 +
    1.29 +    public Convertor createConvertor(CurrencyRate ... currencyRates) {
    1.30 +        return new Convertor(currencyRates);
    1.31 +    }
    1.32 +
    1.33 +    public Convertor mergeConvertors(Convertor ... convertors) {
    1.34 +        if (convertors == null) {
    1.35 +            throw new IllegalArgumentException("Parameter cannot be null.");
    1.36 +        }
    1.37 +        if (convertors.length == 0) {
    1.38 +            throw new IllegalArgumentException("Convertors cannot be empty.");
    1.39 +        }
    1.40 +        List<CurrencyRate> currRates = new ArrayList<CurrencyRate>();
    1.41 +        List<Pair<String,String>> currPairs = new ArrayList<Pair<String,String>>();
    1.42 +        for (Convertor convertor : convertors) {
    1.43 +            if (convertor == null) {
    1.44 +                throw new IllegalArgumentException("Parameter cannot be null.");                
    1.45 +            }
    1.46 +            for (CurrencyRate currRate : convertor.getCurrencyRates()) {
    1.47 +                Pair<String,String> currPair = new Pair<String,String>(currRate.getCurrency1(), currRate.getCurrency2());
    1.48 +                if (currPairs.contains(currPair)) {
    1.49 +                    throw new IllegalArgumentException("Cannot merge - convertors contain same currency rates.");
    1.50 +                }
    1.51 +                currPairs.add(currPair);
    1.52 +                currRates.add(currRate);
    1.53 +            }
    1.54 +        }
    1.55 +        
    1.56 +        return new Convertor(currRates.toArray(new CurrencyRate[0]));
    1.57 +    }
    1.58 +
    1.59  }