一、前言
关于fop的fop-core源码包的org.apache.fop.util.CompareUtil比较工具类,对对象Object见比较判断、Double对象比较判断Double.doubleToLongBits(n1)等。
二、源码说明
package org.apache.fop.util;@b@@b@/**@b@ * A utility class that provides helper methods for implementing equals and hashCode.@b@ */@b@public final class CompareUtil {@b@@b@ private static final Object TIE_LOCK = new Object();@b@@b@ private CompareUtil() {@b@ }@b@@b@ /**@b@ * Compares two objects for equality.@b@ * In order to prevent lock-ordering deadlocks the following strategy is used:@b@ * when two non null objects are passed to the method, the comparison@b@ * is done by calling the {@link Object#equals(Object)} method of the object@b@ * with the lower hash code ({@link System#identityHashCode(Object)});@b@ * in the rare case that two different objects have the same hash code, a lock@b@ * is used.@b@ *@b@ * @param o1 an object@b@ * @param o2 another object@b@ * @return true if either o1 and o2 are null or if o1.equals(o2)@b@ */@b@ public static boolean equal(Object o1, Object o2) {@b@ int o1Hash = System.identityHashCode(o1);@b@ int o2Hash = System.identityHashCode(o2);@b@ if (o1Hash == o2Hash && o1 != o2 && o1Hash != 0) {@b@ // in the rare case of different objects with the same hash code,@b@ // the tieLock object is used to synchronize access@b@ synchronized (TIE_LOCK) {@b@ return o1.equals(o2);@b@ }@b@ }@b@ if (o1Hash > o2Hash) {@b@ Object tmp = o1;@b@ o1 = o2;@b@ o2 = tmp;@b@ }@b@ return o1 == null ? o2 == null : o1 == o2 || o1.equals(o2);@b@ }@b@@b@ /**@b@ * Returns the hash code of the given object.@b@ *@b@ * @param object an object@b@ * @return object.hashCode(), or 0 if object is null@b@ */@b@ public static int getHashCode(Object object) {@b@ return object == null ? 0 : object.hashCode();@b@ }@b@@b@ /**@b@ * Compares two numbers for equality. Uses the same comparison algorithm as@b@ * the {@link Double#equals(Object)} method.@b@ *@b@ * @param n1 a number@b@ * @param n2 another number@b@ * @return true if the two numbers are equal, false otherwise@b@ */@b@ public static boolean equal(double n1, double n2) {@b@ return Double.doubleToLongBits(n1) == Double.doubleToLongBits(n2);@b@ }@b@@b@ /**@b@ * Returns a hash code for the given number. Applies the same algorithm as@b@ * the {@link Double#hashCode()} method.@b@ *@b@ * @param number a number@b@ * @return a hash code for that number@b@ */@b@ public static int getHashCode(double number) {@b@ long bits = Double.doubleToLongBits(number);@b@ return (int) (bits ^ (bits >>> 32));@b@ }@b@@b@}