task4/solution04/src/org/apidesign/apifest08/currency/DateRange.java
changeset 69 420baec87dc5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution04/src/org/apidesign/apifest08/currency/DateRange.java	Fri Oct 17 17:40:14 2008 +0200
     1.3 @@ -0,0 +1,91 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +
     1.7 +import java.util.Date;
     1.8 +
     1.9 +
    1.10 +public final class DateRange
    1.11 +{
    1.12 +    private final Date from;
    1.13 +    private final Date till;
    1.14 +    
    1.15 +    DateRange(final Date f,
    1.16 +              final Date t)
    1.17 +    {
    1.18 +        if(f == null && t != null)
    1.19 +        {
    1.20 +            throw new IllegalArgumentException("f was null but t was not");
    1.21 +        }
    1.22 +        
    1.23 +        if(f != null && t == null)
    1.24 +        {
    1.25 +            throw new IllegalArgumentException("f was null but t was not");
    1.26 +        }
    1.27 +        
    1.28 +        from = f;
    1.29 +        till = t;
    1.30 +    }
    1.31 +    
    1.32 +    public Date getFrom()
    1.33 +    {
    1.34 +        return (from);
    1.35 +    }
    1.36 +    
    1.37 +    public Date getTill()
    1.38 +    {
    1.39 +        return (from);
    1.40 +    }
    1.41 +    
    1.42 +    public boolean isInRange(final Date date)
    1.43 +    {
    1.44 +        final boolean retVal;
    1.45 +
    1.46 +        if(date.equals(from) || date.equals(till))
    1.47 +        {
    1.48 +            retVal = true;
    1.49 +        }
    1.50 +        else if(date.after(from) && date.before(till))
    1.51 +        {
    1.52 +            retVal = true;
    1.53 +        }
    1.54 +        else
    1.55 +        {
    1.56 +            retVal = false;
    1.57 +        }
    1.58 +
    1.59 +        return (retVal);
    1.60 +    }
    1.61 +
    1.62 +    @Override
    1.63 +    public boolean equals(Object obj) {
    1.64 +        if (obj == null) {
    1.65 +            return false;
    1.66 +        }
    1.67 +        if (getClass() != obj.getClass()) {
    1.68 +            return false;
    1.69 +        }
    1.70 +        final DateRange other = (DateRange) obj;
    1.71 +        if (this.from != other.from && (this.from == null || !this.from.equals(other.from))) {
    1.72 +            return false;
    1.73 +        }
    1.74 +        if (this.till != other.till && (this.till == null || !this.till.equals(other.till))) {
    1.75 +            return false;
    1.76 +        }
    1.77 +        return true;
    1.78 +    }
    1.79 +
    1.80 +    @Override
    1.81 +    public int hashCode() {
    1.82 +        int hash = 7;
    1.83 +        hash = 89 * hash + (this.from != null ? this.from.hashCode() : 0);
    1.84 +        hash = 89 * hash + (this.till != null ? this.till.hashCode() : 0);
    1.85 +        return hash;
    1.86 +    }
    1.87 +
    1.88 +    @Override
    1.89 +    public String toString()
    1.90 +    {
    1.91 +        return (from + " until " + till);
    1.92 +    }
    1.93 +
    1.94 +}