diff --git a/broker/src/main/java/io/moquette/broker/subscriptions/CTrieSubscriptionDirectory.java b/broker/src/main/java/io/moquette/broker/subscriptions/CTrieSubscriptionDirectory.java index 8623c45cc..adf322291 100644 --- a/broker/src/main/java/io/moquette/broker/subscriptions/CTrieSubscriptionDirectory.java +++ b/broker/src/main/java/io/moquette/broker/subscriptions/CTrieSubscriptionDirectory.java @@ -102,19 +102,27 @@ private static List selectSubscriptionsWithHigherQoSForEachSession @Override public boolean add(Subscription sub) { - if (sub.hasShareName()) { - throw new IllegalArgumentException("Adding a shared subscription using the non-shared method."); - } + checkIsShared(sub, "Adding a shared subscription using the non-shared method."); boolean notExistingSubscription = ctrie.addToTree(sub); subscriptionsRepository.addNewSubscription(sub); return notExistingSubscription; } - @Override - public void addShared(Subscription sub) { + private static void checkIsShared(Subscription sub, String errorMessage) { + if (sub.hasShareName()) { + throw new IllegalArgumentException(errorMessage); + } + } + + private static void checkIsNotShared(Subscription sub, String errorMessage) { if (!sub.hasShareName()) { - throw new IllegalArgumentException("Adding a non-shared subscription using the shared method."); + throw new IllegalArgumentException(errorMessage); } + } + + @Override + public void addShared(Subscription sub) { + checkIsNotShared(sub, "Adding a non-shared subscription using the shared method."); ctrie.addToTree(sub); subscriptionsRepository.addNewSharedSubscription(sub); List sharedSubscriptions = clientSharedSubscriptions.computeIfAbsent(sub.getClientId(), unused -> new ArrayList<>()); @@ -129,18 +137,14 @@ public void addShared(Subscription sub) { */ @Override public void removeSubscription(Subscription sub) { - if (sub.hasShareName()) { - throw new IllegalArgumentException("Removing a shared subscription using the non-shared method."); - } + checkIsShared(sub, "Removing a shared subscription using the non-shared method."); ctrie.removeFromTree(sub); subscriptionsRepository.removeSubscription(sub); } @Override public void removeSharedSubscription(Subscription subscription) { - if (!subscription.hasShareName()) { - throw new IllegalArgumentException("Removing a non-shared subscription using the shared method."); - } + checkIsNotShared(subscription, "Removing a non-shared subscription using the shared method."); ctrie.removeFromTree(subscription); subscriptionsRepository.removeSharedSubscription(subscription);