KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > internal > ResultList


1 /*
2  * Copyright (c) 2001-2005 OFFIS. This program is made available under the terms of
3  * the MIT License.
4  */

5 package org.easymock.internal;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.LinkedList JavaDoc;
9 import java.util.List JavaDoc;
10
11 public class ResultList {
12
13     private int callCount;
14
15     private LinkedList JavaDoc<Range> ranges = new LinkedList JavaDoc<Range>();
16
17     private List JavaDoc<Result> results = new ArrayList JavaDoc<Result>();
18
19     void add(Result result, Range range) {
20         if (!ranges.isEmpty()) {
21             Range lastRange = ranges.getLast();
22             if (!lastRange.hasFixedCount())
23                 throw new RuntimeExceptionWrapper(
24                         new IllegalStateException JavaDoc(
25                                 "last method called on mock already has a non-fixed count set."));
26         }
27         ranges.add(range);
28         results.add(result);
29     }
30
31     Result next() {
32         int currentPosition = 0;
33         for (int i = 0; i < ranges.size(); i++) {
34             Range interval = ranges.get(i);
35             if (interval.hasOpenCount()) {
36                 callCount += 1;
37                 return getResult(i);
38             }
39             currentPosition += interval.getMaximum();
40             if (currentPosition > callCount) {
41                 callCount += 1;
42                 return getResult(i);
43             }
44         }
45         return null;
46     }
47
48     private Result getResult(int i) {
49         return results.get(i);
50     }
51
52     boolean hasValidCallCount() {
53         return getMainInterval().contains(getCallCount());
54     }
55
56     String JavaDoc getMessage() {
57         return getMessage(getCallCount());
58     }
59
60     String JavaDoc getMessage(int count) {
61         return getMainInterval().expectedAndActual(count);
62     }
63
64     private Range getMainInterval() {
65         int min = 0;
66         int max = 0;
67
68         for (Range interval : ranges) {
69             min += interval.getMinimum();
70             if (interval.hasOpenCount() || max == Integer.MAX_VALUE) {
71                 max = Integer.MAX_VALUE;
72             } else {
73                 max += interval.getMaximum();
74             }
75         }
76
77         return new Range(min, max);
78     }
79
80     public int getCallCount() {
81         return callCount;
82     }
83 }
84
Popular Tags