KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > entities > kernel > Persistent


1 package org.infoglue.cms.entities.kernel;
2
3 import java.io.Serializable JavaDoc;
4
5 import org.infoglue.cms.util.DomainUtils;
6
7 /**
8  * This base class for persistent object makes it easy to define domain objects and
9  * have them implement the basic core services of an object that will allow them to
10  * treated properly by Lists, Sets, HashMaps, Comparators, etc. Once you have these
11  * services ironed out, it becomes much easier to deal with your domain objects and
12  * test the servies that use them
13  *
14  * @author Frank Febbraro (frank@phase2technology.com)
15  */

16 public abstract class Persistent implements BaseEntityVO, Comparable JavaDoc, Serializable JavaDoc
17 {
18     /**
19      * Returns if this persisten object is unsaved (no generated id)
20      */

21     public boolean isUnsaved()
22     {
23         return getId() == null;
24     }
25
26     /**
27      * Returns if this persisten object is saved (has generated id)
28      */

29     public boolean isSaved()
30     {
31         return getId() != null;
32     }
33
34     /**
35      * Compares an object to this one for order. The comparison is based on ID unless both are unsaved, in which case
36      * the comparison is done using hashCode(). This allows us to add multiple unsaved Persistents to sets even though
37      * they have the same ID.
38      * @param o the object to compare
39      * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the
40      * specified object
41      * @throws ClassCastException if o is not an instance of Persistent
42      * @throws NullPointerException if o is null
43      * @see #hashCode
44      */

45     public int compareTo(Object JavaDoc o)
46     {
47         Persistent p = (Persistent)o;
48         return (isUnsaved() && p.isUnsaved())
49                     ? new Integer JavaDoc(hashCode()).compareTo(new Integer JavaDoc(p.hashCode()))
50                     : DomainUtils.compare(getId(), p.getId());
51     }
52
53     /**
54      * Compares an object to this one for equality.
55      * @param o the object to compare
56      * @return true if this == o or o != null and getClass().equals(o.getClass()) and compareTo(o) == 0
57      * @see #compareTo
58      */

59     public boolean equals(Object JavaDoc o)
60     {
61         return super.equals(o) || (o != null && getClass().equals(o.getClass()) && compareTo(o) == 0);
62     }
63
64     /**
65      * Creates a hash code for this persistent.
66      * @return Object.hashCode() if unsaved, otherwise returns the id's hashCode()
67      */

68     public int hashCode()
69     {
70         return (isUnsaved()) ? super.hashCode() : DomainUtils.hashCode(getId());
71     }
72
73     /**
74      * Returns a string representation of this object. Subclasses that require a customized string representation should
75      * override toStringBuffer() rather than this method.
76      * @return toStringBuffer().toString()
77      * @see #toStringBuffer
78      */

79     public String JavaDoc toString()
80     {
81         return toStringBuffer().toString();
82     }
83
84     /**
85      * Returns a string buffer representation of this object. The idea is that subclasses can grab the string buffer
86      * returned by this method and append to it, to save some gratuitous string creation.
87      * @return a StringBuffer representation of this object
88      */

89     protected StringBuffer JavaDoc toStringBuffer()
90     {
91         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(getClass().getName()).append(' ');
92         buffer.append("id=").append(getId());
93         return buffer;
94     }
95
96     /**
97      * Appends the given persistent to the given string buffer. This rather odd-looking method makes it easy for
98      * subclasses overriding toStringBuffer() (or anyone else, for that matter) to add only the ID of a related object
99      * to a string buffer without having to check for null. In practice, adding entire objects to the buffer returned
100      * by an overridden toStringBuffer() can make for a long string that's hard to read when debugging. This method
101      * helps restore some sanity when dealing with a large object that has a lot of relationships.
102      * @param buffer the desired StringBuffer
103      * @param persistent the desired Persistent
104      * @see #toStringBuffer
105      */

106     public static void append(StringBuffer JavaDoc buffer, Persistent persistent)
107     {
108         buffer = (persistent == null) ? buffer.append(persistent) : buffer.append(persistent.getId());
109     }
110 }
111
Popular Tags