1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task4/solution04/src/org/apidesign/apifest08/currency/DatedConvertorImpl.java Fri Oct 17 17:40:14 2008 +0200
1.3 @@ -0,0 +1,82 @@
1.4 +package org.apidesign.apifest08.currency;
1.5 +
1.6 +import java.math.BigDecimal;
1.7 +import java.util.Currency;
1.8 +import java.util.Date;
1.9 +
1.10 +
1.11 +final class DatedConvertorImpl
1.12 + extends ConvertorImpl
1.13 + implements DatedConvertor
1.14 +{
1.15 + private final DateRange range;
1.16 +
1.17 + DatedConvertorImpl(final DateRange rng,
1.18 + final ExchangeRate r)
1.19 + {
1.20 + super(r);
1.21 +
1.22 + if(rng == null)
1.23 + {
1.24 + throw new IllegalArgumentException("rng cannot be null");
1.25 + }
1.26 +
1.27 + range = rng;
1.28 + }
1.29 +
1.30 + public DateRange getDateRange()
1.31 + {
1.32 + return (range);
1.33 + }
1.34 +
1.35 + @Override
1.36 + public BigDecimal convert(final Currency from,
1.37 + final Currency to,
1.38 + final BigDecimal amount)
1.39 + throws InvalidConversionException
1.40 + {
1.41 + final BigDecimal total;
1.42 +
1.43 + total = convert(from, to, amount, new Date(System.currentTimeMillis()));
1.44 +
1.45 + return (total);
1.46 + }
1.47 +
1.48 + public BigDecimal convert(final Currency from,
1.49 + final Currency to,
1.50 + final BigDecimal amount,
1.51 + final Date date)
1.52 + throws InvalidConversionException
1.53 + {
1.54 + final BigDecimal total;
1.55 +
1.56 + if(range.isInRange(date))
1.57 + {
1.58 + total = super.convert(from, to, amount);
1.59 + }
1.60 + else
1.61 + {
1.62 + throw new InvalidConversionException("cannot convert for date", from);
1.63 + }
1.64 +
1.65 + return (total);
1.66 + }
1.67 +
1.68 + public boolean canConvert(final Currency from,
1.69 + final Currency to,
1.70 + final Date date)
1.71 + {
1.72 + final boolean retVal;
1.73 +
1.74 + if(canConvert(from, to))
1.75 + {
1.76 + retVal = range.isInRange(date);
1.77 + }
1.78 + else
1.79 + {
1.80 + retVal = false;
1.81 + }
1.82 +
1.83 + return (retVal);
1.84 + }
1.85 +}