diff -r 000000000000 -r e1089d423881 samples/cloneproblem/src/org/apidesign/cloneproblem/Interval.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/cloneproblem/src/org/apidesign/cloneproblem/Interval.java Tue Mar 01 21:28:39 2011 +0100 @@ -0,0 +1,44 @@ +package org.apidesign.cloneproblem; + +import java.util.Date; + +// BEGIN: interval.api +/** Quiz: Anyone can come up with a JUnit test to generate + * {@link NullPointerException} directly from the code of + * the Interval class? + * + * @author Jaroslav Tulach + */ +public final class Interval { + private final Date from, to; + + /** Constructs interval between two dates. + * + * @param from the 'sooner' date + * @param to the 'later' date + * @throws IllegalArgumentException if from is not less then to + */ + public Interval(Date from, Date to) { + if (from == null) { + throw new IllegalArgumentException("'from' cannot be null!"); + } + if (to == null) { + throw new IllegalArgumentException("'to' cannot be null!"); + } + // shield us from Date's mutability + this.from = (Date) from.clone(); + this.to = (Date)to.clone(); + if (from.compareTo(to) >= 0) { + throw new IllegalArgumentException("'from' must be lower than 'to'!"); + } + } + + /** The length of the interval in milliseconds + * + * @return amount of milliseconds between 'from' and 'to' dates. + */ + public long getLength() { + return to.getTime() - from.getTime(); + } +} +// END: interval.api