KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > ReturnValues


1 package com.mockobjects;
2
3 import junit.framework.*;
4 import java.util.*;
5
6 /**
7  * Sequence values as required by MockMaker
8  * This is a generic class that should have been introduced to the mockobjects code stream instead of
9  * being separately included in org.mockobjects.
10  * It is possibly similar to a ReturnObjectList?
11  */

12 public class ReturnValues {
13     private String JavaDoc myName;
14     protected Vector myContents = new Vector();
15     private boolean myKeepUsingLastReturnValue = false;
16
17 public ReturnValues() {
18     this("Generate me with a useful name!",true);
19 }
20 public ReturnValues(String JavaDoc name, boolean keepUsingLastReturnValue) {
21     myName = name;
22     myKeepUsingLastReturnValue = keepUsingLastReturnValue;
23 }
24 public ReturnValues(boolean keepUsingLastReturnValue) {
25     this("Generate me with a useful name!", keepUsingLastReturnValue);
26 }
27 public void add(Object JavaDoc element){
28     myContents.addElement(element);
29 }
30 public void addAll(Collection returnValues){
31     myContents.addAll(returnValues);
32 }
33 public Object JavaDoc getNext() {
34     if (myContents.isEmpty()) {
35         throw new AssertionFailedError(getClass().getName() + "[" + myName + "] was not setup with enough values");
36     }
37     return pop();
38 }
39 public boolean isEmpty() {
40     return myContents.size() == 0;
41 }
42 protected Object JavaDoc pop() {
43     Object JavaDoc result = myContents.firstElement();
44     boolean shouldNotRemoveElement = myContents.size() == 1 && myKeepUsingLastReturnValue;
45     if (!shouldNotRemoveElement) {
46         myContents.removeElementAt(0);
47     }
48     return result;
49 }
50 }
51
Popular Tags