task4/solution14/src/org/apidesign/apifest08/currency/ConvertorFactory.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 17 Oct 2008 17:35:52 +0200
changeset 67 bf7622ec1713
parent 61 58ec6da75f6f
permissions -rw-r--r--
Solution 14, task4
     1 
     2 package org.apidesign.apifest08.currency;
     3 
     4 import java.util.ArrayList;
     5 import java.util.Collection;
     6 import java.util.Date;
     7 import java.util.List;
     8 
     9 public final class ConvertorFactory {
    10 
    11     //Singleton
    12     private static ConvertorFactory thisFactory = new ConvertorFactory();    
    13     private ConvertorFactory() {};    
    14     public static ConvertorFactory newInstance() { //ehm, mistake - it should be named getInstance
    15         return thisFactory;
    16     }        
    17     
    18     public Convertor createConvertor(String currency1, String currency2, Rate rate) {
    19         return new Convertor(currency1, currency2, rate);
    20     }
    21     
    22     public Convertor createConvertor(String currency1, String currency2, int amount1, int amount2) {
    23         return new Convertor(currency1, currency2, new Rate(amount1, amount2));
    24     }
    25 
    26     public Convertor createConvertor(String currency1, String currency2, double amount1, double amount2) {
    27         return new Convertor(currency1, currency2, new Rate(amount1, amount2));
    28     }
    29     
    30     public Convertor createConvertor(String currency1, String currency2, double rate) {
    31         return new Convertor(currency1, currency2, new Rate(rate));
    32     }
    33 
    34     public Convertor createConvertor(CurrencyRate currencyRate) {
    35         return new Convertor(currencyRate);
    36     }
    37 
    38     public Convertor createConvertor(CurrencyRate ... currencyRates) {
    39         return new Convertor(currencyRates);
    40     }
    41 
    42     public Convertor mergeConvertors(Convertor ... convertors) {
    43         if (convertors == null) {
    44             throw new IllegalArgumentException("Parameter cannot be null.");
    45         }
    46         if (convertors.length == 0) {
    47             throw new IllegalArgumentException("Convertors cannot be empty.");
    48         }
    49         List<CurrencyRate> currRates = new ArrayList<CurrencyRate>();
    50         List<Pair<String,String>> currPairs = new ArrayList<Pair<String,String>>();
    51         for (Convertor convertor : convertors) {
    52             if (convertor == null) {
    53                 throw new IllegalArgumentException("Parameter cannot be null.");                
    54             }
    55             for (CurrencyRate currRate : convertor.getCurrencyRates()) {
    56                 Pair<String,String> currPair = new Pair<String,String>(currRate.getCurrency1(), currRate.getCurrency2());
    57                 if (currPairs.contains(currPair)) {
    58                     throw new IllegalArgumentException("Cannot merge - convertors contain same currency rates.");
    59                 }
    60                 currPairs.add(currPair);
    61                 currRates.add(currRate);
    62             }
    63         }
    64         
    65         return new Convertor(currRates.toArray(new CurrencyRate[0]));
    66     }
    67 
    68     public Convertor mergeConvertorsIgnoreEqualCurrencies(Convertor ... convertors) {
    69         if (convertors == null) {
    70             throw new IllegalArgumentException("Parameter cannot be null.");
    71         }
    72         if (convertors.length == 0) {
    73             throw new IllegalArgumentException("Convertors cannot be empty.");
    74         }
    75         List<TimeLimitedCurrencyRate> currRates = new ArrayList<TimeLimitedCurrencyRate>();
    76         for (Convertor convertor : convertors) {
    77             if (convertor == null) {
    78                 throw new IllegalArgumentException("Parameter cannot be null.");
    79             }
    80             currRates.addAll(convertor.getTimeLimitedCurrencyRates());
    81         }
    82 
    83         return new Convertor(4, currRates.toArray(new TimeLimitedCurrencyRate[0]));
    84     }
    85 
    86     public Convertor limitConvertor(Convertor convertor, Date fromDate, Date toDate) {
    87         if ((convertor == null)||(fromDate == null)||(toDate == null)) {
    88             throw new IllegalArgumentException("Parameter cannot be null");
    89         }
    90         Collection<TimeLimitedCurrencyRate> timeLimitedCurrencyRates = convertor.getTimeLimitedCurrencyRates();
    91         List<TimeLimitedCurrencyRate> filteredRates = new ArrayList<TimeLimitedCurrencyRate>();
    92         for (final TimeLimitedCurrencyRate timeLimitedCurrencyRate : timeLimitedCurrencyRates) {
    93             final long newFrom = java.lang.Math.max(fromDate.getTime(), timeLimitedCurrencyRate.getFromTime());
    94             final long newTo = java.lang.Math.min(toDate.getTime(), timeLimitedCurrencyRate.getToTime());
    95             if (newTo >= newFrom) {
    96                 filteredRates.add(new TimeLimitedCurrencyRate() { //create delegate
    97                     public long getFromTime() {
    98                         return newFrom;
    99                     }
   100                     public long getToTime() {
   101                         return newTo;
   102                     }
   103                     public String getCurrency1() {
   104                         return timeLimitedCurrencyRate.getCurrency1();
   105                     }
   106                     public String getCurrency2() {
   107                         return timeLimitedCurrencyRate.getCurrency2();
   108                     }
   109                     public Rate getRate() {
   110                         return timeLimitedCurrencyRate.getRate();
   111                     }
   112                 });
   113             }
   114         }
   115         return new Convertor(4, filteredRates.toArray(new TimeLimitedCurrencyRate[0]));
   116     }
   117 
   118 }