Completing parser to parse generic/template return types; adding standard method...
[iot2.git] / iotjava / iotpolicy / tree / InterfaceDecl.java
1 package iotpolicy.tree;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9
10 /** Class InterfaceDecl is a data structure for interface
11  *  declaration section in the policy file.
12  *
13  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
14  * @version     1.0
15  * @since       2016-09-20
16  */
17 public class InterfaceDecl extends Declaration {
18
19         /**
20          * A "interface" statement:
21          *              public interface Camera {
22      *                  public void MethodA(int A, int B);
23      *                  public int MethodB(int C, string D);
24      *                  public string MethodC(string E, int F);
25      *                  public float MethodD(int G, float H);
26      *                  public boolean MethodE(Camera I, boolean J);
27      *                  public void MethodF();
28          *              }
29          * In this data structure we will record its interface name, i.e. Camera
30          *              its method names and the parameters for each method.
31          */
32
33         /**
34          * Class properties
35          */
36         private List<String> listMethods;                                       // Method signature (no spaces), e.g. MethodA(intA,SpeakerB)
37         private List<String> listMethodIds;                                     // Method identifiers, e.g. MethodA
38         private List<String> listMethodTypes;                           // Method types, e.g. void
39         private List<List<String>> listMethodParams;            // Method parameter names, e.g. A, B
40         private List<List<String>> listMethodParamTypes;        // Method parameter types, e.g. int, int
41
42         /**
43          * Class constructors
44          */
45         public InterfaceDecl() {
46
47                 super();
48                 listMethods = new ArrayList<String>();
49                 listMethodIds = new ArrayList<String>();
50                 listMethodTypes = new ArrayList<String>();
51                 listMethodParams = new ArrayList<List<String>>();
52                 listMethodParamTypes = new ArrayList<List<String>>();
53         }
54
55
56         public InterfaceDecl(String _origInt) {
57
58                 super(_origInt);
59                 listMethods = new ArrayList<String>();
60                 listMethodIds = new ArrayList<String>();
61                 listMethodTypes = new ArrayList<String>();
62                 listMethodParams = new ArrayList<List<String>>();
63                 listMethodParamTypes = new ArrayList<List<String>>();
64         }
65
66
67         /**
68          * addNewMethod() adds a new method name and type into the list
69          */
70         public void addNewMethod(String newMethod, String newMethodId, String newMethodType) {
71
72                 listMethods.add(newMethod);
73                 listMethodIds.add(newMethodId);
74                 listMethodTypes.add(newMethodType);
75                 listMethodParams.add(new ArrayList<String>());
76                 listMethodParamTypes.add(new ArrayList<String>());
77         }
78
79
80         /**
81          * addMethodParam() adds the name and type of a parameter
82          */
83         public void addMethodParam(String method, String paramName, String paramType) {
84
85                 int index = listMethods.indexOf(method);
86                 List<String> listMethodParam = listMethodParams.get(index);
87                 listMethodParam.add(paramName);
88                 List<String> listMethodParamType = listMethodParamTypes.get(index);
89                 listMethodParamType.add(paramType);
90         }
91
92
93         /**
94          * getMethods() gets list of methods
95          */
96         public List<String> getMethods() {
97
98                 return listMethods;
99         }
100
101
102         /**
103          * getMethodNumId() gets Id number for a method
104          */
105         public int getMethodNumId(String method) {
106
107                 return listMethods.indexOf(method);
108         }
109
110
111         /**
112          * getMethodIds() gets method identifiers
113          */
114         public List<String> getMethodIds() {
115
116                 return listMethodIds;
117         }
118
119
120         /**
121          * getMethodTypes() gets method types
122          */
123         public List<String> getMethodTypes() {
124
125                 return listMethodTypes;
126         }
127
128
129         /**
130          * getMethodId() gets a method identifier
131          */
132         public String getMethodId(String method) {
133
134                 int index = listMethods.indexOf(method);
135                 // If index=-1, it means that it's not found.
136                 // There is perhaps a discrepancy in the policy file
137                 //              between the method signatures in the interface 
138                 //              and capability sections
139                 if (index == -1)
140                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
141                                 method + "! Please check your policy file...");
142                 return listMethodIds.get(index);
143         }
144
145
146         /**
147          * getMethodType() gets a method type
148          */
149         public String getMethodType(String method) {
150
151                 int index = listMethods.indexOf(method);
152                 // If index=-1, it means that it's not found.
153                 // There is perhaps a discrepancy in the policy file
154                 //              between the method signatures in the interface 
155                 //              and capability sections
156                 if (index == -1)
157                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
158                                 method + "! Please check your policy file...");
159                 return listMethodTypes.get(index);
160         }
161
162
163         /**
164          * getMethodParams() gets list of method parameters for a method
165          */
166         public List<String> getMethodParams(String method) {
167
168                 int index = listMethods.indexOf(method);
169                 // If index=-1, it means that it's not found.
170                 // There is perhaps a discrepancy in the policy file
171                 //              between the method signatures in the interface 
172                 //              and capability sections
173                 if (index == -1)
174                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
175                                 method + "! Please check your policy file...");
176                 return listMethodParams.get(index);
177         }
178         
179
180         /**
181          * getMethodParams() gets list of method parameter types for a method
182          */
183         public List<String> getMethodParamTypes(String method) {
184
185                 int index = listMethods.indexOf(method);
186                 // If index=-1, it means that it's not found.
187                 // There is perhaps a discrepancy in the policy file
188                 //              between the method signatures in the interface 
189                 //              and capability sections
190                 if (index == -1)
191                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
192                                 method + "! Please check your policy file...");
193                 return listMethodParamTypes.get(index);
194         }
195 }