Skip to content

Commit e7f49fc

Browse files
committed
make SubscriptionManager key-agnostic
1 parent 759f87c commit e7f49fc

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515

1616
<modelVersion>4.0.0</modelVersion>
1717
<artifactId>mqtt-client</artifactId>
18-
<version>0.1.8</version>
18+
<version>0.1.9</version>
1919
<name>MqttClient</name>
2020
<packaging>jar</packaging>
2121

2222
<dependencies>
2323
<dependency>
2424
<groupId>info.unterrainer.commons</groupId>
2525
<artifactId>jre-utils</artifactId>
26-
<version>0.3.1</version>
26+
<version>0.3.10</version>
2727
</dependency>
2828
<dependency>
2929
<groupId>info.unterrainer.commons</groupId>

src/main/java/info/unterrainer/commons/mqttclient/MqttSubscriptionManager.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,29 @@
77
import 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

Comments
 (0)