⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isequal.java

📁 不管是测试驱动开发或者是其它的开发模式
💻 JAVA
字号:
/*  Copyright (c) 2000-2004 jMock.org
 */
package org.jmock.core.constraint;

import java.lang.reflect.Array;
import org.jmock.core.Constraint;


/**
 * Is the value equal to another value, as tested by the
 * {@link java.lang.Object#equals} invokedMethod?
 */
public class IsEqual implements Constraint
{
    private Object object;

    public IsEqual( Object equalArg ) {
        object = equalArg;
    }

    public boolean eval( Object arg ) {
        return areEqual(object, arg);
    }

    public StringBuffer describeTo( StringBuffer buffer ) {
        buffer.append("eq(");
        if (object == null) {
            buffer.append("null");
        } else {
            buffer.append("<");
            buffer.append(object);
            buffer.append(">");
        }
        buffer.append(")");

        return buffer;
    }

    private static boolean areEqual( Object o1, Object o2 ) {
        if (o1 == null || o2 == null) {
            return o1 == null && o2 == null;
        } else if (isArray(o1)) {
            return isArray(o2) && areArraysEqual(o1, o2);
        } else {
            return o1.equals(o2);
        }
    }

    private static boolean areArraysEqual( Object o1, Object o2 ) {
        return areArrayLengthsEqual(o1, o2)
               && areArrayElementsEqual(o1, o2);
    }

    private static boolean areArrayLengthsEqual( Object o1, Object o2 ) {
        return Array.getLength(o1) == Array.getLength(o2);
    }

    private static boolean areArrayElementsEqual( Object o1, Object o2 ) {
        for (int i = 0; i < Array.getLength(o1); i++) {
            if (!areEqual(Array.get(o1, i), Array.get(o2, i))) return false;
        }
        return true;
    }

    private static boolean isArray( Object o ) {
        return o.getClass().isArray();
    }
}

⌨️ 快捷键说明

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