View Javadoc

1   /* -*- mode: JDE; c-basic-offset: 2; indent-tabs-mode: nil -*-
2    *
3    * $Id: NamedObject.java,v 1.1 2003/07/12 16:13:24 ljnelson Exp $
4    *
5    * Copyright (c) 2003 Laird Jarrett Nelson.
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23   * SOFTWARE.
24   *
25   * The original copy of this license is available at
26   * http://www.opensource.org/license/mit-license.html.
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 }