1 package iotpolicy.tree;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
10 /** Class InterfaceDecl is a data structure for interface
11 * declaration section in the policy file.
13 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
17 public class InterfaceDecl {
22 private String origInt;
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();
34 * In this data structure we will record its interface name, i.e. Camera
35 * its method names and the parameters for each method.
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
46 public InterfaceDecl() {
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>>();
57 public InterfaceDecl(String _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>>();
69 * addNewMethod() adds a new method name and type into the list
71 public void addNewMethod(String newMethod, String newMethodId, String newMethodType) {
73 listMethods.add(newMethod);
74 listMethodIds.add(newMethodId);
75 listMethodTypes.add(newMethodType);
76 listMethodParams.add(new ArrayList<String>());
77 listMethodParamTypes.add(new ArrayList<String>());
82 * addMethodParam() adds the name and type of a parameter
84 public void addMethodParam(String method, String paramName, String paramType) {
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);
95 * getMethods() gets list of methods
97 public List<String> getMethods() {
104 * getMethodIds() gets method identifiers
106 public List<String> getMethodIds() {
108 return listMethodIds;
113 * getMethodTypes() gets method types
115 public List<String> getMethodTypes() {
117 return listMethodTypes;
122 * getMethodId() gets a method identifier
124 public String getMethodId(String method) {
126 int index = listMethods.indexOf(method);
127 return listMethodIds.get(index);
132 * getMethodType() gets a method type
134 public String getMethodType(String method) {
136 int index = listMethods.indexOf(method);
137 return listMethodTypes.get(index);
142 * getMethodParams() gets list of method parameters for a method
144 public List<String> getMethodParams(String method) {
146 int index = listMethods.indexOf(method);
147 return listMethodParams.get(index);
152 * getMethodParams() gets list of method parameter types for a method
154 public List<String> getMethodParamTypes(String method) {
156 int index = listMethods.indexOf(method);
157 return listMethodParamTypes.get(index);
161 public static void main(String[] args) {
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");
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());