task4/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java
changeset 67 bf7622ec1713
parent 61 58ec6da75f6f
     1.1 --- a/task4/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java	Sat Oct 11 23:38:46 2008 +0200
     1.2 +++ b/task4/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java	Fri Oct 17 17:35:52 2008 +0200
     1.3 @@ -2,6 +2,8 @@
     1.4  package org.apidesign.apifest08.currency;
     1.5  
     1.6  import java.util.ArrayList;
     1.7 +import java.util.Collection;
     1.8 +import java.util.Date;
     1.9  import java.util.List;
    1.10  
    1.11  public final class ConvertorFactory {
    1.12 @@ -63,4 +65,54 @@
    1.13          return new Convertor(currRates.toArray(new CurrencyRate[0]));
    1.14      }
    1.15  
    1.16 +    public Convertor mergeConvertorsIgnoreEqualCurrencies(Convertor ... convertors) {
    1.17 +        if (convertors == null) {
    1.18 +            throw new IllegalArgumentException("Parameter cannot be null.");
    1.19 +        }
    1.20 +        if (convertors.length == 0) {
    1.21 +            throw new IllegalArgumentException("Convertors cannot be empty.");
    1.22 +        }
    1.23 +        List<TimeLimitedCurrencyRate> currRates = new ArrayList<TimeLimitedCurrencyRate>();
    1.24 +        for (Convertor convertor : convertors) {
    1.25 +            if (convertor == null) {
    1.26 +                throw new IllegalArgumentException("Parameter cannot be null.");
    1.27 +            }
    1.28 +            currRates.addAll(convertor.getTimeLimitedCurrencyRates());
    1.29 +        }
    1.30 +
    1.31 +        return new Convertor(4, currRates.toArray(new TimeLimitedCurrencyRate[0]));
    1.32 +    }
    1.33 +
    1.34 +    public Convertor limitConvertor(Convertor convertor, Date fromDate, Date toDate) {
    1.35 +        if ((convertor == null)||(fromDate == null)||(toDate == null)) {
    1.36 +            throw new IllegalArgumentException("Parameter cannot be null");
    1.37 +        }
    1.38 +        Collection<TimeLimitedCurrencyRate> timeLimitedCurrencyRates = convertor.getTimeLimitedCurrencyRates();
    1.39 +        List<TimeLimitedCurrencyRate> filteredRates = new ArrayList<TimeLimitedCurrencyRate>();
    1.40 +        for (final TimeLimitedCurrencyRate timeLimitedCurrencyRate : timeLimitedCurrencyRates) {
    1.41 +            final long newFrom = java.lang.Math.max(fromDate.getTime(), timeLimitedCurrencyRate.getFromTime());
    1.42 +            final long newTo = java.lang.Math.min(toDate.getTime(), timeLimitedCurrencyRate.getToTime());
    1.43 +            if (newTo >= newFrom) {
    1.44 +                filteredRates.add(new TimeLimitedCurrencyRate() { //create delegate
    1.45 +                    public long getFromTime() {
    1.46 +                        return newFrom;
    1.47 +                    }
    1.48 +                    public long getToTime() {
    1.49 +                        return newTo;
    1.50 +                    }
    1.51 +                    public String getCurrency1() {
    1.52 +                        return timeLimitedCurrencyRate.getCurrency1();
    1.53 +                    }
    1.54 +                    public String getCurrency2() {
    1.55 +                        return timeLimitedCurrencyRate.getCurrency2();
    1.56 +                    }
    1.57 +                    public Rate getRate() {
    1.58 +                        return timeLimitedCurrencyRate.getRate();
    1.59 +                    }
    1.60 +                });
    1.61 +            }
    1.62 +        }
    1.63 +        return new Convertor(4, filteredRates.toArray(new TimeLimitedCurrencyRate[0]));
    1.64 +    }
    1.65 +
    1.66  }