2 package org.apidesign.apifest08.currency;
4 import java.util.ArrayList;
5 import java.util.Collection;
9 public final class ConvertorFactory {
12 private static ConvertorFactory thisFactory = new ConvertorFactory();
13 private ConvertorFactory() {};
14 public static ConvertorFactory newInstance() { //ehm, mistake - it should be named getInstance
18 public Convertor createConvertor(String currency1, String currency2, Rate rate) {
19 return new Convertor(currency1, currency2, rate);
22 public Convertor createConvertor(String currency1, String currency2, int amount1, int amount2) {
23 return new Convertor(currency1, currency2, new Rate(amount1, amount2));
26 public Convertor createConvertor(String currency1, String currency2, double amount1, double amount2) {
27 return new Convertor(currency1, currency2, new Rate(amount1, amount2));
30 public Convertor createConvertor(String currency1, String currency2, double rate) {
31 return new Convertor(currency1, currency2, new Rate(rate));
34 public Convertor createConvertor(CurrencyRate currencyRate) {
35 return new Convertor(currencyRate);
38 public Convertor createConvertor(CurrencyRate ... currencyRates) {
39 return new Convertor(currencyRates);
42 public Convertor mergeConvertors(Convertor ... convertors) {
43 if (convertors == null) {
44 throw new IllegalArgumentException("Parameter cannot be null.");
46 if (convertors.length == 0) {
47 throw new IllegalArgumentException("Convertors cannot be empty.");
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.");
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.");
60 currPairs.add(currPair);
61 currRates.add(currRate);
65 return new Convertor(currRates.toArray(new CurrencyRate[0]));
68 public Convertor mergeConvertorsIgnoreEqualCurrencies(Convertor ... convertors) {
69 if (convertors == null) {
70 throw new IllegalArgumentException("Parameter cannot be null.");
72 if (convertors.length == 0) {
73 throw new IllegalArgumentException("Convertors cannot be empty.");
75 List<TimeLimitedCurrencyRate> currRates = new ArrayList<TimeLimitedCurrencyRate>();
76 for (Convertor convertor : convertors) {
77 if (convertor == null) {
78 throw new IllegalArgumentException("Parameter cannot be null.");
80 currRates.addAll(convertor.getTimeLimitedCurrencyRates());
83 return new Convertor(4, currRates.toArray(new TimeLimitedCurrencyRate[0]));
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");
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() {
100 public long getToTime() {
103 public String getCurrency1() {
104 return timeLimitedCurrencyRate.getCurrency1();
106 public String getCurrency2() {
107 return timeLimitedCurrencyRate.getCurrency2();
109 public Rate getRate() {
110 return timeLimitedCurrencyRate.getRate();
115 return new Convertor(4, filteredRates.toArray(new TimeLimitedCurrencyRate[0]));