Home | History | Annotate | Download | only in matchers
      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.matchers;
      7 
      8 import org.hamcrest.Description;
      9 import org.mockito.ArgumentMatcher;
     10 
     11 import java.io.Serializable;
     12 
     13 
     14 public class EqualsWithDelta extends ArgumentMatcher<Number> implements Serializable {
     15     private static final long serialVersionUID = 5066980489920383664L;
     16 
     17     private final Number wanted;
     18 
     19     private final Number delta;
     20 
     21     public EqualsWithDelta(Number value, Number delta) {
     22         this.wanted = value;
     23         this.delta = delta;
     24     }
     25 
     26     public boolean matches(Object actual) {
     27         Number actualNumber = (Number) actual;
     28         if (wanted == null ^ actual == null) {
     29             return false;
     30         }
     31 
     32         if (wanted == null && actual == null) {
     33             return true;
     34         }
     35 
     36         return wanted.doubleValue() - delta.doubleValue() <= actualNumber.doubleValue()
     37                 && actualNumber.doubleValue() <= wanted.doubleValue()
     38                         + delta.doubleValue();
     39     }
     40 
     41     public void describeTo(Description description) {
     42         description.appendText("eq(" + wanted + ", " + delta + ")");
     43     }
     44 }
     45