Source code: docs/template/Example.java
1
2
3 package docs.template;
4
5 /**
6 * The point of base is that it contains some example implementations of
7 * code that is commonly written, and often done incorrectly.
8 *
9 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
10 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
11 * Distribution Licence:<br />
12 * Project B is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General Public License,
14 * version 2 as published by the Free Software Foundation.<br />
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.<br />
19 * The License is available on the internet
20 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
21 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
23 * The copyright to this program is held by it's authors.
24 * </font></td></tr></table>
25 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
26 * @see <{docs.Licence}>
27 * @author Joe Walker
28 * @version D8.I8.T0
29 */
30 class Example extends Object implements Cloneable
31 {
32 /**
33 * Get a copy of ourselves. Points to note:
34 * Call clone() not new() on member Objects, and on us.
35 * Do not use Copy Constructors! - they do not inherit well.
36 * Think about this needing to be synchronized
37 * If this is not cloneable then writing cloneable children is harder
38 * @return A complete (shallow of deep) copy of ourselves
39 * @exception java.lang.CloneNotSupportedException We don't do this but our kids might
40 */
41 public Object clone() throws CloneNotSupportedException
42 {
43 // This gets us a shallow copy
44 Example copy = (Example) super.clone();
45
46 // For all of the member objects
47 if (data != null)
48 copy.data = (Example) data.clone();
49
50 return copy;
51 }
52
53 /**
54 * Is this Object equal to us. Points to note:
55 * If you override equals(), you must override hashCode() too.
56 * If you are doing this it is a good idea to be immutable.
57 * @param obj The thing to test against
58 * @return True/False is we are or are not equal to obj
59 */
60 public boolean equals(Object obj)
61 {
62 // Since this can not be null
63 if (obj == null)
64 return false;
65
66 // Check that that is the same as this
67 // Don't use instanceof since that breaks inheritance
68 if (!obj.getClass().equals(this.getClass()))
69 return false;
70
71 // If super does equals ...
72 if (super.equals(obj) == false)
73 return false;
74
75 // The real bit ...
76 Example that = (Example) obj;
77
78 // Remember that many things will be better using that.equals(this)
79 return that.data == this.data;
80 }
81
82 /**
83 * Get a moderately unique id for this Object. Points to note:
84 * JDK1.x - hashCode should not change for an Object.
85 * JDK2.0 - hashCode may change if it's data changes
86 * If you override hashCode(), you must override equals() too.
87 * If you are doing this it is a good idea to be immutable.
88 * @return The hashing number
89 */
90 public int hashCode()
91 {
92 // For JDK1.c this is nigh on impossible so a default
93 // could be something like
94 // return 0;
95
96 // On JDK2 hashCode is allowed to change so:
97 return data.hashCode();
98 }
99
100 /**
101 * Get a sting representation of this object. Points to note:
102 * Think about inheritance when using string names.
103 * @return The string representation
104 */
105 public String toString()
106 {
107 return getClass().getName() + "[" + data + "]";
108 }
109
110 /**
111 * Clean up after ourselves. Points to note:
112 * There is no garantee that this will be called
113 * Call the superclasses filalize()
114 * @exception java.lang.Throwable Almost anything could go wrong
115 */
116 protected void finalize() throws Throwable
117 {
118 try
119 {
120 // Whatever
121 }
122 catch(Throwable ex)
123 {
124 // Should we think about ThreadDeath here?
125 }
126
127 super.finalize();
128 }
129
130 /**
131 * Example member, used in the clone example
132 */
133 private Example data;
134 }