a646e629fe639932ed613f03dfbded6882d46e0f
[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 {
18
19         /**
20          * Class properties
21          */
22         private String origInt;
23
24         /**
25          * A "interface" statement:
26          *              public interface Camera {
27      *                  public void MethodA(int A, int B);
28      *                  public int MethodB(int C, string D);
29      *                  public string MethodC(string E, int F);
30      *                  public float MethodD(int G, float H);
31      *                  public boolean MethodE(Camera I, boolean J);
32      *                  public void MethodF();
33          *              }
34          * In this data structure we will record its interface name, i.e. Camera
35          *              its method names and the parameters for each method.
36          */
37         private List<String> listMethods;                                       // Method signature (no spaces), e.g. MethodA(intA,SpeakerB)
38         private List<String> listMethodIds;                                     // Method identifiers, e.g. MethodA
39         private List<String> listMethodTypes;                           // Method types, e.g. void
40         private List<List<String>> listMethodParams;            // Method parameter names, e.g. A, B
41         private List<List<String>> listMethodParamTypes;        // Method parameter types, e.g. int, int
42
43         /**
44          * Class constructors
45          */
46         public InterfaceDecl() {
47
48                 origInt = null;
49                 listMethods = new ArrayList<String>();
50                 listMethodIds = new ArrayList<String>();
51                 listMethodTypes = new ArrayList<String>();
52                 listMethodParams = new ArrayList<List<String>>();
53                 listMethodParamTypes = new ArrayList<List<String>>();
54         }
55
56
57         public InterfaceDecl(String _origInt) {
58
59                 origInt = _origInt;
60                 listMethods = new ArrayList<String>();
61                 listMethodIds = new ArrayList<String>();
62                 listMethodTypes = new ArrayList<String>();
63                 listMethodParams = new ArrayList<List<String>>();
64                 listMethodParamTypes = new ArrayList<List<String>>();
65         }
66
67
68         /**
69          * addNewMethod() adds a new method name and type into the list
70          */
71         public void addNewMethod(String newMethod, String newMethodId, String newMethodType) {
72
73                 listMethods.add(newMethod);
74                 listMethodIds.add(newMethodId);
75                 listMethodTypes.add(newMethodType);
76                 listMethodParams.add(new ArrayList<String>());
77                 listMethodParamTypes.add(new ArrayList<String>());
78         }
79
80
81         /**
82          * addMethodParam() adds the name and type of a parameter
83          */
84         public void addMethodParam(String method, String paramName, String paramType) {
85
86                 int index = listMethods.indexOf(method);
87                 List<String> listMethodParam = listMethodParams.get(index);
88                 listMethodParam.add(paramName);
89                 List<String> listMethodParamType = listMethodParamTypes.get(index);
90                 listMethodParamType.add(paramType);
91         }
92
93
94         /**
95          * getMethods() gets list of methods
96          */
97         public List<String> getMethods() {
98
99                 return listMethods;
100         }
101
102
103         /**
104          * getMethodIds() gets method identifiers
105          */
106         public List<String> getMethodIds() {
107
108                 return listMethodIds;
109         }
110
111
112         /**
113          * getMethodTypes() gets method types
114          */
115         public List<String> getMethodTypes() {
116
117                 return listMethodTypes;
118         }
119
120
121         /**
122          * getMethodId() gets a method identifier
123          */
124         public String getMethodId(String method) {
125
126                 int index = listMethods.indexOf(method);
127                 return listMethodIds.get(index);
128         }
129
130
131         /**
132          * getMethodType() gets a method type
133          */
134         public String getMethodType(String method) {
135
136                 int index = listMethods.indexOf(method);
137                 return listMethodTypes.get(index);
138         }
139
140
141         /**
142          * getMethodParams() gets list of method parameters for a method
143          */
144         public List<String> getMethodParams(String method) {
145
146                 int index = listMethods.indexOf(method);
147                 return listMethodParams.get(index);
148         }
149         
150
151         /**
152          * getMethodParams() gets list of method parameter types for a method
153          */
154         public List<String> getMethodParamTypes(String method) {
155
156                 int index = listMethods.indexOf(method);
157                 return listMethodParamTypes.get(index);
158         }
159
160
161         public static void main(String[] args) {
162
163                 InterfaceDecl id = new InterfaceDecl("Camera");
164                 id.addNewMethod("MethodA(intA,SpeakerB)", "MethodA", "void");
165                 id.addMethodParam("MethodA", "A", "int");
166                 id.addMethodParam("MethodA", "B", "int");
167                 id.addMethodParam("MethodB", "C", "int");
168                 id.addMethodParam("MethodB", "D", "string");
169                 id.addMethodParam("MethodC", "E", "string");
170                 id.addMethodParam("MethodC", "F", "int");
171
172                 System.out.println("Set of methods: " + id.getMethods().toString());
173                 System.out.println("Set of params: " + id.getMethodParams("MethodA").toString());
174                 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodA").toString());
175                 System.out.println("Set of params: " + id.getMethodParams("MethodB").toString());
176                 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodB").toString());
177                 System.out.println("Set of params: " + id.getMethodParams("MethodC").toString());
178                 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodC").toString());
179         }
180 }