Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / parser / PHPFunctionDeclaration.java
1 package net.sourceforge.phpdt.internal.compiler.parser;
2
3 import java.util.Enumeration;
4 import java.util.Hashtable;
5
6 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
7
8 import org.eclipse.jface.resource.ImageDescriptor;
9
10 /**
11  * A function declaration.
12  * @author khartlage
13  */
14 public class PHPFunctionDeclaration extends PHPSegmentWithChildren {
15
16   /** The parameters of the function. */
17   private final Hashtable parameters;
18
19   /** The parameters of the function. */
20   private final Hashtable declaredVariables;
21
22   /**
23    * A String representation of the function (it's generated once because it needs to iterate
24    * a hashtable == long.
25    */
26   private String stringRepresentation;
27
28   /**
29    * Create a function declaration.
30    * @param parent the parent object (it should be a php class)
31    * @param name the name of the function
32    * @param index where the function is in the file
33    */
34   public PHPFunctionDeclaration(Object parent, String name, int index) {
35     super(parent, name, index);
36     parameters = null;
37     declaredVariables = null;
38   }
39
40   /**
41    * Create a function declaration.
42    * @param parent the parent object (it should be a php class)
43    * @param name the name of the function
44    * @param index where the function is in the file
45    * @param parameters the list of parameters (it should contains only PHPVar)
46    */
47   public PHPFunctionDeclaration(Object parent, String name, int index, Hashtable parameters) {
48     super(parent, name, index);
49     this.parameters = parameters;
50     declaredVariables = (Hashtable) parameters.clone();
51     createStringView();
52   }
53
54   /**
55    * Get the image of a class.
56    * @return the image that represents a php class
57    */
58   public ImageDescriptor getImage() {
59     return PHPUiImages.DESC_FUN;
60   }
61
62   /**
63    * Return the string representation of the function.
64    * @return the string representation of the function
65    */
66   public String toString() {
67     if (parameters == null) {
68       return super.toString();
69     }
70     return stringRepresentation;
71   }
72
73   /**
74    * Create the String representation of the function.
75    */
76   private void createStringView() {
77     StringBuffer buff = new StringBuffer(name).append("(");
78     Enumeration vars = parameters.elements();
79     boolean first = false;
80     while (vars.hasMoreElements()) {
81       PHPVarDeclaration o = (PHPVarDeclaration) vars.nextElement();
82       if (first) {
83         buff.append(",");
84       } else {
85         first = true;
86       }
87       buff.append(o.toString());
88     }
89     buff.append(")");
90     stringRepresentation = buff.toString();
91   }
92
93   /**
94    * Return a parameter of the function
95    * @param parameterName the name of the parameter
96    * @return it will return the PHPVarDeclaration of the parameter asked, or null
97    */
98   public PHPVarDeclaration getParameter(String parameterName) {
99     return (PHPVarDeclaration) parameters.get(parameterName);
100   }
101
102   /**
103    * Return all parameters of the function.
104    * @return a hashtable containing all PHPVarDeclaration
105    */
106   public Hashtable getParameters() {
107     return parameters;
108   }
109
110   /**
111    * Return a variable of the function
112    * @param variableName the name of the parameter
113    * @return it will return the PHPVarDeclaration of the parameter asked, or null
114    */
115   public PHPVarDeclaration getVariable(String variableName) {
116     return (PHPVarDeclaration) declaredVariables.get(variableName);
117   }
118
119   /**
120    * Return all declared variables of the function.
121    * @return a hashtable containing all PHPVarDeclaration
122    */
123   public Hashtable getVariables() {
124     return declaredVariables;
125   }
126
127   public Object addVariable(PHPVarDeclaration var) {
128     return declaredVariables.put(var.getVariable().getName(),var);
129   }
130 }