KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > core > constraint > IsCloseTo


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package org.jmock.core.constraint;
4
5 import org.jmock.core.Constraint;
6
7
8 /**
9  * Is the value a number equal to a value within some range of
10  * acceptable error?
11  */

12 public class IsCloseTo implements Constraint
13 {
14     private double error;
15     private double value;
16
17     public IsCloseTo( double value, double error ) {
18         this.error = error;
19         this.value = value;
20     }
21
22     public boolean eval( Object JavaDoc arg ) {
23         double argValue = ((Number JavaDoc)arg).doubleValue();
24         return Math.abs((argValue - value)) <= error;
25     }
26
27     public StringBuffer JavaDoc describeTo( StringBuffer JavaDoc buffer ) {
28         return buffer.append("a numeric value within ")
29                 .append(error)
30                 .append(" of ")
31                 .append(value);
32     }
33 }
34
Popular Tags