KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > util > Null


1 package com.mockobjects.util;
2
3 /**
4  * A class that represents the <code>null</code> value.
5  * The {@link com.mockobjects.util.Null Null} class is used when an
6  * {@link com.mockobjects.Expectation Expectation} is set to expect nothing.
7  * <p>
8  * <b>Example usage:</b>
9  * <pre>
10  * public class MockX {
11  * private Expectation... anExpectation = new Expectation...(...);
12  *
13  * public MockX() {
14  * anExpectation.setExpectNothing();
15  * }
16  *
17  * public void setAnExpectation(Object value) {
18  * anExpectation.setExpected(value);
19  * }
20  *
21  * public void setActual(Object value) {
22  * anExpectation.setActual(value);
23  * }
24  * }
25  * </pre>
26  * The act of calling {@link com.mockobjects.Expectation#setExpectNothing() Expectation.setExpectNothing()}
27  * tells the expectation that it should expect no values to change. Since
28  * all {@link com.mockobjects.util.Null Null} objects are equal to themselves,
29  * most expectations set their expected value to an instance of
30  * {@link com.mockobjects.util.Null Null}, and at the same time, set their actual
31  * value to another instance of {@link com.mockobjects.util.Null Null}.
32  * This way, when {@link com.mockobjects.Verifiable#verify() verify()} checks
33  * expectations, they will compare two {@link com.mockobjects.util.Null Null}
34  * objects together, which is guaranteed to succeed.
35  * @author <a HREF="mailto:fbos@users.sourceforge.net">Francois Beausoleil (fbos@users.sourceforge.net)</a>
36  * @version $Id: Null.java,v 1.3 2002/03/28 18:16:54 custommonkey Exp $
37  */

38 public class Null {
39     /**
40      * The default description for all {@link com.mockobjects.util.Null Null}
41      * objects.
42      * This String is equal to "<code>Null</code>".
43      */

44     public static final String JavaDoc DEFAULT_DESCRIPTION = "Null";
45
46     /**
47      * A default {@link com.mockobjects.util.Null Null} object.
48      * Instead of always instantiating new {@link com.mockobjects.util.Null Null}
49      * objects, consider using a reference to this object instead. This way,
50      * the virtual machine will not be taking the time required to instantiate
51      * an object everytime it is required.
52      */

53     public static final Null NULL = new Null();
54
55     /**
56      * The description of this {@link com.mockobjects.util.Null Null} object.
57      */

58     final private String JavaDoc myDescription;
59
60     /**
61      * Instantiates a new {@link com.mockobjects.util.Null Null} object with
62      * the default description.
63      * @see com.mockobjects.util.Null#DEFAULT_DESCRIPTION
64      */

65     public Null() {
66         this(DEFAULT_DESCRIPTION);
67     }
68
69     /**
70      * Instantiates a new {@link com.mockobjects.util.Null Null} object and
71      * sets it's description.
72      * @param description
73      */

74     public Null(String JavaDoc description) {
75         super();
76         myDescription = description;
77     }
78
79     /**
80      * Determines equality between two objects.
81      * {@link com.mockobjects.util.Null Null} objects are only equal to
82      * another instance of themselves.
83      * @param other
84      */

85     public boolean equals(Object JavaDoc other) {
86         return other instanceof Null;
87     }
88
89     /**
90      * Returns this {@link com.mockobjects.util.Null Null} object's hashCode.
91      * All {@link com.mockobjects.util.Null Null} return the same
92      * hashCode value.
93      */

94     public int hashCode() {
95         return 0;
96     }
97
98     /**
99      * Returns a string representation of this {@link com.mockobjects.util.Null Null}
100      * object.
101      * This merely returns the string passed to the constructor initially.
102      */

103     public String JavaDoc toString() {
104         return myDescription;
105     }
106 }
107
Popular Tags