KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > mockobjects > util > ReturnObjectList


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.mockobjects.util;
6
7 import java.util.Vector JavaDoc;
8
9 import com.mockobjects.Verifiable;
10
11 import junit.framework.Assert;
12
13 /**
14  * <p>
15  *
16  * Holds a list of objects which are used when returning objects from a method. </p> <p>
17  *
18  * For ever sucessive call to <code>next</code> the next object in the list will returned. When there are no more
19  * objects in the list, either
20  * <ul>
21  * <li> an assertion error will be thrown - if the last object should not be reused
22  * <li> the last object that was in the list will be returned - if the last object should be reused
23  * </ul>
24  * </p> <p>
25  *
26  * If the verify method is called and there are objects still in the list an assertion error will be thrown. </p>
27  *
28  * @author Stig J&oslash;rgensen
29  * @created 5. februar 2003
30  */

31 public class ReturnObjectList implements Verifiable
32 {
33     private final Vector JavaDoc myObjects = new Vector JavaDoc();
34     private final String JavaDoc myName;
35     private boolean myReuseLast = false;
36     private Object JavaDoc myLastObject;
37
38     /**
39      * Construct a new empty list
40      *
41      * @param aName Label used to identify list
42      */

43     public ReturnObjectList(String JavaDoc aName)
44     {
45         myName = aName;
46     }
47
48     /**
49      * Sets whether the last value in the list should be reused if there are no more entries in the list.
50      *
51      * @param reuseLast whether the last value in the list should be reused if there are no more entries in the list.
52      */

53     public void setReuseLast(boolean reuseLast)
54     {
55         myReuseLast = reuseLast;
56     }
57
58     /**
59      * Add an object to the end of the list.
60      *
61      * @param anObjectToReturn object to be added to the list
62      */

63     public void add(Object JavaDoc anObjectToReturn)
64     {
65         myObjects.add(anObjectToReturn);
66
67         if (null == myLastObject) {
68             myLastObject = anObjectToReturn;
69         }
70     }
71
72     /**
73      * Add a boolean to the end of the list.
74      *
75      * @param aBooleanToReturn boolean to be added to the list
76      */

77     public void add(boolean aBooleanToReturn)
78     {
79         myObjects.add(new Boolean JavaDoc(aBooleanToReturn));
80
81         if (null == myLastObject) {
82             myLastObject = new Boolean JavaDoc(aBooleanToReturn);
83         }
84     }
85
86     /**
87      * Add an integer to the end of the list.
88      *
89      * @param anIntegerToReturn integer to be added to the list
90      */

91     public void add(int anIntegerToReturn)
92     {
93         myObjects.add(new Integer JavaDoc(anIntegerToReturn));
94
95         if (null == myLastObject) {
96             myLastObject = new Integer JavaDoc(anIntegerToReturn);
97         }
98     }
99
100     /**
101      * Returns whether there are more objects in the list or not. <p>
102      *
103      * If the user has specified that the last object should be reused, then this method will always return true. </p>
104      *
105      * @return whether there are more objects in the list or not.
106      */

107     public boolean hasNext()
108     {
109         return (myReuseLast || (myObjects.size() > 0));
110     }
111
112     /**
113      * Returns the next object from the list. Each object is returned in the order in which they where added. <p>
114      *
115      * If the user has spesified that the last object should be reused when there are no more entries, it will do this
116      * and never fail. </p>
117      *
118      * @return the next object from the list.
119      */

120     public Object JavaDoc next()
121     {
122         if (myReuseLast && (myObjects.size() == 0)) {
123             return myLastObject;
124         }
125         else {
126             Assert.assertTrue(myName + " has run out of objects.",
127                 myObjects.size() > 0);
128
129             return myObjects.remove(0);
130         }
131     }
132
133     /**
134      * Clears the state.
135      */

136     public void clear()
137     {
138         myObjects.clear();
139         myLastObject = null;
140         myReuseLast = false;
141     }
142
143     /**
144      * Verify that there are no objects left within the list.
145      */

146     public void verify()
147     {
148         Assert.assertEquals(myName + " has un-used objects.", 0,
149             myObjects.size());
150     }
151 }
152
Popular Tags