task4/solution04/src/org/apidesign/apifest08/currency/DateRange.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 25 Oct 2008 20:53:00 +0200
changeset 84 2ae6e4aa7aef
permissions -rw-r--r--
Solutions by Petr Smid
     1 package org.apidesign.apifest08.currency;
     2 
     3 
     4 import java.util.Date;
     5 
     6 
     7 public final class DateRange
     8 {
     9     private final Date from;
    10     private final Date till;
    11     
    12     DateRange(final Date f,
    13               final Date t)
    14     {
    15         if(f == null && t != null)
    16         {
    17             throw new IllegalArgumentException("f was null but t was not");
    18         }
    19         
    20         if(f != null && t == null)
    21         {
    22             throw new IllegalArgumentException("f was null but t was not");
    23         }
    24         
    25         from = f;
    26         till = t;
    27     }
    28     
    29     public Date getFrom()
    30     {
    31         return (from);
    32     }
    33     
    34     public Date getTill()
    35     {
    36         return (from);
    37     }
    38     
    39     public boolean isInRange(final Date date)
    40     {
    41         final boolean retVal;
    42 
    43         if(date.equals(from) || date.equals(till))
    44         {
    45             retVal = true;
    46         }
    47         else if(date.after(from) && date.before(till))
    48         {
    49             retVal = true;
    50         }
    51         else
    52         {
    53             retVal = false;
    54         }
    55 
    56         return (retVal);
    57     }
    58 
    59     @Override
    60     public boolean equals(Object obj) {
    61         if (obj == null) {
    62             return false;
    63         }
    64         if (getClass() != obj.getClass()) {
    65             return false;
    66         }
    67         final DateRange other = (DateRange) obj;
    68         if (this.from != other.from && (this.from == null || !this.from.equals(other.from))) {
    69             return false;
    70         }
    71         if (this.till != other.till && (this.till == null || !this.till.equals(other.till))) {
    72             return false;
    73         }
    74         return true;
    75     }
    76 
    77     @Override
    78     public int hashCode() {
    79         int hash = 7;
    80         hash = 89 * hash + (this.from != null ? this.from.hashCode() : 0);
    81         hash = 89 * hash + (this.till != null ? this.till.hashCode() : 0);
    82         return hash;
    83     }
    84 
    85     @Override
    86     public String toString()
    87     {
    88         return (from + " until " + till);
    89     }
    90 
    91 }