'. '

Ostrava

From APIDesign

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
-
I'll be in [[wikipedia:Ostrava|Ostrava]] at [http://java.cz/article/czjug-ostrava-brezen-2011 JUG meeting] on Mar 2nd, 2011. I'll talk about [[paradox]]es, in a shorter version of the [[ParadoxesVideo]]. Stop by and get a chance to win [[TheAPIBook]].
+
I'll be in [[wikipedia:Ostrava|Ostrava]] at [http://java.cz/article/czjug-ostrava-brezen-2011 JUG meeting] on Mar 2nd, 2011. I'll talk about [[paradox]]es, in a shorter version of the [[ParadoxesVideo]]. Stop by and get a chance to win [[TheAPIBook]]. I am not sure what will be the final quiz to win [[TheAPIBook]], but I'd suggest following one: Can you write a code that throw ''NullPointerException'' directly from the following ''Interval'' class?
 +
 
 +
<source lang="java">
 +
/** Quiz: Anyone can come up with a JUnit test to generate
 +
* {@link NullPointerException} directly from the code of
 +
* the <code>Interval</code> class?
 +
*
 +
* @author Jaroslav Tulach <jtulach@netbeans.org>
 +
*/
 +
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 <code>from</code> is not less then <code>to</code>
 +
*/
 +
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();
 +
}
 +
}
 +
</source>
 +
 
 +
I followed all the advices of [[TheAPIBook]]. I made the class final, I check for wrong arguments. I guard myself against mutability of ''Date''. Is it really write bullet proof code?

Revision as of 20:04, 1 March 2011

I'll be in Ostrava at JUG meeting on Mar 2nd, 2011. I'll talk about paradoxes, in a shorter version of the ParadoxesVideo. Stop by and get a chance to win TheAPIBook. I am not sure what will be the final quiz to win TheAPIBook, but I'd suggest following one: Can you write a code that throw NullPointerException directly from the following Interval class?

/** Quiz: Anyone can come up with a JUnit test to generate
 * {@link NullPointerException} directly from the code of 
 * the <code>Interval</code> class?
 *
 * @author Jaroslav Tulach <jtulach@netbeans.org>
 */
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 <code>from</code> is not less then <code>to</code>
     */
    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();
    }
}

I followed all the advices of TheAPIBook. I made the class final, I check for wrong arguments. I guard myself against mutability of Date. Is it really write bullet proof code?

Personal tools
buy