From 87fac888c62de55a458eaf20f40dfe321dd5ee3a Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 12 Sep 2018 19:56:46 +0200 Subject: [PATCH] Improve MultiUserChat's API to query hosted rooms Return a Map instead of a List. This makes it possible to check for the existence of MUC by looking up the MUC's address in the Map's key set. --- .../smackx/muc/MultiUserChatManager.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java index 8c5a98ccd..33e36e76b 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java @@ -19,6 +19,7 @@ package org.jivesoftware.smackx.muc; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -392,19 +393,46 @@ public final class MultiUserChatManager extends Manager { * @throws NotConnectedException * @throws InterruptedException * @throws NotAMucServiceException + * @deprecated use {@link #getRoomsHostedBy(DomainBareJid)} instead. */ + @Deprecated + // TODO: Remove in Smack 4.4. public List getHostedRooms(DomainBareJid serviceName) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotAMucServiceException { + Map hostedRooms = getRoomsHostedBy(serviceName); + return new ArrayList<>(hostedRooms.values()); + } + + /** + * Returns a Map of HostedRooms where each HostedRoom has the XMPP address of the room and the room's name. + * Once discovered the rooms hosted by a chat service it is possible to discover more detailed room information or + * join the room. + * + * @param serviceName the service that is hosting the rooms to discover. + * @return a map from the room's address to its HostedRoom information. + * @throws XMPPErrorException + * @throws NoResponseException + * @throws NotConnectedException + * @throws InterruptedException + * @throws NotAMucServiceException + * @since 4.3.1 + */ + public Map getRoomsHostedBy(DomainBareJid serviceName) throws NoResponseException, XMPPErrorException, + NotConnectedException, InterruptedException, NotAMucServiceException { if (!providesMucService(serviceName)) { throw new NotAMucServiceException(serviceName); } ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection()); DiscoverItems discoverItems = discoManager.discoverItems(serviceName); List items = discoverItems.getItems(); - List answer = new ArrayList(items.size()); + + Map answer = new HashMap<>(items.size()); for (DiscoverItems.Item item : items) { - answer.add(new HostedRoom(item)); + HostedRoom hostedRoom = new HostedRoom(item); + HostedRoom previousRoom = answer.put(hostedRoom.getJid(), hostedRoom); + assert previousRoom == null; } + return answer; }