Cleaning up; Adding new files
[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          * getMethodIds() gets method identifiers
104          */
105         public List<String> getMethodIds() {
106
107                 return listMethodIds;
108         }
109
110
111         /**
112          * getMethodTypes() gets method types
113          */
114         public List<String> getMethodTypes() {
115
116                 return listMethodTypes;
117         }
118
119
120         /**
121          * getMethodId() gets a method identifier
122          */
123         public String getMethodId(String method) {
124
125                 int index = listMethods.indexOf(method);
126                 // If index=-1, it means that it's not found.
127                 // There is perhaps a discrepancy in the policy file
128                 //              between the method signatures in the interface 
129                 //              and capability sections
130                 if (index == -1)
131                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
132                                 method + "! Please check your policy file...");
133                 return listMethodIds.get(index);
134         }
135
136
137         /**
138          * getMethodType() gets a method type
139          */
140         public String getMethodType(String method) {
141
142                 int index = listMethods.indexOf(method);
143                 // If index=-1, it means that it's not found.
144                 // There is perhaps a discrepancy in the policy file
145                 //              between the method signatures in the interface 
146                 //              and capability sections
147                 if (index == -1)
148                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
149                                 method + "! Please check your policy file...");
150                 return listMethodTypes.get(index);
151         }
152
153
154         /**
155          * getMethodParams() gets list of method parameters for a method
156          */
157         public List<String> getMethodParams(String method) {
158
159                 int index = listMethods.indexOf(method);
160                 // If index=-1, it means that it's not found.
161                 // There is perhaps a discrepancy in the policy file
162                 //              between the method signatures in the interface 
163                 //              and capability sections
164                 if (index == -1)
165                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
166                                 method + "! Please check your policy file...");
167                 return listMethodParams.get(index);
168         }
169         
170
171         /**
172          * getMethodParams() gets list of method parameter types for a method
173          */
174         public List<String> getMethodParamTypes(String method) {
175
176                 int index = listMethods.indexOf(method);
177                 // If index=-1, it means that it's not found.
178                 // There is perhaps a discrepancy in the policy file
179                 //              between the method signatures in the interface 
180                 //              and capability sections
181                 if (index == -1)
182                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
183                                 method + "! Please check your policy file...");
184                 return listMethodParamTypes.get(index);
185         }
186 }