1 package com.mockobjects; 2 3 import junit.framework.*; 4 import java.util.*; 5 6 12 public class ReturnValues { 13 private String 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 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 element){ 28 myContents.addElement(element); 29 } 30 public void addAll(Collection returnValues){ 31 myContents.addAll(returnValues); 32 } 33 public Object 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 pop() { 43 Object 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 |