001package org.kuali.common.util; 002 003public class ObjectUtils { 004 005 /** 006 * <p> 007 * This method returns <code>true</code> if the <code>toString()</code> methods on both objects return matching strings AND both objects are the exact same runtime type. 008 * </p> 009 * 010 * <p> 011 * Returns <code>true</code> immediately if <code>main==other</code> (ie they are the same object). 012 * </p> 013 * 014 * <p> 015 * Returns <code>false</code> immediately if <code>other==null</code> or is a different runtime type than <code>main</code>. 016 * </p> 017 * 018 * <p> 019 * If neither one is <code>null</code>, and both are the exact same runtime type, then compare the <code>toString()</code> methods 020 * </p> 021 * 022 * @param main 023 * The object <code>other</code> is being compared to. 024 * @param other 025 * The object being examined for equality with <code>main</code>. 026 * 027 * @throws NullPointerException 028 * If <code>main</cod> is <code>null</code> or <code>main.toString()</code> returns <code>null</code> 029 */ 030 public static boolean equalsByToString(Object main, Object other) { 031 032 // Main can't be null 033 if (main == null) { 034 throw new NullPointerException("main is null"); 035 } 036 037 // They are the same object 038 if (main == other) { 039 return true; 040 } 041 042 if (notEqual(main, other)) { 043 return false; // Don't bother comparing the toString() methods 044 } else { 045 return main.toString().equals(other.toString()); 046 } 047 } 048 049 /** 050 * Return true if <code>main</code> is definitely not equal to <code>other</code>. More precisely, if <code>other</code> is null <b>OR</b> a different runtime type than 051 * <code>main</code>, return true 052 * 053 * @param main 054 * The object <code>other</code> is being compared to. 055 * @param other 056 * The object being examined for equality with <code>main</code>. 057 * 058 * @throws NullPointerException 059 * If <code>main</cod> is <code>null</code> 060 */ 061 public static boolean notEqual(Object main, Object other) { 062 // Main can't be null 063 if (main == null) { 064 throw new NullPointerException("main is null"); 065 } else if (other == null) { 066 return true; 067 } else { 068 return main.getClass() != other.getClass(); 069 } 070 } 071}