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