View Javadoc

1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "GeneratedMethod.java".  Description: 
10  "This class will allows the creation of a method.  All members are set, then the 
11   toString method is called to return the String representation of the created method." 
12  
13  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
14  2001.  All Rights Reserved. 
15  
16  Contributor(s): James Agnew
17                  Paul Brohman
18                  Mitch Delachevrotiere
19                  Shawn Dyck
20    				Cory Metcalf
21    				
22  Alternatively, the contents of this file may be used under the terms of the 
23  GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
24  applicable instead of those above.  If you wish to allow use of your version of this 
25  file only under the terms of the GPL and not to allow others to use your version 
26  of this file under the MPL, indicate your decision by deleting  the provisions above 
27  and replace  them with the notice and other provisions required by the GPL License.  
28  If you do not delete the provisions above, a recipient may use your version of 
29  this file under either the MPL or the GPL. 
30  
31  */
32  package ca.uhn.hl7v2.conf.classes.generator.genclasses;
33  
34  import java.util.Vector;
35  
36  /** This class will allows the creation of a method.  All members
37   * are set, then the toString method is called to return the String
38   * representation of the created method.
39   * @author <table><tr>James Agnew</tr>
40   *                <tr>Paul Brohman</tr>
41   *                <tr>Mitch Delachevrotiere</tr>
42   *                <tr>Shawn Dyck</tr>
43   * 				  <tr>Cory Metcalf</tr></table>
44   */
45  public class GeneratedMethod {
46     private Vector<String> body;
47     private Vector<String> comments;
48     private Vector<String> description;
49     protected final String INDENT = "   ";
50     private String name;
51     private Vector<String> params;
52     private String returnType;
53     private String throwing;
54     private String visibility;
55  
56     /** Creates a new instance of GeneratedMethod */
57     public GeneratedMethod() {
58        body = new Vector<String>();
59        comments = new Vector<String>();
60        params = new Vector<String>();
61        description = new Vector<String>();
62     }
63  
64     /** A method to set the parameters of the method
65      * @param param the parameters of the created method
66      */
67     public void addParam(String param) {
68        this.params.add(param);
69     }
70  
71     /** A method to set the parameters of the method
72      * @param param the parameters of the created method
73      * @param description the describing this method (for use in the JavaDoc comments)
74      */
75     public void addParam(String param, String description) {
76        this.params.add(param);
77        this.addToComments("@param " + param + " " + description);
78     }
79  
80     /** A method to set the body of the method
81      * @param line a line of code to be added to the body of the created method
82      * @return a reference to the GeneratedMethod object being modified
83      */
84     public GeneratedMethod addToBody(String line) {
85        this.body.add(line);
86        return this;
87     }
88  
89     /** A method to set the comments of the method
90      * @param comments a comment to be added the comments of the created method
91      */
92     public void addToComments(String comments) {
93        this.comments.add(comments);
94     }
95  
96     /** A method to set the body of the method
97      * @param line a description to be added to the created method
98      */
99     public void addToDescription(String line) {
100       this.description.add(line);
101    }
102 
103    /** A method to set the throws of the method
104     * @param throwing sets the throws of the created method
105     */
106    public void addToThrows(String throwing) {
107       this.throwing = throwing;
108    }
109 
110    /** A method to set the name of the method
111     * @param name the name of the created method
112     */
113    public void setName(String name) {
114       this.name = name;
115    }
116 
117    /** A method to set the return type of the method
118     * @param returnType the return type of the created method
119     */
120    public void setReturnType(String returnType) {
121       this.returnType = returnType + " ";
122    }
123 
124    /** A method to set the visibility of the method
125     * @param visibility the visibility of the created method
126     */
127    public void setVisibility(String visibility) {
128       this.visibility = visibility + " ";
129    }
130 
131    /** creates a method with the filled member variables
132     * @return the String representation of the created method
133     */
134    public String toString() {
135       String theMethod;
136 
137       // add the comments and visibility to the method 
138       theMethod = INDENT + "/**\n" + vectorToString(1, description, " * ") + vectorToString(1, comments, " * ") + "    */\n";
139       theMethod += INDENT + visibility;
140 
141       // check if its a constructor
142       if (returnType != null)
143          theMethod += returnType;
144 
145       // add the paramters to the method
146       theMethod += name;
147       if (params.size() > 0)
148          theMethod += "(" + params.toString().replace('[', ' ').replace(']', ' ') + ")";
149       else
150          theMethod += "()";
151 
152       // check if its throwing an exception
153       if (throwing != null)
154          theMethod += " throws " + throwing;
155 
156       // create the body of the method
157       theMethod += " {\n" + vectorToString(2, body, "") + INDENT + "}\n";
158       return theMethod;
159    }
160 
161    /** Creates a string based on a vector representing the individual lines of a string
162     * @param indentLevel the indentation level to use. For example, a level of 2 would use two "tabs" before each line.
163     * @param vec the vector to be converted
164     * @param prefix a String to prepend to each line 
165     *        example " * " for a comment)
166     * @return The generated string
167     */
168    private String vectorToString(int indentLevel, Vector<?> vec, String prefix) {
169       String pString = "";
170       String indent = "".concat(prefix);
171 
172       // Create indentation string
173       for (int i = 0; i < indentLevel; i++)
174          indent = INDENT + indent;
175 
176       // Convert the vector to a string
177       for (int i = 0; i < vec.size(); i++)
178          pString += indent + vec.get(i).toString() + "\n";
179       return pString;
180    }
181 }