LastControl.java revision 47d431f63a66505a645f282416659a9758a91f1c
1/*
2 * Copyright 2001-2009 OFFIS, Tammo Freese
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.easymock.internal;
17
18import java.util.ArrayList;
19import java.util.LinkedList;
20import java.util.List;
21import java.util.Stack;
22
23import org.easymock.IArgumentMatcher;
24import org.easymock.internal.matchers.And;
25import org.easymock.internal.matchers.Not;
26import org.easymock.internal.matchers.Or;
27
28public class LastControl {
29    private static final ThreadLocal<MocksControl> threadToControl = new ThreadLocal<MocksControl>();
30
31    private static final ThreadLocal<Stack<Invocation>> threadToCurrentInvocation = new ThreadLocal<Stack<Invocation>>();
32
33    private static final ThreadLocal<Stack<IArgumentMatcher>> threadToArgumentMatcherStack = new ThreadLocal<Stack<IArgumentMatcher>>();
34
35    public static void reportLastControl(MocksControl control) {
36        if (control != null) {
37            threadToControl.set(control);
38        } else {
39            threadToControl.remove();
40        }
41    }
42
43    public static MocksControl lastControl() {
44        return threadToControl.get();
45    }
46
47    public static void reportMatcher(IArgumentMatcher matcher) {
48        Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get();
49        if (stack == null) {
50            stack = new Stack<IArgumentMatcher>();
51            threadToArgumentMatcherStack.set(stack);
52        }
53        stack.push(matcher);
54    }
55
56    public static List<IArgumentMatcher> pullMatchers() {
57        Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get();
58        if (stack == null) {
59            return null;
60        }
61        threadToArgumentMatcherStack.remove();
62        return new ArrayList<IArgumentMatcher>(stack);
63    }
64
65    public static void reportAnd(int count) {
66        Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get();
67        assertState(stack != null, "no matchers found.");
68        stack.push(new And(popLastArgumentMatchers(count)));
69    }
70
71    public static void reportNot() {
72        Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get();
73        assertState(stack != null, "no matchers found.");
74        stack.push(new Not(popLastArgumentMatchers(1).get(0)));
75    }
76
77    private static List<IArgumentMatcher> popLastArgumentMatchers(int count) {
78        Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get();
79        assertState(stack != null, "no matchers found.");
80        assertState(stack.size() >= count, "" + count + " matchers expected, "
81                + stack.size() + " recorded.");
82        List<IArgumentMatcher> result = new LinkedList<IArgumentMatcher>();
83        result.addAll(stack.subList(stack.size() - count, stack.size()));
84        for (int i = 0; i < count; i++) {
85            stack.pop();
86        }
87        return result;
88    }
89
90    private static void assertState(boolean toAssert, String message) {
91        if (!toAssert) {
92            threadToArgumentMatcherStack.remove();
93            throw new IllegalStateException(message);
94        }
95    }
96
97    public static void reportOr(int count) {
98        Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get();
99        assertState(stack != null, "no matchers found.");
100        stack.push(new Or(popLastArgumentMatchers(count)));
101    }
102
103    public static Invocation getCurrentInvocation() {
104        Stack<Invocation> stack = threadToCurrentInvocation.get();
105        if (stack == null || stack.empty()) {
106            return null;
107        }
108        return stack.lastElement();
109    }
110
111    public static void pushCurrentInvocation(Invocation invocation) {
112        Stack<Invocation> stack = threadToCurrentInvocation.get();
113        if (stack == null) {
114            stack = new Stack<Invocation>();
115            threadToCurrentInvocation.set(stack);
116        }
117        stack.push(invocation);
118    }
119
120    public static void popCurrentInvocation() {
121        Stack<Invocation> stack = threadToCurrentInvocation.get();
122        stack.pop();
123    }
124}
125