1 3 package org.jmock.expectation; 4 5 import java.util.Collection ; 6 import java.util.Vector ; 7 import junit.framework.AssertionFailedError; 8 9 10 16 public class ReturnValues 17 { 18 private String myName; 19 protected Vector myContents = new Vector (); 20 private boolean myKeepUsingLastReturnValue = false; 21 22 public ReturnValues() { 23 this("Generate me with a useful name!", true); 24 } 25 26 public ReturnValues( String name, boolean keepUsingLastReturnValue ) { 27 myName = name; 28 myKeepUsingLastReturnValue = keepUsingLastReturnValue; 29 } 30 31 public ReturnValues( boolean keepUsingLastReturnValue ) { 32 this("Generate me with a useful name!", keepUsingLastReturnValue); 33 } 34 35 public void add( Object element ) { 36 myContents.addElement(element); 37 } 38 39 public void addAll( Collection returnValues ) { 40 myContents.addAll(returnValues); 41 } 42 43 public Object getNext() { 44 if (myContents.isEmpty()) { 45 throw new AssertionFailedError(getClass().getName() + "[" + myName + "] was not setup with enough values"); 46 } 47 return pop(); 48 } 49 50 public boolean isEmpty() { 51 return myContents.size() == 0; 52 } 53 54 protected Object pop() { 55 Object result = myContents.firstElement(); 56 boolean shouldNotRemoveElement = myContents.size() == 1 && myKeepUsingLastReturnValue; 57 if (!shouldNotRemoveElement) { 58 myContents.removeElementAt(0); 59 } 60 return result; 61 } 62 } 63 | Popular Tags |