1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/cloneproblem/src/org/apidesign/cloneproblem/Interval.java Tue Mar 01 21:28:39 2011 +0100
1.3 @@ -0,0 +1,44 @@
1.4 +package org.apidesign.cloneproblem;
1.5 +
1.6 +import java.util.Date;
1.7 +
1.8 +// BEGIN: interval.api
1.9 +/** Quiz: Anyone can come up with a JUnit test to generate
1.10 + * {@link NullPointerException} directly from the code of
1.11 + * the <code>Interval</code> class?
1.12 + *
1.13 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.14 + */
1.15 +public final class Interval {
1.16 + private final Date from, to;
1.17 +
1.18 + /** Constructs interval between two dates.
1.19 + *
1.20 + * @param from the 'sooner' date
1.21 + * @param to the 'later' date
1.22 + * @throws IllegalArgumentException if <code>from</code> is not less then <code>to</code>
1.23 + */
1.24 + public Interval(Date from, Date to) {
1.25 + if (from == null) {
1.26 + throw new IllegalArgumentException("'from' cannot be null!");
1.27 + }
1.28 + if (to == null) {
1.29 + throw new IllegalArgumentException("'to' cannot be null!");
1.30 + }
1.31 + // shield us from Date's mutability
1.32 + this.from = (Date) from.clone();
1.33 + this.to = (Date)to.clone();
1.34 + if (from.compareTo(to) >= 0) {
1.35 + throw new IllegalArgumentException("'from' must be lower than 'to'!");
1.36 + }
1.37 + }
1.38 +
1.39 + /** The length of the interval in milliseconds
1.40 + *
1.41 + * @return amount of milliseconds between 'from' and 'to' dates.
1.42 + */
1.43 + public long getLength() {
1.44 + return to.getTime() - from.getTime();
1.45 + }
1.46 +}
1.47 +// END: interval.api