KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > report > element > MethodElement


1 package csdl.jblanket.report.element;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 /**
9  * Provides a wrapper for the 'Method' element in an XML file. An instance of a MethodElement
10  * represents elements of a method's type signature.
11  * <p>
12  * An example of a method as a MethodElement with no parameters:
13  * <pre>
14  * &lt;Method name="length" package="java.lang.String"/&gt;
15  * </pre>
16  * <p>
17  * An example of a method as a MethodElement with parameters:
18  * <pre>
19  * &lt;Method name="indexOf" package="java.lang.String"&gt;
20  * &lt;Parameter type="java.lang.String"/&gt;
21  * &lt;Parameter type="int"/&gt;
22  * &lt;/Method&gt;
23  * </pre>
24  *
25  * @author Joy M. Agustin
26  * @version $Id: MethodElement.java,v 1.1 2004/11/07 00:32:39 timshadel Exp $
27  */

28 public class MethodElement implements Comparable JavaDoc {
29   
30   /** Name of the class method belongs to */
31   private String JavaDoc className;
32   /** Name of the method represented */
33   private String JavaDoc methodName;
34   /** Parameters of the method represented */
35   private List JavaDoc parameters;
36
37   /**
38    * Constructs a new MethodElement object.
39    *
40    * @param className the name of the class <code>methodName</code> belongs to.
41    * @param methodName the name of the method represented by this MethodElement.
42    */

43   public MethodElement(String JavaDoc className, String JavaDoc methodName) {
44
45     this.className = className;
46     this.methodName = methodName;
47     this.parameters = new ArrayList JavaDoc();
48   }
49   
50   /**
51    * Returns the name of the class of this MethodElement object.
52    *
53    * @return the name of the class of this MethodElement object.
54    */

55   public String JavaDoc getClassName() {
56     return this.className;
57   }
58
59   /**
60    * Returns the name of the method of this MethodElement object.
61    *
62    * @return the name of the method of this MethodElement object.
63    */

64   public String JavaDoc getMethodName() {
65     return this.methodName;
66   }
67
68   /**
69    * Adds a parameter to this MethodElement object.
70    *
71    * @param parameter the parameter to add.
72    */

73   public void addParameter(ParameterElement parameter) {
74     this.parameters.add(parameter);
75   }
76   
77   /**
78    * Returns a String representation of this MethodElement as found in an aggregate XML file.
79    *
80    * @return a String representation of this MethodElement.
81    */

82   public String JavaDoc toString() {
83
84     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
85     buffer.append(" <Method class=\"" + this.className + "\"");
86     buffer.append(" method=\"" + this.methodName + "\"");
87
88     if (this.parameters.isEmpty()) {
89       buffer.append("/>");
90     }
91     else {
92       buffer.append(">\n");
93       for (Iterator JavaDoc i = this.parameters.iterator(); i.hasNext(); ) {
94          buffer.append("" + i.next() + "\n");
95       }
96       buffer.append(" </Method>");
97     }
98
99     return buffer.toString();
100   }
101
102   /**
103    * Writes this MethodElement to writer.
104    *
105    * @param writer points to an output file.
106    */

107   public void write(PrintWriter JavaDoc writer) {
108
109     writer.write(" <Method class=\"" + this.className + "\"");
110     writer.write(" method=\"" + this.methodName + "\"");
111
112     if (this.parameters.isEmpty()) {
113       writer.write("/>");
114     }
115     else {
116       writer.write(">\n");
117       for (Iterator JavaDoc i = this.parameters.iterator(); i.hasNext(); ) {
118         writer.write("" + i.next() + "\n");
119       }
120       writer.write(" </Method>");
121     }
122   }
123
124   /**
125    * Compares this MethodElement object to <code>obj</code>.
126    *
127    * @param obj the MethodElement object to compare to.
128    * @return negative integer if this MethodElement object comes before <code>obj</code>, zero
129    * if this MethodElement object is equal to <code>obj</code>, or positive integer if
130    * <code>obj</code> comes before this MethodElement object.
131    */

132   public int compareTo(Object JavaDoc obj) {
133
134     if (this.className.compareTo(((MethodElement) obj).getClassName()) != 0) {
135       return this.className.compareTo(((MethodElement) obj).getClassName());
136     }
137
138     if (this.methodName.compareTo(((MethodElement) obj).getMethodName()) != 0) {
139       return this.methodName.compareTo(((MethodElement) obj).getMethodName());
140     }
141     else {
142       StringBuffer JavaDoc thisString = new StringBuffer JavaDoc();
143       StringBuffer JavaDoc objString = new StringBuffer JavaDoc();
144
145       for (Iterator JavaDoc i = this.parameters.iterator(); i.hasNext(); ) {
146         thisString.append(((ParameterElement) i.next()).getType());
147       }
148
149       for (Iterator JavaDoc i = ((MethodElement) obj).parameters.iterator(); i.hasNext(); ) {
150         objString.append(((ParameterElement) i.next()).getType());
151       }
152
153       return thisString.toString().compareTo(objString.toString());
154     }
155   }
156 }
157
Popular Tags