1 /*
2 * Created by IntelliJ IDEA.
3 * User: Scott Farquhar
4 * Date: 12/02/2002
5 * Time: 00:52:58
6 * To change template for new class use
7 * Code Style | Class Templates options (Tools | IDE Options).
8 */
9 package com.atlassian.core.util;
10
11 import org.apache.commons.collections.Predicate;
12
13 import java.lang.reflect.Method;
14
15 /**
16 * Some common methods used against {@link Object} objects
17 */
18 public class ObjectUtils
19 {
20 private static final Predicate NOT_EMPTY_PREDICATE = new Predicate()
21 {
22 public boolean evaluate(Object o)
23 {
24 return isNotEmpty(o);
25 }
26 };
27
28 protected static Method hibernateGetClassMethod = null;
29
30 static
31 {
32 try
33 {
34 Class hibernateClass = ClassLoaderUtils.loadClass("net.sf.hibernate.Hibernate", ObjectUtils.class);
35 hibernateGetClassMethod = hibernateClass.getMethod("getClass", new Class[]{Object.class});
36 }
37 catch (Exception e)
38 {
39 }
40 }
41
42
43 /**
44 * @return True if both are null or equal, otherwise returns false
45 */
46 public static boolean isIdentical(Object a, Object b)
47 {
48 return !isDifferent(a, b);
49 }
50
51 /**
52 * @return False if both are null or equal, otherwise returns true
53 */
54 public static boolean isDifferent(Object a, Object b)
55 {
56 if ((a == null && b == null) || (a != null && a.equals(b)))
57 {
58 return false;
59 }
60
61 return true;
62 }
63
64 /**
65 * Similar to {@link org.apache.commons.lang.StringUtils#isNotEmpty} but accepts a Sttring. Type safe
66 * @param o
67 * @return true if the object is not null && != ""
68 */
69 public static boolean isNotEmpty(Object o)
70 {
71 return o != null && !"".equals(o);
72 }
73
74 /**
75 * Returns a predicate for {@link #isNotEmpty(Object)}
76 * @return Predicate
77 */
78 public static Predicate getIsSetPredicate()
79 {
80 return NOT_EMPTY_PREDICATE;
81 }
82
83 /**
84 * Gets the true class of an object, trying to use Hibernate's proxy unwrapping tools if available on the classpath.
85 * <p />
86 * Otherwise simply returns the class of the object passed in if Hibernate not on the classpath.
87 * @param o The object to examine
88 * @return The true class of the object (unwrapping Hibernate proxies etc)
89 */
90 public static Class getTrueClass(Object o)
91 {
92 if (hibernateGetClassMethod != null)
93 {
94 try
95 {
96 return (Class)hibernateGetClassMethod.invoke(null, new Object[] {o});
97 }
98 catch (Exception e)
99 {
100 }
101 }
102
103 return o.getClass();
104 }
105 }