1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package sfutils;
29
30 import java.io.Serializable;
31
32 /***
33 * An {@link Object} that has a canonical name of some kind.
34 *
35 * @author <a href="mailto:ljnelson94@alumni.amherst.edu">Laird Nelson</a>
36 * @version $Revision: 1.1 $ $Date: 2003/07/12 16:13:24 $
37 * @since May 21, 2003
38 */
39 public abstract class NamedObject implements Serializable {
40
41 /***
42 * The name of this {@link NamedObject}. This field may be <code>null</code>.
43 */
44 private String name;
45
46 /***
47 * Creates a new {@link NamedObject}.
48 */
49 public NamedObject() {
50 super();
51 }
52
53 /***
54 * Creates a new {@link NamedObject} with the supplied name. This constructor
55 * calls the {@link #setName(String)} method with the supplied {@link String}.
56 *
57 * @param name
58 * the name for the new {@link NamedObject}; may be
59 * <code>null</code>
60 */
61 public NamedObject(final String name) {
62 super();
63 this.setName(name);
64 }
65
66 /***
67 * Sets the name of this {@link NamedObject}.
68 *
69 * @param name
70 * the new name; may be <code>null</code>
71 */
72 public void setName(final String name) {
73 this.name = name;
74 }
75
76 /***
77 * Returns the name of this {@link NamedObject}. This method may return
78 * <code>null</code>.
79 *
80 * @return the name of this {@link NamedObject}, or <code>null</code>
81 */
82 public String getName() {
83 return this.name;
84 }
85
86 /***
87 * Returns a hashcode for this {@link NamedObject} based off its {@linkplain
88 * #getName() name}.
89 *
90 * @return a hashcode for this {@link NamedObject}
91 */
92 public int hashCode() {
93 final String name = this.getName();
94 if (name == null) {
95 return 0;
96 }
97 return name.hashCode();
98 }
99
100 /***
101 * Tests the supplied {@link Object} to see if it is equal to this {@link
102 * NamedObject}. An {@link Object} is equal to this {@link NamedObject} if it
103 * is an instance of the {@link NamedObject} class and its {@linkplain
104 * #getName() name} is equal to this {@link NamedObject}'s {@linkplain
105 * #getName() name}. {@link NamedObject}s are, in other words, value objects.
106 *
107 * @param anObject
108 * the {@link Object} to test; may be <code>null</code>
109 * @return <code>true</code> if and only if the supplied {@link Object} is
110 * equal to this {@link NamedObject}
111 */
112 public boolean equals(final Object anObject) {
113 if (anObject == this) {
114 return true;
115 } else if (anObject instanceof NamedObject) {
116 final NamedObject other = (NamedObject)anObject;
117 final String name = this.getName();
118 if (name == null) {
119 return other.getName() == null;
120 }
121 return name.equals(other.getName());
122 } else {
123 return false;
124 }
125 }
126
127 /***
128 * Returns a {@link String} representation of this {@link NamedObject}. This
129 * method never returns <code>null</code>.
130 *
131 * @return a {@link String} representation of this {@link NamedObject};
132 * never <code>null</code>
133 */
134 public String toString() {
135 final String name = this.getName();
136 if (name == null) {
137 return "Unnamed";
138 }
139 return name;
140 }
141
142 }