1 package org.kuali.common.util;
2
3 public class ObjectUtils {
4
5 /**
6 * <p>
7 * 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.
8 * </p>
9 *
10 * <p>
11 * Returns <code>true</code> immediately if <code>main==other</code> (ie they are the same object).
12 * </p>
13 *
14 * <p>
15 * Returns <code>false</code> immediately if <code>other==null</code> or is a different runtime type than <code>main</code>.
16 * </p>
17 *
18 * <p>
19 * If neither one is <code>null</code>, and both are the exact same runtime type, then compare the <code>toString()</code> methods
20 * </p>
21 *
22 * @param main
23 * The object <code>other</code> is being compared to.
24 * @param other
25 * The object being examined for equality with <code>main</code>.
26 *
27 * @throws NullPointerException
28 * If <code>main</cod> is <code>null</code> or <code>main.toString()</code> returns <code>null</code>
29 */
30 public static boolean equalsByToString(Object main, Object other) {
31
32 // Main can't be null
33 if (main == null) {
34 throw new NullPointerException("main is null");
35 }
36
37 // They are the same object
38 if (main == other) {
39 return true;
40 }
41
42 if (notEqual(main, other)) {
43 return false; // Don't bother comparing the toString() methods
44 } else {
45 return main.toString().equals(other.toString());
46 }
47 }
48
49 /**
50 * 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
51 * <code>main</code>, return true
52 *
53 * @param main
54 * The object <code>other</code> is being compared to.
55 * @param other
56 * The object being examined for equality with <code>main</code>.
57 *
58 * @throws NullPointerException
59 * If <code>main</cod> is <code>null</code>
60 */
61 public static boolean notEqual(Object main, Object other) {
62 // Main can't be null
63 if (main == null) {
64 throw new NullPointerException("main is null");
65 } else if (other == null) {
66 return true;
67 } else {
68 return main.getClass() != other.getClass();
69 }
70 }
71 }