View Javadoc

1   /* -*- mode: JDE; c-basic-offset: 2; indent-tabs-mode: nil -*-
2    *
3    * $Id: HideableNamedObject.java,v 1.5 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.frs;
29  
30  import sfutils.NamedObject;
31  
32  /***
33   * A {@link NamedObject} that {@linkplain #setHidden(boolean) may be hidden from
34   * view}.
35   *
36   * @author     <a href="mailto:ljnelson94@alumni.amherst.edu">Laird Nelson</a>
37   * @version    $Revision: 1.5 $ $Date: 2003/07/12 16:13:24 $
38   * @since      May 21, 2003
39   */
40  public abstract class HideableNamedObject extends NamedObject {
41  
42    /***
43     * Whether or not this {@link HideableNamedObject} is currently hidden.
44     *
45     * @see        #isHidden()
46     */
47    private boolean hidden;
48  
49    /***
50     * Creates a new {@link HideableNamedObject}.
51     */
52    public HideableNamedObject() {
53      super();
54    }
55  
56    /***
57     * Creates a new {@link HideableNamedObject} with the supplied name.  This
58     * constructor calls its {@link NamedObject#NamedObject(String) superclass'
59     * implementation} with the supplied name.
60     *
61     * @param      name
62     *               the name for the new {@link HideableNamedObject}; may be
63     *               <code>null</code>
64     */
65    public HideableNamedObject(final String name) {
66      super(name);
67    }
68  
69    /***
70     * Returns <code>true</code> if and only if this {@link HideableNamedObject}
71     * is hidden from view.
72     *
73     * @return     <code>true</code> if and only if this {@link
74     *                HideableNamedObject} is hidden from view
75     * @see        #setHidden(boolean)
76     */
77    public boolean isHidden() {
78      return this.hidden;
79    }
80  
81    /***
82     * Sets whether this {@link HideableNamedObject} is hidden from view.
83     *
84     * @param      hidden
85     *               if <code>true</code>, this {@link HideableNamedObject} will
86     *               be hidden from view
87     * @see        #isHidden()
88     */
89    public void setHidden(final boolean hidden) {
90      this.hidden = hidden;
91    }
92  
93    /***
94     * Returns a hashcode for this {@link HideableNamedObject} based off its
95     * {@linkplain #getName() name} and its {@linkplain #isHidden() hidden
96     * status}.
97     *
98     * @return     a hashcode for this {@link HideableNamedObject}
99     */
100   public int hashCode() {
101     int hashCode = super.hashCode();
102     if (this.isHidden()) {
103       ++hashCode;
104     }
105     return hashCode;
106   }
107   
108   /***
109    * Tests the supplied {@link Object} to see if it is equal to this {@link
110    * HideableNamedObject}.  An {@link Object} is equal to this {@link
111    * HideableNamedObject} if it is an instance of the {@link
112    * HideableNamedObject} class and its {@linkplain #getName() name} is equal to
113    * this {@link HideableNamedObject}'s {@linkplain #getName() name}.  {@link
114    * HideableNamedObject}s are, in other words, value objects.
115    *
116    * @param      anObject
117    *               the {@link Object} to test; may be <code>null</code>
118    * @return     <code>true</code> if and only if the supplied {@link Object} is
119    *               equal to this {@link HideableNamedObject} 
120    */
121   public boolean equals(final Object anObject) {
122     if (anObject == this) {
123       return true;
124     } else if (anObject instanceof HideableNamedObject &&
125                super.equals(anObject)) {
126 
127       // Compare hidden statuses.
128       return this.isHidden() == ((HideableNamedObject)anObject).isHidden();
129 
130     } else {
131       return false;
132     }
133   }
134 
135   /***
136    * Returns a {@link String} representation of this {@link
137    * HideableNamedObject}.  This method never returns <code>null</code>.
138    *
139    * @return     a {@link String} representation of this {@link
140    *               HideableNamedObject}; never <code>null</code> 
141    */
142   public String toString() {
143     final StringBuffer returnMe = new StringBuffer();
144     final String parentRep = super.toString();
145     if (parentRep == null) {
146       returnMe.append("Unnamed");
147     } else {
148       returnMe.append(parentRep);
149     }
150     if (this.isHidden()) {
151       returnMe.append(" (hidden)");
152     }
153     return returnMe.toString();
154   }
155 
156 }