View Javadoc

1   /* -*- mode: JDE; c-basic-offset: 2; indent-tabs-mode: nil -*-
2    *
3    * $Id: Project.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   * A {@link NamedObject} that represents a <a
32   * href="http://sourceforge.net/">SourceForge</a> project for the purposes of
33   * interacting with the <a
34   * href="http://sourceforge.net/docman/display_doc.php%3Fdocid=6445&group_id=1">SourceForge
35   * File Release System</a>.
36   *
37   * @author     <a href="mailto:ljnelson94@alumni.amherst.edu">Laird Nelson</a>
38   * @version    $Revision: 1.1 $ $Date: 2003/07/12 16:13:24 $
39   * @since      June 19, 2003
40   */
41  public class Project extends NamedObject {
42  
43    /***
44     * The {@link Administrator} that owns this {@link Project}.  This field may
45     * be <code>null</code>.
46     *
47     * @see        #getAdministrator()
48     * @see        #setAdministrator(Administrator)
49     */
50    private Administrator administrator;
51  
52    /***
53     * The "short name" of this {@link Project}.  The short name of a <a
54     * href="http://sourceforge.net/">SourceForge</a> project ends up being the
55     * directory name its contents are listed under.  This field may be
56     * <code>null</code>.
57     *
58     * @see        #getShortName()
59     * @see        #setShortName(String)
60     */
61    private String shortName;
62  
63    /***
64     * The identifier for this {@link Project}.  {@link Project} identifiers are
65     * normally assigned by <a href="http://sourceforge.net/">SourceForge</a>.
66     * This field may be <code>null</code>.
67     *
68     * @see        #getID()
69     * @see        #setID(String)
70     */
71    private String id;
72  
73    /***
74     * Creates a new {@link Project}.  This constructor calls the {@link
75     * #Project(String, String, Administrator)} constructor with <code>null</code>
76     * arguments.
77     */
78    public Project() {
79      this(null, null, null);
80    }
81  
82    /***
83     * Creates a new {@link Project} with the supplied long and short names and
84     * the supplied {@link Administrator} to oversee it.  This constructor calls
85     * its {@linkplain NamedObject#NamedObject() superclass' implementation} and
86     * then calls the {@link #setName(String)}, {@link #setShortName(String)} and
87     * {@link #setAdministrator(Administrator)} methods with the supplied,
88     * relevant arguments.
89     *
90     * @param      longName
91     *               the long, human-readable name for the {@link Project}; may be
92     *               <code>null</code>
93     * @param      shortName
94     *               the short, Unix-friendly name for the {@link Project}; may be
95     *               <code>null</code>
96     * @param      admin
97     *               the {@link Administrator} for this {@link Project}; may be
98     *               <code>null</code>
99     */
100   public Project(final String longName,
101                  final String shortName,
102                  final Administrator admin) {
103     super();
104     this.setName(longName);
105     this.setShortName(shortName);
106     this.setAdministrator(admin);
107   }
108 
109   /***
110    * Creates a new {@link Project} with the supplied long and short names and
111    * the supplied {@link Administrator} username and password.  This constructor
112    * calls the {@link #Project(String, String, Administrator)} constructor with
113    * the supplied long and short names, and a new {@link Administrator} it
114    * attempts to {@linkplain Administrator#Administrator(String, String) build
115    * with the supplied username and password}.
116    *
117    * @param      longName
118    *               the long, human-readable name for the {@link Project}; may be
119    *               <code>null</code>
120    * @param      shortName
121    *               the short, Unix-friendly name for the {@link Project}; may be
122    *               <code>null</code>
123    * @param      userName
124    *               the username; must not be <code>null</code> or equal to the
125    *               empty {@link String}
126    * @param      password
127    *               the password; must not be <code>null</code> or equal to the
128    *               empty {@link String}
129    * @exception  IllegalArgumentException
130    *               if either <code>userName</code> or <code>password</code> is
131    *               <code>null</code> or equal to the empty {@link String}
132    */
133   public Project(final String longName, 
134                  final String shortName, 
135                  final String userName,
136                  final String password)
137     throws IllegalArgumentException { 
138     this(longName, shortName, new Administrator(userName, password));
139   }
140 
141   /***
142    * Sets the identifier for this {@link Project}.  {@link Project} identifiers
143    * are normally assigned by <a href="http://sourceforge.net/">SourceForge</a>.
144    *
145    * @param      id
146    *               the identifier for this {@link Project}; may be
147    *               <code>null</code>
148    * @see        #getID()
149    */
150   public void setID(final String id) {
151     this.id = id;
152   }
153 
154   /***
155    * Returns the identifier for this {@link Project}.  {@link Project}
156    * identifiers are normally assigned by <a
157    * href="http://sourceforge.net/">SourceForge</a>.  This method may return
158    * <code>null</code>.
159    *
160    * @return     the identifier for this {@link Project}, or <code>null</code>
161    * @see        #setID(String)
162    */
163   public String getID() {
164     return this.id;
165   }
166 
167   /***
168    * Sets the Unix-friendly "short name" for this {@link Project}.
169    * Pragmatically speaking, arguments supplied to this method should be
170    * non-<code>null</code>, all-lowercase {@link String}s containing no spaces.
171    * Technically speaking, they may be anything.
172    *
173    * @param      shortName
174    *               the Unix-friendly "short name" for this {@link Project}; may
175    *               be <code>null</code>
176    * @see        #getShortName()
177    */
178   public void setShortName(final String shortName) {
179     this.shortName = shortName;
180   }
181 
182   /***
183    * Returns the Unix-friendly "short name" for this {@link Project}.  This
184    * method may return <code>null</code>.
185    *
186    * @return     the Unix-friendly "short name" for this {@link Project}, or
187    *               <code>null</code>
188    * @see        #setShortName(String)
189    */
190   public String getShortName() {
191     return this.shortName;
192   }
193 
194   /***
195    * Returns the {@link Administrator} that oversees this {@link Project}.  This
196    * method may return <code>null</code>.
197    *
198    * @return     the {@link Administrator} that oversees this {@link Project},
199    *               or <code>null</code>
200    * @see        #setAdministrator(Administrator)
201    */
202   public Administrator getAdministrator() {
203     return this.administrator;
204   }
205 
206   /***
207    * Sets the {@link Administrator} that oversees this {@link Project}.
208    *
209    * @param      administrator
210    *               the new {@link Administrator}; may be <code>null</code>
211    * @see        #getAdministrator()
212    */
213   public void setAdministrator(final Administrator administrator) {
214     this.administrator = administrator;
215   }
216 
217   /***
218    * Returns a hashcode for this {@link Project} based on all of its attributes.
219    *
220    * @return     a hashcode for this {@link Project}
221    */
222   public int hashCode() {
223     final String id = this.getID();
224     final String shortName = this.getShortName();
225     final String longName = this.getName();
226     final Administrator administrator = this.getAdministrator();
227     int hashCode = 0;
228     if (id != null) {
229       hashCode += id.hashCode();
230     }
231     if (shortName != null) {
232       hashCode += shortName.hashCode();
233     }
234     if (longName != null) {
235       hashCode += longName.hashCode();
236     }
237     if (administrator != null) {
238       hashCode += administrator.hashCode();
239     }
240     return hashCode;
241   }
242 
243   /***
244    * Tests the supplied {@link Object} to see if it is equal to this {@link
245    * Project}.  An {@link Object} is equal to this {@link Project} if it is an
246    * instance of the {@link Project} class and all of its {@link Project}
247    * attributes are equal to this {@link Project}'s attributes.  {@link
248    * Project}s are, in other words, value objects.
249    *
250    * @param      anObject
251    *               the {@link Object} to test; may be <code>null</code>
252    * @return     <code>true</code> if and only if the supplied {@link Object} is
253    *               equal to this {@link Project}
254    */
255   public boolean equals(final Object anObject) {
256     if (anObject == this) {
257       return true;
258     } else if (anObject instanceof Project) {
259       final Project other = (Project)anObject;
260 
261       // Compare IDs.
262       final String id = this.getID();
263       final String otherID = other.getID();
264       if (id == null) {
265         if (otherID != null) {
266           return false;
267         }
268       } else if (!id.equals(otherID)) {
269         return false;
270       }
271 
272       // Compare short names.
273       final String shortName = this.getShortName();
274       final String otherShortName = other.getShortName();
275       if (shortName == null) {
276         if (otherShortName != null) {
277           return false;
278         }
279       } else if (!shortName.equals(otherShortName)) {
280         return false;
281       }
282 
283       // Compare long names.
284       final String longName = this.getName();
285       final String otherLongName = other.getName();
286       if (longName == null) {
287         if (otherLongName != null) {
288           return false;
289         }
290       } else if (!longName.equals(otherLongName)) {
291         return false;
292       }
293 
294       // Compare administrators.
295       final Administrator administrator = this.getAdministrator();
296       final Administrator otherAdministrator = other.getAdministrator();
297       if (administrator == null) {
298         return otherAdministrator == null;
299       }
300       return administrator.equals(otherAdministrator);
301 
302     } else {
303       return false;
304     }
305   }
306 
307   /***
308    * Returns a {@link String} representation for this {@link Project}.  This
309    * method never returns <code>null</code>.
310    *
311    * @return     a {@link String} representation for this {@link Project}; never
312    *               <code>null</code>
313    */
314   public String toString() {
315     final String id = this.getID();
316     final String shortName = this.getShortName();
317     final String longName = this.getName();
318     final Administrator administrator = this.getAdministrator();
319     final StringBuffer returnMe = new StringBuffer();
320     if (longName == null) {
321       returnMe.append("Unnamed project");
322     } else {
323       returnMe.append(longName);
324     }
325     if (shortName != null) {
326       returnMe.append(" (");
327       returnMe.append(shortName);
328       returnMe.append(")");
329     }
330     if (id != null) {
331       returnMe.append(" ID=");
332       returnMe.append(id);
333     }
334     if (administrator != null) {
335       returnMe.append(" administered by ");
336       returnMe.append(administrator.toString());
337     }
338     return returnMe.toString();
339   }
340 
341 }