KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > constraint > IsCloseTo


1 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
2  *
3  * Created on February 10, 2002, 11:35 PM
4  */

5 package com.mockobjects.constraint;
6
7
8 /** Is the value a number equal to a value within some range of
9  * acceptable error?
10  */

11 public class IsCloseTo implements Constraint
12 {
13     private double _error;
14     private double _value;
15     
16     public IsCloseTo( double value, double error ) {
17         _error = error;
18         _value = value;
19     }
20     
21     public boolean eval( Object JavaDoc arg ) {
22         double arg_value = ((Number JavaDoc)arg).doubleValue();
23         return Math.abs( (arg_value - _value) ) <= _error;
24     }
25     
26     public String JavaDoc toString() {
27         return "a numeric value within " + _error + " of " + _value;
28     }
29 }
30
Popular Tags