With complex datatypes, objects references, internalization, and all the other intricacies that go into modern day software development, the matter of equality is often a bit more involved than simply ==. Or at least it can be made to be, as today's two examples demonstrate.

First up is Willy's specimen, who adds "I particularly enjoy understatement of the comments in the dateAreEquals() and equals() methods.

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;


/**
 * Represents methods to compare comparing objects.  
 */
public class EqualUtil {

   private static final Logger log = Logger.getLogger(EqualUtil.class);   

   /**
    * Compare two objects for equality when both objects are potentially null. 
    */
   public static boolean dateAreEquals(Date obj1, Date obj2){
      //Not optimized. Only layout like that for clarity.
      if(obj1==null && obj2 == null){
         return true;
      }else if (obj1!=null && obj2==null){
         return false;
      }else if (obj1==null && obj2!=null){
         return false;
      }else{
          Calendar c1 = new GregorianCalendar();
          c1.setTime(obj1);
          Calendar c2 = new GregorianCalendar();
          c2.setTime(obj2);
          if(c1.get(Calendar.YEAR)==c2.get(Calendar.YEAR) &&
             c1.get(Calendar.MONTH)==c2.get(Calendar.MONTH) &&
             c1.get(Calendar.DAY_OF_MONTH)==c2.get(Calendar.DAY_OF_MONTH)){
              return true;
          }else{
              return false;
          }
       }
      
   }
        
   /**
    * Compare two objects for equality when both objects are potentially null. 
    */
   public static boolean equals(Object obj1, Object obj2){
      //Not optimized. Only layout like that for clarity.
      if(obj1==null && obj2 == null){
         return true;
      }else if (obj1!=null && obj2==null){
         return false;
      }else if (obj1==null && obj2!=null){
         return false;
      }else{
         return obj1.equals(obj2);
       }
      
   }

   /**
    * Compare two double for equality based on a predified precision. 
    */
   public static boolean equalsForQuantity(double value1, double value2){
      return Math.abs(value1 - value2) < SwapConstant.DOUBLE_PRECISION_FOR_QUANTITY;      
   }
   
   /**
    * Compare two double for equality based on a predified precision. 
    */
   public static boolean equalsForMoney(double value1, double value2){
       double diff = Math.abs(value1 - value2);
       boolean equals = diff < SwapConstant.DOUBLE_PRECISION_FOR_MONEY;
//       if(!equals && 
//           diff < SwapConstant.DOUBLE_PRECISION_TWO_DECIMAL &&
//           diff > SwapConstant.DOUBLE_PRECISION_FOR_MONEY){
//           log.debug("Not equal because of new precision. abs(" + value1 + "-" +value2+")="+diff);
//       }       
      return equals;      
   }

   /**
    * Compare two double for equality based on a predified precision. 
    */
   public static boolean equalsForRate(double value1, double value2){
      return Math.abs(value1 - value2) < SwapConstant.DOUBLE_PRECISION_FOR_RATE;      
   }

   /**
    * Compare two int for equality based on a predified precision. 
    */
   public static boolean equals(int value1, int value2){
      return value1 == value2;      
   }
   
   

}

 

And then Neville found this in the utilities class of an application he recently inherited.

public static bool notEqual(Object obj1, Object obj2)
{
    if (obj1 == null && obj2 == null)
	return false;
    if (obj1 == "" && obj2 == null)
	return false;
    if (obj1 == null && obj2 == "")
	return false;
    if (obj1 == null || obj2 == null)
	return true;
    if (obj1.Equals(obj2))
	return false;
    return true;
}

public static bool isEqual(Object obj1, Object obj2)
{
    if (obj1 == null || obj2 == null)
	return false;
    if (obj1 == "" && obj2 == null)
	return false;
    if (obj1 == null && obj2 == "")
	return false;
    if (obj1.Equals(obj2))
	return false;
    return true;
}
public static bool isNotEqual(Object obj1, Object obj2)
{
    if (obj1 == null && obj2 == null)
	return false;
    if (obj1 == null && obj2 == "")
	return false;
    if (obj1 == "" && obj2 == null)
	return false;
    if (obj1 == null)
    {
	if (obj2 != null) return true;
    }
    if (obj2 == null)
    {
	if (obj1 != null) return true;
    }
    if (obj1.Equals(obj2))
	return false;
    return true;
}