forked from mockito/mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EqualsWithDelta.java
40 lines (31 loc) · 1 KB
/
EqualsWithDelta.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
/*
* 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 java.util.Objects;
import org.mockito.ArgumentMatcher;
public class EqualsWithDelta implements ArgumentMatcher<Number>, Serializable {
private final Number wanted;
private final Number delta;
public EqualsWithDelta(Number value, Number delta) {
this.wanted = value;
this.delta = delta;
}
@Override
public boolean matches(Number actual) {
if (wanted == null ^ actual == null) {
return false;
}
if (Objects.equals(wanted, actual)) {
return true;
}
return wanted.doubleValue() - delta.doubleValue() <= actual.doubleValue()
&& actual.doubleValue() <= wanted.doubleValue() + delta.doubleValue();
}
@Override
public String toString() {
return "eq(" + wanted + ", " + delta + ")";
}
}