7
7
import lombok .RequiredArgsConstructor ;
8
8
9
9
@ RequiredArgsConstructor
10
- public class MqttSubscriptionManager <T extends MqttSubscription > {
10
+ public class MqttSubscriptionManager <U , T extends MqttSubscription > {
11
11
12
12
private final MqttClient client ;
13
13
private boolean takeSecondMap ;
14
- private Map <String , T > map1 = new HashMap <>();
15
- private Map <String , T > map2 = new HashMap <>();
14
+ private Map <U , T > map1 = new HashMap <>();
15
+ private Map <U , T > map2 = new HashMap <>();
16
16
17
17
public void updateDifferentialSubscriptionsOnClient () {
18
18
client .connect ();
19
- SetIntersection intersection = SetIntersection .of (getOldMap ().keySet (), getCurrentMap ().keySet ());
20
- for (String topic : intersection .getDelete ())
21
- client .unsubscribe (topic );
22
- Map <String , T > map = getCurrentMap ();
23
- for (String topic : intersection .getCreate ()) {
24
- T subscription = map .get (topic );
19
+ Map <U , T > oldMap = getOldMap ();
20
+ Map <U , T > newMap = getCurrentMap ();
21
+ SetIntersection <U > intersection = SetIntersection .of (oldMap .keySet (), newMap .keySet ());
22
+ for (U id : intersection .getDelete ())
23
+ client .unsubscribe (oldMap .get (id ).getTopic ());
24
+ for (U id : intersection .getCreate ()) {
25
+ T subscription = newMap .get (id );
25
26
client .subscribe (subscription .getTopic (), subscription .getType (), subscription .getSetter ());
26
27
}
27
28
}
28
29
29
30
public void unsubscribeAllSubscriptionsOnClient () {
30
31
client .connect ();
31
- Map <String , T > map = getCurrentMap ();
32
+ Map <U , T > map = getCurrentMap ();
32
33
for (T subscription : map .values ())
33
34
client .unsubscribe (subscription .getTopic ());
34
35
}
@@ -38,15 +39,15 @@ public void switchBuffer() {
38
39
getCurrentMap ().clear ();
39
40
}
40
41
41
- public Map <String , T > getCurrentMap () {
42
- Map <String , T > map = map1 ;
42
+ public Map <U , T > getCurrentMap () {
43
+ Map <U , T > map = map1 ;
43
44
if (takeSecondMap )
44
45
map = map2 ;
45
46
return map ;
46
47
}
47
48
48
- public Map <String , T > getOldMap () {
49
- Map <String , T > map = map1 ;
49
+ public Map <U , T > getOldMap () {
50
+ Map <U , T > map = map1 ;
50
51
if (!takeSecondMap )
51
52
map = map2 ;
52
53
return map ;
0 commit comments