KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > Alias


1 /*
2
3   Copyright 2004, Martian Software, Inc.
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16
17 */

18
19 package com.martiansoftware.nailgun;
20
21 /**
22  * Provides a means to map memorable, short names to classes in order
23  * to make the issuing of commands more convenient. For example, an
24  * Alias can map the "<code>mycommand</code>" command to the <code>com.yourdomain.yourpackage.YourClass</code>
25  * class. Obviously, it's a lot easier to type "<code>ng mycommand</code>" than the fully
26  * qualified class name.
27  *
28  * @author <a HREF="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
29  */

30 public class Alias implements Comparable JavaDoc {
31
32     /**
33      * The alias name
34      */

35     private String JavaDoc name;
36     
37     /**
38      * The alias description (may be used to provide help to users)
39      */

40     private String JavaDoc description;
41     
42     /**
43      * The class providing a <code>main()</code> or <code>nailMain()</code> method
44      */

45     private Class JavaDoc clazz;
46     
47     /**
48      * Creates a new Alias with the specified properties.
49      * @param name the alias name (short command)
50      * @param description a description of the command
51      * @param clazz the class implementing the command
52      */

53     public Alias(String JavaDoc name, String JavaDoc description, Class JavaDoc clazz) {
54         if (name == null) throw (new IllegalArgumentException JavaDoc("Alias must have a name."));
55         this.name = name.trim();
56         if (this.name.length() == 0) throw (new IllegalArgumentException JavaDoc("Alias must have a name."));
57         
58         if (clazz == null) throw (new IllegalArgumentException JavaDoc("Alias must have an associated class."));
59         this.description = description;
60         this.clazz = clazz;
61     }
62     
63     /**
64      * Returns the <code>Class</code> object providing a static <code>main()</code> or <code>nailMain()</code> method
65      * for this command.
66      * @return the <code>Class</code> object providing a static <code>main()</code> or <code>nailMain()</code> method
67      * for this command.
68      */

69     public Class JavaDoc getAliasedClass() {
70         return(clazz);
71     }
72     
73     /**
74      * Returns the name of the aliased command
75      * @return the name of the aliased command
76      */

77     public String JavaDoc getName() {
78         return (name);
79     }
80     
81     /**
82      * Returns a description for the aliased command
83      * @return a description for the aliased command
84      */

85     public String JavaDoc getDescription() {
86         return (description);
87     }
88     
89     /**
90      * @see Object#hashCode()
91      */

92     public int hashCode() {
93         return (name.hashCode());
94     }
95     
96     /**
97      * Checks whether two Aliases have the same name. Does <b>not</b>
98      * compare any other fields.
99      * @param o the other Alias to check
100      * @return true if the specified Alias has the same name as this Alias.
101      */

102     public boolean equals(Object JavaDoc o) {
103         return (compareTo(o) == 0);
104     }
105     
106     /**
107      * Compares Alias <b>names</b> - no other fields are compared.
108      * @see Comparable#compareTo(Object)
109      */

110     public int compareTo(Object JavaDoc o) {
111         return (name.compareTo(((Alias) o).getName()));
112     }
113 }
114
Popular Tags