1 package org.apidesign.cloneproblem;
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?
10 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
12 public final class Interval {
13 private final Date from, to;
15 /** Constructs interval between two dates.
17 * @param from the 'sooner' date
18 * @param to the 'later' date
19 * @throws IllegalArgumentException if <code>from</code> is not less then <code>to</code>
21 public Interval(Date from, Date to) {
23 throw new IllegalArgumentException("'from' cannot be null!");
26 throw new IllegalArgumentException("'to' cannot be null!");
28 // shield us from Date's mutability
29 this.from = (Date) from.clone();
30 this.to = (Date)to.clone();
31 if (from.compareTo(to) >= 0) {
32 throw new IllegalArgumentException("'from' must be lower than 'to'!");
36 /** The length of the interval in milliseconds
38 * @return amount of milliseconds between 'from' and 'to' dates.
40 public long getLength() {
41 return to.getTime() - from.getTime();