/** n o w j a v a . c o m 提供 **/
//package com.nowjava;
public class Main {
public static void main(String[] argv) {
Object value = "nowjava.com";
Object otherValue = "nowjava.com";
System.out.println(areEqual(value, otherValue));
}
/**
* A comprehensive isEqual method that handles nulls and arrays safely.
*
* @param value Object
* @param otherValue Object
* @return boolean
*/
public static final boolean areEqual(Object value, Object otherValue) {
if (value == otherValue)
return true;
if (value == null)
return false;/** from N o w J a v a . c o m**/
if (otherValue == null)
return false;
if (value.getClass().getComponentType() != null)
return arraysAreEqual(value, otherValue);
return value.equals(otherValue);
}
/**
* Returns true if the objects are array instances and each of their elements compares
* via equals as well.
*
* @param value Object
* @param otherValue Object
* @return boolean
*/
public static final boolean arraysAreEqual(Object value,
Object otherValue) {
if (value instanceof Object[]) {
if (otherValue instanceof Object[])
return valuesAreTransitivelyEqual((Object[]) value,
(Object[]) otherValue);
return false;
}
return false;
}
/**
* Returns whether the arrays are equal by examining each of their elements, even if they are
* arrays themselves.
*
* @param thisArray Object[]
* @param thatArray Object[]
* @return boolean
*/
public static final boolean valuesAreTransitivelyEqual(
Object[] thisArray, Object[] thatArray) {
if (thisArray == thatArray)
return true;
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。