task4/solution07/src/org/apidesign/apifest08/currency/TimeRangeSpecificConvertor.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task4/solution07/src/org/apidesign/apifest08/currency/TimeRangeSpecificConvertor.java Fri Oct 17 17:33:32 2008 +0200
1.3 @@ -0,0 +1,109 @@
1.4 +package org.apidesign.apifest08.currency;
1.5 +
1.6 +import java.util.Currency;
1.7 +import java.util.Date;
1.8 +import org.apidesign.apifest08.currency.Convertor.ConversionRequest;
1.9 +import org.apidesign.apifest08.currency.Convertor.ConversionResult;
1.10 +
1.11 +/**
1.12 + * This {@link Convertor} delegates to an underlying one, provided that the request specifies time between from (included) and till (excluded).
1.13 + * Otherwise it just refuses to convert.
1.14 + * @author jdvorak
1.15 + */
1.16 +public class TimeRangeSpecificConvertor extends DelegatingConvertor {
1.17 +
1.18 + private final Date from;
1.19 + private final Date till;
1.20 +
1.21 + /**
1.22 + * A new time range specific convertor.
1.23 + * @param old the underlying convertor one delegates to
1.24 + * @param from the beginning of the time interval
1.25 + * @param till the end of the time interval
1.26 + * @throws IllegalArgumentException unless from comes before till
1.27 + */
1.28 + public TimeRangeSpecificConvertor( final Convertor old, final Date from, final Date till ) {
1.29 + super( old );
1.30 + this.from = from;
1.31 + this.till = till;
1.32 + if (! from.before( till ) ) {
1.33 + throw new IllegalArgumentException( "from must come before till" );
1.34 + }
1.35 + }
1.36 +
1.37 + /**
1.38 + * The beginning of the time interval.
1.39 + */
1.40 + public Date getFrom() {
1.41 + return from;
1.42 + }
1.43 +
1.44 + /**
1.45 + * The end of the time interval.
1.46 + */
1.47 + public Date getTill() {
1.48 + return till;
1.49 + }
1.50 +
1.51 + /**
1.52 + * The conversion method.
1.53 + * Takes a {@link TimeSpecificConversionRequest}, other {@link ConversionRequest}s will result in an exception being thrown.
1.54 + * @param req the request; must not be null; must be {@link TimeSpecificConversionRequest} or a subclass thereof
1.55 + * @return the response
1.56 + * @throws IllegalRequestSubtypeException iff req is not instance of {@link TimeSpecificConversionRequest}
1.57 + */
1.58 + @Override
1.59 + public ConversionResult convert( final ConversionRequest req ) {
1.60 + if ( req instanceof TimeSpecificConversionRequest ) {
1.61 + final Date time = ( (TimeSpecificConversionRequest) req ).getTime();
1.62 + if ( ( from == null || !from.after(time) ) && ( till == null || time.before(till) ) ) {
1.63 + return super.convert( req );
1.64 + } else {
1.65 + return new ConversionResult( null );
1.66 + }
1.67 + } else {
1.68 + throw new IllegalRequestSubtypeException( this.getClass().getName() + ".convert() requires a TimeSpecificConversionRequest to be passed in" );
1.69 + }
1.70 + }
1.71 +
1.72 + /**
1.73 + * The request for converting a monetary amount into another currency using the conditions that apply at a particular time.
1.74 + * Immutable.
1.75 + */
1.76 + public static class TimeSpecificConversionRequest extends Convertor.ConversionRequest {
1.77 +
1.78 + private final Date time;
1.79 +
1.80 + /**
1.81 + * A request to convert srcAmount into tgtCurrency at the current time.
1.82 + * @param srcAmount the source amount; must not be null
1.83 + * @param tgtCurrency the currency we want it in afterwards; must not be null
1.84 + */
1.85 + public TimeSpecificConversionRequest( final MonetaryAmount srcAmount, final Currency tgtCurrency ) {
1.86 + this( srcAmount, tgtCurrency, new Date() );
1.87 + }
1.88 +
1.89 + /**
1.90 + * A request to convert srcAmount into tgtCurrency at given time.
1.91 + * @param srcAmount the source amount; must not be null
1.92 + * @param tgtCurrency the currency we want it in afterwards; must not be null
1.93 + * @param time the time instant when the conversion is to be carried out
1.94 + */
1.95 + public TimeSpecificConversionRequest( final MonetaryAmount srcAmount, final Currency tgtCurrency, final Date time ) {
1.96 + super( srcAmount, tgtCurrency );
1.97 + this.time = time;
1.98 + if ( time == null ) {
1.99 + throw new NullPointerException( "The time of conversion" );
1.100 + }
1.101 + }
1.102 +
1.103 + /**
1.104 + * The time as of which the conversion is to be carried out.
1.105 + */
1.106 + public Date getTime() {
1.107 + return time;
1.108 + }
1.109 +
1.110 + }
1.111 +
1.112 +}