View Javadoc

1   /* -*- mode: JDE; c-basic-offset: 2; indent-tabs-mode: nil -*-
2    *
3    * $Id: Administrator.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  /***
31   * An administrator of a <a href="http://sourceforge.net/">SourceForge</a>
32   * project.
33   *
34   * @author     <a href="mailto:ljnelson94@alumni.amherst.edu">Laird Nelson</a>
35   * @version    $Revision: 1.1 $ $Date: 2003/07/12 16:13:24 $
36   * @since      June 17, 2003
37   */
38  public class Administrator extends NamedObject {
39  
40    /***
41     * This {@link Administrator}'s password.  This field will never be
42     * <code>null</code> or equal to the empty {@link String}.
43     */
44    private String password;
45  
46    /***
47     * Creates a new {@link Administrator} without a username or password.  This
48     * new {@link Administrator} will be in an illegal state until both the
49     * username and password are set to non-<code>null</code> values that are not
50     * equal to the empty {@link String}.
51     *
52     * @see        #setName(String)
53     * @see        #setPassword(String)
54     */
55    public Administrator() {
56      super();
57    }
58  
59    /***
60     * Creates a new {@link Administrator} with the supplied username and
61     * password.  Neither the username nor the password may be <code>null</code>
62     * or equal to the empty {@link String}.
63     *
64     * @param      name
65     *               the username; must not be <code>null</code> or equal to the
66     *               empty {@link String}
67     * @param      password
68     *               the password; must not be <code>null</code> or equal to the
69     *               empty {@link String}
70     * @exception  IllegalArgumentException
71     *               if either <code>name</code> or <code>password</code> is
72     *               <code>null</code> or equal to the empty {@link String}
73     */
74    public Administrator(final String name,
75                         final String password)
76      throws IllegalArgumentException {
77      this();
78      this.setName(name);
79      this.setPassword(password);
80    }
81  
82    /***
83     * Returns the username of this {@link Administrator}.  If the username has
84     * not been previously set via the {@link #setName(String)} method, then this
85     * method will throw an {@link IllegalStateException}.  This method will never
86     * return <code>null</code> or the empty {@link String}, and subclasses must
87     * honor this contract.
88     *
89     * @return     the username of this {@link Administrator}; never
90     *               <code>null</code> or the empty {@link String}
91     * @exception  IllegalStateException
92     *               if the {@link #setName(String)} method has not yet been
93     *               called
94     * @see        #setName(String)
95     */
96    public String getName() throws IllegalStateException {
97      final String name = super.getName();
98      if (name == null) {
99        throw new IllegalStateException("Set name first");
100     }
101     return name;
102   }
103 
104   /***
105    * Sets the username of this {@link Administrator}.  The supplied username
106    * must not be <code>null</code> or equal to the empty {@link String}, or an
107    * {@link IllegalArgumentException} will be thrown.
108    *
109    * @param      username
110    *               the username; must not be <code>null</code> or equal to the
111    *               empty {@link String}
112    * @exception  IllegalArgumentException
113    *               if <code>username</code> is <code>null</code> or equal to the
114    *               empty {@link String}
115    */
116   public void setName(final String username)
117     throws IllegalArgumentException {
118     if (username == null || username.length() <= 0) {
119       throw new IllegalArgumentException("Username cannot be null or empty");
120     }
121     super.setName(username);
122   }
123 
124   /***
125    * Returns the password of this {@link Administrator}.  If the password has
126    * not been previously set via the {@link #setPassword(String)} method, then
127    * this method will throw an {@link IllegalStateException}.  This method will
128    * never return <code>null</code> or the empty {@link String}, and subclasses
129    * must honor this contract.
130    *
131    * @return     the password of this {@link Administrator}; never
132    *               <code>null</code> or the empty {@link String}
133    * @exception  IllegalStateException
134    *               if the {@link #setPassword(String)} method has not yet been
135    *               called
136    * @see        #setPassword(String)
137    */
138   public String getPassword() throws IllegalStateException {
139     if (this.password == null) {
140       throw new IllegalStateException("Set password first");
141     }
142     return this.password;
143   }
144 
145   /***
146    * Sets the password of this {@link Administrator}.  The supplied password
147    * must not be <code>null</code> or equal to the empty {@link String}, or an
148    * {@link IllegalArgumentException} will be thrown.
149    *
150    * @param      password
151    *               the password; must not be <code>null</code> or equal to the
152    *               empty {@link String}
153    * @exception  IllegalArgumentException
154    *               if <code>password</code> is <code>null</code> or equal to the
155    *               empty {@link String}
156    */
157   public void setPassword(final String password)
158     throws IllegalArgumentException {
159     if (password == null || password.length() <= 0) {
160       throw new IllegalArgumentException("Password cannot be null or empty");
161     }
162     this.password = password;
163   }
164   
165   /***
166    * Returns a hashcode for this {@link Administrator} based off its {@linkplain
167    * #getName() username} and {@linkplain #getPassword() password}.
168    *
169    * @return     a hashcode for this {@link Administrator}
170    */
171   public int hashCode() {
172     String userName = null;
173     try {
174       userName = this.getName();
175     } catch (final IllegalStateException ignore) {
176       userName = null;
177     }
178     String password = null;
179     try {
180       password = this.getPassword();
181     } catch (final IllegalStateException oops) {
182       password = null;
183     }
184     if (userName == null) {
185       if (password == null) {
186         return 0;
187       } else {
188         return password.hashCode();
189       }
190     } else if (password == null) {
191       return userName.hashCode();
192     } else {
193       final StringBuffer buffer = new StringBuffer(userName);
194       buffer.append(password);
195       return buffer.hashCode();
196     }
197   }
198 
199   /***
200    * Tests the supplied {@link Object} to see if it is equal to this {@link
201    * Administrator}.  An {@link Object} is equal to this {@link Administrator}
202    * if it is an instance of the {@link Administrator} class, its {@linkplain
203    * #getName() username} is equal to this {@link Administrator}'s {@linkplain
204    * #getName() username} and its {@linkplain #getPassword() password} is equal
205    * to this {@link Administrator}'s {@linkplain #getPassword() password}.
206    * {@link Administrator}s are, in other words, value objects.
207    *
208    * @param      anObject
209    *               the {@link Object} to test; may be <code>null</code>
210    * @return     <code>true</code> if and only if the supplied {@link Object} is
211    *               equal to this {@link Administrator} 
212    */
213   public boolean equals(final Object anObject) {
214     if (anObject == this) {
215       return true;
216     } else if (anObject instanceof Administrator) {
217       final Administrator other = (Administrator)anObject;
218       
219       // Compare usernames.  We do not invoke NamedObject.equals() here because
220       // getName() can throw an IllegalStateException.
221       String userName = null;
222       try {
223         userName = this.getName();
224       } catch (final IllegalStateException ignore) {
225         userName = null;
226       }
227       String otherUserName = null;
228       try {
229         otherUserName = other.getName();
230       } catch (final IllegalStateException ignore) {
231         otherUserName = null;
232       }
233       if (userName == null) {
234         if (otherUserName != null) {
235           return false;
236         }
237       } else if (!userName.equals(otherUserName)) {
238         return false;
239       }
240 
241       // Compare passwords.
242       String password = null;
243       try {
244         password = this.getPassword();
245       } catch (final IllegalStateException oops) {
246         password = null;
247       }
248       String otherPassword = null;
249       try {
250         otherPassword = other.getPassword();
251       } catch (final IllegalStateException oosp) {
252         otherPassword = null;
253       }
254       if (password == null) {
255         return otherPassword == null;
256       } else {
257         return password.equals(otherPassword);
258       }
259     
260     } else {
261       return false;
262     }
263   }
264 
265   /***
266    * Returns a {@link String} representation of this {@link Administrator}.  The
267    * {@linkplain #getPassword() password} is not displayed.  This method never
268    * returns <code>null</code>
269    *
270    * @return     a {@link String} representation of this {@link Administrator};
271    *               never <code>null</code>
272    */
273   public String toString() {
274     String userName = null;
275     try {
276       userName = this.getName();
277     } catch (final IllegalStateException ignore) {
278       userName = null;
279     }
280     final StringBuffer returnMe = new StringBuffer();
281     if (userName == null) {
282       returnMe.append("Unspecified Administrator");
283     } else {
284       returnMe.append(userName);
285     }
286     returnMe.append(" (password ");
287     String password = null;
288     try {
289       password = this.getPassword();
290     } catch (final IllegalStateException ignore) {
291       password = null;
292     }
293     if (password == null) {
294       returnMe.append("not ");
295     }
296     returnMe.append("set)");
297     return returnMe.toString();
298   }
299 
300 }