1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.common.all;
17
18 import java.io.Serializable;
19 import java.text.DateFormat;
20
21 import org.apache.commons.beanutils.BeanUtils;
22 import org.apache.commons.beanutils.PropertyUtils;
23 import org.apache.commons.lang.builder.EqualsBuilder;
24 import org.apache.commons.lang.builder.HashCodeBuilder;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /***
29 * <p>
30 * Base class for objects, implementing toString, equals and hashCode using
31 * commons-lang.
32 * </p>
33 *
34 * <p>
35 * Also implements swallow clone using commons-beanutils.
36 * </p>
37 *
38 * <p>
39 * Note that there are security and performance constraints with this
40 * implementation, specifically they may fail under a security manager. Check
41 * commons-lang documentation for more info.
42 * </p>
43 *
44 * @author Carlos Sanchez
45 * @version $Revision: 1.7 $
46 */
47 public abstract class BaseObject implements Serializable, Cloneable {
48
49 protected final transient Log log = LogFactory.getLog(getClass());
50
51 /***
52 * This method delegates to ReflectionToStringBuilder.
53 *
54 * @see java.lang.Object#toString()
55 * @see ReflectionToStringBuilder
56 * @see DateFormat#getDateTimeInstance()
57 */
58 public String toString() {
59 return new ReflectionToStringBuilder(this).toString();
60 }
61
62 /***
63 * This method delegates to EqualsBuilder.reflectionEquals()
64 *
65 * @see java.lang.Object#equals(Object)
66 * @see EqualsBuilder#reflectionEquals(Object, Object)
67 */
68 public boolean equals(Object o) {
69 return EqualsBuilder.reflectionEquals(this, o);
70 }
71
72 /***
73 * This method delegates to HashCodeBuilder.reflectionHashCode()
74 *
75 * @see java.lang.Object#hashCode()
76 * @see HashCodeBuilder#reflectionHashCode(Object)
77 */
78 public int hashCode() {
79 return HashCodeBuilder.reflectionHashCode(this);
80 }
81
82 /***
83 * Swallow cloning. This method delegates to BeanUtils.cloneBean(). Returns
84 * <code>null</code> on any exception
85 *
86 * @see java.lang.Object#clone()
87 * @see BeanUtils#cloneBean(Object)
88 * @see PropertyUtils#copyProperties(Object, Object)
89 */
90 public Object clone() {
91 try {
92 return BeanUtils.cloneBean(this);
93 } catch (Exception e) {
94 log.error("Exception cloning bean: " + this + "\n"
95 + "Exception was " + e + ": " + e.getLocalizedMessage());
96 return null;
97 }
98 }
99 }