1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.mockito.internal.verification;
7
8import org.mockito.internal.util.ObjectMethodsGuru;
9import org.mockito.internal.util.collections.ListUtil;
10import org.mockito.internal.util.collections.ListUtil.Filter;
11import org.mockito.invocation.Invocation;
12
13import java.io.Serializable;
14import java.util.LinkedList;
15import java.util.List;
16
17
18public class DefaultRegisteredInvocations implements RegisteredInvocations, Serializable {
19
20    private static final long serialVersionUID = -2674402327380736290L;
21    private final LinkedList<Invocation> invocations = new LinkedList<Invocation>();
22
23    public void add(Invocation invocation) {
24        synchronized (invocations) {
25            invocations.add(invocation);
26        }
27    }
28
29    public void removeLast() {
30        //TODO: add specific test for synchronization of this block (it is tested by InvocationContainerImplTest at the moment)
31        synchronized (invocations) {
32            if (! invocations.isEmpty()) {
33                invocations.removeLast();
34            }
35        }
36    }
37
38    public List<Invocation> getAll() {
39    	List<Invocation> copiedList;
40    	synchronized (invocations) {
41			copiedList = new LinkedList<Invocation>(invocations) ;
42		}
43
44        return ListUtil.filter(copiedList, new RemoveToString());
45    }
46
47    public boolean isEmpty() {
48        synchronized (invocations) {
49            return invocations.isEmpty();
50        }
51    }
52
53    private static class RemoveToString implements Filter<Invocation> {
54        public boolean isOut(Invocation invocation) {
55            return new ObjectMethodsGuru().isToString(invocation.getMethod());
56        }
57    }
58
59}
60