Adding last version of iotruntime and iotinstaller; preparing to extend IoTMaster...
[iot2.git] / iotjava / iotruntime / slave / IRelation.java
1 package iotruntime.slave;
2
3 import java.io.Serializable;
4 import java.lang.UnsupportedOperationException;
5
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Map;
9 import java.util.Set;
10
11 /** Class IRelation is another wrapper class of IoTRelation that is the
12  *  actual implementation of @config IoTRelation<...>.
13  *  The function is the same as the IoTRelation class, but this class
14  *  is meant for the class instrumenter to have full access to
15  *  our class object. The IoTRelation class functions as an immutable
16  *  interface to clients/users.
17  *
18  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
19  * @version     1.0
20  * @since       2016-01-05
21  */
22 public final class IRelation<K,V> implements Serializable {
23
24         /**
25          * Reference to an object Map<T>
26          */
27         private Map<K,HashSet<V> > mapRelation;
28
29         /**
30          * Size counter
31          */
32         private int iSize;
33
34         /**
35          * Class constructor (create object in this mutable wrapper)
36          */
37         protected IRelation() {
38
39                 mapRelation = new HashMap<K,HashSet<V> >();
40         }
41
42         /**
43          * Method containsKey() inherited from Map interface
44          *
45          * @param  key  The first Object that is usually a key in a Map
46          * @return      boolean
47          */
48         public boolean containsKey(Object key) {
49
50                 return mapRelation.containsKey(key);
51
52         }
53
54         /**
55          * Method relationMap() returns this mapRelation object
56          *
57          * @return      Map<K,HashSet<V>>>
58          */
59         public Map<K,HashSet<V> > relationMap() {
60
61                 return mapRelation;
62
63         }
64
65         /**
66          * Method entrySet() inherited from Map interface
67          *
68          * @return      Set<Map.Entry<K,HashSet<V>>>
69          */
70         public Set<Map.Entry<K,HashSet<V>>> entrySet() {
71
72                 return mapRelation.entrySet();
73
74         }
75
76         /**
77          * Method keySet() inherited from Map interface
78          *
79          * @return      Set<K>
80          */
81         public Set<K> keySet() {
82
83                 return mapRelation.keySet();
84
85         }
86
87         /**
88          * Method get() inherited from Map interface
89          *
90          * @param  key  The first Object that is usually a key in a Map
91          * @return      HashSet<V>
92          */
93         public HashSet<V> get(Object key) {
94
95                 return mapRelation.get(key);
96
97         }
98
99         /**
100          * Method isEmpty() inherited from Map interface
101          *
102          * @return      boolean
103          */
104         public boolean isEmpty() {
105
106                 return mapRelation.isEmpty();
107
108         }
109
110         /**
111          * put() method
112          * <p>
113          * We check whether the same object has existed or not
114          * If it has, then we don't insert it again if it is a key
115          * If it is a value and it maps to the same value, we don't
116          * try to create a new key; we just map it to the same key
117          * This method is just used the first time it is needed to
118          * add new objects, then it is going to be made immutable
119          * <p>
120          * Mental picture:
121          * -------------------------------
122          * |   Obj 1   |   Set Obj 1.1   |
123          * |           |       Obj 1.2   |
124          * |           |       Obj 1.2   |
125          * |           |       ...       |
126          * -------------------------------
127          * |   Obj 2   |   Set Obj 2.1   |
128          * -------------------------------
129          * |   Obj 3   |   Set Obj 3.1   |
130          * |           |       ...       |
131          * -------------------------------
132          *
133          * @param  key    The first Object that is usually a key in a Map
134          * @param  value  The second Object that is usually a value in a Map
135          * @return        boolean
136          */
137         public boolean put(K key, V value) {
138
139                 HashSet<V> hsSecond;
140
141                 // Go to our map first
142                 if (mapRelation.containsKey(key)) {
143                         // Such a key (first element) exists already
144                         hsSecond = mapRelation.get(key);
145                 } else {
146                         // It is a new key ...
147                         hsSecond = new HashSet<V>();
148                         mapRelation.put(key, hsSecond);
149                 }
150
151                 // Go to our Set of objects
152                 if (hsSecond.contains(value)) {
153                         // This object exists
154                         return false;
155                 } else {
156                         // This is a new object
157                         iSize++;
158                         hsSecond.add(value);
159                         return true;
160                 }
161         }
162
163         /**
164          * size() method
165          *
166          * @return      int
167          */
168         public int size() {
169
170                 return this.iSize;
171
172         }
173 }