KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jmock.expectation;
4
5 import java.util.Hashtable JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import org.jmock.core.Verifiable;
8
9
10 /**
11  * The ReturnObjectBag is a map containing instances of ReturnObjectList.
12  * A single instance is held for each mapkey. Every time a call to putObjectToReturn or
13  * getNextReturnObject is made an object is added or removed from the ReturnObjectList for
14  * the given key.
15  * This allows the ReturnObjectBag to be used to return an ordered list of objects for each key
16  * regardless of the order in which the key requests are made.
17  *
18  * @author Jeff Martin
19  * @version $Revision: 1.6 $
20  * @see ReturnObjectList
21  */

22 public class ReturnObjectBag implements Verifiable
23 {
24     private final Hashtable JavaDoc returnObjectLists = new Hashtable JavaDoc();
25     private final String JavaDoc name;
26
27     /**
28      * @param name Name used to describe an instance of ReturnObjectBag in error messages
29      */

30     public ReturnObjectBag( String JavaDoc name ) {
31         this.name = name;
32     }
33
34     /**
35      * Places an object into the list of return objects for a particular key
36      *
37      * @param key the key against which the object will be stored
38      * @param value the value to be added to the list for that key
39      * @see ReturnObjectList#addObjectToReturn
40      */

41     public void putObjectToReturn( Object JavaDoc key, Object JavaDoc value ) {
42         if (key == null) {
43             key = Null.NULL;
44         }
45         ReturnObjectList returnObjectList = (ReturnObjectList)returnObjectLists.get(key);
46         if (returnObjectList == null) {
47             returnObjectList = new ReturnObjectList(name + "." + key.toString());
48             returnObjectLists.put(key, returnObjectList);
49         }
50
51         returnObjectList.addObjectToReturn(value);
52     }
53
54     /**
55      * Places an object into the list of return objects for a particular int key
56      *
57      * @param key the key against which the object will be stored
58      * @param value the value to be added to the list for that key
59      * @see ReturnObjectList#addObjectToReturn
60      */

61     public void putObjectToReturn( int key, Object JavaDoc value ) {
62         putObjectToReturn(new Integer JavaDoc(key), value);
63     }
64
65     /**
66      * Places an int into the list of return objects for a particular key. The value can be retrieved
67      * using the getNextReturnInt invokedMethod
68      *
69      * @param key the key against which the object will be stored
70      * @param value the value to be added to the list for that key
71      * @see ReturnObjectList#addObjectToReturn
72      * @see #getNextReturnInt
73      */

74     public void putObjectToReturn( Object JavaDoc key, int value ) {
75         putObjectToReturn(key, new Integer JavaDoc(value));
76     }
77
78     /**
79      * Places an boolean into the list of return objects for a particular key. The value can be retrieved
80      * using the getNextReturnBoolean invokedMethod
81      *
82      * @param key the key against which the object will be stored
83      * @param value the value to be added to the list for that key
84      * @see ReturnObjectList#addObjectToReturn
85      * @see #getNextReturnBoolean
86      */

87     public void putObjectToReturn( Object JavaDoc key, boolean value ) {
88         putObjectToReturn(key, new Boolean JavaDoc(value));
89     }
90
91     /**
92      * Checks each the list for each key to verify that all no objects remain
93      * in the list for that key.
94      *
95      * @see ReturnObjectList#verify
96      */

97     public void verify() {
98         for (Iterator JavaDoc it = returnObjectLists.values().iterator(); it.hasNext();) {
99             ((ReturnObjectList)it.next()).verify();
100         }
101     }
102
103     /**
104      * Returns the next object in the ReturnObjectList for a given key.
105      * The call will throw an AssertFailError if the requested key is
106      * not present within this ReturnObjectBag.
107      *
108      * @param key The key for which the next object should be returned.
109      * @return The next object from the ReturnObjectList stored against the given key.
110      * @see ReturnObjectList#nextReturnObject
111      */

112     public Object JavaDoc getNextReturnObject( Object JavaDoc key ) {
113         if (key == null) {
114             key = Null.NULL;
115         }
116         ReturnObjectList returnObjectList = (ReturnObjectList)returnObjectLists.get(key);
117         AssertMo.assertNotNull(name + " does not contain " + key.toString(), returnObjectList);
118         return returnObjectList.nextReturnObject();
119     }
120
121     /**
122      * Returns the next object in the ReturnObjectList for a given int key.
123      * The call will throw an AssertFailError if the requested key is
124      * not present within this ReturnObjectBag.
125      *
126      * @param key The key for which the next object should be returned.
127      * @return The next object from the ReturnObjectList stored against the given key.
128      * @see ReturnObjectList#nextReturnObject
129      */

130     public Object JavaDoc getNextReturnObject( int key ) {
131         return getNextReturnObject(new Integer JavaDoc(key));
132     }
133
134     public Hashtable JavaDoc getHashTable() {
135         return returnObjectLists;
136     }
137
138     public int getNextReturnInt( Object JavaDoc key ) {
139         return ((Integer JavaDoc)getNextReturnObject(key)).intValue();
140     }
141
142     public boolean getNextReturnBoolean( Object JavaDoc key ) {
143         return ((Boolean JavaDoc)getNextReturnObject(key)).booleanValue();
144     }
145 }
146
Popular Tags