View Javadoc

1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.call;
4   
5   import java.util.Iterator;
6   import java.util.LinkedList;
7   import java.util.List;
8   import java.util.ListIterator;
9   
10  
11  /***
12   * This class represents a sequence of calls.  Calls added are stored
13   * sequentially, and can be retrieved in same order they were added.  Calls
14   * can also be retrieved by Signature.  Utility methods are also provided to
15   * manage the queue.
16   *
17   * @author Chad Woolley
18   */
19  public class CallQueue {
20      /*** Contains the calls that have been added to the queue. */
21      private List calls = new LinkedList();
22  
23      /***
24       * This is a list of the Signatures for the calls that are contained in the
25       * calls list.  It is used to facilitate lookup of specific calls based on
26       * signature (without having to traverse the entire calls list).
27       */
28      private List signatures = new LinkedList();
29  
30      /***
31       * Gets a call based on the callId.
32       *
33       * @param callId the unique identifier for the call
34       *
35       * @return the call which matches the CallId, or null if no match was
36       *         found.
37       */
38      public Call getCallByCallId(String callId) {
39          Iterator iterator = calls.iterator();
40  
41          while (iterator.hasNext()) {
42              Call call = (Call) iterator.next();
43  
44              if (call.getCallId().equals(callId)) {
45                  return call;
46              }
47          }
48  
49          return null;
50      }
51  
52      /***
53       * Get all calls that have been added, in order.
54       *
55       * @return all calls that have been added
56       */
57      public List getCalls() {
58          return calls;
59      }
60  
61      /***
62       * Gets the first call in the queue which has a signature matching the
63       * parameter.
64       *
65       * @param signature the signature used to look up the call
66       *
67       * @return the first call in the queue which has a signature matching the
68       *         parameter
69       */
70      public Call getFirstCallWithMatchingSignature(Signature signature) {
71          int indexOfFirstCallWithMatchingSignature =
72              getIndexOfFirstCallWithMatchingSignature(signature);
73  
74          if (indexOfFirstCallWithMatchingSignature == -1) {
75              // return null if no matching call was found
76              return null;
77          }
78  
79          return (Call) calls.get(indexOfFirstCallWithMatchingSignature);
80      }
81  
82      /***
83       * Gets the index of the next call in the queue which has a signature
84       * matching the parameter, starting at the specified index.
85       *
86       * @param startIndex the index to start looking at
87       * @param signature the signature used to look up the call
88       *
89       * @return the next call in the queue which has a signature matching the
90       *         parameter, or null if no match is found
91       */
92      public int getIndexOfNextCallWithMatchingSignature(int startIndex,
93          Signature signature) {
94          ListIterator iterator = getCalls().listIterator(startIndex);
95          int callIndex = startIndex;
96  
97          // iterate through the list starting at the first match
98          while (iterator.hasNext()) {
99              Call call = (Call) iterator.next();
100 
101             if (call.hasMatchingSignature(signature)) {
102                 return callIndex;
103             }
104 
105             callIndex++;
106         }
107 
108         return -1;
109     }
110 
111     /***
112      * Returns the number of calls in the queue with the specified signature.
113      *
114      * @param signature the signature to match
115      *
116      * @return the number of calls in the queue with the specified signature
117      */
118     public int getMatchingSignatureCount(Signature signature) {
119         int matchingSignatureCount = 0;
120 
121         Iterator iterator = getSignatures().iterator();
122 
123         while (iterator.hasNext()) {
124             Signature nextSignature = (Signature) iterator.next();
125 
126             if (nextSignature.equals(signature)) {
127                 matchingSignatureCount++;
128             }
129         }
130 
131         return matchingSignatureCount;
132     }
133 
134     /***
135      * Gets the next call in the queue which has a signature matching the
136      * parameter, starting at the specified index.
137      *
138      * @param startIndex the index to start looking at
139      * @param signature the signature used to look up the call
140      *
141      * @return the next call in the queue which has a signature matching the
142      *         parameter, or null if no match is found
143      */
144     public Call getNextCallWithMatchingSignature(int startIndex,
145         Signature signature) {
146         ListIterator iterator = getCalls().listIterator(startIndex);
147 
148         // iterate through the list starting at the first match
149         while (iterator.hasNext()) {
150             Call call = (Call) iterator.next();
151 
152             if (call.hasMatchingSignature(signature)) {
153                 return call;
154             }
155         }
156 
157         return null;
158     }
159 
160     /***
161      * Adds a new call to the end of the queue, and also sets the
162      * signatureSequence for the call.
163      *
164      * @param call a call to add to the queue
165      */
166     public void add(Call call) {
167         int matchingCallCount = getMatchingSignatureCount(call.getSignature());
168         int signatureSequence = ++matchingCallCount;
169         call.setSignatureSequence(signatureSequence);
170         calls.add(call);
171         signatures.add(call.getSignature());
172     }
173 
174     /***
175      * Reinitialize by clearing out all calls.
176      */
177     public void clear() {
178         calls.clear();
179         signatures.clear();
180     }
181 
182     /***
183      * Indicates if the queue has a call matching the specified signature.
184      *
185      * @param signature the signature to match
186      *
187      * @return true if the queue has a call matching the specified signature
188      */
189     public boolean hasCallWithMatchingSignature(Signature signature) {
190         if (getFirstCallWithMatchingSignature(signature) == null) {
191             return false;
192         }
193 
194         return true;
195     }
196 
197     /***
198      * Indicates if the queue has a call matching the specified signature.
199      *
200      * @param call the call to match
201      *
202      * @return true if the queue has a call matching the specified signature
203      */
204     public boolean hasCallWithMatchingSignature(Call call) {
205         return hasCallWithMatchingSignature(call.getSignature());
206     }
207 
208     /***
209      * Get the number of calls in the queue.
210      *
211      * @return the number of calls in the queue
212      */
213     public int size() {
214         return calls.size();
215     }
216 
217     /***
218      * Gets the index of the first call which has the same signature.
219      *
220      * @param signature The signature to match on
221      *
222      * @return the index of the first call which has the same signature
223      */
224     protected int getIndexOfFirstCallWithMatchingSignature(Signature signature) {
225         int indexOfFirstCallWithMatchingSignature =
226             getSignatures().indexOf(signature);
227 
228         return indexOfFirstCallWithMatchingSignature;
229     }
230 
231     /***
232      * Get the signatures for all the calls in the queue, in the same order as
233      * the calls.
234      *
235      * @return Get the signatures for all the calls in the queue, in the same
236      *         order as the calls.
237      */
238     protected List getSignatures() {
239         return signatures;
240     }
241 }