27e932615d7c40776e560dd5167e675811b20902
[iot2.git] / iotjava / iotpolicy / tree / InterfaceDecl.java
1 package iotpolicy.tree;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 /** Class InterfaceDecl is a data structure for interface
7  *  declaration section in the policy file.
8  *
9  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
10  * @version     1.0
11  * @since       2016-09-20
12  */
13 public final class InterfaceDecl {
14
15         /**
16          * Class properties
17          */
18         private String origInt;
19
20         /**
21          * A "interface" statement:
22          *              public interface Camera {
23      *                  public void MethodA(int A, int B);
24      *                  public int MethodB(int C, string D);
25      *                  public string MethodC(string E, int F);
26      *                  public float MethodD(int G, float H);
27      *                  public boolean MethodE(Camera I, boolean J);
28      *                  public void MethodF();
29          *              }
30          * In this data structure we will record its interface name, i.e. Camera
31          *              its method names and the parameters for each method.
32          */
33         private List<String> listMethods;                                       // Method names, e.g. MethodA
34         private List<String> listMethodTypes;                           // Method types, e.g. void
35         private List<List<String>> listMethodParams;            // Method parameter names, e.g. A, B
36         private List<List<String>> listMethodParamTypes;        // Method parameter types, e.g. int, int
37
38         /**
39          * Class constructors
40          */
41         public InterfaceDecl() {
42
43                 origInt = null;
44                 listMethods = new ArrayList<String>();
45                 listMethodTypes = new ArrayList<String>();
46                 listMethodParams = new ArrayList<List<String>>();
47                 listMethodParamTypes = new ArrayList<List<String>>();
48         }
49
50
51         public InterfaceDecl(String _origInt) {
52
53                 origInt = _origInt;
54                 listMethods = new ArrayList<String>();
55                 listMethodTypes = new ArrayList<String>();
56                 listMethodParams = new ArrayList<List<String>>();
57                 listMethodParamTypes = new ArrayList<List<String>>();
58         }
59
60
61         /**
62          * addNewMethod() adds a new method name and type into the list
63          */
64         public void addNewMethod(String newMethod, String newMethodType) {
65
66                 listMethods.add(newMethod);
67                 listMethodTypes.add(newMethodType);
68                 listMethodParams.add(new ArrayList<String>());
69                 listMethodParamTypes.add(new ArrayList<String>());
70         }
71
72
73         /**
74          * addMethodParam() adds the name and type of a parameter
75          */
76         public void addMethodParam(String method, String paramName, String paramType) {
77
78                 int index = listMethods.indexOf(method);
79                 List<String> listMethodParam = listMethodParams.get(index);
80                 listMethodParam.add(paramName);
81                 List<String> listMethodParamType = listMethodParamTypes.get(index);
82                 listMethodParamType.add(paramType);
83         }
84
85
86         /**
87          * getMethods() gets list of methods
88          */
89         public List<String> getMethods() {
90
91                 return listMethods;
92         }
93         
94         
95         /**
96          * getMethodTypes() gets method types
97          */
98         public List<String> getMethodTypes() {
99
100                 return listMethodTypes;
101         }
102
103
104         /**
105          * getMethodType() gets a method type
106          */
107         public String getMethodType(String method) {
108
109                 int index = listMethods.indexOf(method);
110                 return listMethodTypes.get(index);
111         }
112
113
114         /**
115          * getMethodParams() gets list of method parameters for a method
116          */
117         public List<String> getMethodParams(String method) {
118
119                 int index = listMethods.indexOf(method);
120                 return listMethodParams.get(index);
121         }
122         
123
124         /**
125          * getMethodParams() gets list of method parameter types for a method
126          */
127         public List<String> getMethodParamTypes(String method) {
128
129                 int index = listMethods.indexOf(method);
130                 return listMethodParamTypes.get(index);
131         }
132
133
134         public static void main(String[] args) {
135
136                 InterfaceDecl id = new InterfaceDecl("Camera");
137                 id.addNewMethod("MethodA", "void");
138                 id.addNewMethod("MethodB", "int");
139                 id.addNewMethod("MethodC", "String");
140                 id.addMethodParam("MethodA", "A", "int");
141                 id.addMethodParam("MethodA", "B", "int");
142                 id.addMethodParam("MethodB", "C", "int");
143                 id.addMethodParam("MethodB", "D", "string");
144                 id.addMethodParam("MethodC", "E", "string");
145                 id.addMethodParam("MethodC", "F", "int");
146
147
148
149                 System.out.println("Set of methods: " + id.getMethods().toString());
150                 System.out.println("Set of params: " + id.getMethodParams("MethodA").toString());
151                 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodA").toString());
152                 System.out.println("Set of params: " + id.getMethodParams("MethodB").toString());
153                 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodB").toString());
154                 System.out.println("Set of params: " + id.getMethodParams("MethodC").toString());
155                 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodC").toString());
156         }
157 }