77import lombok .RequiredArgsConstructor ;
88
99@ RequiredArgsConstructor
10- public class MqttSubscriptionManager <T extends MqttSubscription > {
10+ public class MqttSubscriptionManager <U , T extends MqttSubscription > {
1111
1212 private final MqttClient client ;
1313 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 <>();
1616
1717 public void updateDifferentialSubscriptionsOnClient () {
1818 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 );
2526 client .subscribe (subscription .getTopic (), subscription .getType (), subscription .getSetter ());
2627 }
2728 }
2829
2930 public void unsubscribeAllSubscriptionsOnClient () {
3031 client .connect ();
31- Map <String , T > map = getCurrentMap ();
32+ Map <U , T > map = getCurrentMap ();
3233 for (T subscription : map .values ())
3334 client .unsubscribe (subscription .getTopic ());
3435 }
@@ -38,15 +39,15 @@ public void switchBuffer() {
3839 getCurrentMap ().clear ();
3940 }
4041
41- public Map <String , T > getCurrentMap () {
42- Map <String , T > map = map1 ;
42+ public Map <U , T > getCurrentMap () {
43+ Map <U , T > map = map1 ;
4344 if (takeSecondMap )
4445 map = map2 ;
4546 return map ;
4647 }
4748
48- public Map <String , T > getOldMap () {
49- Map <String , T > map = map1 ;
49+ public Map <U , T > getOldMap () {
50+ Map <U , T > map = map1 ;
5051 if (!takeSecondMap )
5152 map = map2 ;
5253 return map ;
0 commit comments