KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > ReturnObjectBag


1 package com.mockobjects;
2
3 import com.mockobjects.ReturnObjectList;
4 import com.mockobjects.Verifiable;
5 import com.mockobjects.util.AssertMo;
6 import com.mockobjects.util.Null;
7
8 import java.util.*;
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  * @see ReturnObjectList
18  * @author Jeff Martin
19  * @version $Revision: 1.4 $
20  */

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

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

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

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

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

81     public void putObjectToReturn(Object JavaDoc key, boolean value) {
82         putObjectToReturn(key, new Boolean JavaDoc(value));
83     }
84
85     /**
86      * Checks each the list for each key to verify that all no objects remain
87      * in the list for that key.
88      * @see ReturnObjectList#verify
89      */

90     public void verify() {
91         for (Iterator it = returnObjectLists.values().iterator(); it.hasNext();) {
92             ((ReturnObjectList) it.next()).verify();
93         }
94     }
95
96     /**
97      * Returns the next object in the ReturnObjectList for a given key.
98      * The call will throw an AssertFailError if the requested key is
99      * not present within this ReturnObjectBag.
100      * @param key The key for which the next object should be returned.
101      * @return The next object from the ReturnObjectList stored against the given key.
102      * @see ReturnObjectList#nextReturnObject
103      */

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

121     public Object JavaDoc getNextReturnObject(int key) {
122         return getNextReturnObject(new Integer JavaDoc(key));
123     }
124
125     public Hashtable getHashTable(){
126         return returnObjectLists;
127     }
128
129     public int getNextReturnInt(Object JavaDoc key) {
130         return ((Integer JavaDoc)getNextReturnObject(key)).intValue();
131     }
132
133     public boolean getNextReturnBoolean(Object JavaDoc key) {
134         return ((Boolean JavaDoc)getNextReturnObject(key)).booleanValue();
135     }
136 }
137
Popular Tags