|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ReturnObjectBag.java | 87.5% | 95.5% | 90.9% | 92.7% |
|
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 | 18 |
public ReturnObjectBag(String name) {
|
27 | 18 |
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 | 18 |
public void putObjectToReturn(Object key, Object value) { |
38 | 18 |
if (key == null) { |
39 | 2 |
key = Null.NULL; |
40 |
} |
|
41 | 18 |
ReturnObjectList returnObjectList = (ReturnObjectList) returnObjectLists.get(key); |
42 | 18 |
if (returnObjectList == null) { |
43 | 18 |
returnObjectList = new ReturnObjectList(name + "." + key.toString()); |
44 | 18 |
returnObjectLists.put(key, returnObjectList); |
45 |
} |
|
46 |
|
|
47 | 18 |
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 | 4 |
public void putObjectToReturn(int key, Object value) { |
58 | 4 |
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 | 2 |
public void putObjectToReturn(Object key, int value) { |
71 | 2 |
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 | 2 |
public void putObjectToReturn(Object key, boolean value) { |
84 | 2 |
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 | 12 |
public void verify() { |
94 | 12 |
for (Iterator it = returnObjectLists.values().iterator(); it.hasNext();) {
|
95 | 14 |
((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 | 20 |
public Object getNextReturnObject(Object key) {
|
109 | 20 |
if (key == null) { |
110 | 2 |
key = Null.NULL; |
111 |
} |
|
112 | 20 |
ReturnObjectList returnObjectList = (ReturnObjectList) returnObjectLists.get(key); |
113 | 20 |
AssertMo.assertNotNull(name + " does not contain " + key.toString(), returnObjectList);
|
114 | 18 |
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 | 4 |
public Object getNextReturnObject(int key) { |
127 | 4 |
return getNextReturnObject(new Integer(key)); |
128 |
} |
|
129 |
|
|
130 | 0 |
public Hashtable getHashTable() {
|
131 | 0 |
return returnObjectLists;
|
132 |
} |
|
133 |
|
|
134 | 2 |
public int getNextReturnInt(Object key) { |
135 | 2 |
return ((Integer) getNextReturnObject(key)).intValue();
|
136 |
} |
|
137 |
|
|
138 | 2 |
public boolean getNextReturnBoolean(Object key) { |
139 | 2 |
return ((Boolean) getNextReturnObject(key)).booleanValue();
|
140 |
} |
|
141 |
} |
|
142 |
|
|