From: David ‘Bombe’ Roden Date: Fri, 1 May 2020 12:51:23 +0000 (+0200) Subject: ⚗ Use a multimap to keep track of registered USKs X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b4de2c2f2e5fbbe50e417692d972d2998328a14e;p=Sone.git ⚗ Use a multimap to keep track of registered USKs --- diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index 50ee9d0..e77badf 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -24,6 +24,7 @@ import static java.util.logging.Logger.getLogger; import java.io.IOException; import java.net.MalformedURLException; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -42,6 +43,8 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.TemporaryImage; import com.google.common.base.Function; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; import com.google.common.eventbus.EventBus; import com.google.inject.Singleton; @@ -98,7 +101,7 @@ public class FreenetInterface { private final RequestClient requestClient = new RequestClientBuilder().realTime().build(); /** The USK callbacks. */ - private final Map soneUskCallbacks = new HashMap<>(); + private final Multimap soneUskCallbacks = ArrayListMultimap.create(); /** The not-Sone-related USK callbacks. */ private final Map uriUskCallbacks = Collections.synchronizedMap(new HashMap()); @@ -280,17 +283,19 @@ public class FreenetInterface { * The Sone to unregister */ public void unregisterUsk(Sone sone) { - USKCallback uskCallback = soneUskCallbacks.remove(sone.getId()); - if (uskCallback == null) { + Collection uskCallbacks = soneUskCallbacks.removeAll(sone.getId()); + if (uskCallbacks.isEmpty()) { return; } - try { - logger.log(Level.FINE, String.format("Unsubscribing from USK for %s…", sone)); - logger.log(Level.FINEST, String.format("USKs left: %d", soneUskCallbacks.size())); - node.clientCore.uskManager.unsubscribe(USK.create(soneUriCreator.getRequestUri(sone)), uskCallback); - } catch (MalformedURLException mue1) { - logger.log(Level.FINE, String.format("Could not unsubscribe USK “%s”!", soneUriCreator.getRequestUri(sone)), mue1); - } + logger.log(Level.FINE, String.format("Unsubscribing %d from USK for %s…", uskCallbacks.size(), sone)); + logger.log(Level.FINEST, String.format("USKs left: %d", soneUskCallbacks.size())); + uskCallbacks.forEach(uskCallback -> { + try { + node.clientCore.uskManager.unsubscribe(USK.create(soneUriCreator.getRequestUri(sone)), uskCallback); + } catch (MalformedURLException mue1) { + logger.log(Level.FINE, String.format("Could not unsubscribe USK “%s”!", soneUriCreator.getRequestUri(sone)), mue1); + } + }); } /**