task4/solution07/src/org/apidesign/apifest08/currency/TimeRangeSpecificConvertor.java
1 package org.apidesign.apifest08.currency;
3 import java.util.Currency;
5 import org.apidesign.apifest08.currency.Convertor.ConversionRequest;
6 import org.apidesign.apifest08.currency.Convertor.ConversionResult;
9 * This {@link Convertor} delegates to an underlying one, provided that the request specifies time between from (included) and till (excluded).
10 * Otherwise it just refuses to convert.
13 public class TimeRangeSpecificConvertor extends DelegatingConvertor {
15 private final Date from;
16 private final Date till;
19 * A new time range specific convertor.
20 * @param old the underlying convertor one delegates to
21 * @param from the beginning of the time interval
22 * @param till the end of the time interval
23 * @throws IllegalArgumentException unless from comes before till
25 public TimeRangeSpecificConvertor( final Convertor old, final Date from, final Date till ) {
29 if (! from.before( till ) ) {
30 throw new IllegalArgumentException( "from must come before till" );
35 * The beginning of the time interval.
37 public Date getFrom() {
42 * The end of the time interval.
44 public Date getTill() {
49 * The conversion method.
50 * Takes a {@link TimeSpecificConversionRequest}, other {@link ConversionRequest}s will result in an exception being thrown.
51 * @param req the request; must not be null; must be {@link TimeSpecificConversionRequest} or a subclass thereof
52 * @return the response
53 * @throws IllegalRequestSubtypeException iff req is not instance of {@link TimeSpecificConversionRequest}
56 public ConversionResult convert( final ConversionRequest req ) {
57 if ( req instanceof TimeSpecificConversionRequest ) {
58 final Date time = ( (TimeSpecificConversionRequest) req ).getTime();
59 if ( ( from == null || !from.after(time) ) && ( till == null || time.before(till) ) ) {
60 return super.convert( req );
62 return new ConversionResult( null );
65 throw new IllegalRequestSubtypeException( this.getClass().getName() + ".convert() requires a TimeSpecificConversionRequest to be passed in" );
70 * The request for converting a monetary amount into another currency using the conditions that apply at a particular time.
73 public static class TimeSpecificConversionRequest extends Convertor.ConversionRequest {
75 private final Date time;
78 * A request to convert srcAmount into tgtCurrency at the current time.
79 * @param srcAmount the source amount; must not be null
80 * @param tgtCurrency the currency we want it in afterwards; must not be null
82 public TimeSpecificConversionRequest( final MonetaryAmount srcAmount, final Currency tgtCurrency ) {
83 this( srcAmount, tgtCurrency, new Date() );
87 * A request to convert srcAmount into tgtCurrency at given time.
88 * @param srcAmount the source amount; must not be null
89 * @param tgtCurrency the currency we want it in afterwards; must not be null
90 * @param time the time instant when the conversion is to be carried out
92 public TimeSpecificConversionRequest( final MonetaryAmount srcAmount, final Currency tgtCurrency, final Date time ) {
93 super( srcAmount, tgtCurrency );
96 throw new NullPointerException( "The time of conversion" );
101 * The time as of which the conversion is to be carried out.
103 public Date getTime() {