From: Jeff M. <cus...@us...> - 2002-12-04 12:36:41
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects In directory sc8-pr-cvs1:/tmp/cvs-serv24401/src/core/com/mockobjects Added Files: ReturnObjectBag.java Log Message: Added new Mock Object container to help handle maps --- NEW FILE: ReturnObjectBag.java --- package com.mockobjects; import com.mockobjects.ReturnObjectList; import com.mockobjects.Verifiable; import com.mockobjects.util.AssertMo; import java.util.HashMap; import java.util.Iterator; /** * The ReturnObjectBag is a map containing instances of ReturnObjectList. * A single instance is held for each mapkey. Every time a call to putObjectToReturn or * getNextReturnObject is made an object is added or removed from the ReturnObjectList for * the given key. * This allows the ReturnObjectBag to be used to return an ordered list of objects for each key * regardless of the order in which the key requests are made. * @see ReturnObjectList * @author Jeff Martin * @version $Revision: 1.1 $ */ public class ReturnObjectBag implements Verifiable { private final HashMap returnObjectLists = new HashMap(); private final String name; /** * @param name Name used to describe an instance of ReturnObjectBag in error messages */ public ReturnObjectBag(String name) { this.name = name; } /** * Places an object into the list of return objects for a particular key * @param key the key against which the object will be stored * @param value the value to be added to the list for that key * @see ReturnObjectList#addObjectToReturn */ public void putObjectToReturn(Object key, Object value) { ReturnObjectList returnObjectList = (ReturnObjectList)returnObjectLists.get(key); if(returnObjectList==null){ returnObjectList = new ReturnObjectList(name+"."+key.toString()); returnObjectLists.put(key, returnObjectList); } returnObjectList.addObjectToReturn(value); } /** * Checks each the list for each key to verify that all no objects remain * in the list for that key. * @see ReturnObjectList#verify */ public void verify() { for(Iterator it = returnObjectLists.values().iterator();it.hasNext();){ ((ReturnObjectList)it.next()).verify(); } } /** * Returns the next object in the ReturnObjectList for a given key. * The call will throw an AssertFailError if the requested key is * not present within this ReturnObjectBag. * @param key The key for which the next object should be returned. * @return The next object from the ReturnObjectList stored against the given key. * @see ReturnObjectList#nextReturnObject */ public Object getNextReturnObject(Object key) { ReturnObjectList returnObjectList = (ReturnObjectList)returnObjectLists.get(key); AssertMo.assertNotNull(name + " does not contain " + key.toString(), returnObjectList); return returnObjectList.nextReturnObject(); } } |