samples/cloneproblem/src/org/apidesign/cloneproblem/Interval.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 368 e1089d423881
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
     1 package org.apidesign.cloneproblem;
     2 
     3 import java.util.Date;
     4 
     5 // BEGIN: interval.api
     6 /** Quiz: Anyone can come up with a JUnit test to generate
     7  * {@link NullPointerException} directly from the code of 
     8  * the <code>Interval</code> class?
     9  *
    10  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    11  */
    12 public final class Interval {
    13     private final Date from, to;
    14     
    15     /** Constructs interval between two dates.
    16      * 
    17      * @param from the 'sooner' date
    18      * @param to the 'later' date
    19      * @throws IllegalArgumentException 
    20      *      if <code>from</code> is not less then <code>to</code>
    21      */
    22     public Interval(Date from, Date to) {
    23         if (from == null) {
    24             throw new IllegalArgumentException("'from' cannot be null!");
    25         }
    26         if (to == null) {
    27             throw new IllegalArgumentException("'to' cannot be null!");
    28         }
    29         // shield us from Date's mutability
    30         this.from = (Date) from.clone();
    31         this.to = (Date)to.clone();
    32         if (from.compareTo(to) >= 0) {
    33             throw new IllegalArgumentException(
    34                 "'from' must be lower than 'to'!"
    35             );
    36         }
    37     }
    38     
    39     /** The length of the interval in milliseconds 
    40      * 
    41      * @return amount of milliseconds between 'from' and 'to' dates.
    42      */
    43     public long getLength() {
    44         return to.getTime() - from.getTime();
    45     }
    46 }
    47 // END: interval.api