jtulach@368: package org.apidesign.cloneproblem; jtulach@368: jtulach@368: import java.util.Date; jtulach@368: jtulach@368: // BEGIN: interval.api jtulach@368: /** Quiz: Anyone can come up with a JUnit test to generate jtulach@368: * {@link NullPointerException} directly from the code of jtulach@368: * the Interval class? jtulach@368: * jtulach@368: * @author Jaroslav Tulach jtulach@368: */ jtulach@368: public final class Interval { jtulach@368: private final Date from, to; jtulach@368: jtulach@368: /** Constructs interval between two dates. jtulach@368: * jtulach@368: * @param from the 'sooner' date jtulach@368: * @param to the 'later' date jtulach@369: * @throws IllegalArgumentException jtulach@369: * if from is not less then to jtulach@368: */ jtulach@368: public Interval(Date from, Date to) { jtulach@368: if (from == null) { jtulach@368: throw new IllegalArgumentException("'from' cannot be null!"); jtulach@368: } jtulach@368: if (to == null) { jtulach@368: throw new IllegalArgumentException("'to' cannot be null!"); jtulach@368: } jtulach@368: // shield us from Date's mutability jtulach@368: this.from = (Date) from.clone(); jtulach@368: this.to = (Date)to.clone(); jtulach@368: if (from.compareTo(to) >= 0) { jtulach@369: throw new IllegalArgumentException( jtulach@369: "'from' must be lower than 'to'!" jtulach@369: ); jtulach@368: } jtulach@368: } jtulach@368: jtulach@368: /** The length of the interval in milliseconds jtulach@368: * jtulach@368: * @return amount of milliseconds between 'from' and 'to' dates. jtulach@368: */ jtulach@368: public long getLength() { jtulach@368: return to.getTime() - from.getTime(); jtulach@368: } jtulach@368: } jtulach@368: // END: interval.api