/
Equals.java
68 lines (54 loc) · 1.63 KB
/
Equals.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers;
import java.io.Serializable;
import org.mockito.ArgumentMatcher;
import org.mockito.internal.matchers.text.ValuePrinter;
public class Equals implements ArgumentMatcher<Object>, ContainsExtraTypeInfo, Serializable {
private final Object wanted;
public Equals(Object wanted) {
this.wanted = wanted;
}
@Override
public boolean matches(Object actual) {
return Equality.areEqual(this.wanted, actual);
}
@Override
public Class<?> type() {
return wanted != null ? wanted.getClass() : ArgumentMatcher.super.type();
}
@Override
public String toString() {
return describe(wanted);
}
private String describe(Object object) {
return ValuePrinter.print(object);
}
@Override
public final Object getWanted() {
return wanted;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Equals)) {
return false;
}
Equals other = (Equals) o;
return (this.wanted == null && other.wanted == null)
|| this.wanted != null && this.wanted.equals(other.wanted);
}
@Override
public int hashCode() {
return 1;
}
@Override
public String toStringWithType(String className) {
return "(" + className + ") " + describe(wanted);
}
@Override
public boolean typeMatches(Object target) {
return wanted != null && target != null && target.getClass() == wanted.getClass();
}
}