1 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
2 package org.jmock.expectation;
3
4 import java.util.Hashtable;
5 import java.util.Iterator;
6
7 /***
8 * The ReturnObjectBag is a map containing instances of ReturnObjectList.
9 * A single instance is held for each mapkey. Every time a call to putObjectToReturn or
10 * getNextReturnObject is made an object is added or removed from the ReturnObjectList for
11 * the given key.
12 * This allows the ReturnObjectBag to be used to return an ordered list of objects for each key
13 * regardless of the order in which the key requests are made.
14 *
15 * @author Jeff Martin
16 * @version $Revision: 1.4 $
17 * @see ReturnObjectList
18 */
19 public class ReturnObjectBag implements Verifiable {
20 private final Hashtable returnObjectLists = new Hashtable();
21 private final String name;
22
23 /***
24 * @param name Name used to describe an instance of ReturnObjectBag in error messages
25 */
26 public ReturnObjectBag(String name) {
27 this.name = name;
28 }
29
30 /***
31 * Places an object into the list of return objects for a particular key
32 *
33 * @param key the key against which the object will be stored
34 * @param value the value to be added to the list for that key
35 * @see ReturnObjectList#addObjectToReturn
36 */
37 public void putObjectToReturn(Object key, Object value) {
38 if (key == null) {
39 key = Null.NULL;
40 }
41 ReturnObjectList returnObjectList = (ReturnObjectList) returnObjectLists.get(key);
42 if (returnObjectList == null) {
43 returnObjectList = new ReturnObjectList(name + "." + key.toString());
44 returnObjectLists.put(key, returnObjectList);
45 }
46
47 returnObjectList.addObjectToReturn(value);
48 }
49
50 /***
51 * Places an object into the list of return objects for a particular int key
52 *
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 value) {
58 putObjectToReturn(new Integer(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 *
65 * @param key the key against which the object will be stored
66 * @param value the value to be added to the list for that key
67 * @see ReturnObjectList#addObjectToReturn
68 * @see #getNextReturnInt
69 */
70 public void putObjectToReturn(Object key, int value) {
71 putObjectToReturn(key, new Integer(value));
72 }
73
74 /***
75 * Places an boolean into the list of return objects for a particular key. The value can be retrieved
76 * using the getNextReturnBoolean method
77 *
78 * @param key the key against which the object will be stored
79 * @param value the value to be added to the list for that key
80 * @see ReturnObjectList#addObjectToReturn
81 * @see #getNextReturnBoolean
82 */
83 public void putObjectToReturn(Object key, boolean value) {
84 putObjectToReturn(key, new Boolean(value));
85 }
86
87 /***
88 * Checks each the list for each key to verify that all no objects remain
89 * in the list for that key.
90 *
91 * @see ReturnObjectList#verify
92 */
93 public void verify() {
94 for (Iterator it = returnObjectLists.values().iterator(); it.hasNext();) {
95 ((ReturnObjectList) it.next()).verify();
96 }
97 }
98
99 /***
100 * Returns the next object in the ReturnObjectList for a given key.
101 * The call will throw an AssertFailError if the requested key is
102 * not present within this ReturnObjectBag.
103 *
104 * @param key The key for which the next object should be returned.
105 * @return The next object from the ReturnObjectList stored against the given key.
106 * @see ReturnObjectList#nextReturnObject
107 */
108 public Object getNextReturnObject(Object key) {
109 if (key == null) {
110 key = Null.NULL;
111 }
112 ReturnObjectList returnObjectList = (ReturnObjectList) returnObjectLists.get(key);
113 AssertMo.assertNotNull(name + " does not contain " + key.toString(), returnObjectList);
114 return returnObjectList.nextReturnObject();
115 }
116
117 /***
118 * Returns the next object in the ReturnObjectList for a given int key.
119 * The call will throw an AssertFailError if the requested key is
120 * not present within this ReturnObjectBag.
121 *
122 * @param key The key for which the next object should be returned.
123 * @return The next object from the ReturnObjectList stored against the given key.
124 * @see ReturnObjectList#nextReturnObject
125 */
126 public Object getNextReturnObject(int key) {
127 return getNextReturnObject(new Integer(key));
128 }
129
130 public Hashtable getHashTable() {
131 return returnObjectLists;
132 }
133
134 public int getNextReturnInt(Object key) {
135 return ((Integer) getNextReturnObject(key)).intValue();
136 }
137
138 public boolean getNextReturnBoolean(Object key) {
139 return ((Boolean) getNextReturnObject(key)).booleanValue();
140 }
141 }
This page was automatically generated by Maven