Adding feature for enum and struct generations
[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                         listStructs.add(structType);
71                         List<String> newMemberTypeList = new ArrayList<String>();
72                         newMemberTypeList.add(newMemberType);
73                         listMemberTypes.add(newMemberTypeList);
74                         List<String> newMemberList = new ArrayList<String>();
75                         newMemberList.add(newMember);
76                         listMembers.add(newMemberList);
77                 }
78         }
79
80
81         /**
82          * getStructTypes() gets list of recorded list structs
83          */
84         public List<String> getStructTypes() {
85
86                 return listStructs;
87         }
88
89
90         /**
91          * getMemberTypes() gets list of member types
92          */
93         public List<String> getMemberTypes(String structType) {
94
95                 int index = listStructs.indexOf(structType);
96                 return listMemberTypes.get(index);
97         }
98
99
100         /**
101          * getMembers() gets list of members
102          */
103         public List<String> getMembers(String structType) {
104
105                 int index = listStructs.indexOf(structType);
106                 return listMembers.get(index);
107         }
108 }