KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > expectation > Null


1 /* Copyright (c) 2000-2004 jMock.org
2  */

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

41 public class Null
42 {
43     /**
44      * The default description for all {@link org.jmock.expectation.Null Null}
45      * objects.
46      * This String is equal to "<code>Null</code>".
47      */

48     public static final String JavaDoc DEFAULT_DESCRIPTION = "Null";
49
50     /**
51      * A default {@link org.jmock.expectation.Null Null} object.
52      * Instead of always instantiating new {@link org.jmock.expectation.Null Null}
53      * objects, consider using a reference to this object instead. This way,
54      * the virtual machine will not be taking the time required to instantiate
55      * an object everytime it is required.
56      */

57     public static final Null NULL = new Null();
58
59     /**
60      * The description of this {@link org.jmock.expectation.Null Null} object.
61      */

62     final private String JavaDoc myDescription;
63
64     /**
65      * Instantiates a new {@link org.jmock.expectation.Null Null} object with
66      * the default description.
67      *
68      * @see org.jmock.expectation.Null#DEFAULT_DESCRIPTION
69      */

70     public Null() {
71         this(DEFAULT_DESCRIPTION);
72     }
73
74     /**
75      * Instantiates a new {@link org.jmock.expectation.Null Null} object and
76      * sets it's description.
77      *
78      * @param description
79      */

80     public Null( String JavaDoc description ) {
81         super();
82         myDescription = description;
83     }
84
85     /**
86      * Determines equality between two objects.
87      * {@link org.jmock.expectation.Null Null} objects are only equal to
88      * another instance of themselves.
89      *
90      * @param other
91      */

92     public boolean equals( Object JavaDoc other ) {
93         return other instanceof Null;
94     }
95
96     /**
97      * Returns this {@link org.jmock.expectation.Null Null} object's hashCode.
98      * All {@link org.jmock.expectation.Null Null} return the same
99      * hashCode value.
100      */

101     public int hashCode() {
102         return 0;
103     }
104
105     /**
106      * Returns a string representation of this {@link org.jmock.expectation.Null Null}
107      * object.
108      * This merely returns the string passed to the constructor initially.
109      */

110     public String JavaDoc toString() {
111         return myDescription;
112     }
113 }
114
Popular Tags