1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.matchers;
6 
7 import java.io.Serializable;
8 import java.util.LinkedList;
9 import java.util.List;
10 
11 import org.hamcrest.Description;
12 import org.mockito.ArgumentMatcher;
13 import org.mockito.exceptions.Reporter;
14 
15 @SuppressWarnings("unchecked")
16 public class CapturingMatcher<T> extends ArgumentMatcher<T> implements CapturesArguments, Serializable {
17 
18     private static final long serialVersionUID = 4274067078639307295L;
19     private LinkedList<Object> arguments = new LinkedList<Object>();
20 
21     /* (non-Javadoc)
22      * @see org.mockito.ArgumentMatcher#matches(java.lang.Object)
23      */
matches(Object argument)24     public boolean matches(Object argument) {
25         return true;
26     }
27 
28     /* (non-Javadoc)
29      * @see org.mockito.ArgumentMatcher#describeTo(org.hamcrest.Description)
30      */
describeTo(Description description)31     public void describeTo(Description description) {
32         description.appendText("<Capturing argument>");
33     }
34 
getLastValue()35     public T getLastValue() {
36         if (arguments.isEmpty()) {
37             new Reporter().noArgumentValueWasCaptured();
38             return null;
39         } else {
40             return (T) arguments.getLast();
41         }
42     }
43 
getAllValues()44     public List<T> getAllValues() {
45         return (List) arguments;
46     }
47 
captureFrom(Object argument)48     public void captureFrom(Object argument) {
49         this.arguments.add(argument);
50     }
51 }