iscloseto.java

来自「不管是测试驱动开发或者是其它的开发模式」· Java 代码 · 共 34 行

JAVA
34
字号
/*  Copyright (c) 2000-2004 jMock.org
 */
package org.jmock.core.constraint;

import org.jmock.core.Constraint;


/**
 * Is the value a number equal to a value within some range of
 * acceptable error?
 */
public class IsCloseTo implements Constraint
{
    private double error;
    private double value;

    public IsCloseTo( double value, double error ) {
        this.error = error;
        this.value = value;
    }

    public boolean eval( Object arg ) {
        double argValue = ((Number)arg).doubleValue();
        return Math.abs((argValue - value)) <= error;
    }

    public StringBuffer describeTo( StringBuffer buffer ) {
        return buffer.append("a numeric value within ")
                .append(error)
                .append(" of ")
                .append(value);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?