56a6fe47542617595d0cd7cb1a176178ee503951
[iot2.git] / iotjava / iotpolicy / tree / StructDecl.java
1 package iotpolicy.tree;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6
7 /** Class StructDecl is a data structure for struct
8  *  declaration section in the policy file.
9  *
10  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
11  * @version     1.0
12  * @since       2016-11-11
13  */
14 public class StructDecl extends Declaration {
15
16         /**
17          * A "struct" declaration:
18          *      struct Struct {
19          *
20          *              string  name;
21          *              float   value;
22          *              int             year;
23          *}
24          * In this data structure we will record its struct name, i.e. Struct,
25          *              and its member types and members.
26          */
27
28         /**
29          * Class properties
30          */
31         private List<String> listStructs;                       // Struct types/names (more than one struct)
32         private List<List<String>> listMemberTypes;     // Member types, e.g. string, float, int, etc.
33         private List<List<String>> listMembers;         // Member names, e.g. name, value, year, etc.
34
35         /**
36          * Class constructors
37          */
38         public StructDecl() {
39
40                 super();
41                 listStructs = new ArrayList<String>();
42                 listMemberTypes = new ArrayList<List<String>>();
43                 listMembers = new ArrayList<List<String>>();
44         }
45
46
47         public StructDecl(String _origInt) {
48
49                 super(_origInt);
50                 listStructs = new ArrayList<String>();
51                 listMemberTypes = new ArrayList<List<String>>();
52                 listMembers = new ArrayList<List<String>>();
53         }
54
55
56         /**
57          * addNewMember() adds a new member type and value into the list
58          */
59         public void addNewMember(String structType, String newMemberType, String newMember) {
60
61                 if (listStructs.contains(structType)) {
62                 // Existing enum declaration
63                         int index = listStructs.indexOf(structType);
64                         List<String> memberTypeList = listMemberTypes.get(index);
65                         memberTypeList.add(newMemberType);
66                         List<String> memberList = listMembers.get(index);
67                         memberList.add(newMember);
68                 } else {
69                 // New declaration
70                         List<String> newMemberTypeList = new ArrayList<String>();
71                         newMemberTypeList.add(newMemberType);
72                         listMemberTypes.add(newMemberTypeList);
73                         List<String> newMemberList = new ArrayList<String>();
74                         newMemberList.add(newMember);
75                         listMembers.add(newMemberList);
76                 }
77         }
78
79
80         /**
81          * getStructTypes() gets list of recorded list structs
82          */
83         public List<String> getStructTypes() {
84
85                 return listStructs;
86         }
87
88
89         /**
90          * getMemberTypes() gets list of member types
91          */
92         public List<String> getMemberTypes(String structType) {
93
94                 int index = listStructs.indexOf(structType);
95                 return listMemberTypes.get(index);
96         }
97
98
99         /**
100          * getMembers() gets list of members
101          */
102         public List<String> getMembers(String structType) {
103
104                 int index = listStructs.indexOf(structType);
105                 return listMembers.get(index);
106         }
107 }